Skip to content

Commit d14cdc9

Browse files
committed
Text rendering fixes
* process overbars * approximate italics behavior better Fixes #154
1 parent 82928fb commit d14cdc9

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

InteractiveHtmlBom/web/render.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ function calcFontPoint(linepoint, text, offsetx, offsety, tilt) {
1111
linepoint[0] * text.width + offsetx,
1212
linepoint[1] * text.height + offsety
1313
];
14-
// Adding half a line height here is technically a bug
15-
// but pcbnew currently does the same, text is slightly shifted.
16-
point[0] -= (point[1] + text.height * 0.5) * tilt;
14+
// This approximates pcbnew behavior with how text tilts depending on horizontal justification
15+
point[0] -= (linepoint[1] + 0.5 * (1 + text.horiz_justify)) * text.height * tilt;
1716
return point;
1817
}
1918

@@ -32,6 +31,7 @@ function drawtext(ctx, text, color, flip) {
3231
return;
3332
}
3433
ctx.translate(...text.pos);
34+
ctx.translate(text.thickness * 0.5, 0);
3535
var angle = -text.angle;
3636
if (text.attr.includes("mirrored")) {
3737
ctx.scale(-1, 1);
@@ -48,13 +48,18 @@ function drawtext(ctx, text, color, flip) {
4848
ctx.rotate(deg2rad(angle));
4949
for (var i in txt) {
5050
var offsety = (-(txt.length - 1) + i * 2) * interline + text.height / 2;
51-
var lineWidth = 0;
52-
for (var c of txt[i]) {
53-
if (c == '\t') {
51+
var lineWidth = text.thickness + interline * tilt;
52+
for (var j = 0; j < txt[i].length; j++) {
53+
if (txt[i][j] == '\t') {
5454
var fourSpaces = 4 * pcbdata.font_data[' '].w * text.width;
5555
lineWidth += fourSpaces - lineWidth % fourSpaces;
5656
} else {
57-
lineWidth += pcbdata.font_data[c].w * text.width;
57+
if (txt[i][j] == '~') {
58+
j++;
59+
if (j == txt[i].length)
60+
break;
61+
}
62+
lineWidth += pcbdata.font_data[txt[i][j]].w * text.width;
5863
}
5964
}
6065
var offsetx = 0;
@@ -71,21 +76,45 @@ function drawtext(ctx, text, color, flip) {
7176
offsetx -= lineWidth;
7277
break;
7378
}
74-
for (var c of txt[i]) {
75-
if (c == '\t') {
79+
var inOverbar = false;
80+
for (var j = 0; j < txt[i].length; j++) {
81+
if (txt[i][j] == '\t') {
7682
var fourSpaces = 4 * pcbdata.font_data[' '].w * text.width;
7783
offsetx += fourSpaces - offsetx % fourSpaces;
7884
continue;
85+
} else if (txt[i][j] == '~') {
86+
j++;
87+
if (j == txt[i].length)
88+
break;
89+
if (txt[i][j] != '~') {
90+
inOverbar = !inOverbar;
91+
}
92+
}
93+
var glyph = pcbdata.font_data[txt[i][j]];
94+
if (inOverbar) {
95+
var overbarStart = [offsetx, -text.height * 1.4 + offsety];
96+
var overbarEnd = [offsetx + text.width * glyph.w, overbarStart[1]];
97+
98+
if (!lastHadOverbar) {
99+
overbarStart[0] += text.height * 1.4 * tilt;
100+
lastHadOverbar = true;
101+
}
102+
ctx.beginPath();
103+
ctx.moveTo(...overbarStart);
104+
ctx.lineTo(...overbarEnd);
105+
ctx.stroke();
106+
} else {
107+
lastHadOverbar = false;
79108
}
80-
for (var line of pcbdata.font_data[c].l) {
109+
for (var line of glyph.l) {
81110
ctx.beginPath();
82111
ctx.moveTo(...calcFontPoint(line[0], text, offsetx, offsety, tilt));
83-
for (var i = 1; i < line.length; i++) {
84-
ctx.lineTo(...calcFontPoint(line[i], text, offsetx, offsety, tilt));
112+
for (var k = 1; k < line.length; k++) {
113+
ctx.lineTo(...calcFontPoint(line[k], text, offsetx, offsety, tilt));
85114
}
86115
ctx.stroke();
87116
}
88-
offsetx += pcbdata.font_data[c].w * text.width;
117+
offsetx += glyph.w * text.width;
89118
}
90119
}
91120
ctx.restore();

0 commit comments

Comments
 (0)