Python cairo.Matrix() Examples

The following are 30 code examples of cairo.Matrix(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cairo , or try the search function .
Example #1
Source File: renderer.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def cdc_draw_frame(self, start, end, temp_surfase=False):
        if start and end:
            if self.frame:
                if start == self.frame[0] and end == self.frame[1]:
                    return
            cpath = libcairo.convert_bbox_to_cpath(start + end)
            bbox = self.cdc_to_int(*libcairo.get_cpath_bbox(cpath))
            frame = self.cdc_bbox_to_frame(bbox)
            if not self.frame:
                self.frame = frame
            bbox2 = self.cdc_frame_to_bbox(self.frame)
            frame_sum = self.cdc_bbox_to_frame(libgeom.sum_bbox(bbox, bbox2))
            x, y, w, h = self.cdc_normalize_rect(*frame_sum)
            self.frame = frame
            surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w + 2, h + 2)
            ctx = cairo.Context(surface)
            if temp_surfase:
                ctx.set_source_surface(self.temp_surface, -x + 1, -y + 1)
            else:
                ctx.set_source_surface(self.surface, -x + 1, -y + 1)
            ctx.paint()
            ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, -x + 1, -y + 1))
            self._cdc_draw_cpath(ctx, cpath)
            self.canvas.dc.put_surface(surface, x - 1, y - 1)
            self.cdc_reflect_snapping() 
Example #2
Source File: painter.py    From RAFCON with Eclipse Public License 1.0 6 votes vote down vote up
def paint(self, context, selected):
        view = self.view
        item = self.item
        cr = context.cairo
        h = item.handles()
        side_length = get_side_length_of_resize_handle(self.view, item.parent) / 1.5
        for h1, h2 in zip(h[1:-2], h[2:-1]):
            p1, p2 = h1.pos, h2.pos
            cx = (p1.x + p2.x) / 2
            cy = (p1.y + p2.y) / 2
            cr.save()
            cr.set_line_width(self.view.get_zoom_factor() / 4.)
            cr.identity_matrix()
            m = Matrix(*view.get_matrix_i2v(item))

            # cr.set_antialias(Antialias.NONE)
            cr.translate(*m.transform_point(cx, cy))
            cr.rectangle(-side_length / 2., -side_length / 2., side_length, side_length)
            cr.set_source_rgba(*get_col_rgba(self.fill_color))
            cr.fill_preserve()
            cr.set_source_rgba(*get_col_rgba(self.border_color))
            cr.set_line_width(1)
            cr.stroke()
            cr.restore() 
Example #3
Source File: BoardView.py    From pychess with GNU General Public License v3.0 6 votes vote down vote up
def matrixAround(rotated_matrix, anchor_x, anchor_y):
    """
    Description : Rotates a matrix through the hypotenuse so that the original
    matrix becomes the inverse matrix and the inverse matrix becomes matrix
    Returns a tuple representing the matrix and its inverse

    """
    corner = rotated_matrix[0]
    side = rotated_matrix[1]
    anchor_yside = anchor_y * side
    anchor_xside = anchor_x * side
    anchor_ycorner = anchor_y * (1 - corner)
    anchor_xcorner = anchor_x * (1 - corner)
    matrix = cairo.Matrix(corner, side, -side, corner,
                          anchor_xcorner + anchor_yside,
                          anchor_ycorner - anchor_xside)
    invmatrix = cairo.Matrix(corner, -side, side, corner,
                             anchor_xcorner - anchor_yside,
                             anchor_ycorner + anchor_xside)
    return matrix, invmatrix 
Example #4
Source File: draw_mod.py    From agg-kicad with MIT License 6 votes vote down vote up
def hatch(positive, rgba):
    hs = 64
    hatch = cairo.ImageSurface(cairo.FORMAT_ARGB32, hs, hs)
    hctx = cairo.Context(hatch)
    if positive:
        hctx.move_to(0, 0)
        hctx.line_to(hs, hs)
    else:
        hctx.move_to(0, hs)
        hctx.line_to(hs, 0)
    hctx.set_line_width(16)
    hctx.set_source_rgba(*rgba)
    hctx.stroke()
    hpat = cairo.SurfacePattern(hatch)
    hpat.set_extend(cairo.EXTEND_REPEAT)
    hpat.set_matrix(cairo.Matrix(xx=image_size, yy=image_size))
    return hpat 
Example #5
Source File: ruler.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def paint(self):
        if self.presenter is None:
            return
        w, h = self.dc.get_size()
        fmt = cairo.FORMAT_RGB24
        if self.surface is None or self.width != w or self.height != h:
            self.surface = cairo.ImageSurface(fmt, w, h)
            self.width, self.height = w, h
        self.ctx = cairo.Context(self.surface)
        self.ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0))
        self.ctx.set_source_rgb(*config.ruler_bg)
        self.ctx.paint()
        self.ctx.set_antialias(cairo.ANTIALIAS_NONE)
        self.ctx.set_line_width(1.0)
        self.ctx.set_dash([])
        self.ctx.set_source_rgb(*config.ruler_fg)
        if self.vertical:
            self.vrender(w, h)
        else:
            self.hrender(w, h)
        self.dc.draw_surface(self.surface, 0, 0) 
Example #6
Source File: fontctrl.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def generate_fontsample_cache(fonts):
    w = config.font_preview_width
    fontsize = config.font_preview_size
    color = cms.val_255(config.font_preview_color)
    text = config.font_preview_text.decode('utf-8')
    for item in fonts:
        h = libpango.get_sample_size(text, item, fontsize)[1]
        if not h:
            h = 10
            LOG.warn('Incorrect font <%s>: zero font height', item)
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        ctx = cairo.Context(surface)
        ctx.set_source_rgb(0.0, 0.0, 0.0)
        ctx.paint()
        matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
        ctx.set_matrix(matrix)
        ctx.set_source_rgb(1.0, 1.0, 1.0)
        ctx.set_antialias(cairo.ANTIALIAS_DEFAULT)
        libpango.render_sample(ctx, text, item, fontsize)
        ctx.fill()
        bmp = wal.copy_surface_to_bitmap(surface)
        FONTSAMPLE_CACHE.append(wal.invert_text_bitmap(bmp, color)) 
Example #7
Source File: crenderer.py    From uniconvertor with GNU Affero General Public License v3.0 6 votes vote down vote up
def render(objs, cms):
    bbox = reduce(lambda a, b: libgeom.sum_bbox(a, b.cache_bbox), objs, [])
    w, h = libgeom.bbox_size(bbox)
    x, y = libgeom.bbox_center(bbox)
    trafo = (1.0, 0, 0, -1.0, w / 2.0 - x, h / 2.0 + y)
    canvas_matrix = cairo.Matrix(*trafo)

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w), int(h))
    ctx = cairo.Context(surface)
    ctx.set_matrix(canvas_matrix)
    rend = crenderer.CairoRenderer(cms)
    rend.antialias_flag = True
    rend.render(ctx, objs)

    image_stream = StringIO()
    surface.write_to_png(image_stream)
    return image_stream 
Example #8
Source File: __init__.py    From uniconvertor with GNU Affero General Public License v3.0 6 votes vote down vote up
def png_saver(sk2_doc, filename=None, fileptr=None, translate=True, cnf=None,
              **kw):
    cnf = merge_cnf(cnf, kw)
    if filename and not fileptr:
        fileptr = get_fileptr(filename, True)
    page = sk2_doc.methods.get_page()
    scale = abs(float(cnf.get('scale', 1.0))) or 1.0
    w, h = [scale * item for item in page.page_format[1]]
    trafo = (scale, 0, 0, -scale, w / 2.0, h / 2.0)

    canvas_matrix = cairo.Matrix(*trafo)
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w), int(h))
    ctx = cairo.Context(surface)
    ctx.set_matrix(canvas_matrix)
    
    antialias_flag = not cnf.get('antialiasing') in (False, 0)
    layers = sk2_doc.methods.get_visible_layers(page)
    rend = CairoRenderer(sk2_doc.cms)

    for item in layers:
        rend.antialias_flag = not any([not item.properties[3], not antialias_flag])
        rend.render(ctx, item.childs)

    surface.write_to_png(fileptr)
    fileptr.close() 
Example #9
Source File: ruler.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def paint(self):
        w, h = self.get_size()
        if self.surface is None or self.width != w or self.height != h:
            self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
            self.width, self.height = w, h
        self.ctx = cairo.Context(self.surface)
        self.ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0))
        self.ctx.set_source_rgb(*config.ruler_bg)
        self.ctx.paint()
        self.ctx.set_antialias(cairo.ANTIALIAS_NONE)
        self.ctx.set_line_width(1.0)
        self.ctx.set_dash([])
        self.ctx.set_source_rgb(*config.ruler_fg)
        if self.horizontal:
            self.hrender(w, h)
        else:
            self.vrender(w, h)
        self.draw_bitmap(wal.copy_surface_to_bitmap(self.surface)) 
Example #10
Source File: crenderer.py    From uniconvertor with GNU Affero General Public License v3.0 5 votes vote down vote up
def render_image(self, ctx, obj):
        surface = self.get_surface(obj)
        if not surface:
            return
        canvas_matrix = ctx.get_matrix()
        canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix)
        zoom = canvas_trafo[0]

        h = obj.size[1]
        lu_corner = libgeom.apply_trafo_to_point([0.0, float(h)], obj.trafo)
        x0, y0 = libgeom.apply_trafo_to_point(lu_corner, canvas_trafo)

        m11, m12, m21, m22 = obj.trafo[:4]
        matrix = cairo.Matrix(zoom * m11, -zoom * m12,
                              - zoom * m21, zoom * m22, x0, y0)
        ctx.set_matrix(matrix)

        ctx.set_source_surface(surface)
        if zoom * abs(m11) > .98:
            ctx.get_source().set_filter(cairo.FILTER_NEAREST)

        if self.contour_flag:
            ctx.paint_with_alpha(0.3)
        else:
            ctx.paint()

        ctx.set_matrix(canvas_matrix) 
Example #11
Source File: __init__.py    From uniconvertor with GNU Affero General Public License v3.0 5 votes vote down vote up
def generate_preview(presenter, renderer_cls, size=(100, 100),
                     transparent=False, img_format='PNG', encoded=True):
    wp, hp = size
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(wp), int(hp))
    ctx = cairo.Context(surface)
    if not transparent:
        ctx.set_source_rgb(1.0, 1.0, 1.0)
        ctx.paint()
    # ---rendering
    mthds = presenter.methods
    layers = mthds.get_visible_layers(mthds.get_page())
    bbox = mthds.count_bbox(layers)
    if bbox:
        x, y, x1, y1 = bbox
        w = abs(x1 - x) or 1.0
        h = abs(y1 - y) or 1.0
        coef = min(wp / w, hp / h) * 0.99
        trafo0 = [1.0, 0.0, 0.0, 1.0, -x - w / 2.0, -y - h / 2.0]
        trafo1 = [coef, 0.0, 0.0, -coef, 0.0, 0.0]
        trafo2 = [1.0, 0.0, 0.0, 1.0, wp / 2.0, hp / 2.0]
        trafo = libgeom.multiply_trafo(trafo0, trafo1)
        trafo = libgeom.multiply_trafo(trafo, trafo2)
        ctx.set_matrix(cairo.Matrix(*trafo))
        rend = renderer_cls(presenter.cms)
        rend.antialias_flag = True
        for item in layers:
            rend.render(ctx, item.childs)
    # ---rendering
    image_stream = StringIO()
    surface.write_to_png(image_stream)

    if not img_format == 'PNG':
        image_stream.seek(0, 0)
        image = Image.open(image_stream)
        image.load()
        image_stream = StringIO()
        image.save(image_stream, format=img_format)

    image_str = image_stream.getvalue()
    return b64encode(image_str) if encoded else image_str 
Example #12
Source File: abstract_canvas_tool.py    From drawing with GNU General Public License v3.0 5 votes vote down vote up
def get_deformed_surface(self, source_surface, coefs):
		"""Use cairo.Matrix to apply a transformation to `source_surface` using
		the coefficients in `coefs` and return a new surface with the result."""
		p_xx, p_yx, p_xy, p_yy, p_x0, p_y0 = coefs

		source_w = source_surface.get_width()
		source_h = source_surface.get_height()
		normal_w = p_xx * source_w + p_xy * source_h + p_x0
		normal_h = p_yx * source_w + p_yy * source_h + p_y0
		w = max(normal_w, source_w + p_x0)
		h = max(normal_h, source_h + p_y0) # XXX bof, pas sûr

		new_surface = cairo.ImageSurface(cairo.Format.ARGB32, int(w), int(h))
		cairo_context = cairo.Context(new_surface)
		# m = cairo.Matrix(xx=1.0, yx=0.0, xy=0.0, yy=1.0, x0=0.0, y0=0.0)
		m = cairo.Matrix(xx=p_xx, yx=p_yx, xy=p_xy, yy=p_yy, x0=p_x0, y0=p_y0)
		try:
			cairo_context.transform(m)
		except:
			self.show_error(_("Error: invalid values"))
			return source_surface
		cairo_context.set_source_surface(source_surface, 0, 0)
		# FIXME scroll and zoom ?
		cairo_context.paint()
		return new_surface

	############################################################################
################################################################################ 
Example #13
Source File: paths.py    From uniconvertor with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_rtl_glyphs(ctx, layout_data, log_layout_data, byte_dict, rtl_regs,
                   text, width, text_style, markup):
    glyphs = []
    for item in layout_data:
        try:
            index = byte_dict[item[5]]
            txt = text[index]
            if is_item_in_rtl(index, rtl_regs):
                text_range = [index - len(txt) + 1, index + 1]
            else:
                text_range = [index, index + len(txt)]

        except Exception:
            continue

        if txt in NONPRINTING_CHARS:
            glyphs.append(None)
            continue

        ctx.new_path()
        ctx.move_to(0, 0)
        layout = core.create_layout(ctx)
        vpos = core.set_glyph_layout(txt, width, text_style,
                                     markup, text_range, True, layout)
        if vpos:
            for index in range(*text_range):
                x, y, w, h, base_line, byte_index = log_layout_data[index]
                dh = (y - base_line) * vpos
                log_layout_data[index] = (x, y + dh, w, h,
                                          base_line + dh, byte_index)
        core.layout_path(ctx, layout)
        cpath = ctx.copy_path()
        m00 = 1.0
        m11 = -1.0
        if os.name == 'nt':
            m00 *= 0.1
            m11 *= 0.1
        matrix = cairo.Matrix(m00, 0.0, 0.0, m11, item[0], item[1])
        libcairo.apply_cmatrix(cpath, matrix)
        glyphs.append(cpath)
    return glyphs 
Example #14
Source File: backend_cairo.py    From ImageFusion with MIT License 5 votes vote down vote up
def set_width_height(self, width, height):
        self.width  = width
        self.height = height
        self.matrix_flipy = cairo.Matrix (yy=-1, y0=self.height)
        # use matrix_flipy for ALL rendering?
        # - problem with text? - will need to switch matrix_flipy off, or do a
        # font transform? 
Example #15
Source File: extras.py    From pympress with GNU General Public License v2.0 5 votes vote down vote up
def get_matrix(self, ww, wh):
        """ Returns the :class:`~cairo.Matrix` used to perform the zoom for the widget of size ww x wh.

        Args:
            ww (`float`):  widget width
            wh (`float`):  widget height

        Returns:
            :class:`~cairo.Matrix`: the zoom transformation matrix
        """
        return cairo.Matrix(xx = self.scale, x0 = ww * self.shift[0],
                            yy = self.scale, y0 = wh * self.shift[1]) 
