Пример #1
0
def show_text(cr, x, y, width, size, text, center=False):
    layout = PangoCairo.create_layout(cr)
    layout.set_text(text, -1)

    while True:
        desc = Pango.FontDescription("{} {}".format(FONT_DESCRIPTION, size))
        layout.set_font_description(desc)

        ink_rect, logical_rect = layout.get_pixel_extents()
        logical_width = logical_rect.x + logical_rect.width

        if logical_width + STROKE_WIDTH < width:
            break

        size -= 1

    if center:
        x += (width - logical_width) / 2

    cr.move_to(x, y)

    line = layout.get_line(0)
    PangoCairo.layout_line_path(cr, line)

    cr.set_source_rgb(0, 0, 0)
    cr.set_line_join(cairo.LINE_JOIN_ROUND)
    cr.set_line_width(STROKE_WIDTH)
    cr.stroke_preserve()

    cr.set_source_rgb(1, 1, 1)
    cr.fill()
Пример #2
0
    def draw_page(self, _oPrintOp, oContext, iPageNum):
        """Page drawing callback.
           """
        # pylint: disable=too-many-locals
        # We use lots of variables for clarity
        iStartPageLine, iEndPageLine = 0, 0
        aPageBreaks = self._aPageBreaks
        oLayout = self._oPangoLayout

        if iPageNum > 0:
            iStartPageLine = aPageBreaks[iPageNum - 1]

        if iPageNum < len(aPageBreaks):
            iEndPageLine = aPageBreaks[iPageNum]
        else:
            iEndPageLine = oLayout.get_line_count()

        oCairoContext = oContext.get_cairo_context()
        oCairoContext.set_source_rgb(0, 0, 0)
        oCairoContext.set_line_width(0.1)

        oIter = oLayout.get_iter()

        fStartPos = 0.0
        iLine = 0

        while True:
            if iLine >= iStartPageLine:
                # oIter.get_line is a bit buggy with pyGtkcompat,
                # so we side step it by querying the layout directly
                oLine = oLayout.get_line(iLine)
                _oInkRect, oLogicalRect = oLine.get_extents()

                fBaseLine = float(oIter.get_baseline()) / Pango.SCALE

                if iLine == iStartPageLine:
                    fStartPos = fBaseLine

                # line x co-ordinate / SCALE
                fXPos = float(oLogicalRect.x) / Pango.SCALE
                # baseline - start pos - line y-co-ordinate / SCALE
                fYPos = fBaseLine - fStartPos - (float(oLogicalRect.y) /
                                                 Pango.SCALE)

                oCairoContext.move_to(fXPos, fYPos)
                PangoCairo.layout_line_path(oCairoContext, oLine)
                oCairoContext.stroke_preserve()
                oCairoContext.fill()

            iLine += 1
            if not (iLine < iEndPageLine and oIter.next_line()):
                break
Пример #3
0
def text_path(context, font, size, text, debug=False):
    """Create a Pango text layout and return it as a Cairo path"""

    context.save()

    pg_layout = PangoCairo.create_layout(context)
    pg_context = pg_layout.get_context()

    font_options = cairo.FontOptions()
    font_options.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
    PangoCairo.context_set_font_options(pg_context, font_options)
    font_descp = "{0} {1:d}".format(font, size)
    pg_layout.set_font_description(Pango.FontDescription(font_descp))
    pg_layout.set_text(text, -1)  # force length calculation

    PangoCairo.update_layout(context, pg_layout)
    # TODO watch out for ink & logical, need check
    extents = pg_layout.get_pixel_extents()[0]
    # TODO debug necessary?
    if debug:
        context.set_source_rgb(0, 0, 0)
        PangoCairo.show_layout(context, pg_layout)
        print("text_path: ({0}, {1}, {2}, {3})".format(extents.x, extents.y,
                                                       extents.width,
                                                       extents.height))
        context.rectangle(extents.x, extents.y, extents.width, extents.height)
        context.set_line_width(1.0)
        context.set_line_join(cairo.LINE_JOIN_MITER)
        context.set_source_rgb(0, 0.8, 0)
        context.stroke()

    #PangoCairo.layout_path(context, pg_layout)
    PangoCairo.layout_line_path(context, pg_layout.get_line(0))
    path = context.copy_path()
    # clear the path
    context.new_path()
    context.restore()
    return (path, extents, xheight(pg_layout).height)