From 48d4aeee947a913bf949ab60f09754089129835f Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 6 Apr 2023 16:41:10 +0200 Subject: Make SVG export even smaller --- gerbonara/cad/protoboard.py | 1 - gerbonara/cam.py | 4 +++- gerbonara/graphic_primitives.py | 4 ++-- gerbonara/layers.py | 35 ++++++++++++++++++++++++++++------- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/gerbonara/cad/protoboard.py b/gerbonara/cad/protoboard.py index c9547aa..41276e2 100644 --- a/gerbonara/cad/protoboard.py +++ b/gerbonara/cad/protoboard.py @@ -287,7 +287,6 @@ class EmptyProtoArea: group = ObjectGroup(0, 0, top_copper=[Region([(min_x, min_y), (max_x, min_y), (max_x, max_y), (min_x, max_y)], unit=unit, polarity_dark=True)]) group.bounding_box = lambda *args, **kwargs: None - print('adding', self, bbox, group.bounding_box(), file=sys.stderr) yield group @property diff --git a/gerbonara/cam.py b/gerbonara/cam.py index 49c2df3..0ee67a5 100644 --- a/gerbonara/cam.py +++ b/gerbonara/cam.py @@ -249,7 +249,9 @@ class Polyline: (x0, y0), *rest = self.coords d = f'M {float(x0):.6} {float(y0):.6} ' + ' '.join(f'L {float(x):.6} {float(y):.6}' for x, y in rest) width = f'{self.width:.6}' if not math.isclose(self.width, 0) else '0.01mm' - return tag('path', d=d, style=f'fill: none; stroke: {color}; stroke-width: {float(width):.6}; stroke-linejoin: round; stroke-linecap: round') + return tag('path', d=d, + fill='none', stroke=color, + stroke_width=f'{float(width):.6}') class CamFile: diff --git a/gerbonara/graphic_primitives.py b/gerbonara/graphic_primitives.py index 13d626d..13854e1 100644 --- a/gerbonara/graphic_primitives.py +++ b/gerbonara/graphic_primitives.py @@ -189,7 +189,7 @@ class Line(GraphicPrimitive): color = fg if self.polarity_dark else bg width = f'{self.width:.6}' if not math.isclose(self.width, 0) else '0.01mm' return tag('path', d=f'M {float(self.x1):.6} {float(self.y1):.6} L {float(self.x2):.6} {float(self.y2):.6}', - fill='none', stroke=color, stroke_width=str(width), stroke_linecap='round') + fill='none', stroke=color, stroke_width=str(width)) @dataclass(frozen=True) class Arc(GraphicPrimitive): @@ -241,7 +241,7 @@ class Arc(GraphicPrimitive): arc = svg_arc((self.x1, self.y1), (self.x2, self.y2), (self.cx, self.cy), self.clockwise) width = f'{self.width:.6}' if not math.isclose(self.width, 0) else '0.01mm' return tag('path', d=f'M {float(self.x1):.6} {float(self.y1):.6} {arc}', - fill='none', stroke=color, stroke_width=width, stroke_linecap='round') + fill='none', stroke=color, stroke_width=width) @dataclass(frozen=True) class Rectangle(GraphicPrimitive): diff --git a/gerbonara/layers.py b/gerbonara/layers.py index 22a845f..10101dc 100644 --- a/gerbonara/layers.py +++ b/gerbonara/layers.py @@ -703,15 +703,17 @@ class LayerStack: bounds = svg_unit.convert_bounds_from(arg_unit, force_bounds) else: bounds = self.bounding_box(svg_unit, default=((0, 0), (0, 0))) + + stroke_attrs = {'stroke_linejoin': 'round', 'stroke_linecap': 'round'} tags = [] for (side, use), layer in self.graphic_layers.items(): tags.append(tag('g', list(layer.svg_objects(svg_unit=svg_unit, fg='black', bg="white", tag=Tag)), - id=f'l-{side}-{use}')) + **stroke_attrs, id=f'l-{side}-{use}')) for i, layer in enumerate(self.drill_layers): tags.append(tag('g', list(layer.svg_objects(svg_unit=svg_unit, fg='black', bg="white", tag=Tag)), - id=f'l-drill-{i}')) + **stroke_attrs, id=f'l-drill-{i}')) return setup_svg(tags, bounds, margin=margin, arg_unit=arg_unit, svg_unit=svg_unit, tag=tag) @@ -779,6 +781,7 @@ class LayerStack: '''.strip())) inkscape_attrs = lambda label: dict(inkscape__groupmode='layer', inkscape__label=label) if inkscape else {} + stroke_attrs = {'stroke_linejoin': 'round', 'stroke_linecap': 'round'} layers = [] for use in ['copper', 'mask', 'silk', 'paste']: @@ -788,18 +791,36 @@ class LayerStack: layer = self[(side, use)] fg, bg = ('white', 'black') if use != 'mask' else ('black', 'white') - objects = list(layer.instance.svg_objects(svg_unit=svg_unit, fg=fg, bg=bg, tag=Tag)) + + default_fill = {'copper': fg, 'mask': fg, 'silk': 'none', 'paste': fg}[use] + default_stroke = {'copper': 'none', 'mask': 'none', 'silk': fg, 'paste': 'none'}[use] + + objects = [] + for obj in layer.instance.svg_objects(svg_unit=svg_unit, fg=fg, bg=bg, tag=Tag): + if obj.attrs.get('fill') == default_fill: + del obj.attrs['fill'] + elif 'fill' not in obj.attrs: + obj.attrs['fill'] = 'none' + + if obj.attrs.get('stroke') == default_stroke: + del obj.attrs['stroke'] + elif default_stroke != 'none' and 'stroke' not in obj.attrs: + obj.attrs['stroke'] = 'none' + objects.append(obj) + if use == 'mask': - objects.insert(0, tag('path', id='outline-path', d=self.outline_svg_d(unit=svg_unit), style='fill:white')) - layers.append(tag('g', objects, id=f'l-{side}-{use}', filter=f'url(#f-{use})', **inkscape_attrs(f'{side} {use}'))) + objects.insert(0, tag('path', id='outline-path', d=self.outline_svg_d(unit=svg_unit), fill='white')) + layers.append(tag('g', objects, id=f'l-{side}-{use}', filter=f'url(#f-{use})', + fill=default_fill, stroke=default_stroke, **stroke_attrs, + **inkscape_attrs(f'{side} {use}'))) for i, layer in enumerate(self.drill_layers): layers.append(tag('g', list(layer.instance.svg_objects(svg_unit=svg_unit, fg='white', bg='black', tag=Tag)), - id=f'g-drill-{i}', filter=f'url(#f-drill)', **inkscape_attrs(f'drill-{i}'))) + id=f'g-drill-{i}', filter=f'url(#f-drill)', **stroke_attrs, **inkscape_attrs(f'drill-{i}'))) if self.outline: layers.append(tag('g', list(self.outline.instance.svg_objects(svg_unit=svg_unit, fg='white', bg='black', tag=Tag)), - id=f'g-outline-{i}', **inkscape_attrs(f'outline-{i}'))) + id=f'g-outline-{i}', **stroke_attrs, **inkscape_attrs(f'outline-{i}'))) layer_group = tag('g', layers, transform=f'translate(0 {bounds[0][1] + bounds[1][1]}) scale(1 -1)') tags = [tag('defs', filter_defs), layer_group] -- cgit