Commit bde17bbe authored by Michal Čihař's avatar Michal Čihař

Fit text to widget if it is too big

parent 6f965e3f
...@@ -203,6 +203,20 @@ def widgets(request, project): ...@@ -203,6 +203,20 @@ def widgets(request, project):
'form': form, 'form': form,
})) }))
def render_text(pangocairo_context, line, text, params, font_size):
'''
Generates Pango layout for text.
'''
# Create pango layout and set font
layout = pangocairo_context.create_layout()
font = pango.FontDescription('%s %d' % (line['font'], font_size))
layout.set_font_description(font)
# Add text
layout.set_text(text)
return layout
@cache_page(3600) @cache_page(3600)
def render(request, project, widget = '287x66', color = None, lang = None): def render(request, project, widget = '287x66', color = None, lang = None):
obj = get_object_or_404(Project, slug = project) obj = get_object_or_404(Project, slug = project)
...@@ -286,16 +300,21 @@ def render(request, project, widget = '287x66', color = None, lang = None): ...@@ -286,16 +300,21 @@ def render(request, project, widget = '287x66', color = None, lang = None):
text = text.replace('English', lang.name) text = text.replace('English', lang.name)
text = text % params text = text % params
# Create pango layout and set font font_size = line['font_size']
layout = pangocairo_context.create_layout()
font = pango.FontDescription('%s %d' % (line['font'], line['font_size']))
layout.set_font_description(font)
# Set color and position # Render text
ctx.move_to(*line['pos']) layout = render_text(pangocairo_context, line, text, params, font_size)
# Fit text to image if it is too big
extent = layout.get_pixel_extents()
width = surface.get_width()
while extent[1][2] + line['pos'][0] > width and font_size > 4:
font_size -= 1
layout = render_text(pangocairo_context, line, text, params, font_size)
extent = layout.get_pixel_extents()
# Add text # Set position
layout.set_text(text) ctx.move_to(*line['pos'])
# Render to cairo context # Render to cairo context
pangocairo_context.update_layout(layout) pangocairo_context.update_layout(layout)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment