This patch attempts to address the single-line case in http://savannah.nongnu.org/bugs/?36166 where a particular translation could be so wide, it would wrap into multiple lines, thus breaking the layout.
This patch determines how wide the text would be before drawing it and rescales it if it comes close to wrapping. CC: Thomas Petazzoni <thomas.petazz...@enix.org> CC: David Mentré <dmen...@linux-france.org> Signed-off-by: Jeroen van Rijn <jvr...@gmail.com> --- ocitysmap2/indexlib/commons.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/ocitysmap2/indexlib/commons.py b/ocitysmap2/indexlib/commons.py index 6942577..35257a1 100644 --- a/ocitysmap2/indexlib/commons.py +++ b/ocitysmap2/indexlib/commons.py @@ -28,6 +28,9 @@ import os, sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import draw_utils +import logging +LOG = logging.getLogger('ocitysmap') + class IndexEmptyError(Exception): """This exception is raised when no data is to be rendered in the index.""" pass @@ -77,12 +80,47 @@ class IndexCategory: baseline_y (int): base Y axis position. """ + target_width = layout.get_width() ctx.save() + ctx.set_source_rgb(0.9, 0.9, 0.9) ctx.rectangle(baseline_x, baseline_y - fascent, - layout.get_width() / pango.SCALE, fheight) + target_width / pango.SCALE, fheight) ctx.fill() + # Measure header text and rescale if needed + ctx.save() + # Tell Pango to not wrap text + layout.set_width(-1) + # Let it relayout by giving it the text + layout.set_text(self.name) + layout.context_changed() + # What's the width when not wrapping? + actual_width = layout.get_size()[0] + # Do we need to rescale? + scale = float(target_width / actual_width) + LOG.debug('Drawing category: %s; target_width: %d, actual_width: %d' \ + % (str(self.name), int(target_width), int(actual_width))) + + if scale < 1.2: + LOG.debug('Rescaling category header: %s' % str(self.name)) + old_height = layout.get_size()[1] + # Get target font size for this header + fd = layout.get_font_description() + # Rescale the font and set it + fd.set_size(int(fd.get_size() * scale / 1.2)) + layout.set_font_description(fd) + # Need to take into account that a rescaled font is also less tall + new_height = layout.get_size()[1] + baseline_y += int((old_height - new_height) / pango.SCALE) + fheight = new_height / pango.SCALE + + # Restore target width + layout.set_width(target_width) + layout.context_changed() + # Restore context + ctx.restore() + ctx.set_source_rgb(0.0, 0.0, 0.0) draw_utils.draw_text_center(ctx, pc, layout, fascent, fheight, baseline_x, baseline_y, self.name) -- 1.7.10.128.g7945c