示例#1
0
 def text_height(self, width, pango_context):
     fontn = pango.FontDescription()
     fontn.set_family(self.font_family)
     fontn.set_size(pango.units_from_double(self.font_size))
     pango_context.set_font_description(fontn)
     layout = pango.layout.Layout(pango_context)
     layout.set_width(pango.units_from_double(width))
     layout.set_text(self.text)
     _, raw_height = layout.get_size()
     return pango.units_to_double(raw_height)
示例#2
0
def write_text(ctx, text, start_x, start_y):
    ctx.move_to(start_x, start_y)
    layout = pangocairocffi.create_layout(ctx)
    layout.set_width(pangocffi.units_from_double(width))
    #layout.set_alignment(pangocffi.Alignment.CENTER)
    fontdesc = pangocffi.FontDescription()
    fontdesc.set_size(pangocffi.units_from_double(3))
    fontdesc.set_family(font_family)
    layout.set_font_description(fontdesc)
    layout.set_text(text)
    pangocairocffi.show_layout(ctx, layout)
示例#3
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
示例#4
0
def test_t2s() -> None:
    size = 1
    temp_pango_text = Text("Helloworld", t2s={"world": ITALIC})
    surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH)
    context = cairocffi.Context(surface)
    context.move_to(START_X, START_Y)
    layout = pangocairocffi.create_layout(context)
    layout.set_width(pangocffi.units_from_double(WIDTH))
    fontdesc = pangocffi.FontDescription()
    fontdesc.set_size(pangocffi.units_from_double(size * 10))
    layout.set_font_description(fontdesc)
    layout.set_markup(
        'Hello<span style="italic">world</span>')  # yay, pango markup
    pangocairocffi.show_layout(context, layout)
    surface.finish()
    assert compare_SVGObject_with_PangoText(temp_pango_text, filename)
示例#5
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)
示例#6
0
def test_rtl_text_to_svgobject() -> None:
    """Checks number of submobjects generated when directly
    called using ``SVGMobject``"""
    size = 1
    text = RTL_TEXT.replace("\n", "")
    temp_pango_text = Text(text, size=1)
    surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH)
    context = cairocffi.Context(surface)
    context.move_to(START_X, START_Y)
    layout = pangocairocffi.create_layout(context)
    layout.set_width(pangocffi.units_from_double(WIDTH))
    fontdesc = pangocffi.FontDescription()
    fontdesc.set_size(pangocffi.units_from_double(size * 10))
    layout.set_font_description(fontdesc)
    layout.set_text(text)
    pangocairocffi.show_layout(context, layout)
    surface.finish()
    assert compare_SVGObject_with_PangoText(temp_pango_text, filename)
示例#7
0
def test_tabs_replace() -> None:
    """Checks whether are there in end svg image.
    Pango should handle tabs and line breaks."""
    size = 1
    temp_pango_text = Text("hello\thi\nf")
    assert temp_pango_text.text == "hellohif"
    surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH)
    context = cairocffi.Context(surface)
    context.move_to(START_X, START_Y)
    layout = pangocairocffi.create_layout(context)
    layout.set_width(pangocffi.units_from_double(WIDTH))
    fontdesc = pangocffi.FontDescription()
    fontdesc.set_size(pangocffi.units_from_double(size * 10))
    layout.set_font_description(fontdesc)
    layout.set_text("hellohif")
    pangocairocffi.show_layout(context, layout)
    surface.finish()
    assert compare_SVGObject_with_PangoText(temp_pango_text, filename)
示例#8
0
def test_font_face() -> None:
    """Checks font face using submobject len"""
    size = 1
    text = RTL_TEXT.replace("\n", "")
    font_face = "sans"
    temp_pango_text = Text(text, size=1, font=font_face)
    surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH)
    context = cairocffi.Context(surface)
    context.move_to(START_X, START_Y)
    layout = pangocairocffi.create_layout(context)
    layout.set_width(pangocffi.units_from_double(WIDTH))
    fontdesc = pangocffi.FontDescription()
    fontdesc.set_family(font_face)
    fontdesc.set_size(pangocffi.units_from_double(size * 10))
    layout.set_font_description(fontdesc)
    layout.set_text(text)
    pangocairocffi.show_layout(context, layout)
    surface.finish()
    assert compare_SVGObject_with_PangoText(temp_pango_text, filename)
示例#9
0
文件: params.py 项目: fergusq/mango
    def __init__(self, args: Namespace):
        self.width = float(args.width)
        self.height = float(args.height)
        self.margin = float(args.margin)
        self.line_width = float(self.width - 2 * self.margin)
        self.line_height = float(16)
        self.page_height = float(self.height - 2 * self.margin)
        self.pg_gap = float(16)
        self.column_gap = float(10)
        self.min_word_gap = float(5)
        self.indent = float(0)
        self.quote_indent = float(50)
        self.text_align = "justify"
        self.font_size = float(10)

        self.font = "rm"
        self.fonts = {}
        self.fonts["rm"] = pango.FontDescription()
        self.fonts["rm"].set_family(args.font)
        self.fonts["rm"].set_size(pango.units_from_double(10))

        print(f"Paperin koko {self.width}x{self.height}")
        print(f"Piirtoalueen koko {self.line_width}x{self.page_height}")
示例#10
0
文件: params.py 项目: fergusq/mango
 def addFont(self, varname, fontname):
     print(f"Lisätään fontti {varname} = {repr(fontname)}")
     self.fonts[varname] = pango.FontDescription()
     self.fonts[varname].set_family(fontname)
     self.fonts[varname].set_size(pango.units_from_double(10))