Text extents with Python Cairo
source link: https://blog.mathieu-leplatre.info/text-extents-with-python-cairo.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Text extents with Python Cairo
Mon 10 August 2009I needed this and could not find it. So I share it here (and even better if Google'd index it!)
def textwidth(text, fontsize=14): try: import cairo except Exception, e: return len(str) * fontsize surface = cairo.SVGSurface('undefined.svg', 1280, 200) cr = cairo.Context(surface) cr.select_font_face('Arial', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) cr.set_font_size(fontsize) xbearing, ybearing, width, height, xadvance, yadvance = cr.text_extents(text) return width
#cairo, #python - Posted in the Dev category
© Copyright 2020 by Mathieu Leplatre. mnmlist Theme
Content licensed under the Creative Commons attribution-noncommercial-sharealike License.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK