示例#1
0
    def draw(self, ctx: Context):

        text_baseline = self.get_text_baseline(ctx)

        default_font_max_height = ctx.get_scaled_font().extents()[0]

        text_max_length = float('inf')
        line_string_bottom_length = text_baseline.length

        trimmed = False
        while text_max_length > line_string_bottom_length:
            text_max_length = ctx.text_extents(self.text)[4] / default_font_max_height * self.text_height + \
                              (len(self.text)-1) * self.text_spacing
            if text_max_length > line_string_bottom_length:
                self.text = self.text[:-1].strip()
                trimmed = True

        text_start_interp_pos_min = 0
        text_start_interp_pos_max = line_string_bottom_length - text_max_length
        if self.text_alignment == 'left':
            text_start_interp_pos = self.text_alignment_offset
        elif self.text_alignment == 'center':
            text_start_interp_pos = line_string_bottom_length / 2 - text_max_length / 2 - self.text_alignment_offset
        else:
            text_start_interp_pos = line_string_bottom_length - text_max_length - self.text_alignment_offset
        text_start_interp_pos = min(
            text_start_interp_pos_max,
            max(text_start_interp_pos_min, text_start_interp_pos))

        text_interp_pos = text_start_interp_pos
        for char in self.text:
            char_pos_origin = text_baseline.interpolate(text_interp_pos)
            char_advance_x = ctx.text_extents(
                char)[4] / default_font_max_height * self.text_height
            char_pos_end = text_baseline.interpolate(text_interp_pos +
                                                     char_advance_x)

            ctx.save()
            ctx.translate(char_pos_origin.x, char_pos_origin.y)
            text_angle = math.atan2(char_pos_end.y - char_pos_origin.y,
                                    char_pos_end.x - char_pos_origin.x)
            ctx.rotate(text_angle)
            ctx.scale(1 / default_font_max_height * self.text_height)
            if not trimmed:
                ctx.set_source_rgba(0, 0, 0, 1)
            else:
                ctx.set_source_rgba(0, 0, 0.2, 1)
            ctx.show_text(char)
            ctx.fill()
            ctx.restore()

            text_interp_pos += char_advance_x + self.text_spacing
 def _generate_layer(self, transcription, page, layer):
     surface = PDFSurface(layer, *page.page_size)
     context = Context(surface)
     # context.select_font_face('Georgia')
     context.set_source_rgba(1, 1, 1, 1 / 256)  # almost invisible
     context.set_font_size(2)
     for line_ink, line_transcription in zip(page.lines, transcription):
         ink, transformation = writing.normalized(line_ink)
         context.save()
         context.transform(
             Matrix(*(Transformation.translation(
                 0, page.page_size[1]).parameter)))
         context.transform(Matrix(*(Transformation.mirror(0).parameter)))
         context.transform(Matrix(*((~transformation).parameter)))
         context.transform(Matrix(*(Transformation.mirror(0).parameter)))
         HANDWRITING_WIDTH = ink.boundary_box[1]
         TYPEWRITING_WIDTH = context.text_extents(line_transcription)[2]
         context.scale(HANDWRITING_WIDTH / TYPEWRITING_WIDTH, 1)
         context.move_to(0, 0)
         context.show_text(line_transcription)
         context.restore()
     context.stroke()
     context.show_page()
     surface.flush()
示例#3
0
        ctx.set_operator(OPERATOR_OVER)

        ctx.select_font_face("Roboto", FONT_SLANT_NORMAL, FONT_WEIGHT_BOLD)
        ctx.set_font_size(FONT_SIZE)

        labels = base_labels + variant_info

        for pass_ in range(2):
            for pin_list, type, note in labels:
                text = f"{pin_list}: {note}" if note else pin_list

                pin_names = pin_list.split("+")

                # calculate label size
                (x_bearing, y_bearing, width, height, x_advance,
                 y_advance) = ctx.text_extents(text)

                label_width = width + 2 * label_pad[0]
                label_height = height + 2 * label_pad[1]

                _, _, align = pin_defs[pin_names[0]]  # HACK

                if align == RIGHT:
                    x = w * pad_sides - label_margin[0] - label_width
                    line_origin_x = x + label_width
                else:
                    x = w * pad_sides + w + label_margin[0]
                    line_origin_x = x

                # https://lospec.com/palette-list/dawnbringer-16
                color_table = {