Exemplo n.º 1
0
def paint_txt(ctx, txt, box):
    font_option = cairo.FontOptions()
    font_option.set_antialias(cairo.Antialias.SUBPIXEL)
    ctx.set_source_rgb(0, 0, 0)
    ctx.set_font_options(font_option) 
    ctx.select_font_face("Purisa", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_BOLD)
    ctx.set_font_size(60)
    # ctx.set_operator(cairo.OPERATOR_ADD)
    x = box[0]; y = box[1] + 50
    w = box[2] - box[0] + 1
    h = box[3] - box[1] + 1

    ctx.move_to(x, y)
    ctx.show_text(txt)
Exemplo n.º 2
0
    ctx.rel_move_to(wid / -2, 0)
    ctx.show_text(note[0])

    if len(note) > 1:
        ctx.rel_move_to(axofs, 0)
        ctx.set_font_size(ACCIDENTAL_FONT_SIZE)
        ctx.show_text(note[1])


surface = cairo.PDFSurface('cairo.pdf', 8.5 * 72, 11 * 72)
context = cairo.Context(surface)

context.set_line_cap(cairo.LINE_CAP_SQUARE)
context.set_line_join(cairo.LINE_JOIN_MITER)

options = cairo.FontOptions()
options.set_antialias(cairo.ANTIALIAS_BEST)

with context:
    context.translate(36, 36)
    context.scale(2.0)

    context.translate(STRING_WIDTH / 2, FRET_HEIGHT)

    # Draw the special frets
    for f in range(1, math.floor(FRET_COUNT) + 1):
        if SPECIAL_FRETS[(f % 12)]:
            with context:
                context.set_source_rgb(*SPECIAL_FRET_FILL_COLOR)
                context.new_path()
                context.rectangle(0, FRET_HEIGHT * (f - 1),
Exemplo n.º 3
0
    def render_text(self):
        imwid = LabelObjectCollection._TEXTURE_SIZE
        # Create a memory surface to render to and a drawing context
        img = cairo.ImageSurface(cairo.FORMAT_ARGB32, imwid, imwid)
        ctx = cairo.Context(img)

        # Get the font, make it a color
        italic = cairo.FONT_SLANT_ITALIC if self._italic else cairo.FONT_SLANT_NORMAL
        bold = cairo.FONT_WEIGHT_BOLD if self._bold else cairo.FONT_WEIGHT_NORMAL
        font = cairo.ToyFontFace(self._font, italic, bold)
        ctx.set_font_face(font)
        # sys.stderr.write("Setting text color to r={}, g={}, b={}\n"
        #                  .format(self.color[0], self.color[1], self.color[2]))
        ctx.set_source_rgb(self.color[0], self.color[1], self.color[2])

        # Figure out how big the word is going to be with font size 100
        ctx.set_font_size(100)
        scf = cairo.ScaledFont(font, ctx.get_font_matrix(), ctx.get_matrix(),
                               cairo.FontOptions())
        xbear, ybear, wid, hei, xadv, yadv = scf.text_extents(self._text)

        # We are given self._height as the point size of the font,self._border in
        # points as the distance from the edge of the word to the
        # border, and self._linewidth in points as the width of the border
        # line.  We want to just fit this into a imwid × imwid image.  Figure out
        # what point size fsize we should tell Cairo to make this work

        if self._box:
            fullwid = wid + 2 * ((self._border + self._linewidth) *
                                 (100. / self._height))
            fullhei = hei + 2 * ((self._border + self._linewidth) *
                                 (100. / self._height))
        else:
            fullwid = wid
            fullhei = hei

        if fullwid > fullhei:
            fsize = imwid / fullwid * 100
        else:
            fsize = imwid / fullhei * 100

        ctx.set_font_size(fsize)
        bordersp = self._border * fsize / self._height
        linew = self._linewidth * fsize / self._height

        # Get a path representing the text, and figure out how big it is
        ctx.text_path(self._text)
        x0, y0, x1, y1 = ctx.fill_extents()

        # Set the position offset from the center of
        #   the image by half the size of the path,
        #   get a new path at the right place,
        #   figure it what it covers on the image,
        #   and draw it.
        xpos = imwid // 2 - (x1 - x0) / 2
        ypos = imwid - (y1 + bordersp + linew if self._box else y1)
        ctx.new_path()
        ctx.move_to(xpos, ypos)
        ctx.text_path(self._text)
        x0, y0, x1, y1 = ctx.fill_extents()
        ctx.fill()

        if self._box:
            if self._linecolor is not None:
                ctx.set_source_rgb(self._linecolor[0], self._linecolor[1],
                                   self._linecolor[2])
            udx, udy = ctx.device_to_user_distance(linew, linew)
            if udx > udy:
                ctx.set_line_width(udx)
            else:
                ctx.set_line_width(udy)
            ctx.move_to(x0 - bordersp, y0 - bordersp)
            ctx.line_to(x1 + bordersp, y0 - bordersp)
            ctx.line_to(x1 + bordersp, y1 + bordersp)
            ctx.line_to(x0 - bordersp, y1 + bordersp)
            ctx.close_path()
            ctx.stroke()

        # Make sure the image is fully drawn
        img.flush()

        data = numpy.frombuffer(img.get_data(), dtype=numpy.ubyte)
        data.shape = (imwid, imwid, 4)

        # Move the colors around to what OpenGL needs
        if LabelObject._is_little_endian:
            self.texturedata[:, :, 0] = data[:, :, 2]
            self.texturedata[:, :, 1] = data[:, :, 1]
            self.texturedata[:, :, 2] = data[:, :, 0]
            self.texturedata[:, :, 3] = data[:, :, 3]
        else:
            self.texturedata[:, :, 0:2] = data[:, :, 1:3]
            self.texturedata[:, :, 3] = data[:, :, 0]

        # Figure out what the width and height of the polygon should be.
        # They get stored in fullwid and fullhei (the renderer will use
        # those two fields)

        factor = 1.
        if self._units == "pixels":
            raise Exception("Pixel units for labels isn't implemented")
        elif self.units == "centidisplay":
            factor = 0.01

        self.glxoff = factor * self._xoff
        self.glyoff = factor * self._yoff

        # For the size of the polygon: factor * self._refheight is supposed
        #   to be the world-space unit height of 12 pixels on the image
        #   (for a self._height-point font).
        # fullheight units / imwid pixels = refheight units / 12 points
        # But my effective refheight is refheight * height / fsize
        #
        # so fullheight = imwid * refheight * height / (12 * fsize)

        self.fullhei = factor * self._refheight * imwid * self._height / (
            12 * fsize)
        self.fullwid = self.fullhei