forked from nodetiles/nodetiles-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcartoRenderer.js
1455 lines (1266 loc) · 49 KB
/
cartoRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const { createCanvas } = require('canvas')
var carto = require("carto");
var UTFGrid = require('./utfgrid');
var fs = require('fs');
var path = require('path');
var util = require('util');
var __ = require('lodash');
var MIGURSKI = true;
var MAX_ZOOM = 23;
var LINE_DASH_PROPERTY = (function() {
const canvas = createCanvas(8, 8)
var ctx = canvas.getContext("2d");
var property = (ctx.dash && "dash") || (ctx.lineDash && "lineDash");
if (!property) {
for (var prefixes = ["webkit", "moz", "o", "ms"], i = prefixes.length - 1; i > -1; i--) {
var prefix = prefixes[i];
if (ctx[prefix + "Dash"]) return prefix + "Dash"
else if (ctx[prefix + "LineDash"]) return prefix + "LineDash";
}
}
return property;
})();
// Handles canvas and context management, then passes things off to an actual rendering routine
// TODO: support arbitrary canvas size (or at least @2x size)
// TODO: support arbitrary render routines
var renderImage = exports.renderImage = function(options) {
// var start = new Date;
var bounds = options.bounds;
var width = options.width;
var height = options.height;
var layers = options.layers;
var styles = options.styles;
var callback = options.callback;
var zoom = options.zoom;
// Give each style an ID
//console.log("styles", styles);
for (var i = styles.length - 1; i >= 0; i--) {
styles[i].id = i;
}
const canvas = createCanvas(width, height),
ctx = canvas.getContext('2d'),
// we're using the same ratio for width and height, so the actual maxY may not match specified maxY...
pxPtRatio = width / (bounds.maxX - bounds.minX);
// once upon a time, we used to scale and translate the canvas instead of transforming the points.
// However, this causes problems when drawing images and patterns, so we can't do that anymore :(
function transform(p) {
var point = [];
point[0] = (p[0] - bounds.minX) * pxPtRatio;
point[1] = ((p[1] - bounds.minY) * -pxPtRatio) + height;
return point;
};
cartoImageRenderer(ctx, 256/width, options.layers, options.styles, options.zoom, bounds.minX, bounds.maxX, bounds.minY, transform);
// console.log("Rendered image in " + (Date.now() - start) + "ms");
options.callback && options.callback(null, canvas);
};
var renderGrid = exports.renderGrid = function(options) {
var bounds = options.bounds;
var width = options.width;
var height = options.height;
var callback = options.callback;
var featureImage = options.drawImage; // Optionally draw the grid as an image
// Set up a new canvas
const canvas = createCanvas(width, width),
ctx = canvas.getContext('2d'),
// we're using the same ratio for width and height, so the actual maxY may not match specified maxY...
pxPtRatio = width / (bounds.maxX - bounds.minX),
gridSize = width;
function transform(p) {
var point = [];
point[0] = (p[0] - bounds.minX) * pxPtRatio;
point[1] = ((p[1] - bounds.minY) * -pxPtRatio) + height;
return point;
};
// Paint the canvas black by default?
// TODO
ctx.antialias = 'none';
ctx.fillStyle = '#000000'; // Paint it black
ctx.fillRect(0, 0, gridSize, gridSize);
if (featureImage) {
ctx.fillStyle = '#fff';
ctx.fillRect(0,0,ctx.canvas.width, ctx.canvas.height);
}
// TODO: what is going on here?
// renderer is provided somehow (but we'll have a simple default)
var colorIndex = cartoGridRenderer(ctx, 256/width, options.layers, options.styles, options.zoom, transform);
// For debugging, we can draw the grid as an image, rather than creating the
// actual UTFGrid data.
if (featureImage) {
callback(null, canvas);
return;
}
var delegate = function delegate(point) {
// Use our raster (ctx) and colorIndex to lookup the corresponding feature
//look up the the rgba values for the pixel at x,y
// scan rows and columns; each pixel is 4 separate values (R,G,B,A) in the array
var startPixel = (gridSize * point.y + point.x) * 4;
// convert those rgba elements to hex then an integer
var intColor = h2d(d2h(pixels[startPixel], 2) + d2h(pixels[startPixel + 1], 2) + d2h(pixels[startPixel + 2], 2));
return colorIndex[intColor]; // returns the feature that's referenced in colorIndex.
};
// Set up the UTF grid
var pixels = ctx.getImageData(0, 0, gridSize, gridSize).data; // array of all pixels
var utfgrid = (new UTFGrid(gridSize, delegate)).encodeAsObject();
// Populate the UTFgrid feature table with properties
for(var featureId in utfgrid.data) {
utfgrid.data[featureId] = utfgrid.data[featureId].properties;
}
callback(undefined, utfgrid);
colorIndex = null;
delegate = null;
};
// Functions to render paths for different geometries
// NB:- these functions are called using 'this' as our canvas context
// it's not clear to me whether this architecture is right but it's neat ATM.
function makeRenderers(transform) {
var renderPath = {
'MultiPolygon': function(mp) {
for (var i = mp.length - 1; i >= 0; i--) {
renderPath.Polygon.call(this, mp[i]);
}
},
'Polygon': function(p) {
for (var i = p.length - 1; i >= 0; i--) {
renderPath.LineString.call(this, p[i]);
}
},
'MultiLineString': function(ml) {
ml.forEach(renderPath.LineString, this);
},
'LineString': function(l) {
// Figure out where we need to start at
var start = l[0];
if (transform) {
start = transform(start);
}
this.moveTo(start[0], start[1]);
// Make a new array with just the remaining vertices
// (equivalent to x[1:] in python)
var i;
var rest = Array(l.length - 1);
for (i = 0; i < rest.length; i++) {
rest[i] = l[i + 1];
}
// draw the shape
for (i = 0; i < rest.length; i++) {
if (transform) {
rest[i] = transform(rest[i]);
}
this.lineTo(rest[i][0], rest[i][1]);
}
},
// TODO: Don't render paths for points. Use ellipse marker styles for points.
'MultiPoint': function(p, scale) {
// Can't use forEach here because we need to pass scale along
for (var i = 0, len = p.length; i < len; i++) {
renderPath.Point.call(this, p[i], scale);
}
},
'Point': function(p, scale) {
if (transform) {
p = transform(p);
this.arc(p[0], p[1], 8 / scale, 0, Math.PI * 2, true);
}
else {
this.arc(p[0], p[1], 8 / scale, 0, Math.PI * 2, true);
}
}
};
// TODO: where / why do we use this?
var roundPoint = function(point) {
return point.map(Math.round);
};
// Functions to render dashed lines
// Many canvas implementations don't support the dash/lineDash property, so we do it by hand :\
// NOTE also lineDash is in the WHATWG HTML draft but not W3C:
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html
var renderDashedPath = {
'MultiPolygon': function(dashArray, mp) {
mp.forEach(renderDashedPath.Polygon.bind(this, dashArray));
},
'Polygon': function(dashArray, p) {
p.forEach(renderDashedPath.LineString.bind(this, dashArray));
},
'MultiLineString': function(dashArray, ml) {
ml.forEach(renderDashedPath.LineString.bind(this, dashArray));
},
'LineString': function(dashArray, l, offset) {
// if there's no dashArray, just go render a solid line
if (!dashArray) {
return renderPath.LineString.call(this, l);
}
offset = offset || 0;
// don't render segments less than this length (in px)
// for best fidelity, this should be at least 1, but not much higher
var minSegmentLength = 2;
// round off the start and end points to get as close as we can to drawing on pixel boundaries
// loop through line combining segments until they match the minimum length
var start = roundPoint(transform(l[0]));
for (var i = 1, len = l.length; i < len; i++) {
var end = roundPoint(transform(l[i])),
dx = end[0] - start[0];
dy = end[1] - start[1];
lineLength = Math.sqrt(dx * dx + dy * dy);
// only draw segments of 1px or greater
if (lineLength >= minSegmentLength) {
var angle = Math.atan2(dy, dx);
offset = renderDashedPath._screenLine.call(this, dashArray, start, end, lineLength, angle, offset);
start = end;
}
}
},
// TODO: What does this do?
_screenLine: function(dashArray, start, end, realLength, angle, offset) {
// we're gonna do some transforms
this.save();
// move the line out by half a pixel for more crisp 1px drawing
var yOffset = -0.5;
// In order to reduce artifacts of trying to draw fractions of a pixel,
// only draw even pixels worth of length
var length = Math.floor(realLength);
// Skip zero length/less-than-one length lines
if (length === 0) {
return offset;
}
// decimal offset is left over from refraining from drawing fractions of a pixel on a previous segment
// (see where the length is floor()'d above)
// We'll eventually move the start point back by the fractional offset to account for what we
// didn't draw in the previous segment (we potentially underdraw because we floor()'d the length).
var intOffset = Math.ceil(offset);
var decOffset = offset - intOffset;
offset = intOffset;
// transform the context so we can simplify the work by pretending to draw a straight line
this.translate(start[0], start[1]);
this.rotate(angle);
// Move the start point back by the fractional offset (see deeper description above)
this.moveTo(decOffset, yOffset);
var dashCount = dashArray.length;
var dashIndex = 0;
// Move the start point by the integer offset (the fractional bit is already accounted for above)
var x = offset || 0;
// keep track of how much of the pattern we drew (used to offset the next segment)
var patternDistance = 0;
var draw = true;
while (x < length) {
// reset the pattern distance when we loop back to the start of the dash array
if (dashIndex === 0) {
patternDistance = 0;
}
// get the distance of this dash
var dashLength = dashArray[dashIndex];
dashIndex = (dashIndex + 1) % dashCount;
x += dashLength;
patternDistance += dashLength;
// if we are about to draw past the end of the segment, don't
if (x > length) {
patternDistance += length - x;
x = length;
}
// only draw once we've moved past the offset
if (x > 0) {
if (draw) {
this.lineTo(x, yOffset);
}
else {
this.moveTo(x, yOffset);
}
}
draw = !draw;
}
// Add the fractional extra distance that we didn't draw back in
patternDistance += realLength - length;
this.restore();
return -patternDistance;
},
'MultiPoint': function(dashArray, p, scale) {
// Can't use forEach here because we need to pass scale along
for (var i = 0, len = p.length; i < len; i++) {
renderDashedPath.Point.call(this, null, p[i], scale);
}
},
'Point': function(dashArray, p, scale) {
if (transform) {
p = transform(p);
this.arc(p[0], p[1], 8, 0, Math.PI * 2, true);
}
else {
this.arc(p[0], p[1], 8 / scale, 0, Math.PI * 2, true);
}
}
};
var renderImage = {
'MultiPolygon': function(image, mp) {
mp.forEach(renderImage.Polygon.bind(this, image));
},
'Polygon': function(image, p) {
renderImage.LineString.call(this, image, p[0]);
},
'MultiLineString': function(image, ml) {
ml.forEach(renderImage.LineString.bind(this, image));
},
'LineString': function(image, l) {
// put the point at the center
var minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
l.forEach(function(point) {
minX = Math.min(minX, point[0]);
minY = Math.min(minY, point[1]);
maxX = Math.max(maxX, point[0]);
maxY = Math.max(maxY, point[1]);
});
return renderImage.Point.call(this, image, [minX + (maxX - minX) / 2, minY + (maxY - minY) / 2])
},
'MultiPoint': function(image, p, scale) {
// Can't use forEach here because we need to pass scale along
for (var i = 0, len = p.length; i < len; i++) {
renderImage.Point.call(this, image, p[i], scale);
}
},
'Point': function(image, p, scale) {
if (transform) {
p = transform(p);
}
this.drawImage(image, p[0] - image.width / 2, p[1] - image.height / 2);
}
};
var renderDot = {
'MultiPolygon': function(radius, mp) {
mp.forEach(renderDot.Polygon.bind(this, radius));
},
'Polygon': function(radius, p) {
renderDot.LineString.call(this, radius, p[0]);
},
'MultiLineString': function(radius, ml) {
ml.forEach(renderDot.LineString.bind(this, radius));
},
'LineString': function(radius, l) {
// put the point at the center
var minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
l.forEach(function(point) {
minX = Math.min(minX, point[0]);
minY = Math.min(minY, point[1]);
maxX = Math.max(maxX, point[0]);
maxY = Math.max(maxY, point[1]);
});
return renderDot.Point.call(this, radius, [minX + (maxX - minX) / 2, minY + (maxY - minY) / 2])
},
'MultiPoint': function(radius, p, scale) {
// Can't use forEach here because we need to pass scale along
for (var i = 0, len = p.length; i < len; i++) {
renderDot.Point.call(this, radius, p[i], scale);
}
},
'Point': function(radius, p) {
if (transform) {
p = transform(p);
}
this.arc(p[0], p[1], radius || 10, 0, Math.PI * 2, true);
}
};
var renderText = {
'MultiPolygon': function(text, mp) {
mp.forEach(renderText.Polygon.bind(this, text));
},
'Polygon': function(text, p) {
renderText.LineString.call(this, text, p[0]);
},
'MultiLineString': function(text, ml) {
ml.forEach(renderText.LineString.bind(this, text));
},
'LineString': function(text, l) {
// we support line and point, point is default
if (text.placement === "line") {
this.textAlign = "left";
var totalLength = 0;
var segmentLengths = [];
for (var i = 1, len = l.length; i < len; i++) {
var start = transform(l[i - 1]);
end = transform(l[i]),
dx = end[0] - start[0];
dy = end[1] - start[1];
lineLength = Math.sqrt(dx * dx + dy * dy);
totalLength += lineLength;
segmentLengths.push(lineLength);
}
var closedShape = l[0][0] === l[len - 1][0] && l[0][1] === l[len - 1][1];
// NOTE: should we really bail here if there's not enough room on the line to write the text?
// maybe this should be an option (on by default)
var textLength = this.measureText(text.text).width;
if (totalLength < textLength) {
return;
}
// determine distance along line to start placing text
var startPoint = 0;
// TODO: support BIDI text (right now end = right and start = left)
if (text.align === "end" || text.align === "right") {
startPoint = totalLength - textLength;
}
else if (text.align === "center" || text.align === "middle" || !text.align) {
startPoint = totalLength / 2 - textLength / 2;
}
// these operations may be destructive to the text object, so save old values here
// TODO: maybe we should make a new object that uses the text argument as its prototype?
var fullText = text.text;
var originalXOffset = 0;
if (text.offset) {
originalXOffset = text.offset.x || 0;
startPoint += originalXOffset;
text.offset.x = 0;
// if the line represents a closed shape, loop startPoint around the line
if (closedShape) {
while (startPoint < 0) {
startPoint += totalLength;
}
while (startPoint > totalLength) {
startPoint -= totalLength;
}
}
}
var segmentLengthsTotal = 0;
for (var i = 0, len = segmentLengths.length; i < len; i++) {
var segmentLength = segmentLengths[i];
if (segmentLengthsTotal + segmentLength >= startPoint || i === len - 1) {
var segmentDistance = startPoint - segmentLengthsTotal;
var start = transform(l[i]),
end = transform(l[i + 1]),
dx = end[0] - start[0],
dy = end[1] - start[1],
angle = Math.atan2(dy, dx),
centerPoint = [segmentDistance * Math.cos(angle) + start[0], start[1] + segmentDistance * Math.sin(angle)];
// keep street names from going upside-down
var flippedText = false;
var flippedMinus = false;
if (angle > 0.5 * Math.PI) {
angle -= Math.PI;
flippedText = true;
flippedMinus = true;
}
else if (angle < -0.5 * Math.PI) {
angle += Math.PI;
flippedText = true;
}
this.save();
this.translate(centerPoint[0], centerPoint[1]);
this.rotate(angle);
if (MIGURSKI) {
var textPixels = 0;
var resetIndex = 0;
var segmentOffset = 0;
for (var j = 0, jLen = fullText.length; j < jLen; j++) {
// GO BACKWARDS FOR FLIPPED TEXT
if (flippedText) {
if (segmentOffset + segmentDistance + textPixels > segmentLength && (i < len - 1 || closedShape)) {
// TODO: Potentially add some spacing if the angle causes the text top to bend "in"
segmentOffset = (segmentOffset + segmentDistance + textPixels) - segmentLength;
i = (i + 1) % len;
segmentLength = segmentLengths[i];
segmentDistance = 0;
var start = transform(l[i]),
end = transform(l[i + 1]),
dx = end[0] - start[0],
dy = end[1] - start[1],
angle = Math.atan2(dy, dx);
// keep street names from going upside-down
if (flippedText) {
if (flippedMinus) {
angle -= Math.PI;
}
else {
angle += Math.PI;
}
}
this.restore();
this.save();
this.translate(start[0], start[1]);
this.rotate(angle);
textPixels = 0;
resetIndex = j;
}
var textIndex = jLen - 1 - j;
textPixels = this.measureText(fullText.slice(textIndex, jLen - resetIndex)).width;
text.text = fullText[textIndex];
renderText.Point.call(this, text, [-(segmentOffset + textPixels), 0], true);
}
else {
var textIndex = j;
var textResetIndex = resetIndex;
if (j > 0) {
textPixels = this.measureText(fullText.slice(resetIndex, j)).width;
}
if (segmentOffset + segmentDistance + textPixels > segmentLength && (i < len - 1 || closedShape)) {
// TODO: Potentially add some spacing if the angle causes the text top to bend "in"
segmentOffset = (segmentOffset + segmentDistance + textPixels) - segmentLength;
i = (i + 1) % len;
segmentLength = segmentLengths[i];
segmentDistance = 0;
var start = transform(l[i]),
end = transform(l[i + 1]),
dx = end[0] - start[0],
dy = end[1] - start[1],
angle = Math.atan2(dy, dx);
this.restore();
this.save();
this.translate(start[0], start[1]);
this.rotate(angle);
textPixels = 0;
resetIndex = j;
}
text.text = fullText[textIndex];
renderText.Point.call(this, text, [segmentOffset + textPixels, 0], true);
}
}
}
else {
renderText.Point.call(this, text, [0, 0], true);
}
this.restore();
break;
}
segmentLengthsTotal += segmentLength;
}
// repair text object before returning
text.text = fullText;
text.offset.x = originalXOffset;
}
else {
// put the point at the center
var minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
l.forEach(function(point) {
minX = Math.min(minX, point[0]);
minY = Math.min(minY, point[1]);
maxX = Math.max(maxX, point[0]);
maxY = Math.max(maxY, point[1]);
});
return renderText.Point.call(this, text, [minX + (maxX - minX) / 2, minY + (maxY - minY) / 2])
}
},
'MultiPoint': function(text, p, scale) {
// Can't use forEach here because we need to pass scale along
for (var i = 0, len = p.length; i < len; i++) {
renderText.Point.call(this, radius, p[i], scale);
}
},
'Point': function(text, p, preTransformed) {
if (transform && !preTransformed) {
p = transform(p);
}
var x = p[0] + (text.offset ? text.offset.x : 0);
var y = p[1] + (text.offset ? text.offset.y : 0);
var textMethod = text.stroke ? "strokeText" : "fillText";
this[textMethod](text.text, x, y);
}
};
return {
renderPath: renderPath,
renderDashedPath: renderDashedPath,
renderImage: renderImage,
renderText: renderText,
renderDot: renderDot
};
}
// var didATile = false;
var cartoImageRenderer = function (ctx, scale, layers, styles, zoom, minX, maxX, minY, transform) {
var suite = makeRenderers(transform);
var renderPath = suite.renderPath;
var renderDashedPath = suite.renderDashedPath;
var renderImage = suite.renderImage;
var renderText = suite.renderText;
var renderDot = suite.renderDot;
styles.forEach(function(style) {
if (cartoSelectorIsMatch(style, null, null, zoom)) {
style.rules.forEach(function(rule) {
if (rule.name === "background-color") {
ctx.fillStyle = rule.value.toString();
}
else if (rule.name === "background-image") {
if (!(rule.value instanceof Canvas.Image)) {
var content = rule.value.toString();
var img = new Canvas.Image();
var imagePath = path.normalize(__dirname + "/../static/images/" + content);
img.src = fs.readFileSync(imagePath);
rule.value = img;
}
img = rule.value;
ctx.fillStyle = ctx.createPattern(img, "repeat");
}
});
ctx.fillRect(0,0,ctx.canvas.width, ctx.canvas.height);
}
});
// create list of attachments (in order) so that we can walk through and render them together for each layer
// TODO: should be able to do this as part of the processing stage
var attachments = styles.reduce(function(attachmentList, style) {
if (style.attachment !== "__default__" && attachmentList.indexOf(style.attachment) === -1) {
attachmentList.push(style.attachment);
}
return attachmentList;
}, []);
attachments.push("__default__");
layers.forEach(function(layer, i) {
var source = layer.source;
var features = layer.features || layer;
var numStyles = styles.length;
attachments.forEach(function(attachment) {
// Keep track of already collapased styles so we don't do work twice.
var collapsedStylesBySignature = {};
var instanceOrdersBySignature = {};
var uniqueValuesForKey = [];
// For each feature:
for (var i = 0; i < features.length; i++) {
var feature = features[i];
var collapsedStyle = {};
var instanceOrder = [];
// We're going to give each style+feature combination a unique signature
var signature = new Array(numStyles);
// Check each style
// to see if the style applies to this feature
for (var styleIdx = 0; styleIdx < styles.length; styleIdx++) {
if (styles[styleIdx].attachment === attachment && cartoSelectorIsMatch(styles[styleIdx], feature, source, zoom)) {
signature[styleIdx] = 1;
}else {
signature[styleIdx] = 0;
}
}
// Quickly turn the signature into a string
var sigIdx;
var pow = 1;
var num = 0;
for (sigIdx = 0; sigIdx < signature.length; sigIdx += 1) {
num += signature[sigIdx] * pow;
pow = 2 * pow;
}
var signatureString = num.toString();
// Check if we need to compute the collapsed style
// (or just get the value if we already did that)
if (collapsedStylesBySignature[signatureString] === undefined) {
// Ok, looks like we actually have to go collapse this style.
// Go through the signature and collapse all the styles that are
// actually used.
//
// Using forEach here because this doesn't happen that often
signature.forEach(function(use, index){
if(use === 0) { return; }
var style = styles[index];
style.rules.forEach(function(rule) {
// Create the key and add the rule instance
if (!collapsedStyle[rule.instance]) {
collapsedStyle[rule.instance] = {};
instanceOrder.push(rule.instance);
}
// Don't add a rule twice
if (!collapsedStyle[rule.instance].hasOwnProperty(rule.name)) {
var value = rule.value.toString();
// Precompute values as needed
// (some of these might come in the right format before toString,
// but this isn't that expensive if run only once per render)
if (rule.name === 'line-width') {
value = parseInt(value, 10);
}
if (rule.name === 'polygon-opacity') {
value = parseFloat(rule.value);
if (isNaN(value)) {
value = 1.0;
}
}
if (rule.name === 'line-opacity') {
value = parseFloat(rule.value);
if (isNaN(value)) {
value = 1.0;
}
}
collapsedStyle[rule.instance][rule.name] = value;
}
collapsedStylesBySignature[signatureString] = collapsedStyle;
instanceOrdersBySignature[signatureString] = instanceOrder;
});
});
}else {
// Hey, we've already got the style!
// Let's save ourselves some work.
collapsedStyle = collapsedStylesBySignature[signatureString];
instanceOrder = instanceOrdersBySignature[signatureString];
}
var renderInstance = function(instanceName) {
var instanceStyle = collapsedStyle[instanceName];
ctx.save();
var shouldFill = false,
shouldStroke = false,
shouldMark = false,
shouldPoint = false,
shouldText = false,
dashedStroke = false;
for (var key in instanceStyle) {
// TODO: moving toString() up gives us performance improvements
// But breaks background images. Gotta fix this sometime.
// var rawValue = instanceStyle[key],
// value = rawValue.toString();
var value = instanceStyle[key];
if (key === "background-color" || key === "polygon-fill") {
ctx.fillStyle = value;
shouldFill = true;
}
else if (key === "background-image" || key === "polygon-pattern-file") {
if (rawValue) {
ctx.fillStyle = ctx.createPattern(rawValue, "repeat");
shouldFill = true;
}
}
else if (key === "line-width") {
ctx.lineWidth = value;
}
else if (key === "line-color") {
ctx.strokeStyle = value;
shouldStroke = true;
}
else if (key === "line-opacity") {
// handled at stroke time below
}
else if (key === "line-join") {
ctx.lineJoin = value;
}
else if (key === "line-cap") {
ctx.lineCap = value;
}
else if (key === "line-miterlimit") {
ctx.miterLimit = value;
}
else if (key === "line-dasharray") {
// TODO: dasharray support
// console.log("Dasharray: ", value);
var dashedStroke = value.split(",").map(parseFloat);
// console.log(" now: ", dashArray);
// ctx.dash = dashArray;
// ctx.lineDash = dashArray;
}
else if (key === "polygon-opacity") {
// handled at fill time below
}
else if (key === "line-pattern-file") {
if (rawValue) {
ctx.strokeStyle = ctx.createPattern(rawValue, "repeat");
shouldStroke = true;
}
}
else if (key === "marker-type") {
shouldMark = true;
}
else if (key === "point-file") {
shouldPoint = true;
}
else if (key === "text-name") {
shouldText = true;
}
}
// It's drawing time!
if (shouldFill || shouldStroke || shouldMark || shouldPoint || shouldText) {
ctx.beginPath();
var shape = feature.geometry || feature;
var coordinates = shape.coordinates;
renderPath[shape.type].call(ctx, coordinates, scale);
if (shouldFill) {
ctx.globalAlpha = instanceStyle["polygon-opacity"];
var fillColor = instanceStyle["polygon-fill"];
var fillPattern = instanceStyle["polygon-pattern-file"];
if (fillColor) {
ctx.fillStyle = fillColor;
ctx.fill();
}
if (fillPattern) {
ctx.fillStyle = ctx.createPattern(fillPattern, "repeat");
ctx.fill();
}
}
if (shouldStroke) {
ctx.globalAlpha = instanceStyle["line-opacity"];
if (dashedStroke) {
// since canvas doesn't yet have dashed line support, we have to draw a dashed path :(
ctx.closePath();
ctx.beginPath();
renderDashedPath[shape.type].call(ctx, dashedStroke, coordinates);
}
ctx.stroke();
}
ctx.closePath();
if (shouldMark) {
if (instanceStyle["marker-file"]) {
renderImage[shape.type].call(ctx, instanceStyle["marker-file"], coordinates);
}
else {
// we only support the "ellipse" type
// we only support circles, not ellipses right now :\
var radius = instanceStyle["marker-width"] || instanceStyle["marker-height"];
radius = ((radius !== undefined) ? parseInt(radius, 10) : 10) / 2;
var shouldFillMarker = false;
var shouldStrokeMarker = false;
if (instanceStyle["marker-fill"]) {
ctx.fillStyle = instanceStyle["marker-fill"].toString();
shouldFillMarker = true;
}
if (instanceStyle["marker-line-color"]) {
ctx.strokeStyle = instanceStyle["marker-line-color"].toString();
shouldStrokeMarker = true;
}
if (instanceStyle["marker-line-width"]) {
var lineWidth = parseInt(instanceStyle["marker-line-width"], 10);
ctx.lineWidth = lineWidth;
shouldStrokeMarker = !!lineWidth;
}
ctx.beginPath();
renderDot[shape.type].call(ctx, radius, coordinates);
if (shouldFillMarker) {
ctx.globalAlpha = instanceStyle["marker-fill-opacity"];
ctx.fill();
}
if (shouldStrokeMarker) {
ctx.globalAlpha = instanceStyle["marker-line-opacity"];
ctx.stroke();
}
ctx.closePath();
}
}
if (shouldPoint && instanceStyle["point-file"]) {
renderImage[shape.type].call(ctx, instanceStyle["point-file"], coordinates);
}
if (shouldText) {
var text = instanceStyle["text-name"];
if (text.is === "propertyLookup") {
text = text.toString(feature);
}
if (text) {
var textSize = (instanceStyle["text-size"] || "10").toString();
var textFace = (instanceStyle["text-face-name"] || "sans-serif").toString();
ctx.font = textSize + "px '" + textFace + "'";
var textInfo = {
text: text
};
// offsetting
textInfo.offset = {
x: instanceStyle["text-dx"] ? instanceStyle["text-dx"].value : 0,
y: instanceStyle["text-dy"] ? instanceStyle["text-dy"].value : 0
};
// vertical alignment
var verticalAlign = instanceStyle["text-vertical-alignment"];
if (verticalAlign && verticalAlign.value !== "auto") {
ctx.textBaseline = {
top: "top",
middle: "middle",
bottom: "alphabetic"
}[verticalAlign];
}
else {
ctx.textBaseline = textInfo.offset.y === 0 ? "middle" : (textInfo.offset.y < 0 ? "top" : "alphabetic");
}
// horizontal alignment
var textHorizontalAlignment = instanceStyle["text-horizontal-alignment"];
if (textHorizontalAlignment) {
ctx.textAlign = textHorizontalAlignment.toString();
}
else {
// default to center
ctx.textAlign = "center";
}
textInfo.align = ctx.textAlign;
// text-transform
var textTransform = instanceStyle["text-transform"];
textTransform = textTransform && textTransform.toString();
if (textTransform === "uppercase") {
textInfo.text = text.toUpperCase();
}