-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathshaping.js
208 lines (159 loc) · 6.91 KB
/
shaping.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
'use strict';
const scriptDetection = require('../util/script_detection');
const verticalizePunctuation = require('../util/verticalize_punctuation');
const WritingMode = {
horizontal: 1,
vertical: 2
};
module.exports = {
shapeText: shapeText,
shapeIcon: shapeIcon,
WritingMode: WritingMode
};
// The position of a glyph relative to the text's anchor point.
function PositionedGlyph(codePoint, x, y, glyph, angle) {
this.codePoint = codePoint;
this.x = x;
this.y = y;
this.glyph = glyph || null;
this.angle = angle;
}
// A collection of positioned glyphs and some metadata
function Shaping(positionedGlyphs, text, top, bottom, left, right, writingMode) {
this.positionedGlyphs = positionedGlyphs;
this.text = text;
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
this.writingMode = writingMode;
}
const newLine = 0x0a;
function shapeText(text, glyphs, maxWidth, lineHeight, horizontalAlign, verticalAlign, justify, spacing, translate, verticalHeight, writingMode) {
text = text.trim();
if (writingMode === WritingMode.vertical) text = verticalizePunctuation(text);
const positionedGlyphs = [];
const shaping = new Shaping(positionedGlyphs, text, translate[1], translate[1], translate[0], translate[0], writingMode);
// the y offset *should* be part of the font metadata
const yOffset = -17;
let x = 0;
for (let i = 0; i < text.length; i++) {
const codePoint = text.charCodeAt(i);
const glyph = glyphs[codePoint];
if (!glyph && codePoint !== newLine) continue;
if (!scriptDetection.charHasUprightVerticalOrientation(codePoint) || writingMode === WritingMode.horizontal) {
positionedGlyphs.push(new PositionedGlyph(codePoint, x, yOffset, glyph, 0));
if (glyph) x += glyph.advance + spacing;
} else {
positionedGlyphs.push(new PositionedGlyph(codePoint, x, 0, glyph, -Math.PI / 2));
if (glyph) x += verticalHeight + spacing;
}
}
if (!positionedGlyphs.length) return false;
linewrap(shaping, glyphs, lineHeight, maxWidth, horizontalAlign, verticalAlign, justify, translate, scriptDetection.allowsIdeographicBreaking(text), writingMode);
return shaping;
}
const invisible = {
0x20: true, // space
0x200b: true // zero-width space
};
const breakable = {
0x20: true, // space
0x26: true, // ampersand
0x2b: true, // plus sign
0x2d: true, // hyphen-minus
0x2f: true, // solidus
0xad: true, // soft hyphen
0xb7: true, // middle dot
0x200b: true, // zero-width space
0x2010: true, // hyphen
0x2013: true // en dash
};
invisible[newLine] = breakable[newLine] = true;
function linewrap(shaping, glyphs, lineHeight, maxWidth, horizontalAlign, verticalAlign, justify, translate, useBalancedIdeographicBreaking, writingMode) {
let lastSafeBreak = null;
let lengthBeforeCurrentLine = 0;
let lineStartIndex = 0;
let line = 0;
let maxLineLength = 0;
const positionedGlyphs = shaping.positionedGlyphs;
if (writingMode === WritingMode.horizontal && maxWidth) {
if (useBalancedIdeographicBreaking) {
const lastPositionedGlyph = positionedGlyphs[positionedGlyphs.length - 1];
const estimatedLineCount = Math.max(1, Math.ceil(lastPositionedGlyph.x / maxWidth));
maxWidth = lastPositionedGlyph.x / estimatedLineCount;
}
for (let i = 0; i < positionedGlyphs.length; i++) {
const positionedGlyph = positionedGlyphs[i];
positionedGlyph.x -= lengthBeforeCurrentLine;
positionedGlyph.y += lineHeight * line;
if (lastSafeBreak !== null && (positionedGlyph.x > maxWidth ||
positionedGlyphs[lastSafeBreak].codePoint === newLine)) {
const lineLength = positionedGlyphs[lastSafeBreak + 1].x;
maxLineLength = Math.max(lineLength, maxLineLength);
for (let k = lastSafeBreak + 1; k <= i; k++) {
positionedGlyphs[k].y += lineHeight;
positionedGlyphs[k].x -= lineLength;
}
if (justify && lastSafeBreak > lineStartIndex) {
// Collapse invisible characters.
let lineEnd = lastSafeBreak;
if (invisible[positionedGlyphs[lastSafeBreak].codePoint]) {
lineEnd--;
}
justifyLine(positionedGlyphs, glyphs, lineStartIndex, lineEnd, justify);
}
lineStartIndex = lastSafeBreak + 1;
lastSafeBreak = null;
lengthBeforeCurrentLine += lineLength;
line++;
}
if (useBalancedIdeographicBreaking || breakable[positionedGlyph.codePoint] || scriptDetection.charAllowsIdeographicBreaking(positionedGlyph.codePoint)) {
lastSafeBreak = i;
}
}
}
const lastPositionedGlyph = positionedGlyphs[positionedGlyphs.length - 1];
const lastLineLength = lastPositionedGlyph.x + glyphs[lastPositionedGlyph.codePoint].advance;
maxLineLength = Math.max(maxLineLength, lastLineLength);
const height = (line + 1) * lineHeight;
justifyLine(positionedGlyphs, glyphs, lineStartIndex, positionedGlyphs.length - 1, justify);
align(positionedGlyphs, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, line, translate);
// Calculate the bounding box
shaping.top += -verticalAlign * height;
shaping.bottom = shaping.top + height;
shaping.left += -horizontalAlign * maxLineLength;
shaping.right = shaping.left + maxLineLength;
}
function justifyLine(positionedGlyphs, glyphs, start, end, justify) {
const lastAdvance = glyphs[positionedGlyphs[end].codePoint].advance;
const lineIndent = (positionedGlyphs[end].x + lastAdvance) * justify;
for (let j = start; j <= end; j++) {
positionedGlyphs[j].x -= lineIndent;
}
}
function align(positionedGlyphs, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, line, translate) {
const shiftX = (justify - horizontalAlign) * maxLineLength + translate[0];
const shiftY = (-verticalAlign * (line + 1) + 0.5) * lineHeight + translate[1];
for (let j = 0; j < positionedGlyphs.length; j++) {
positionedGlyphs[j].x += shiftX;
positionedGlyphs[j].y += shiftY;
}
}
function shapeIcon(image, layout) {
if (!image || !image.rect) return null;
const dx = layout['icon-offset'][0];
const dy = layout['icon-offset'][1];
const x1 = dx - image.width / 2;
const x2 = x1 + image.width;
const y1 = dy - image.height / 2;
const y2 = y1 + image.height;
return new PositionedIcon(image, y1, y2, x1, x2);
}
function PositionedIcon(image, top, bottom, left, right) {
this.image = image;
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
}