Example #16
Source File: gif_backend.py    From pympress with GNU General Public License v2.0 5 votes vote down vote up
def set_transform(self, *args):
        """ Compute the transform to scale (not stretch nor crop) the gif.
        """
        widget_size = (self.movie_zone.get_allocated_width(), self.movie_zone.get_allocated_height())
        scale = min(widget_size[0] / self.base_size[0], widget_size[1] / self.base_size[1])
        dx = widget_size[0] - scale * self.base_size[0]
        dy = widget_size[1] - scale * self.base_size[1]

        self.transform = cairo.Matrix(xx = scale, yy = scale, x0 = dx / 2, y0 = dy / 2) 
Example #17
Source File: __init__.py    From uniconvertor with GNU Affero General Public License v3.0 5 votes vote down vote up
def _get_trafo(cmatrix):
    result = []
    val = cmatrix.__str__()
    val = val.replace('cairo.Matrix(', '')
    val = val.replace(')', '')
    items = val.split(', ')
    for item in items:
        val = item.replace(',', '.')
        result.append(float(val))
    return result 
Example #18
Source File: BoardView.py    From pychess with GNU General Public License v3.0 5 votes vote down vote up
def _setRotation(self, radians):
        if not conf.get("fullAnimation"):
            self._rotation = radians
            self.next_rotation = radians
            self.matrix = cairo.Matrix.init_rotate(radians)
            self.redrawCanvas()
        else:
            if hasattr(self, "next_rotation") and \
                    self.next_rotation != self.rotation:
                return
            self.next_rotation = radians
            oldr = self.rotation
            start = time()

            def rotate():
                amount = (time() - start) / ANIMATION_TIME
                if amount > 1:
                    amount = 1
                    next = False
                    self.animating = False
                else:
                    next = True
                self._rotation = new = oldr + amount * (radians - oldr)
                self.matrix = cairo.Matrix.init_rotate(new)
                self.redrawCanvas()
                return next

            self.animating = True
            GLib.idle_add(rotate) 
Example #19
Source File: BoardView.py    From pychess with GNU General Public License v3.0 5 votes vote down vote up
def __drawPiece(self, context, piece, x_loc, y_loc):
        # Maybe a premove was reset from another thread
        if piece is None:
            print("Trying to draw a None piece")
            return
        if self.model.variant == BlindfoldBoard:
            return
        elif self.model.variant == HiddenPawnsBoard:
            if piece.piece == PAWN:
                return
        elif self.model.variant == HiddenPiecesBoard:
            if piece.piece != PAWN:
                return

        if piece.captured and not self.showCaptured:
            return

        side = self.square[3]

        if not self.faceToFace:
            matrix, invmatrix, cx_loc, cy_loc = self.getCordMatrices(x_loc, y_loc)
        else:
            cx_loc, cy_loc = self.cord2Point(x_loc, y_loc)
            if piece.color == BLACK:
                matrix, invmatrix = matrixAround((-1, 0), cx_loc + side / 2., cy_loc + side / 2.)
            else:
                matrix = invmatrix = cairo.Matrix(1, 0, 0, 1, 0, 0)

        context.transform(invmatrix)
        Pieces.drawPiece(piece, context,
                         cx_loc + CORD_PADDING, cy_loc + CORD_PADDING,
                         side - CORD_PADDING * 2, allwhite=self.allwhite, asean=self.asean,
                         variant=self.model.variant.variant)
        context.transform(matrix) 
Example #20
Source File: printout.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def on_print_page(self, page):
        page_obj = self.get_print_pages()[page - 1]
        dc = self.GetDC()
        w, h = dc.GetSizeTuple()
        pw, ph = self.GetPageSizeMM()
        pw *= uc2const.mm_to_pt
        ph *= uc2const.mm_to_pt

        trafo = (w / pw, 0, 0, -h / ph,
                 w / 2.0 - self.shifts[0], h / 2.0 - self.shifts[1])

        matrix = cairo.Matrix(*trafo)

        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        ctx = cairo.Context(surface)
        ctx.set_source_rgb(1, 1, 1)
        ctx.paint()
        ctx.set_matrix(matrix)

        for group in page_obj.childs:
            self.renderer.render(ctx, group.childs)

        win_surface = cairo.Win32PrintingSurface(dc.GetHDC())
        win_ctx = cairo.Context(win_surface)

        win_ctx.set_source_surface(surface, 0, 0)
        win_ctx.paint() 
