/[suikacvs]/test/html-whatpm/table-script.js
Suika

Contents of /test/html-whatpm/table-script.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download) (as text)
Mon May 5 06:56:01 2008 UTC (16 years ago) by wakaba
Branch: MAIN
Changes since 1.5: +4 -3 lines
File MIME type: application/javascript
++ ChangeLog	5 May 2008 06:55:59 -0000
	* table-script.js: Table header/footer fill started at a
	wrong point.

2008-05-05  Wakaba  <wakaba@suika.fam.cx>

1 function tableToCanvas (table, parent, idPrefix) {
2 var canvas = document.createElement ('canvas');
3 parent.appendChild (canvas);
4 if (window.G_vmlCanvasManager) {
5 canvas = G_vmlCanvasManager.initElement (canvas);
6 }
7 var c2d = canvas.getContext ('2d');
8
9 var param = {
10 columnLeft: 20,
11 columnWidth: 20,
12 columnSpacing: 5,
13 columnGroupTop: 10,
14 columnTop: 15,
15 rowTop: 20,
16 rowHeight: 20,
17 rowSpacing: 5,
18 rowGroupLeft: 10,
19 rowGroupFillLeft: 20, /* Must be same as columnLeft */
20 rowLeft: 15,
21 cellTop: 20,
22 cellLeft: 20, /* Must be same as columnLeft */
23 cellBottom: 20,
24 cellRight: 20,
25 explicitColumnGroupStrokeStyle: 'black',
26 explicitColumnStrokeStyle: 'black',
27 impliedColumnStrokeStyle: '#C0C0C0',
28 explicitHeaderRowGroupStrokeStyle: 'black',
29 explicitHeaderRowGroupFillStyle: 'rgba(220, 220, 220, 0.3)',
30 explicitBodyRowGroupStrokeStyle: 'black',
31 explicitBodyRowGroupFillStyle: 'rgba(0, 0, 0, 0)',
32 explicitFooterRowGroupStrokeStyle: 'black',
33 explicitFooterRowGroupFillStyle: 'rgba(220, 220, 220, 0.3)',
34 explicitRowStrokeStyle: 'black',
35 impliedRowStrokeStyle: '#C0C0C0',
36 headerCellFillStyle: 'rgba(192, 192, 192, 0.5)',
37 headerCellStrokeStyle: 'black',
38 dataCellFillStyle: 'rgba(0, 0, 0, 0)',
39 dataCellStrokeStyle: 'black',
40 overlappingCellFillStyle: 'red',
41 overlappingCellStrokeStyle: 'rgba(0, 0, 0, 0)'
42 };
43
44 var columnNumber = table.column.length;
45 if (columnNumber < table.cell.length) columnNumber = table.cell.length;
46 var rowNumber = 0;
47 for (var i = 0; i < table.cell.length; i++) {
48 if (table.cell[i] && rowNumber < table.cell[i].length) {
49 rowNumber = table.cell[i].length;
50 }
51 }
52
53 canvas.style.width = 'auto'; // NOTE: Opera9 has default style=""
54 canvas.style.height = 'auto';
55 // NOTE: Set style="" before width/height="" for ExplorerCanvas compatibility
56 canvas.width = param.cellLeft
57 + (param.columnWidth + param.columnSpacing) * columnNumber
58 + param.cellRight;
59 canvas.height = param.cellTop
60 + (param.rowHeight + param.rowSpacing) * rowNumber
61 + param.cellBottom;
62
63 var y = param.rowTop;
64 for (var i = 0; i < table.row_group.length; i++) {
65 var rg = table.row_group[i];
66 c2d.beginPath ();
67 if (rg.type == 'thead') {
68 c2d.strokeStyle = param.explicitHeaderRowGroupStrokeStyle;
69 c2d.fillStyle = param.explicitHeaderRowGroupFillStyle;
70 } else if (rg.type == 'tfoot') {
71 c2d.strokeStyle = param.explicitFooterRowGroupStrokeStyle;
72 c2d.fillStyle = param.explicitFooterRowGroupFillStyle;
73 } else {
74 c2d.strokeStyle = param.explicitBodyRowGroupStrokeStyle;
75 c2d.fillStyle = param.explicitBodyRowGroupFillStyle;
76 }
77 var dy = (param.rowHeight + param.rowSpacing) * rg.height;
78 c2d.moveTo (param.rowGroupLeft, y);
79 c2d.lineTo (param.rowGroupLeft, y + dy - param.rowSpacing);
80 c2d.stroke ();
81 c2d.closePath ();
82 c2d.beginPath ();
83 c2d.rect (param.rowGroupFillLeft,
84 y,
85 (param.columnWidth + param.columnSpacing) * columnNumber - param.columnSpacing,
86 dy - param.rowSpacing);
87 c2d.fill ();
88 c2d.closePath ();
89 y += dy;
90 i += rg.height - 1;
91 }
92
93 c2d.beginPath ();
94 c2d.strokeStyle = param.explicitColumnGroupStrokeStyle;
95 var x = param.columnLeft;
96 for (var i = 0; i < table.column_group.length; i++) {
97 var cg = table.column_group[i];
98 c2d.moveTo (x, param.columnGroupTop);
99 x += (param.columnWidth + param.columnSpacing) * cg.width;
100 c2d.lineTo (x - param.columnSpacing, param.columnGroupTop);
101 i += cg.width - 1;
102 }
103 c2d.stroke ();
104 c2d.closePath ();
105
106 var x = param.columnLeft;
107 for (var i = 0; i < columnNumber; i++) {
108 var c = table.column[i];
109 c2d.beginPath ();
110 c2d.moveTo (x, param.columnTop);
111 x += param.columnWidth + param.columnSpacing;
112 c2d.lineTo (x - param.columnSpacing, param.columnTop);
113 if (c) {
114 c2d.strokeStyle = param.explicitColumnStrokeStyle;
115 } else {
116 c2d.strokeStyle = param.impliedColumnStrokeStyle;
117 }
118 c2d.stroke ();
119 c2d.closePath ();
120 }
121
122 var map = document.createElement ('map');
123 var x = param.cellLeft;
124 for (var i = 0; i < table.cell.length; i++) {
125 var y = param.cellTop;
126 if (!table.cell[i]) continue;
127 for (var j = 0; j < table.cell[i].length; j++) {
128 var c = table.cell[i][j];
129 if (c && ((c[0].x == i && c[0].y == j) || c.length > 1)) {
130 c2d.beginPath ();
131 var width = (param.columnWidth + param.columnSpacing) * c[0].width
132 - param.columnSpacing;
133 var height = (param.rowHeight + param.rowSpacing) * c[0].height
134 - param.rowSpacing;
135 if (c.length == 1) {
136 c2d.rect (x, y, width, height);
137 c2d.fillStyle = c[0].is_header
138 ? param.headerCellFillStyle : param.dataCellFillStyle;
139 c2d.strokeStyle = c[0].is_header
140 ? param.headerCellStrokeStyle : param.dataCellStrokeStyle;
141 if (c[0].id) {
142 var area = document.createElement ('area');
143 area.shape = 'rect';
144 area.coords = [x, y, x + width, y + height].join (',');
145 area.alt = 'Cell (' + c[0].x + ', ' + c[0].y + ')';
146 area.href = '#' + idPrefix + 'node-' + c[0].id;
147 area.id = idPrefix + 'cell-' + c[0].id;
148 map.appendChild (area);
149 }
150 } else {
151 c2d.rect (x, y, param.columnWidth, param.rowHeight);
152 c2d.fillStyle = param.overlappingCellFillStyle;
153 c2d.strokeStyle = param.overlappingCellStrokeStyle;
154 }
155 c2d.fill ();
156 c2d.stroke ();
157 c2d.closePath ();
158 }
159 y += param.rowHeight + param.rowSpacing;
160 }
161 x += param.columnWidth + param.columnSpacing;
162 }
163
164 var y = param.rowTop;
165 for (var i = 0; i < rowNumber; i++) {
166 c2d.beginPath ();
167 c2d.moveTo (param.rowLeft, y);
168 y += param.rowHeight + param.rowSpacing;
169 c2d.lineTo (param.rowLeft, y - param.rowSpacing);
170 //if (true) {
171 c2d.strokeStyle = param.explicitRowStrokeStyle;
172 //} else {
173 // c2d.strokeStyle = param.impliedRowStrokeStyle;
174 //}
175 c2d.stroke ();
176 c2d.closePath ();
177 }
178
179 if (map.hasChildNodes ()) {
180 var mapid = /* idPrefix + */ 'table-map-' + ++document.TableMapId;
181 map.name = mapid;
182 parent.appendChild (map);
183 var img = document.createElement ('img');
184 img.src = canvas.toDataURL ();
185 img.useMap = '#' + mapid;
186 parent.appendChild (img);
187 canvas.style.display = 'none';
188 }
189 } // tableToCanvas
190
191 if (!document.TableMapId) document.TableMapId = 0;
192
193 /*
194
195 Copyright 2007-2008 Wakaba <w@suika.fam.cx>
196
197 This library is free software; you can redistribute it
198 and/or modify it under the same terms as Perl itself.
199
200 */
201 /* $Date: 2008/05/05 06:11:34 $ */

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24