-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathquads.js
209 lines (178 loc) · 7.41 KB
/
quads.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
'use strict';
const Point = require('point-geometry');
module.exports = {
getIconQuads: getIconQuads,
getGlyphQuads: getGlyphQuads,
SymbolQuad: SymbolQuad
};
/**
* A textured quad for rendering a single icon or glyph.
*
* The zoom range the glyph can be shown is defined by minScale and maxScale.
*
* @param {Point} tl The offset of the top left corner from the anchor.
* @param {Point} tr The offset of the top right corner from the anchor.
* @param {Point} bl The offset of the bottom left corner from the anchor.
* @param {Point} br The offset of the bottom right corner from the anchor.
* @param {Object} tex The texture coordinates.
*
* @class SymbolQuad
* @private
*/
function SymbolQuad(tl, tr, bl, br, tex, writingMode, glyphOffset, lineOffset) {
this.tl = tl;
this.tr = tr;
this.bl = bl;
this.br = br;
this.tex = tex;
this.writingMode = writingMode;
this.glyphOffset = glyphOffset;
this.lineOffset = lineOffset;
}
/**
* Create the quads used for rendering an icon.
*
* @param {Anchor} anchor
* @param {PositionedIcon} shapedIcon
* @param {StyleLayer} layer
* @param {boolean} alongLine Whether the icon should be placed along the line.
* @param {Shaping} shapedText Shaping for corresponding text
* @param {Object} globalProperties
* @param {Object} featureProperties
* @returns {Array<SymbolQuad>}
* @private
*/
function getIconQuads(anchor, shapedIcon, layer, alongLine, shapedText, globalProperties, featureProperties) {
const image = shapedIcon.image;
const layout = layer.layout;
// If you have a 10px icon that isn't perfectly aligned to the pixel grid it will cover 11 actual
// pixels. The quad needs to be padded to account for this, otherwise they'll look slightly clipped
// on one edge in some cases.
const border = 1;
const top = shapedIcon.top - border / image.pixelRatio;
const left = shapedIcon.left - border / image.pixelRatio;
const bottom = shapedIcon.bottom + border / image.pixelRatio;
const right = shapedIcon.right + border / image.pixelRatio;
let tl, tr, br, bl;
// text-fit mode
if (layout['icon-text-fit'] !== 'none' && shapedText) {
const iconWidth = (right - left),
iconHeight = (bottom - top),
size = layout['text-size'] / 24,
textLeft = shapedText.left * size,
textRight = shapedText.right * size,
textTop = shapedText.top * size,
textBottom = shapedText.bottom * size,
textWidth = textRight - textLeft,
textHeight = textBottom - textTop,
padT = layout['icon-text-fit-padding'][0],
padR = layout['icon-text-fit-padding'][1],
padB = layout['icon-text-fit-padding'][2],
padL = layout['icon-text-fit-padding'][3],
offsetY = layout['icon-text-fit'] === 'width' ? (textHeight - iconHeight) * 0.5 : 0,
offsetX = layout['icon-text-fit'] === 'height' ? (textWidth - iconWidth) * 0.5 : 0,
width = layout['icon-text-fit'] === 'width' || layout['icon-text-fit'] === 'both' ? textWidth : iconWidth,
height = layout['icon-text-fit'] === 'height' || layout['icon-text-fit'] === 'both' ? textHeight : iconHeight;
tl = new Point(textLeft + offsetX - padL, textTop + offsetY - padT);
tr = new Point(textLeft + offsetX + padR + width, textTop + offsetY - padT);
br = new Point(textLeft + offsetX + padR + width, textTop + offsetY + padB + height);
bl = new Point(textLeft + offsetX - padL, textTop + offsetY + padB + height);
// Normal icon size mode
} else {
tl = new Point(left, top);
tr = new Point(right, top);
br = new Point(right, bottom);
bl = new Point(left, bottom);
}
const angle = layer.getLayoutValue('icon-rotate', globalProperties, featureProperties) * Math.PI / 180;
if (angle) {
const sin = Math.sin(angle),
cos = Math.cos(angle),
matrix = [cos, -sin, sin, cos];
tl._matMult(matrix);
tr._matMult(matrix);
bl._matMult(matrix);
br._matMult(matrix);
}
// Icon quad is padded, so texture coordinates also need to be padded.
const textureRect = {
x: image.textureRect.x - border,
y: image.textureRect.y - border,
w: image.textureRect.w + border * 2,
h: image.textureRect.h + border * 2
};
return [new SymbolQuad(tl, tr, bl, br, textureRect, undefined, [0, 0])];
}
/**
* Create the quads used for rendering a text label.
*
* @param {Anchor} anchor
* @param {Shaping} shaping
* @param {StyleLayer} layer
* @param {boolean} alongLine Whether the label should be placed along the line.
* @param {Object} globalProperties
* @param {Object} featureProperties
* @returns {Array<SymbolQuad>}
* @private
*/
function getGlyphQuads(anchor, shaping, layer, alongLine, globalProperties, featureProperties) {
const oneEm = 24;
const textRotate = layer.getLayoutValue('text-rotate', globalProperties, featureProperties) * Math.PI / 180;
const textOffset = layer.getLayoutValue('text-offset', globalProperties, featureProperties).map((t)=> t * oneEm);
const positionedGlyphs = shaping.positionedGlyphs;
const quads = [];
for (let k = 0; k < positionedGlyphs.length; k++) {
const positionedGlyph = positionedGlyphs[k];
const glyph = positionedGlyph.glyph;
if (!glyph) continue;
const rect = glyph.rect;
if (!rect) continue;
const halfAdvance = glyph.advance / 2;
const glyphOffset = alongLine ?
[positionedGlyph.x + halfAdvance, positionedGlyph.y] :
[0, 0];
const builtInOffset = alongLine ?
[0, 0] :
[positionedGlyph.x + halfAdvance + textOffset[0], positionedGlyph.y + textOffset[1]];
const x1 = glyph.left - halfAdvance + builtInOffset[0];
const y1 = -glyph.top + builtInOffset[1];
const x2 = x1 + rect.w;
const y2 = y1 + rect.h;
const tl = new Point(x1, y1);
const tr = new Point(x2, y1);
const bl = new Point(x1, y2);
const br = new Point(x2, y2);
const center = new Point(builtInOffset[0] - halfAdvance, glyph.advance / 2);
if (positionedGlyph.angle !== 0) {
tl._sub(center)._rotate(positionedGlyph.angle)._add(center);
tr._sub(center)._rotate(positionedGlyph.angle)._add(center);
bl._sub(center)._rotate(positionedGlyph.angle)._add(center);
br._sub(center)._rotate(positionedGlyph.angle)._add(center);
}
if (textRotate) {
const sin = Math.sin(textRotate),
cos = Math.cos(textRotate),
matrix = [cos, -sin, sin, cos];
tl._matMult(matrix);
tr._matMult(matrix);
bl._matMult(matrix);
br._matMult(matrix);
}
quads.push(new SymbolQuad(tl, tr, bl, br, rect, shaping.writingMode, glyphOffset));
}
// Quads need to be in a strict order so that the render-time projection algorithm can be more efficient.
quads.sort((qa, qb) => {
const a = qa.glyphOffset[0];
const b = qb.glyphOffset[0];
const aIsForward = a > 0;
const bIsForward = b > 0;
if (aIsForward === bIsForward) {
return Math.abs(a) - Math.abs(b);
} else if (aIsForward) {
return -1;
} else {
return 1;
}
});
return quads;
}