Python cairo 模块,FontOptions() 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用cairo.FontOptions()

项目:infofeld    作者:vain    | 项目源码 | 文件源码
def shadowed_text(cr, args, x, y, string):
    cr.save()
    cr.new_path()
    cr.move_to(x, y)

    if not args.antialias_font:
        fo = cairo.FontOptions()
        fo.set_antialias(cairo.ANTIALIAS_NONE)
        cr.set_font_options(fo)
    cr.set_font_size(args.size_font)

    cr.text_path(string)
    cr.set_source_rgb(*[float(f) for f in args.color_font_shadow.split(',')])
    cr.stroke_preserve()
    cr.set_source_rgb(*[float(f) for f in args.color_font.split(',')])
    cr.fill()
    cr.new_path()
    cr.restore()
项目:syntrax    作者:kevinpt    | 项目源码 | 文件源码
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]
项目:syntrax    作者:kevinpt    | 项目源码 | 文件源码
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()
项目:symbolator    作者:kevinpt    | 项目源码 | 文件源码
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()
项目:neferset    作者:andburn    | 项目源码 | 文件源码
def text_path(context, font, size, text, debug=False):
    """Create a Pango text layout and return it as a Cairo path"""

    context.save()

    pg_layout = PangoCairo.create_layout(context)
    pg_context = pg_layout.get_context()

    font_options = cairo.FontOptions()
    font_options.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
    PangoCairo.context_set_font_options(pg_context, font_options)
    font_descp = "{0} {1:d}".format(font, size)
    pg_layout.set_font_description(Pango.FontDescription(font_descp))
    pg_layout.set_text(text, -1) # force length calculation

    PangoCairo.update_layout(context, pg_layout)
    # TODO watch out for ink & logical, need check
    extents = pg_layout.get_pixel_extents()[0]
    # TODO debug necessary?
    if debug:
        context.set_source_rgb(0, 0, 0)
        PangoCairo.show_layout(context, pg_layout)
        print("text_path: ({0}, {1}, {2}, {3})".format(
            extents.x, extents.y, extents.width, extents.height))
        context.rectangle(extents.x, extents.y, extents.width, extents.height)
        context.set_line_width(1.0)
        context.set_line_join(cairo.LINE_JOIN_MITER)
        context.set_source_rgb(0, 0.8, 0)
        context.stroke()

    #PangoCairo.layout_path(context, pg_layout)
    PangoCairo.layout_line_path(context, pg_layout.get_line(0))
    path = context.copy_path()
    # clear the path
    context.new_path()
    context.restore()
    return (path, extents, xheight(pg_layout).height)
项目:symbolator    作者:kevinpt    | 项目源码 | 文件源码
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]
项目:neferset    作者:andburn    | 项目源码 | 文件源码
def text_block(ctx, obj, text, font, lang="en-US", debug=False):
    ctx.save()

    lyt = PangoCairo.create_layout(ctx)
    pg_ctx = lyt.get_context()
    pg_ctx.set_language(Pango.Language.from_string(lang))

    fo = cairo.FontOptions()
    fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
    PangoCairo.context_set_font_options(pg_ctx, fo)
    font_family = font.family if not font.replace else font.replace
    pg_font = Pango.FontDescription("{} {}px".format(font_family, font.size))
    lyt.set_font_description(pg_font)
    lyt.set_markup(text, -1) # force length calculation

    lyt.set_height(obj.height * Pango.SCALE * 1.5) # TODO what?
    lyt.set_width(obj.width * Pango.SCALE)
    lyt.set_alignment(Pango.Alignment.CENTER)
    #PangoCairo.update_layout(ctx, lyt)

    pg_size = lyt.get_pixel_size()
    ink, logical = lyt.get_pixel_extents()

    while ink.height > obj.height and pg_font.get_size() > 0:
        pg_font.set_size(pg_font.get_size() - Pango.SCALE)
        lyt.set_font_description(pg_font)
        ink, logical = lyt.get_pixel_extents()

    #x = obj["x"] - pext.x - pext.width / 2
    #y = obj["y"] - pext.y - pext.height / 2
    x = (obj.x + obj.width / 2) - ((ink.x + ink.width / 2))
    y = (obj.y + obj.height / 2) - ((ink.y + ink.height / 2))
    #x = obj["x"]
    #y = obj["y"]
    if debug:
        print("x,y: %s, %s" % (x, y))
    ctx.translate(x, y)

    PangoCairo.update_layout(ctx, lyt)
    PangoCairo.layout_path(ctx, lyt)
    ctx.set_line_width(9.0)
    ctx.set_line_cap(cairo.LINE_CAP_ROUND)
    ctx.set_line_join(cairo.LINE_JOIN_ROUND)
    ctx.set_source_rgb(*font.color)
    PangoCairo.show_layout(ctx, lyt)

    ctx.new_path()
    ctx.restore()

    if debug:
        rectangle(ctx, ink.x, ink.y, ink.width, ink.height, True, 2.0, (0, 1, 0))
        rectangle(ctx, obj.x, obj.y, obj.width, obj.height, True, 2.0, (0.8, 0.8, 0))