Example #21
Source File: canvas.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def _keep_center(self):
        w, h = self.dc.get_size()
        w = float(w)
        h = float(h)
        if not w == self.width or not h == self.height:
            _dx = (w - self.width) / 2.0
            _dy = (h - self.height) / 2.0
            m11, m12, m21, m22, dx, dy = self.trafo
            dx += _dx
            dy += _dy
            self.trafo = [m11, m12, m21, m22, dx, dy]
            self.matrix = cairo.Matrix(m11, m12, m21, m22, dx, dy)
            self.width = w
            self.height = h
            self.update_scrolls() 
Example #22
Source File: canvas.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def _fit_to_page(self):
        width, height = self.printer.get_page_size()
        w, h = self.get_size()
        w = float(w)
        h = float(h)
        self.width = w
        self.height = h
        zoom = min(w / width, h / height) * PAGEFIT
        dx = w / 2.0
        dy = h / 2.0
        self.trafo = [zoom, 0, 0, -zoom, dx, dy]
        self.zoom_stack.append([] + self.trafo)
        self.matrix = cairo.Matrix(zoom, 0, 0, -zoom, dx, dy)
        self.zoom = zoom
        self.update_scrolls() 
Example #23
Source File: canvas.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def _keep_center(self):
        w, h = self.get_size()
        w = float(w)
        h = float(h)
        if not w == self.width or not h == self.height:
            _dx = (w - self.width) / 2.0
            _dy = (h - self.height) / 2.0
            m11, m12, m21, m22, dx, dy = self.trafo
            dx += _dx
            dy += _dy
            self.trafo = [m11, m12, m21, m22, dx, dy]
            self.matrix = cairo.Matrix(m11, m12, m21, m22, dx, dy)
            self.width = w
            self.height = h
            self.update_scrolls() 
Example #24
Source File: canvas.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def _zoom(self, dzoom=1.0):
        m11, m12, m21, m22, dx, dy = self.trafo
        _dx = (self.width * dzoom - self.width) / 2.0
        _dy = (self.height * dzoom - self.height) / 2.0
        dx = dx * dzoom - _dx
        dy = dy * dzoom - _dy
        self.trafo = [m11 * dzoom, m12, m21, m22 * dzoom, dx, dy]
        self.zoom_stack.append([] + self.trafo)
        self.matrix = cairo.Matrix(*self.trafo)
        self.zoom = m11 * dzoom
        self.update_scrolls()
        self.refresh() 
Example #25
Source File: prefs_ruler.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def paint(self):
        w, h = self.get_size()
        fmt = cairo.FORMAT_RGB24
        self.surface = cairo.ImageSurface(fmt, w, h)
        self.ctx = cairo.Context(self.surface)
        self.ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0))
        self.ctx.set_source_rgb(*self.prefs.bg_btn.get_value())
        self.ctx.paint()
        self.ctx.set_antialias(cairo.ANTIALIAS_NONE)
        self.ctx.set_line_width(1.0)
        self.ctx.set_dash([])
        self.ctx.set_source_rgb(*self.prefs.fg_btn.get_value())

        self.ctx.move_to(0, h)
        self.ctx.line_to(w, h)
        self.ctx.stroke()

        small_l = self.prefs.ruler_small_tick.get_value()
        for item in SMALL_TICKS:
            self.ctx.move_to(item, h - small_l)
            self.ctx.line_to(item, h - 1)

        large_l = self.prefs.ruler_large_tick.get_value()
        for pos, txt in TEXT_TICKS:
            self.ctx.move_to(pos, h - large_l)
            self.ctx.line_to(pos, h - 1)

        self.ctx.stroke()

        vshift = self.prefs.ruler_text_vshift.get_value()
        hshift = self.prefs.ruler_text_hshift.get_value()
        for pos, txt in TEXT_TICKS:
            for character in txt:
                data = self.font[character]
                position = int(pos) + hshift
                self.ctx.set_source_surface(data[1], position, vshift)
                self.ctx.paint()
                pos += data[0]

        self.draw_surface(self.surface) 
