MCPcopy Create free account
hub / github.com/openscopeproject/InteractiveHtmlBom / drawText

Function drawText

InteractiveHtmlBom/web/render.js:19–122  ·  view source on GitHub ↗
(ctx, text, color)

Source from the content-addressed store, hash-verified

17}
18
19function drawText(ctx, text, color) {
20 if ("ref" in text && !settings.renderReferences) return;
21 if ("val" in text && !settings.renderValues) return;
22 ctx.save();
23 ctx.fillStyle = color;
24 ctx.strokeStyle = color;
25 ctx.lineCap = "round";
26 ctx.lineJoin = "round";
27 if ("svgpath" in text) {
28 if ("thickness" in text) {
29 ctx.lineWidth = text.thickness;
30 ctx.stroke(new Path2D(text.svgpath));
31 } else if ("fillrule" in text) {
32 ctx.fill(new Path2D(text.svgpath), text.fillrule);
33 }
34 ctx.restore();
35 return;
36 }
37 ctx.lineWidth = text.thickness;
38 if ("polygons" in text) {
39 ctx.fill(getPolygonsPath(text));
40 ctx.restore();
41 return;
42 }
43 ctx.translate(...text.pos);
44 ctx.translate(text.thickness * 0.5, 0);
45 var angle = -text.angle;
46 if (text.attr.includes("mirrored")) {
47 ctx.scale(-1, 1);
48 angle = -angle;
49 }
50 var tilt = 0;
51 if (text.attr.includes("italic")) {
52 tilt = 0.125;
53 }
54 var interline = text.height * 1.5 + text.thickness;
55 var txt = text.text.split("\n");
56 // KiCad ignores last empty line.
57 if (txt[txt.length - 1] == '') txt.pop();
58 ctx.rotate(deg2rad(angle));
59 var offsety = (1 - text.justify[1]) / 2 * text.height; // One line offset
60 offsety -= (txt.length - 1) * (text.justify[1] + 1) / 2 * interline; // Multiline offset
61 for (var i in txt) {
62 var lineWidth = text.thickness + interline / 2 * tilt;
63 for (var j = 0; j < txt[i].length; j++) {
64 if (txt[i][j] == '\t') {
65 var fourSpaces = 4 * pcbdata.font_data[' '].w * text.width;
66 lineWidth += fourSpaces - lineWidth % fourSpaces;
67 } else {
68 if (txt[i][j] == '~') {
69 j++;
70 if (j == txt[i].length)
71 break;
72 }
73 lineWidth += pcbdata.font_data[txt[i][j]].w * text.width;
74 }
75 }
76 var offsetx = -lineWidth * (text.justify[0] + 1) / 2;

Callers 2

drawDrawingFunction · 0.85
drawBgLayerFunction · 0.85

Calls 4

getPolygonsPathFunction · 0.85
deg2radFunction · 0.85
calcFontPointFunction · 0.85
saveMethod · 0.80

Tested by

no test coverage detected