Skip to content

Commit 7d78c11

Browse files
committed
Use kicad's own text rendering when possible
Transform kicad text objects into segments list using native rendering method and then build an svgpath from it for the web renderer. Fixes #198
1 parent 361866a commit 7d78c11

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

InteractiveHtmlBom/ecad/kicad.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .common import EcadParser, Component
77
from .kicad_extra import find_latest_schematic_data, parse_schematic_data
8+
from .svgpath import create_path
89
from ..core import ibom
910
from ..core.config import Config
1011
from ..core.fontparser import FontParser
@@ -134,6 +135,21 @@ def parse_text(self, d):
134135
pos = self.normalize(d.GetPosition())
135136
if not d.IsVisible():
136137
return None
138+
if hasattr(d, "GetTextThickness"):
139+
thickness = d.GetTextThickness() * 1e-6
140+
else:
141+
thickness = d.GetThickness() * 1e-6
142+
if hasattr(d, 'TransformToSegmentList'):
143+
segments = [self.normalize(p) for p in d.TransformToSegmentList()]
144+
lines = []
145+
for i in range(0, len(segments), 2):
146+
if i == 0 or segments[i-1] != segments[i]:
147+
lines.append([segments[i]])
148+
lines[-1].append(segments[i+1])
149+
return {
150+
"thickness": thickness,
151+
"svgpath": create_path(lines)
152+
}
137153
if d.GetClass() == "MTEXT":
138154
angle = d.GetDrawRotation() * 0.1
139155
else:
@@ -147,10 +163,6 @@ def parse_text(self, d):
147163
else:
148164
height = d.GetHeight() * 1e-6
149165
width = d.GetWidth() * 1e-6
150-
if hasattr(d, "GetTextThickness"):
151-
thickness = d.GetTextThickness() * 1e-6
152-
else:
153-
thickness = d.GetThickness() * 1e-6
154166
if hasattr(d, "GetShownText"):
155167
text = d.GetShownText()
156168
else:
@@ -163,6 +175,7 @@ def parse_text(self, d):
163175
attributes.append("italic")
164176
if d.IsBold():
165177
attributes.append("bold")
178+
166179
return {
167180
"pos": pos,
168181
"text": text,
@@ -188,8 +201,8 @@ def parse_edges(self, pcb):
188201
edges = []
189202
drawings = list(pcb.GetDrawings())
190203
bbox = None
191-
for m in self.footprints:
192-
for g in m.GraphicalItems():
204+
for f in self.footprints:
205+
for g in f.GraphicalItems():
193206
drawings.append(g)
194207
for d in drawings:
195208
if d.GetLayer() == pcbnew.Edge_Cuts:
@@ -228,10 +241,10 @@ def parse_drawings_on_layers(self, drawings, f_layer, b_layer):
228241

229242
def get_all_drawings(self):
230243
drawings = [(d.GetClass(), d) for d in list(self.board.GetDrawings())]
231-
for m in self.footprints:
232-
drawings.append(("ref", m.Reference()))
233-
drawings.append(("val", m.Value()))
234-
for d in m.GraphicalItems():
244+
for f in self.footprints:
245+
drawings.append(("ref", f.Reference()))
246+
drawings.append(("val", f.Value()))
247+
for d in f.GraphicalItems():
235248
drawings.append((d.GetClass(), d))
236249
return drawings
237250

InteractiveHtmlBom/ecad/svgpath.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,15 @@ def parse_path(pathdef, logger, current_pos=0j):
507507
current_pos = end
508508

509509
return segments
510+
511+
512+
def create_path(lines):
513+
"""Returns a path d-string."""
514+
515+
parts = []
516+
for line in lines:
517+
parts.append('M{},{}'.format(line[0][0], line[0][1]))
518+
for point in line[1:]:
519+
parts.append('L{},{}'.format(point[0], point[1]))
520+
521+
return ''.join(parts)

InteractiveHtmlBom/web/render.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function calcFontPoint(linepoint, text, offsetx, offsety, tilt) {
1616
return point;
1717
}
1818

19-
function drawtext(ctx, text, color, flip) {
19+
function drawText(ctx, text, color) {
2020
if ("ref" in text && !settings.renderReferences) return;
2121
if ("val" in text && !settings.renderValues) return;
2222
ctx.save();
@@ -226,13 +226,13 @@ function drawPolygonShape(ctx, shape, color) {
226226
ctx.restore();
227227
}
228228

229-
function drawDrawing(ctx, layer, scalefactor, drawing, color) {
229+
function drawDrawing(ctx, scalefactor, drawing, color) {
230230
if (["segment", "arc", "circle", "curve"].includes(drawing.type)) {
231231
drawedge(ctx, scalefactor, drawing, color);
232232
} else if (drawing.type == "polygon") {
233233
drawPolygonShape(ctx, drawing, color);
234234
} else {
235-
drawtext(ctx, drawing, color, layer == "B");
235+
drawText(ctx, drawing, color);
236236
}
237237
}
238238

@@ -313,7 +313,7 @@ function drawFootprint(ctx, layer, scalefactor, footprint, padcolor, outlinecolo
313313
// draw drawings
314314
for (var drawing of footprint.drawings) {
315315
if (drawing.layer == layer) {
316-
drawDrawing(ctx, layer, scalefactor, drawing.drawing, padcolor);
316+
drawDrawing(ctx, scalefactor, drawing.drawing, padcolor);
317317
}
318318
}
319319
// draw pads
@@ -364,7 +364,7 @@ function drawBgLayer(layername, canvas, layer, scalefactor, edgeColor, polygonCo
364364
} else if (d.type == "polygon") {
365365
drawPolygonShape(ctx, d, polygonColor);
366366
} else {
367-
drawtext(ctx, d, textColor, layer == "B");
367+
drawText(ctx, d, textColor);
368368
}
369369
}
370370
}

0 commit comments

Comments
 (0)