Python pangocairo.context_set_font_options() Examples

The following are 4 code examples of pangocairo.context_set_font_options(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pangocairo , or try the search function .
Example #1
Source File: cairo_backend.py    From symbolator with MIT License 5 votes vote down vote up
def draw_text(x, y, text, font, text_color, spacing, c):
    c.save()
    c.set_source_rgba(*rgb_to_cairo(text_color))
    font = cairo_font(font)

    c.translate(x, y)
    
    if use_pygobject:
      status, attrs, plain_text, _ = pango.parse_markup(text, len(text), '\0')
      
      layout = pangocairo.create_layout(c)
      pctx = layout.get_context()
      fo = cairo.FontOptions()
      fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
      pangocairo.context_set_font_options(pctx, fo)
      layout.set_font_description(font)
      layout.set_spacing(spacing * pango.SCALE)
      layout.set_text(plain_text, len(plain_text))
      layout.set_attributes(attrs)
      pangocairo.update_layout(c, layout)
      pangocairo.show_layout(c, layout)

    else: # pyGtk
      attrs, plain_text, _ = pango.parse_markup(text)
      
      pctx = pangocairo.CairoContext(c)
      pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
      layout = pctx.create_layout()
      layout.set_font_description(font)
      layout.set_spacing(spacing * pango.SCALE)
      layout.set_text(plain_text)
      layout.set_attributes(attrs)
      pctx.update_layout(layout)
      pctx.show_layout(layout)
      
    c.restore() 
Example #2
Source File: syntrax.py    From syntrax with MIT License 5 votes vote down vote up
def cairo_text_bbox(text, font_params, scale=1.0):
  surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8)
  ctx = cairo.Context(surf)

  # The scaling must match the final context.
  # If not there can be a mismatch between the computed extents here
  # and those generated for the final render.
  ctx.scale(scale, scale)

  font = cairo_font(font_params)

  if use_pygobject:
    layout = pangocairo.create_layout(ctx)
    pctx = layout.get_context()
    fo = cairo.FontOptions()
    fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
    pangocairo.context_set_font_options(pctx, fo)
    layout.set_font_description(font)
    layout.set_text(text, len(text))
    re = layout.get_pixel_extents()[1]
    extents = (re.x, re.y, re.x + re.width, re.y + re.height)

  else: # pyGtk
    pctx = pangocairo.CairoContext(ctx)
    pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
    layout = pctx.create_layout()
    layout.set_font_description(font)
    layout.set_text(text)

    #print('@@ EXTENTS:', layout.get_pixel_extents()[1])
    extents = layout.get_pixel_extents()[1]
  w = extents[2] - extents[0]
  h = extents[3] - extents[1]
  x0 = - w // 2.0
  y0 = - h // 2.0
  return [x0,y0, x0+w,y0+h] 
Example #3
Source File: syntrax.py    From syntrax with MIT License 5 votes vote down vote up
def cairo_draw_text(x, y, text, font, text_color, c):
  c.save()
  #print('## TEXT COLOR:', text_color)
  c.set_source_rgba(*rgb_to_cairo(text_color))
  font = cairo_font(font)

  c.translate(x, y)

  if use_pygobject:
    layout = pangocairo.create_layout(c)
    pctx = layout.get_context()
    fo = cairo.FontOptions()
    fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
    pangocairo.context_set_font_options(pctx, fo)
    layout.set_font_description(font)
    layout.set_text(text, len(text))
    pangocairo.update_layout(c, layout)
    pangocairo.show_layout(c, layout)

  else: # pyGtk
    pctx = pangocairo.CairoContext(c)
    pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
    layout = pctx.create_layout()
    layout.set_font_description(font)
    layout.set_text(text)
    pctx.update_layout(layout)
    pctx.show_layout(layout)

  c.restore() 
Example #4
Source File: cairo_backend.py    From symbolator with MIT License 4 votes vote down vote up
def cairo_text_bbox(text, font_params, spacing=0, scale=1.0):
    surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8)
    ctx = cairo.Context(surf)

    # The scaling must match the final context.
    # If not there can be a mismatch between the computed extents here
    # and those generated for the final render.
    ctx.scale(scale, scale)
    
    font = cairo_font(font_params)


    if use_pygobject:
      status, attrs, plain_text, _ = pango.parse_markup(text, len(text), '\0')
      
      layout = pangocairo.create_layout(ctx)
      pctx = layout.get_context()
      fo = cairo.FontOptions()
      fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
      pangocairo.context_set_font_options(pctx, fo)
      layout.set_font_description(font)
      layout.set_spacing(spacing * pango.SCALE)
      layout.set_text(plain_text, len(plain_text))
      layout.set_attributes(attrs)

      li = layout.get_iter() # Get first line of text
      baseline = li.get_baseline() / pango.SCALE

      re = layout.get_pixel_extents()[1] # Get logical extents
      extents = (re.x, re.y, re.x + re.width, re.y + re.height)

    else: # pyGtk
      attrs, plain_text, _ = pango.parse_markup(text)

      pctx = pangocairo.CairoContext(ctx)
      pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
      layout = pctx.create_layout()
      layout.set_font_description(font)
      layout.set_spacing(spacing * pango.SCALE)
      layout.set_text(plain_text)
      layout.set_attributes(attrs)

      li = layout.get_iter() # Get first line of text
      baseline = li.get_baseline() / pango.SCALE

      #print('@@ EXTENTS:', layout.get_pixel_extents()[1], spacing)
      extents = layout.get_pixel_extents()[1] # Get logical extents

    return [extents[0], extents[1], extents[2], extents[3], baseline]