summaryrefslogtreecommitdiff
path: root/gerber/render
diff options
context:
space:
mode:
Diffstat (limited to 'gerber/render')
-rw-r--r--gerber/render/__init__.py1
-rw-r--r--gerber/render/cairo_backend.py172
-rw-r--r--gerber/render/render.py19
-rw-r--r--gerber/render/theme.py23
4 files changed, 113 insertions, 102 deletions
diff --git a/gerber/render/__init__.py b/gerber/render/__init__.py
index f76d28f..3598c4d 100644
--- a/gerber/render/__init__.py
+++ b/gerber/render/__init__.py
@@ -25,3 +25,4 @@ SVG is the only supported format.
from .cairo_backend import GerberCairoContext
+from .render import RenderSettings
diff --git a/gerber/render/cairo_backend.py b/gerber/render/cairo_backend.py
index 2370eb9..df4fcf1 100644
--- a/gerber/render/cairo_backend.py
+++ b/gerber/render/cairo_backend.py
@@ -17,6 +17,7 @@
import cairocffi as cairo
+import os
import tempfile
import copy
@@ -36,16 +37,16 @@ class GerberCairoContext(GerberContext):
super(GerberCairoContext, self).__init__()
self.scale = (scale, scale)
self.surface = None
+ self.surface_buffer = None
self.ctx = None
self.active_layer = None
self.active_matrix = None
self.output_ctx = None
- self.bg = False
- self.mask = None
- self.mask_ctx = None
+ self.has_bg = False
self.origin_in_inch = None
self.size_in_inch = None
self._xform_matrix = None
+ self._render_count = 0
@property
def origin_in_pixels(self):
@@ -66,10 +67,8 @@ class GerberCairoContext(GerberContext):
self.size_in_inch = size_in_inch if self.size_in_inch is None else self.size_in_inch
if (self.surface is None) or new_surface:
self.surface_buffer = tempfile.NamedTemporaryFile()
- self.surface = cairo.SVGSurface(
- self.surface_buffer, size_in_pixels[0], size_in_pixels[1])
+ self.surface = cairo.SVGSurface(self.surface_buffer, size_in_pixels[0], size_in_pixels[1])
self.output_ctx = cairo.Context(self.surface)
- self.output_ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)
self.output_ctx.scale(1, -1)
self.output_ctx.translate(-(origin_in_inch[0] * self.scale[0]),
(-origin_in_inch[1] * self.scale[0]) - size_in_pixels[1])
@@ -77,20 +76,44 @@ class GerberCairoContext(GerberContext):
x0=-self.origin_in_pixels[0],
y0=self.size_in_pixels[1] + self.origin_in_pixels[1])
- def render_layers(self, layers, filename, theme=THEMES['default']):
+ def render_layer(self, layer, filename=None, settings=None, bgsettings=None,
+ verbose=False):
+ if settings is None:
+ settings = THEMES['default'].get(layer.layer_class, RenderSettings())
+ if bgsettings is None:
+ bgsettings = THEMES['default'].get('background', RenderSettings())
+
+ if self._render_count == 0:
+ if verbose:
+ print('[Render]: Rendering Background.')
+ self.clear()
+ self.set_bounds(layer.bounds)
+ self._paint_background(bgsettings)
+ if verbose:
+ print('[Render]: Rendering {} Layer.'.format(layer.layer_class))
+ self._render_count += 1
+ self._render_layer(layer, settings)
+ if filename is not None:
+ self.dump(filename, verbose)
+
+ def render_layers(self, layers, filename, theme=THEMES['default'],
+ verbose=False):
""" Render a set of layers
"""
- self.set_bounds(layers[0].bounds, True)
- self._paint_background(True)
-
+ self.clear()
+ bgsettings = theme['background']
for layer in layers:
- self._render_layer(layer, theme)
- self.dump(filename)
+ settings = theme.get(layer.layer_class, RenderSettings())
+ self.render_layer(layer, settings=settings, bgsettings=bgsettings,
+ verbose=verbose)
+ self.dump(filename, verbose)
- def dump(self, filename):
+ def dump(self, filename, verbose=False):
""" Save image as `filename`
"""
- is_svg = filename.lower().endswith(".svg")
+ is_svg = os.path.splitext(filename.lower())[1] == '.svg'
+ if verbose:
+ print('[Render]: Writing image to {}'.format(filename))
if is_svg:
self.surface.finish()
self.surface_buffer.flush()
@@ -115,30 +138,33 @@ class GerberCairoContext(GerberContext):
self.surface_buffer.flush()
return self.surface_buffer.read()
- def _render_layer(self, layer, theme=THEMES['default']):
- settings = theme.get(layer.layer_class, RenderSettings())
- self.color = settings.color
- self.alpha = settings.alpha
- self.invert = settings.invert
+ def clear(self):
+ self.surface = None
+ self.output_ctx = None
+ self.has_bg = False
+ self.origin_in_inch = None
+ self.size_in_inch = None
+ self._xform_matrix = None
+ self._render_count = 0
+ if hasattr(self.surface_buffer, 'close'):
+ self.surface_buffer.close()
+ self.surface_buffer = None
+ def _render_layer(self, layer, settings):
+ self.invert = settings.invert
# Get a new clean layer to render on
self._new_render_layer(mirror=settings.mirror)
for prim in layer.primitives:
self.render(prim)
# Add layer to image
- self._flatten()
+ self._paint(settings.color, settings.alpha)
def _render_line(self, line, color):
start = [pos * scale for pos, scale in zip(line.start, self.scale)]
end = [pos * scale for pos, scale in zip(line.end, self.scale)]
- if not self.invert:
- self.ctx.set_source_rgba(*color, alpha=self.alpha)
- self.ctx.set_operator(cairo.OPERATOR_OVER
- if line.level_polarity == 'dark'
- else cairo.OPERATOR_CLEAR)
- else:
- self.ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
- self.ctx.set_operator(cairo.OPERATOR_CLEAR)
+ self.ctx.set_operator(cairo.OPERATOR_SOURCE
+ if line.level_polarity == 'dark' and
+ (not self.invert) else cairo.OPERATOR_CLEAR)
if isinstance(line.aperture, Circle):
width = line.aperture.diameter
self.ctx.set_line_width(width * self.scale[0])
@@ -162,14 +188,9 @@ class GerberCairoContext(GerberContext):
angle1 = arc.start_angle
angle2 = arc.end_angle
width = arc.aperture.diameter if arc.aperture.diameter != 0 else 0.001
- if not self.invert:
- self.ctx.set_source_rgba(*color, alpha=self.alpha)
- self.ctx.set_operator(cairo.OPERATOR_OVER
- if arc.level_polarity == 'dark'
- else cairo.OPERATOR_CLEAR)
- else:
- self.ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
- self.ctx.set_operator(cairo.OPERATOR_CLEAR)
+ self.ctx.set_operator(cairo.OPERATOR_SOURCE
+ if arc.level_polarity == 'dark' and
+ (not self.invert) else cairo.OPERATOR_CLEAR)
self.ctx.set_line_width(width * self.scale[0])
self.ctx.set_line_cap(cairo.LINE_CAP_ROUND)
self.ctx.move_to(*start) # You actually have to do this...
@@ -181,14 +202,9 @@ class GerberCairoContext(GerberContext):
self.ctx.move_to(*end) # ...lame
def _render_region(self, region, color):
- if not self.invert:
- self.ctx.set_source_rgba(*color, alpha=self.alpha)
- self.ctx.set_operator(cairo.OPERATOR_OVER
- if region.level_polarity == 'dark'
- else cairo.OPERATOR_CLEAR)
- else:
- self.ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
- self.ctx.set_operator(cairo.OPERATOR_CLEAR)
+ self.ctx.set_operator(cairo.OPERATOR_SOURCE
+ if region.level_polarity == 'dark' and
+ (not self.invert) else cairo.OPERATOR_CLEAR)
self.ctx.set_line_width(0)
self.ctx.set_line_cap(cairo.LINE_CAP_ROUND)
self.ctx.move_to(*self.scale_point(region.primitives[0].start))
@@ -210,29 +226,22 @@ class GerberCairoContext(GerberContext):
def _render_circle(self, circle, color):
center = self.scale_point(circle.position)
- if not self.invert:
- self.ctx.set_source_rgba(*color, alpha=self.alpha)
- self.ctx.set_operator(
- cairo.OPERATOR_OVER if circle.level_polarity == 'dark' else cairo.OPERATOR_CLEAR)
- else:
- self.ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
- self.ctx.set_operator(cairo.OPERATOR_CLEAR)
+ self.ctx.set_operator(cairo.OPERATOR_SOURCE
+ if circle.level_polarity == 'dark' and
+ (not self.invert) else cairo.OPERATOR_CLEAR)
self.ctx.set_line_width(0)
- self.ctx.arc(*center, radius=circle.radius *
- self.scale[0], angle1=0, angle2=2 * math.pi)
+ self.ctx.arc(*center, radius=(circle.radius * self.scale[0]), angle1=0,
+ angle2=(2 * math.pi))
self.ctx.fill()
def _render_rectangle(self, rectangle, color):
lower_left = self.scale_point(rectangle.lower_left)
- width, height = tuple([abs(coord) for coord in self.scale_point((rectangle.width, rectangle.height))])
-
- if not self.invert:
- self.ctx.set_source_rgba(*color, alpha=self.alpha)
- self.ctx.set_operator(
- cairo.OPERATOR_OVER if rectangle.level_polarity == 'dark' else cairo.OPERATOR_CLEAR)
- else:
- self.ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
- self.ctx.set_operator(cairo.OPERATOR_CLEAR)
+ width, height = tuple([abs(coord) for coord in
+ self.scale_point((rectangle.width,
+ rectangle.height))])
+ self.ctx.set_operator(cairo.OPERATOR_SOURCE
+ if rectangle.level_polarity == 'dark' and
+ (not self.invert) else cairo.OPERATOR_CLEAR)
self.ctx.set_line_width(0)
self.ctx.rectangle(*lower_left, width=width, height=height)
self.ctx.fill()
@@ -247,34 +256,31 @@ class GerberCairoContext(GerberContext):
self._render_circle(circle, color)
def _render_test_record(self, primitive, color):
- position = [pos + origin for pos, origin in zip(primitive.position, self.origin_in_inch)]
- self.ctx.set_operator(cairo.OPERATOR_OVER)
+ position = [pos + origin for pos, origin in
+ zip(primitive.position, self.origin_in_inch)]
self.ctx.select_font_face(
'monospace', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
self.ctx.set_font_size(13)
self._render_circle(Circle(position, 0.015), color)
- self.ctx.set_source_rgba(*color, alpha=self.alpha)
- self.ctx.set_operator(
- cairo.OPERATOR_OVER if primitive.level_polarity == 'dark' else cairo.OPERATOR_CLEAR)
- self.ctx.move_to(*[self.scale[0] * (coord + 0.015)
- for coord in position])
+ self.ctx.set_operator(cairo.OPERATOR_SOURCE
+ if primitive.level_polarity == 'dark' and
+ (not self.invert) else cairo.OPERATOR_CLEAR)
+ self.ctx.move_to(*[self.scale[0] * (coord + 0.015) for coord in position])
self.ctx.scale(1, -1)
self.ctx.show_text(primitive.net_name)
self.ctx.scale(1, -1)
def _new_render_layer(self, color=None, mirror=False):
size_in_pixels = self.scale_point(self.size_in_inch)
+ matrix = copy.copy(self._xform_matrix)
layer = cairo.SVGSurface(None, size_in_pixels[0], size_in_pixels[1])
ctx = cairo.Context(layer)
- ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)
ctx.scale(1, -1)
ctx.translate(-(self.origin_in_inch[0] * self.scale[0]),
- (-self.origin_in_inch[1] * self.scale[0]) - size_in_pixels[1])
+ (-self.origin_in_inch[1] * self.scale[0]) - size_in_pixels[1])
if self.invert:
ctx.set_operator(cairo.OPERATOR_OVER)
- ctx.set_source_rgba(*self.color, alpha=self.alpha)
ctx.paint()
- matrix = copy.copy(self._xform_matrix)
if mirror:
matrix.xx = -1.0
matrix.x0 = self.origin_in_pixels[0] + self.size_in_pixels[0]
@@ -282,21 +288,23 @@ class GerberCairoContext(GerberContext):
self.active_layer = layer
self.active_matrix = matrix
- def _flatten(self):
- self.output_ctx.set_operator(cairo.OPERATOR_OVER)
+ def _paint(self, color=None, alpha=None):
+ color = color if color is not None else self.color
+ alpha = alpha if alpha is not None else self.alpha
ptn = cairo.SurfacePattern(self.active_layer)
ptn.set_matrix(self.active_matrix)
- self.output_ctx.set_source(ptn)
- self.output_ctx.paint()
+ self.output_ctx.set_source_rgba(*color, alpha=alpha)
+ self.output_ctx.mask(ptn)
self.ctx = None
self.active_layer = None
self.active_matrix = None
- def _paint_background(self, force=False):
- if (not self.bg) or force:
- self.bg = True
- self.output_ctx.set_operator(cairo.OPERATOR_OVER)
- self.output_ctx.set_source_rgba(*self.background_color, alpha=1.0)
+ def _paint_background(self, settings=None):
+ color = settings.color if settings is not None else self.background_color
+ alpha = settings.alpha if settings is not None else 1.0
+ if not self.has_bg:
+ self.has_bg = True
+ self.output_ctx.set_source_rgba(*color, alpha=alpha)
self.output_ctx.paint()
def scale_point(self, point):
diff --git a/gerber/render/render.py b/gerber/render/render.py
index d7a62e1..724aaea 100644
--- a/gerber/render/render.py
+++ b/gerber/render/render.py
@@ -45,7 +45,8 @@ class GerberContext(object):
Measurement units. 'inch' or 'metric'
color : tuple (<float>, <float>, <float>)
- Color used for rendering as a tuple of normalized (red, green, blue) values.
+ Color used for rendering as a tuple of normalized (red, green, blue)
+ values.
drill_color : tuple (<float>, <float>, <float>)
Color used for rendering drill hits. Format is the same as for `color`.
@@ -62,8 +63,9 @@ class GerberContext(object):
self._units = units
self._color = (0.7215, 0.451, 0.200)
self._background_color = (0.0, 0.0, 0.0)
+ self._drill_color = (0.0, 0.0, 0.0)
self._alpha = 1.0
- self._invert = False
+ self.invert = False
self.ctx = None
@property
@@ -125,14 +127,6 @@ class GerberContext(object):
raise ValueError('Alpha must be between 0.0 and 1.0')
self._alpha = alpha
- @property
- def invert(self):
- return self._invert
-
- @invert.setter
- def invert(self, invert):
- self._invert = invert
-
def render(self, primitive):
color = self.color
if isinstance(primitive, Line):
@@ -156,7 +150,6 @@ class GerberContext(object):
else:
return
-
def _render_line(self, primitive, color):
pass
@@ -186,8 +179,8 @@ class GerberContext(object):
class RenderSettings(object):
-
- def __init__(self, color=(0.0, 0.0, 0.0), alpha=1.0, invert=False, mirror=False):
+ def __init__(self, color=(0.0, 0.0, 0.0), alpha=1.0, invert=False,
+ mirror=False):
self.color = color
self.alpha = alpha
self.invert = invert
diff --git a/gerber/render/theme.py b/gerber/render/theme.py
index 4d325c5..d382a8d 100644
--- a/gerber/render/theme.py
+++ b/gerber/render/theme.py
@@ -25,12 +25,12 @@ COLORS = {
'green': (0.0, 1.0, 0.0),
'blue': (0.0, 0.0, 1.0),
'fr-4': (0.290, 0.345, 0.0),
- 'green soldermask': (0.0, 0.612, 0.396),
+ 'green soldermask': (0.0, 0.412, 0.278),
'blue soldermask': (0.059, 0.478, 0.651),
'red soldermask': (0.968, 0.169, 0.165),
'black soldermask': (0.298, 0.275, 0.282),
'purple soldermask': (0.2, 0.0, 0.334),
- 'enig copper': (0.686, 0.525, 0.510),
+ 'enig copper': (0.694, 0.533, 0.514),
'hasl copper': (0.871, 0.851, 0.839)
}
@@ -39,11 +39,11 @@ class Theme(object):
def __init__(self, name=None, **kwargs):
self.name = 'Default' if name is None else name
- self.background = kwargs.get('background', RenderSettings(COLORS['black'], alpha=0.0))
+ self.background = kwargs.get('background', RenderSettings(COLORS['fr-4']))
self.topsilk = kwargs.get('topsilk', RenderSettings(COLORS['white']))
self.bottomsilk = kwargs.get('bottomsilk', RenderSettings(COLORS['white'], mirror=True))
- self.topmask = kwargs.get('topmask', RenderSettings(COLORS['green soldermask'], alpha=0.8, invert=True))
- self.bottommask = kwargs.get('bottommask', RenderSettings(COLORS['green soldermask'], alpha=0.8, invert=True, mirror=True))
+ self.topmask = kwargs.get('topmask', RenderSettings(COLORS['green soldermask'], alpha=0.85, invert=True))
+ self.bottommask = kwargs.get('bottommask', RenderSettings(COLORS['green soldermask'], alpha=0.85, invert=True, mirror=True))
self.top = kwargs.get('top', RenderSettings(COLORS['hasl copper']))
self.bottom = kwargs.get('bottom', RenderSettings(COLORS['hasl copper'], mirror=True))
self.drill = kwargs.get('drill', RenderSettings(COLORS['black']))
@@ -60,12 +60,21 @@ class Theme(object):
THEMES = {
'default': Theme(),
'OSH Park': Theme(name='OSH Park',
+ background=RenderSettings(COLORS['purple soldermask']),
top=RenderSettings(COLORS['enig copper']),
bottom=RenderSettings(COLORS['enig copper'], mirror=True),
- topmask=RenderSettings(COLORS['purple soldermask'], alpha=0.8, invert=True),
- bottommask=RenderSettings(COLORS['purple soldermask'], alpha=0.8, invert=True, mirror=True)),
+ topmask=RenderSettings(COLORS['purple soldermask'], alpha=0.85, invert=True),
+ bottommask=RenderSettings(COLORS['purple soldermask'], alpha=0.85, invert=True, mirror=True),
+ topsilk=RenderSettings(COLORS['white'], alpha=0.8),
+ bottomsilk=RenderSettings(COLORS['white'], alpha=0.8, mirror=True)),
'Blue': Theme(name='Blue',
topmask=RenderSettings(COLORS['blue soldermask'], alpha=0.8, invert=True),
bottommask=RenderSettings(COLORS['blue soldermask'], alpha=0.8, invert=True)),
+
+ 'Transparent Copper': Theme(name='Transparent',
+ background=RenderSettings((0.9, 0.9, 0.9)),
+ top=RenderSettings(COLORS['red'], alpha=0.5),
+ bottom=RenderSettings(COLORS['blue'], alpha=0.5),
+ drill=RenderSettings((0.3, 0.3, 0.3))),
}