Example #1
0
 def clean(self):
     from impositions import utils
     # make sure color values are valid
     if self.allowed_colors:
         colors = []
         for c in self.allowed_colors.split(','):
             try:
                 colors.append(utils.parse_color(c, True))
             except ValueError, e:
                 raise ValidationError(str(e))
         self.allowed_colors = ','.join(colors)
Example #2
0
 def clean(self):
     from impositions import utils
     # make sure color values are valid
     if self.color_palette:
         palette = []
         for c in self.color_palette.split(','):
             try:
                 palette.append(utils.parse_color(c, True))
             except ValueError, e:
                 raise ValidationError(str(e))
         self.color_palette = ','.join(palette)
Example #3
0
    def render_text_region(self, region):
        x, y = region.template_region.left, region.template_region.top
        self.cr.move_to(x, y)
        pc_context = pangocairo.CairoContext(self.cr)
        pc_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
        
        # setup pango layout
        layout = pc_context.create_layout()
        font_size = float(region.get_font_size()) * FONT_SCALE
        font_desc = '{} {}'.format(region.get_font(), font_size)
        font = pango.FontDescription(font_desc)
        layout.set_font_description(font)

        # if a width is given, set width and word wrap
        if region.template_region.width:
            layout.set_width(region.template_region.width * pango.SCALE)
            layout.set_wrap(pango.WRAP_WORD)

        content = region.text
        if not region.template_region.allow_markup:
            content = re.sub(r'<[^>]*?>', '', content)

        # construct surrounding span tag if any style attrs were given
        style = region.template_region.text_style
        if style:
            content = '<span {}>{}</span>'.format(style, content)
        
        if region.template_region.text_align == 'JUSTIFY':
            layout.set_justify(True)
        else:
            align = region.template_region.text_align or 'LEFT'
            layout.set_alignment(getattr(pango, 'ALIGN_{}'.format(align)))

        layout.set_markup(content)
        rgb = (0,0,0)
        color = region.get_fg_color()
        if color:
            rgb = utils.parse_color(color)
        self.cr.set_source_rgb(*[int(c) for c in rgb])
        pc_context.update_layout(layout)
        pc_context.show_layout(layout)