@huaqiu/js-gerbv 0.1.2 → 0.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +16 -3
- package/README.md +100 -22
- package/examples/example.js +266 -0
- package/examples/example.py +30 -0
- package/index.js +618 -4
- package/lib/add-text.js +552 -0
- package/lib/fill-holes.js +648 -0
- package/lib/hershey-futural.json +1 -0
- package/lib/spline.js +211 -0
- package/lib/system-font.js +166 -0
- package/package.json +8 -3
- package/prebuilds/darwin-arm64/gerbv_node.node +0 -0
- package/prebuilds/darwin-arm64/libX11.6.dylib +0 -0
- package/prebuilds/darwin-arm64/libXau.6.dylib +0 -0
- package/prebuilds/darwin-arm64/libXdmcp.6.dylib +0 -0
- package/prebuilds/darwin-arm64/libXext.6.dylib +0 -0
- package/prebuilds/darwin-arm64/libXrender.1.dylib +0 -0
- package/prebuilds/darwin-arm64/libbrotlicommon.1.dylib +0 -0
- package/prebuilds/darwin-arm64/libbrotlidec.1.dylib +0 -0
- package/prebuilds/darwin-arm64/libcairo.2.dylib +0 -0
- package/prebuilds/darwin-arm64/libfontconfig.1.dylib +0 -0
- package/prebuilds/darwin-arm64/libfreetype.6.dylib +0 -0
- package/prebuilds/darwin-arm64/libpixman-1.0.dylib +0 -0
- package/prebuilds/darwin-arm64/libpng16.16.dylib +0 -0
- package/prebuilds/darwin-arm64/libxcb-render.0.dylib +0 -0
- package/prebuilds/darwin-arm64/libxcb-shm.0.dylib +0 -0
- package/prebuilds/darwin-arm64/libxcb.1.dylib +0 -0
- package/prebuilds/darwin-arm64/meta.json +1 -1
- package/prebuilds/win32-x64/gerbv_node.node +0 -0
- package/prebuilds/win32-x64/libbrotlicommon.dll +0 -0
- package/prebuilds/win32-x64/libbrotlidec.dll +0 -0
- package/prebuilds/win32-x64/libbz2-1.dll +0 -0
- package/prebuilds/win32-x64/libcairo-2.dll +0 -0
- package/prebuilds/win32-x64/libexpat-1.dll +0 -0
- package/prebuilds/win32-x64/libfontconfig-1.dll +0 -0
- package/prebuilds/win32-x64/libfreetype-6.dll +0 -0
- package/prebuilds/win32-x64/libgraphite2.dll +0 -0
- package/prebuilds/win32-x64/libharfbuzz-0.dll +0 -0
- package/prebuilds/win32-x64/libpixman-1-0.dll +0 -0
- package/prebuilds/win32-x64/libpng16-16.dll +0 -0
- package/prebuilds/win32-x64/meta.json +2 -2
- package/scripts/cmake-build.js +8 -0
- package/scripts/pack-prebuild.js +110 -4
- package/scripts/smoke.js +268 -6
- package/src/addon.cc +1086 -16
- package/src/export_png.cc +416 -0
- package/src/export_png.h +50 -0
package/lib/add-text.js
ADDED
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JS 侧矢量文字:
|
|
3
|
+
* - 默认 Hershey Futural 描边 → addLine(ASCII)
|
|
4
|
+
* - fontFile 为路径 / "system",或文本含非 ASCII 时:opentype 填轮廓(可中文)
|
|
5
|
+
*
|
|
6
|
+
* Hershey 字体数据公有领域(Dr. A. V. Hershey / NBS)。
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
const fs = require("fs");
|
|
11
|
+
const path = require("path");
|
|
12
|
+
const hershey = require("./hershey-futural.json");
|
|
13
|
+
const {
|
|
14
|
+
resolveSystemFont,
|
|
15
|
+
isSystemFontToken,
|
|
16
|
+
} = require("./system-font.js");
|
|
17
|
+
|
|
18
|
+
const DEG2RAD = Math.PI / 180;
|
|
19
|
+
|
|
20
|
+
/** @type {Map<string, object>} */
|
|
21
|
+
const fontCache = new Map();
|
|
22
|
+
|
|
23
|
+
/** Hershey 仅覆盖 ASCII 可打印区;其余需系统/指定 TTF。 */
|
|
24
|
+
function needsOutlineFont(text) {
|
|
25
|
+
for (let i = 0; i < text.length; i++) {
|
|
26
|
+
const c = text.charCodeAt(i);
|
|
27
|
+
if (c === 10 || c === 13) continue; // \n \r
|
|
28
|
+
if (c < 32 || c > 126) return true;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} text
|
|
35
|
+
* @param {{
|
|
36
|
+
* height?: number,
|
|
37
|
+
* strokeWidth?: number,
|
|
38
|
+
* align?: 'left'|'center'|'right',
|
|
39
|
+
* valign?: 'baseline'|'bottom'|'center'|'top',
|
|
40
|
+
* angle?: number,
|
|
41
|
+
* letterSpacing?: number,
|
|
42
|
+
* lineGap?: number,
|
|
43
|
+
* fontFile?: string|boolean,
|
|
44
|
+
* }} [opts]
|
|
45
|
+
*/
|
|
46
|
+
function normalizeOpts(opts, text) {
|
|
47
|
+
const o = opts || {};
|
|
48
|
+
const height = typeof o.height === "number" && o.height > 0 ? o.height : 1;
|
|
49
|
+
const strokeWidth =
|
|
50
|
+
typeof o.strokeWidth === "number" && o.strokeWidth > 0
|
|
51
|
+
? o.strokeWidth
|
|
52
|
+
: height * 0.08;
|
|
53
|
+
const align = o.align === "center" || o.align === "right" ? o.align : "left";
|
|
54
|
+
const valign =
|
|
55
|
+
o.valign === "bottom" || o.valign === "center" || o.valign === "top"
|
|
56
|
+
? o.valign
|
|
57
|
+
: "baseline";
|
|
58
|
+
const angle = typeof o.angle === "number" ? o.angle : 0;
|
|
59
|
+
const letterSpacing =
|
|
60
|
+
typeof o.letterSpacing === "number" ? o.letterSpacing : 0;
|
|
61
|
+
const lineGap = typeof o.lineGap === "number" && o.lineGap > 0 ? o.lineGap : 1.2;
|
|
62
|
+
|
|
63
|
+
let fontFile = "";
|
|
64
|
+
const raw = o.fontFile;
|
|
65
|
+
const wantSystem = isSystemFontToken(raw);
|
|
66
|
+
const wantAuto = raw == null || raw === "" || wantSystem;
|
|
67
|
+
const preferCjk = needsOutlineFont(text || "");
|
|
68
|
+
|
|
69
|
+
if (typeof raw === "string" && raw.trim() && !wantSystem) {
|
|
70
|
+
fontFile = path.resolve(raw.trim());
|
|
71
|
+
} else if (wantSystem || (wantAuto && preferCjk)) {
|
|
72
|
+
const sys = resolveSystemFont({ preferCjk: preferCjk || wantSystem });
|
|
73
|
+
if (!sys) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
"addText: no system TTF/OTF found" +
|
|
76
|
+
(preferCjk ? " for CJK" : "") +
|
|
77
|
+
". Set fontFile to a .ttf/.otf path, or JS_GERBV_FONT."
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
fontFile = sys;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
height,
|
|
85
|
+
strokeWidth,
|
|
86
|
+
align,
|
|
87
|
+
valign,
|
|
88
|
+
angle,
|
|
89
|
+
letterSpacing,
|
|
90
|
+
lineGap,
|
|
91
|
+
fontFile,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function glyphForHershey(ch) {
|
|
96
|
+
if (Object.prototype.hasOwnProperty.call(hershey.glyphs, ch)) {
|
|
97
|
+
return hershey.glyphs[ch];
|
|
98
|
+
}
|
|
99
|
+
return hershey.glyphs["?"] || { o: 10, s: [] };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @param {{ o?: number, s?: number[][] }} g
|
|
104
|
+
*/
|
|
105
|
+
function glyphAdvanceHershey(g) {
|
|
106
|
+
const declared = typeof g.o === "number" && g.o > 0 ? g.o : 0;
|
|
107
|
+
let minX = Infinity;
|
|
108
|
+
let maxX = -Infinity;
|
|
109
|
+
const strokes = g.s || [];
|
|
110
|
+
for (const flat of strokes) {
|
|
111
|
+
if (!flat) continue;
|
|
112
|
+
for (let i = 0; i + 1 < flat.length; i += 2) {
|
|
113
|
+
const x = flat[i];
|
|
114
|
+
if (x < minX) minX = x;
|
|
115
|
+
if (x > maxX) maxX = x;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!Number.isFinite(minX) || !Number.isFinite(maxX)) {
|
|
119
|
+
return declared || 10;
|
|
120
|
+
}
|
|
121
|
+
const leftBearing = Math.max(0, minX);
|
|
122
|
+
return Math.max(declared, maxX + leftBearing);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function lineWidthHershey(line, letterSpacing) {
|
|
126
|
+
let w = 0;
|
|
127
|
+
for (let i = 0; i < line.length; i++) {
|
|
128
|
+
w += glyphAdvanceHershey(glyphForHershey(line[i]));
|
|
129
|
+
if (i + 1 < line.length) w += letterSpacing;
|
|
130
|
+
}
|
|
131
|
+
return w;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function loadOpenTypeFont(fontFile) {
|
|
135
|
+
if (fontCache.has(fontFile)) return fontCache.get(fontFile);
|
|
136
|
+
if (!fs.existsSync(fontFile)) {
|
|
137
|
+
throw new Error("addText fontFile not found: " + fontFile);
|
|
138
|
+
}
|
|
139
|
+
const opentype = require("opentype.js");
|
|
140
|
+
let font;
|
|
141
|
+
try {
|
|
142
|
+
font = opentype.loadSync(fontFile);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
const msg = e && e.message ? e.message : String(e);
|
|
145
|
+
throw new Error(
|
|
146
|
+
"addText failed to load font (TTF/OTF only, not TTC): " +
|
|
147
|
+
fontFile +
|
|
148
|
+
" — " +
|
|
149
|
+
msg
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
fontCache.set(fontFile, font);
|
|
153
|
+
return font;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {object[]} commands
|
|
158
|
+
*/
|
|
159
|
+
function commandsToContours(commands) {
|
|
160
|
+
/** @type {object[][]} */
|
|
161
|
+
const contours = [];
|
|
162
|
+
/** @type {object[]|null} */
|
|
163
|
+
let cur = null;
|
|
164
|
+
for (const c of commands || []) {
|
|
165
|
+
if (c.type === "M") {
|
|
166
|
+
if (cur && cur.length) contours.push(cur);
|
|
167
|
+
cur = [{ t: "L", x: c.x, y: c.y }];
|
|
168
|
+
} else if (!cur) {
|
|
169
|
+
continue;
|
|
170
|
+
} else if (c.type === "L") {
|
|
171
|
+
cur.push({ t: "L", x: c.x, y: c.y });
|
|
172
|
+
} else if (c.type === "Q") {
|
|
173
|
+
cur.push({ t: "B2", c1x: c.x1, c1y: c.y1, x: c.x, y: c.y });
|
|
174
|
+
} else if (c.type === "C") {
|
|
175
|
+
cur.push({
|
|
176
|
+
t: "B3",
|
|
177
|
+
c1x: c.x1,
|
|
178
|
+
c1y: c.y1,
|
|
179
|
+
c2x: c.x2,
|
|
180
|
+
c2y: c.y2,
|
|
181
|
+
x: c.x,
|
|
182
|
+
y: c.y,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (cur && cur.length) contours.push(cur);
|
|
187
|
+
return contours;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @param {{x?:number,y?:number}[]} pts
|
|
192
|
+
*/
|
|
193
|
+
function signedAreaApprox(pts) {
|
|
194
|
+
let a = 0;
|
|
195
|
+
const n = pts.length;
|
|
196
|
+
if (n < 3) return 0;
|
|
197
|
+
for (let i = 0; i < n; i++) {
|
|
198
|
+
const p = pts[i];
|
|
199
|
+
const q = pts[(i + 1) % n];
|
|
200
|
+
a += (p.x || 0) * (q.y || 0) - (q.x || 0) * (p.y || 0);
|
|
201
|
+
}
|
|
202
|
+
return a / 2;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function contourSamplePts(contour) {
|
|
206
|
+
return contour
|
|
207
|
+
.filter((c) => typeof c.x === "number" && typeof c.y === "number")
|
|
208
|
+
.map((c) => ({ x: c.x, y: c.y }));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function strokeContours(img, contours, mapPoint, strokeWidth) {
|
|
212
|
+
for (const contour of contours) {
|
|
213
|
+
if (contour.length < 2) continue;
|
|
214
|
+
let prev = null;
|
|
215
|
+
for (const c of contour) {
|
|
216
|
+
if (c.t === "L") {
|
|
217
|
+
const pt = mapPoint(c.x, c.y);
|
|
218
|
+
if (prev) img.addLine(prev[0], prev[1], pt[0], pt[1], strokeWidth);
|
|
219
|
+
prev = pt;
|
|
220
|
+
} else if (c.t === "B2") {
|
|
221
|
+
const c1 = mapPoint(c.c1x, c.c1y);
|
|
222
|
+
const pt = mapPoint(c.x, c.y);
|
|
223
|
+
if (prev && typeof img.addBezier2 === "function") {
|
|
224
|
+
img.addBezier2(prev[0], prev[1], c1[0], c1[1], pt[0], pt[1], strokeWidth);
|
|
225
|
+
} else if (prev) {
|
|
226
|
+
img.addLine(prev[0], prev[1], pt[0], pt[1], strokeWidth);
|
|
227
|
+
}
|
|
228
|
+
prev = pt;
|
|
229
|
+
} else if (c.t === "B3") {
|
|
230
|
+
const c1 = mapPoint(c.c1x, c.c1y);
|
|
231
|
+
const c2 = mapPoint(c.c2x, c.c2y);
|
|
232
|
+
const pt = mapPoint(c.x, c.y);
|
|
233
|
+
if (prev && typeof img.addBezier3 === "function") {
|
|
234
|
+
img.addBezier3(
|
|
235
|
+
prev[0],
|
|
236
|
+
prev[1],
|
|
237
|
+
c1[0],
|
|
238
|
+
c1[1],
|
|
239
|
+
c2[0],
|
|
240
|
+
c2[1],
|
|
241
|
+
pt[0],
|
|
242
|
+
pt[1],
|
|
243
|
+
strokeWidth
|
|
244
|
+
);
|
|
245
|
+
} else if (prev) {
|
|
246
|
+
img.addLine(prev[0], prev[1], pt[0], pt[1], strokeWidth);
|
|
247
|
+
}
|
|
248
|
+
prev = pt;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function mapContourCmds(contour, mapPoint) {
|
|
255
|
+
const cmds = [];
|
|
256
|
+
for (const c of contour) {
|
|
257
|
+
if (c.t === "L") {
|
|
258
|
+
const [x, y] = mapPoint(c.x, c.y);
|
|
259
|
+
cmds.push({ t: "L", x, y });
|
|
260
|
+
} else if (c.t === "B2") {
|
|
261
|
+
const [c1x, c1y] = mapPoint(c.c1x, c.c1y);
|
|
262
|
+
const [x, y] = mapPoint(c.x, c.y);
|
|
263
|
+
cmds.push({ t: "B2", c1x, c1y, x, y });
|
|
264
|
+
} else if (c.t === "B3") {
|
|
265
|
+
const [c1x, c1y] = mapPoint(c.c1x, c.c1y);
|
|
266
|
+
const [c2x, c2y] = mapPoint(c.c2x, c.c2y);
|
|
267
|
+
const [x, y] = mapPoint(c.x, c.y);
|
|
268
|
+
cmds.push({ t: "B3", c1x, c1y, c2x, c2y, x, y });
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return cmds;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function pointInPoly(x, y, poly) {
|
|
275
|
+
let inside = false;
|
|
276
|
+
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
277
|
+
const xi = poly[i].x;
|
|
278
|
+
const yi = poly[i].y;
|
|
279
|
+
const xj = poly[j].x;
|
|
280
|
+
const yj = poly[j].y;
|
|
281
|
+
const intersect =
|
|
282
|
+
yi > y !== yj > y &&
|
|
283
|
+
x < ((xj - xi) * (y - yi)) / (yj - yi + 0.0) + xi;
|
|
284
|
+
if (intersect) inside = !inside;
|
|
285
|
+
}
|
|
286
|
+
return inside;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function ringCentroid(pts) {
|
|
290
|
+
let x = 0;
|
|
291
|
+
let y = 0;
|
|
292
|
+
for (const p of pts) {
|
|
293
|
+
x += p.x;
|
|
294
|
+
y += p.y;
|
|
295
|
+
}
|
|
296
|
+
const n = Math.max(1, pts.length);
|
|
297
|
+
return { x: x / n, y: y / n };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* TrueType:外环+孔三角化为仅 dark(与 createPolygon/createHole 同路径,无 %LPC*%)。
|
|
302
|
+
* path 为 canvas 系(Y 向下);mapPoint 已翻转到世界坐标。
|
|
303
|
+
*/
|
|
304
|
+
function drawOpenTypeContours(img, contours, mapPoint, strokeWidth) {
|
|
305
|
+
if (typeof img.__drawContour !== "function") {
|
|
306
|
+
strokeContours(img, contours, mapPoint, strokeWidth);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const { fillWithHolesNoLpc, cmdsToRing } = require("./fill-holes.js");
|
|
311
|
+
|
|
312
|
+
/** @type {{cmds:object[], area:number, ring:{x:number,y:number}[]}[]} */
|
|
313
|
+
const rings = [];
|
|
314
|
+
for (const contour of contours) {
|
|
315
|
+
if (contour.length < 2) continue;
|
|
316
|
+
const area = signedAreaApprox(contourSamplePts(contour));
|
|
317
|
+
const cmds = mapContourCmds(contour, mapPoint);
|
|
318
|
+
const ring = cmdsToRing(cmds);
|
|
319
|
+
if (ring.length < 3) continue;
|
|
320
|
+
rings.push({ cmds, area, ring });
|
|
321
|
+
}
|
|
322
|
+
if (!rings.length) return;
|
|
323
|
+
|
|
324
|
+
const outers = rings
|
|
325
|
+
.filter((r) => r.area >= 0)
|
|
326
|
+
.sort((a, b) => Math.abs(b.area) - Math.abs(a.area));
|
|
327
|
+
const holes = rings.filter((r) => r.area < 0);
|
|
328
|
+
if (!outers.length) {
|
|
329
|
+
rings.sort((a, b) => Math.abs(b.area) - Math.abs(a.area));
|
|
330
|
+
outers.push(rings[0]);
|
|
331
|
+
for (let i = 1; i < rings.length; i++) holes.push(rings[i]);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const draw = (cmds, clear) => img.__drawContour(cmds, !!clear);
|
|
335
|
+
const fillHoles =
|
|
336
|
+
typeof img.__fillWithHoles === "function"
|
|
337
|
+
? (outerCmds, holeCmds) => img.__fillWithHoles(outerCmds, holeCmds)
|
|
338
|
+
: (outerCmds, holeCmds) => fillWithHolesNoLpc(draw, outerCmds, holeCmds);
|
|
339
|
+
const used = new Set();
|
|
340
|
+
|
|
341
|
+
for (const outer of outers) {
|
|
342
|
+
/** @type {object[][]} */
|
|
343
|
+
const myHoles = [];
|
|
344
|
+
holes.forEach((h, idx) => {
|
|
345
|
+
if (used.has(idx)) return;
|
|
346
|
+
const c = ringCentroid(h.ring);
|
|
347
|
+
if (pointInPoly(c.x, c.y, outer.ring)) {
|
|
348
|
+
myHoles.push(h.cmds);
|
|
349
|
+
used.add(idx);
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
const ok = fillHoles(outer.cmds, myHoles);
|
|
353
|
+
if (ok === false) {
|
|
354
|
+
draw(outer.cmds, false);
|
|
355
|
+
for (const hc of myHoles) draw(hc, true);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
holes.forEach((h, idx) => {
|
|
359
|
+
if (used.has(idx)) return;
|
|
360
|
+
fillHoles(h.cmds, []) || draw(h.cmds, false);
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function addTextOpenType(img, x, y, text, o) {
|
|
365
|
+
const font = loadOpenTypeFont(o.fontFile);
|
|
366
|
+
const unitsPerEm = font.unitsPerEm || 1000;
|
|
367
|
+
const scale = o.height / unitsPerEm;
|
|
368
|
+
const fontSize = o.height;
|
|
369
|
+
const ascender = (font.ascender || unitsPerEm * 0.8) * scale;
|
|
370
|
+
const descender = (font.descender || -unitsPerEm * 0.2) * scale;
|
|
371
|
+
const lines = text.split(/\r?\n/);
|
|
372
|
+
const lineHeights = o.height * o.lineGap;
|
|
373
|
+
|
|
374
|
+
const widths = lines.map((ln) => {
|
|
375
|
+
let w = 0;
|
|
376
|
+
for (let i = 0; i < ln.length; i++) {
|
|
377
|
+
const g = font.charToGlyph(ln[i]);
|
|
378
|
+
w += (g.advanceWidth || 0) * scale;
|
|
379
|
+
if (i + 1 < ln.length) w += o.letterSpacing * scale;
|
|
380
|
+
}
|
|
381
|
+
return w;
|
|
382
|
+
});
|
|
383
|
+
const blockWidth = widths.length ? Math.max(0, ...widths) : 0;
|
|
384
|
+
const blockHeight =
|
|
385
|
+
lines.length <= 1 ? o.height : o.height + (lines.length - 1) * lineHeights;
|
|
386
|
+
|
|
387
|
+
let anchorDy = 0;
|
|
388
|
+
const ascent = ascender;
|
|
389
|
+
const descent = Math.abs(descender);
|
|
390
|
+
if (o.valign === "top") {
|
|
391
|
+
anchorDy = -ascent;
|
|
392
|
+
} else if (o.valign === "center") {
|
|
393
|
+
anchorDy =
|
|
394
|
+
(-ascent + (lines.length - 1) * lineHeights + descent) / 2;
|
|
395
|
+
} else if (o.valign === "bottom") {
|
|
396
|
+
anchorDy = (lines.length - 1) * lineHeights + descent;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const cos = Math.cos(o.angle * DEG2RAD);
|
|
400
|
+
const sin = Math.sin(o.angle * DEG2RAD);
|
|
401
|
+
|
|
402
|
+
function mapWorld(lx, ly) {
|
|
403
|
+
const rx = lx * cos - ly * sin;
|
|
404
|
+
const ry = lx * sin + ly * cos;
|
|
405
|
+
return [x + rx, y + ry];
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
for (let li = 0; li < lines.length; li++) {
|
|
409
|
+
const line = lines[li];
|
|
410
|
+
const w = widths[li];
|
|
411
|
+
let ox = 0;
|
|
412
|
+
if (o.align === "center") ox = -w / 2;
|
|
413
|
+
else if (o.align === "right") ox = -w;
|
|
414
|
+
|
|
415
|
+
const baselineY = anchorDy - li * lineHeights;
|
|
416
|
+
let pen = 0;
|
|
417
|
+
for (let ci = 0; ci < line.length; ci++) {
|
|
418
|
+
const glyph = font.charToGlyph(line[ci]);
|
|
419
|
+
const p = glyph.getPath(0, 0, fontSize);
|
|
420
|
+
const contours = commandsToContours(p.commands);
|
|
421
|
+
const penLocal = pen;
|
|
422
|
+
function mapPoint(px, py) {
|
|
423
|
+
const lx = ox + penLocal + px;
|
|
424
|
+
const ly = baselineY - py; // path +y down → local +y up
|
|
425
|
+
return mapWorld(lx, ly);
|
|
426
|
+
}
|
|
427
|
+
drawOpenTypeContours(img, contours, mapPoint, o.strokeWidth);
|
|
428
|
+
pen += (glyph.advanceWidth || 0) * scale;
|
|
429
|
+
if (ci + 1 < line.length) pen += o.letterSpacing * scale;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
img.__lastTextBounds = {
|
|
434
|
+
width: blockWidth,
|
|
435
|
+
height: blockHeight,
|
|
436
|
+
ascent,
|
|
437
|
+
descent,
|
|
438
|
+
};
|
|
439
|
+
return img;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function addTextHershey(img, x, y, text, o) {
|
|
443
|
+
const scale = o.height / hershey.capHeight;
|
|
444
|
+
const lines = text.split(/\r?\n/);
|
|
445
|
+
const lineHeights = o.height * o.lineGap;
|
|
446
|
+
const widths = lines.map((ln) => lineWidthHershey(ln, o.letterSpacing) * scale);
|
|
447
|
+
const blockWidth = widths.length ? Math.max(0, ...widths) : 0;
|
|
448
|
+
const blockHeight =
|
|
449
|
+
lines.length <= 1 ? o.height : o.height + (lines.length - 1) * lineHeights;
|
|
450
|
+
|
|
451
|
+
let anchorDy = 0;
|
|
452
|
+
const ascent = hershey.ascent * scale;
|
|
453
|
+
const descent = hershey.descent * scale;
|
|
454
|
+
if (o.valign === "top") {
|
|
455
|
+
anchorDy = -ascent;
|
|
456
|
+
} else if (o.valign === "center") {
|
|
457
|
+
anchorDy =
|
|
458
|
+
(-ascent + (lines.length - 1) * lineHeights + descent) / 2;
|
|
459
|
+
} else if (o.valign === "bottom") {
|
|
460
|
+
anchorDy = (lines.length - 1) * lineHeights + descent;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const cos = Math.cos(o.angle * DEG2RAD);
|
|
464
|
+
const sin = Math.sin(o.angle * DEG2RAD);
|
|
465
|
+
|
|
466
|
+
function mapPoint(lx, ly) {
|
|
467
|
+
const rx = lx * cos - ly * sin;
|
|
468
|
+
const ry = lx * sin + ly * cos;
|
|
469
|
+
return [x + rx, y + ry];
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
for (let li = 0; li < lines.length; li++) {
|
|
473
|
+
const line = lines[li];
|
|
474
|
+
const w = widths[li];
|
|
475
|
+
let ox = 0;
|
|
476
|
+
if (o.align === "center") ox = -w / 2;
|
|
477
|
+
else if (o.align === "right") ox = -w;
|
|
478
|
+
|
|
479
|
+
const baselineY = anchorDy - li * lineHeights;
|
|
480
|
+
let pen = 0;
|
|
481
|
+
for (let ci = 0; ci < line.length; ci++) {
|
|
482
|
+
const g = glyphForHershey(line[ci]);
|
|
483
|
+
const strokes = g.s || [];
|
|
484
|
+
for (const flat of strokes) {
|
|
485
|
+
if (!flat || flat.length < 4) continue;
|
|
486
|
+
let prev = null;
|
|
487
|
+
for (let i = 0; i + 1 < flat.length; i += 2) {
|
|
488
|
+
const fx = flat[i];
|
|
489
|
+
const fy = flat[i + 1];
|
|
490
|
+
const lx = ox + pen + fx * scale;
|
|
491
|
+
const ly = baselineY + (hershey.baseline - fy) * scale;
|
|
492
|
+
const [wx, wy] = mapPoint(lx, ly);
|
|
493
|
+
if (prev) {
|
|
494
|
+
img.addLine(prev[0], prev[1], wx, wy, o.strokeWidth);
|
|
495
|
+
}
|
|
496
|
+
prev = [wx, wy];
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
pen += glyphAdvanceHershey(g) * scale;
|
|
500
|
+
if (ci + 1 < line.length) pen += o.letterSpacing * scale;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
img.__lastTextBounds = {
|
|
505
|
+
width: blockWidth,
|
|
506
|
+
height: blockHeight,
|
|
507
|
+
ascent,
|
|
508
|
+
descent,
|
|
509
|
+
};
|
|
510
|
+
return img;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* @param {object} img
|
|
515
|
+
* @param {number} x
|
|
516
|
+
* @param {number} y
|
|
517
|
+
* @param {string} text
|
|
518
|
+
* @param {object} [opts]
|
|
519
|
+
*/
|
|
520
|
+
function addText(img, x, y, text, opts) {
|
|
521
|
+
if (typeof text !== "string") {
|
|
522
|
+
throw new Error("addText(x, y, text, opts?): text must be a string");
|
|
523
|
+
}
|
|
524
|
+
if (
|
|
525
|
+
typeof x !== "number" ||
|
|
526
|
+
typeof y !== "number" ||
|
|
527
|
+
Number.isNaN(x) ||
|
|
528
|
+
Number.isNaN(y)
|
|
529
|
+
) {
|
|
530
|
+
throw new Error("addText(x, y, text) requires numeric anchor");
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const o = normalizeOpts(opts, text);
|
|
534
|
+
if (o.fontFile) {
|
|
535
|
+
return addTextOpenType(img, x, y, text, o);
|
|
536
|
+
}
|
|
537
|
+
return addTextHershey(img, x, y, text, o);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
module.exports = {
|
|
541
|
+
addText,
|
|
542
|
+
fontMeta: {
|
|
543
|
+
name: hershey.name,
|
|
544
|
+
capHeight: hershey.capHeight,
|
|
545
|
+
baseline: hershey.baseline,
|
|
546
|
+
ascent: hershey.ascent,
|
|
547
|
+
descent: hershey.descent,
|
|
548
|
+
},
|
|
549
|
+
loadOpenTypeFont,
|
|
550
|
+
needsOutlineFont,
|
|
551
|
+
resolveSystemFont,
|
|
552
|
+
};
|