Skip to content

Commit

Permalink
Fix tab handling in text rendering
Browse files Browse the repository at this point in the history
Fixes #120
  • Loading branch information
qu1ck committed Nov 12, 2019
1 parent 70239a4 commit d0bd8d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions InteractiveHtmlBom/core/fontparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def parse_font_char(self, chr):

def parse_font_for_string(self, s):
for c in s:
if c == '\t' and ' ' not in self.parsed_font:
# tabs rely on space char to calculate offset
self.parsed_font[' '] = self.parse_font_char(' ')
if c not in self.parsed_font and ord(c) >= ord(' '):
self.parsed_font[c] = self.parse_font_char(c)

Expand Down
12 changes: 11 additions & 1 deletion InteractiveHtmlBom/web/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ function drawtext(ctx, text, color, flip) {
var offsety = (-(txt.length - 1) + i * 2) * interline + text.height / 2;
var lineWidth = 0;
for (var c of txt[i]) {
lineWidth += pcbdata.font_data[c].w * text.width;
if (c == '\t') {
var fourSpaces = 4 * pcbdata.font_data[' '].w * text.width;
lineWidth += fourSpaces - lineWidth % fourSpaces;
} else {
lineWidth += pcbdata.font_data[c].w * text.width;
}
}
var offsetx = 0;
switch (text.horiz_justify) {
Expand All @@ -75,6 +80,11 @@ function drawtext(ctx, text, color, flip) {
break;
}
for (var c of txt[i]) {
if (c == '\t') {
var fourSpaces = 4 * pcbdata.font_data[' '].w * text.width;
offsetx += fourSpaces - offsetx % fourSpaces;
continue;
}
for (var line of pcbdata.font_data[c].l) {
ctx.beginPath();
ctx.moveTo(...calcFontPoint(line[0], text, offsetx, offsety, tilt));
Expand Down

0 comments on commit d0bd8d5

Please sign in to comment.