Exemplo n.º 1
0
 def text2svg(self):
     """Internally used function.
     Convert the text to SVG using Pango
     """
     size = self.size * 10
     line_spacing = self.line_spacing * 10
     dir_name = config.get_dir("text_dir")
     disable_liga = self.disable_ligatures
     if not os.path.exists(dir_name):
         os.makedirs(dir_name)
     hash_name = self.text2hash()
     file_name = os.path.join(dir_name, hash_name) + ".svg"
     if os.path.exists(file_name):
         return file_name
     surface = cairocffi.SVGSurface(file_name, 600, 400)
     context = cairocffi.Context(surface)
     context.move_to(START_X, START_Y)
     settings = self.text2settings()
     offset_x = 0
     last_line_num = 0
     layout = pangocairocffi.create_layout(context)
     layout.set_width(pangocffi.units_from_double(600))
     for setting in settings:
         family = setting.font
         style = self.str2style(setting.slant)
         weight = self.str2weight(setting.weight)
         text = self.text[setting.start : setting.end].replace("\n", " ")
         fontdesc = pangocffi.FontDescription()
         fontdesc.set_size(pangocffi.units_from_double(size))
         if family:
             fontdesc.set_family(family)
         fontdesc.set_style(style)
         fontdesc.set_weight(weight)
         layout.set_font_description(fontdesc)
         if setting.line_num != last_line_num:
             offset_x = 0
             last_line_num = setting.line_num
         context.move_to(
             START_X + offset_x, START_Y + line_spacing * setting.line_num
         )
         pangocairocffi.update_layout(context, layout)
         if disable_liga:
             text = escape(text)
             layout.set_markup(f"<span font_features='liga=0'>{text}</span>")
         else:
             layout.set_text(text)
         logger.debug(f"Setting Text {text}")
         pangocairocffi.show_layout(context, layout)
         offset_x += pangocffi.units_to_double(layout.get_size()[0])
     surface.finish()
     return file_name
Exemplo n.º 2
0
 def render_text(self, content: FormattedText, width):
     fontn = pango.FontDescription()
     fontn.set_family(content.font_family)
     fontn.set_size(pango.units_from_double(content.font_size))
     self.pango_context.set_font_description(fontn)
     layout = pango.layout.Layout(self.pango_context)
     layout.set_text(content.text)
     if content.alignment == 'l':
         layout.set_alignment(pango.Alignment.LEFT)
     elif content.alignment == 'c':
         layout.set_alignment(pango.Alignment.CENTER)
     elif content.alignment == 'r':
         layout.set_alignment(pango.Alignment.RIGHT)
     layout.set_width(pango.units_from_double(width))
     pc.update_layout(self.cairo_context, layout)
     pc.show_layout(self.cairo_context, layout)