summaryrefslogtreecommitdiff
path: root/gerber/render/cairo_backend.py
diff options
context:
space:
mode:
authorGarret Fick <garret@ficksworkshop.com>2016-07-20 23:20:15 +0800
committerGarret Fick <garret@ficksworkshop.com>2016-07-20 23:20:15 +0800
commit76a49ec8e7d286f887603d05ee64b78e50ca3112 (patch)
tree6a19bbaefefd0042920f208467a87291f27c679b /gerber/render/cairo_backend.py
parent34f20ee90fc54ca046460bfae06bce927870536f (diff)
parent6f876edd09d9b81649691e529f85653f14b8fd1c (diff)
downloadgerbonara-76a49ec8e7d286f887603d05ee64b78e50ca3112.tar.gz
gerbonara-76a49ec8e7d286f887603d05ee64b78e50ca3112.tar.bz2
gerbonara-76a49ec8e7d286f887603d05ee64b78e50ca3112.zip
Manually merge change 6f876edd09d9b81649691e529f85653f14b8fd1c
Diffstat (limited to 'gerber/render/cairo_backend.py')
-rw-r--r--gerber/render/cairo_backend.py65
1 files changed, 48 insertions, 17 deletions
diff --git a/gerber/render/cairo_backend.py b/gerber/render/cairo_backend.py
index fe27d60..7063808 100644
--- a/gerber/render/cairo_backend.py
+++ b/gerber/render/cairo_backend.py
@@ -14,13 +14,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-
try:
import cairo
except ImportError:
import cairocffi as cairo
-from operator import mul
+from operator import mul, div
import math
import tempfile
@@ -42,16 +41,16 @@ class GerberCairoContext(GerberContext):
self.bg = False
self.mask = None
self.mask_ctx = None
- self.origin_in_pixels = None
- self.size_in_pixels = None
+ self.origin_in_inch = None
+ self.size_in_inch = None
- def set_bounds(self, bounds):
+ def set_bounds(self, bounds, new_surface=False):
origin_in_inch = (bounds[0][0], bounds[1][0])
size_in_inch = (abs(bounds[0][1] - bounds[0][0]), abs(bounds[1][1] - bounds[1][0]))
size_in_pixels = map(mul, size_in_inch, self.scale)
- self.origin_in_pixels = tuple(map(mul, origin_in_inch, self.scale)) if self.origin_in_pixels is None else self.origin_in_pixels
- self.size_in_pixels = size_in_pixels if self.size_in_pixels is None else self.size_in_pixels
- if self.surface is None:
+ self.origin_in_inch = origin_in_inch if self.origin_in_inch is None else self.origin_in_inch
+ 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.ctx = cairo.Context(self.surface)
@@ -64,6 +63,36 @@ class GerberCairoContext(GerberContext):
self.mask_ctx.scale(1, -1)
self.mask_ctx.translate(-(origin_in_inch[0] * self.scale[0]), (-origin_in_inch[1]*self.scale[0]) - size_in_pixels[1])
+ def render_layers(self, layers, filename):
+ """ Render a set of layers
+ """
+ self.set_bounds(layers[0].bounds, True)
+ self._paint_background(True)
+ for layer in layers:
+ self._render_layer(layer)
+ self.dump(filename)
+
+ @property
+ def origin_in_pixels(self):
+ return tuple(map(mul, self.origin_in_inch, self.scale)) if self.origin_in_inch is not None else (0.0, 0.0)
+
+ @property
+ def size_in_pixels(self):
+ return tuple(map(mul, self.size_in_inch, self.scale)) if self.size_in_inch is not None else (0.0, 0.0)
+
+ def _render_layer(self, layer):
+ self.color = layer.settings.color
+ self.alpha = layer.settings.alpha
+ self.invert = layer.settings.invert
+ if layer.settings.mirror:
+ raise Warning('mirrored layers aren\'t supported yet...')
+ if self.invert:
+ self._clear_mask()
+ for p in layer.primitives:
+ self.render(p)
+ if self.invert:
+ self._render_mask()
+
def _render_line(self, line, color):
start = map(mul, line.start, self.scale)
end = map(mul, line.end, self.scale)
@@ -267,12 +296,13 @@ class GerberCairoContext(GerberContext):
self.ctx.paint_with_alpha(1)
def _render_test_record(self, primitive, color):
- self.ctx.select_font_face('monospace', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
- self.ctx.set_font_size(200)
- self._render_circle(Circle(primitive.position, 0.01), color)
+ position = tuple(map(add, 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_rgb(*color)
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.01) for coord in primitive.position])
+ 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)
@@ -285,14 +315,15 @@ class GerberCairoContext(GerberContext):
def _render_mask(self):
self.ctx.set_operator(cairo.OPERATOR_OVER)
ptn = cairo.SurfacePattern(self.mask)
- ptn.set_matrix(cairo.Matrix(xx=1.0, yy=-1.0, x0=-self.origin_in_pixels[0], y0=self.size_in_pixels[1] + self.origin_in_pixels[1]))
+ ptn.set_matrix(cairo.Matrix(xx=1.0, yy=-1.0, x0=-self.origin_in_pixels[0],
+ y0=self.size_in_pixels[1] + self.origin_in_pixels[1]))
self.ctx.set_source(ptn)
self.ctx.paint()
- def _paint_background(self):
- if not self.bg:
- self.bg = True
- self.ctx.set_source_rgba(self.background_color[0], self.background_color[1], self.background_color[2])
+ def _paint_background(self, force=False):
+ if (not self.bg) or force:
+ self.bg = True
+ self.ctx.set_source_rgba(self.background_color[0], self.background_color[1], self.background_color[2], alpha=1.0)
self.ctx.paint()
def dump(self, filename):