Example #26
Source File: __init__.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def render(self, sel_flag=False):
        doc = self.app.current_doc
        if not doc:
            return
        if sel_flag:
            if not doc.selection.objs:
                return None
            w, h = libgeom.bbox_size(doc.selection.bbox)
            x, y = libgeom.bbox_center(doc.selection.bbox)
            trafo = (1.0, 0, 0, -1.0, w / 2.0 - x, h / 2.0 + y)
        else:
            page = doc.active_page
            w, h = page.page_format[1]
            trafo = (1.0, 0, 0, -1.0, w / 2.0, h / 2.0)

        canvas_matrix = cairo.Matrix(*trafo)
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w), int(h))
        ctx = cairo.Context(surface)
        ctx.set_matrix(canvas_matrix)

        rend = crenderer.CairoRenderer(doc.cms)

        if sel_flag:
            objs = doc.selection.objs
            for obj in objs:
                layer = doc.methods.get_parent_layer(obj)
                rend.antialias_flag = layer.properties[3] == 1
                rend.render(ctx, [obj, ])
        else:
            page = doc.active_page
            layers = doc.methods.get_visible_layers(page)
            for item in layers:
                rend.antialias_flag = item.properties[3] == 1
                rend.render(ctx, item.childs)

        image_stream = StringIO()
        surface.write_to_png(image_stream)
        return image_stream 
Example #27
Source File: colorctrls.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def draw_pattern(self):
        w, h = self.get_size()
        pattern = self.fill[2]
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        ctx = cairo.Context(surface)
        self.draw_cairo_background(ctx)
        if self.cms.app.current_doc:
            cfg = self.cms.app.current_doc.model.config
        else:
            cfg = sk2_config.SK2_Config()
            config_file = os.path.join(self.cms.app.appdata.app_config_dir,
                                       'sk2_config.xml')
            cfg.load(config_file)
        image_obj = sk2_model.Pixmap(cfg)
        hndl=image_obj.handler
        hndl.load_from_b64str(self.cms, pattern[1])
        hndl.flip_left_to_right()
        if pattern[0] == sk2const.PATTERN_IMG and len(pattern) > 2:
            image_obj.style[3] = deepcopy(pattern[2])
        sp = cairo.SurfacePattern(hndl.get_surface(self.cms))
        sp.set_extend(cairo.EXTEND_REPEAT)
        trafo = [-1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
        if len(pattern) > 3:
            trafo = libgeom.multiply_trafo(pattern[3], trafo)
        pattern_matrix = cairo.Matrix(*trafo)
        pattern_matrix.invert()
        sp.set_matrix(pattern_matrix)
        ctx.set_source(sp)
        ctx.rectangle(0, 0, w, h)
        ctx.fill()
        self.gc_draw_bitmap(wal.copy_surface_to_bitmap(surface), 0, 0) 
Example #28
Source File: renderer.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, canvas, cms=None):
        CairoRenderer.__init__(self, cms)
        self.canvas = canvas
        self.direct_matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) 
Example #29
Source File: backend_cairo.py    From neural-network-animation with MIT License 5 votes vote down vote up
def set_width_height(self, width, height):
        self.width  = width
        self.height = height
        self.matrix_flipy = cairo.Matrix (yy=-1, y0=self.height)
        # use matrix_flipy for ALL rendering?
        # - problem with text? - will need to switch matrix_flipy off, or do a
        # font transform? 
Example #30
Source File: canvas.py    From sk1-wx with GNU General Public License v3.0 5 votes vote down vote up
def _set_center(self, center):
        x, y = center
        _dx = self.width / 2.0 - x
        _dy = self.height / 2.0 - y
        m11, m12, m21, m22, dx, dy = self.trafo
        dx += _dx
        dy += _dy
        self.trafo = [m11, m12, m21, m22, dx, dy]
        self.matrix = cairo.Matrix(m11, m12, m21, m22, dx, dy)
        self.update_scrolls()