Ticket #1030: 0001-Prevent-line-wrapping-for-short-labels.patch
File 0001-Prevent-line-wrapping-for-short-labels.patch, 3.7 KB (added by dsd, 14 years ago) |
---|
-
svgcard.py
From 9418f662092a92284c467624f6ea49e18334df83 Mon Sep 17 00:00:00 2001 From: Daniel Drake <dsd@laptop.org> Date: Thu, 9 Jul 2009 16:16:52 +0100 Subject: [PATCH] Prevent line wrapping for short labels On the XO, text on the tiles within the default game is wrapping because the font is too big. Add a mechanism to detect wraps and decrease the font size until an appropriate size is found. --- svgcard.py | 55 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 44 insertions(+), 11 deletions(-) diff --git a/svgcard.py b/svgcard.py index fdbb343..bc53701 100644
a b class SvgCard(gtk.DrawingArea): 221 221 stroke_color = self.default_propsfront_text.get('front_border').get('stroke_color') 222 222 self.set_border(fill_color, stroke_color) 223 223 self.flop() 224 224 225 # Attempt to fit some text (of length text_length) which is already present 226 # in a layout (with width max_width) so that it doesn't wrap or overflow. 227 # Works through a set of descending candidate sizes until a suitable 228 # size is found. The starting point within the candidate size array is 229 # guessed based on the length of the text. 230 def _fit_to_layout(self, layout, text_length, max_width, candidate_sizes): 231 # optimize for 1-character only: we can never wrap, and we'll assume 232 # that the largest font provided is suitable 233 if text_length <= 1: 234 desc = pango.FontDescription('Deja Vu Sans bold ' + 235 str(candidate_sizes[0])) 236 layout.set_font_description(desc) 237 return 238 239 text_length = text_length -1 240 241 # if we've got more text than we have suggested sizes for, then just 242 # assume the smallest size is the one we want 243 if text_length >= len(candidate_sizes): 244 desc = pango.FontDescription('Deja Vu Sans bold ' + 245 str(candidate_sizes[len(candidate_sizes) - 1])) 246 layout.set_font_description(desc) 247 return 248 249 idx = text_length 250 while idx < len(candidate_sizes): 251 # Try the candidate font size 252 fsize = str(candidate_sizes[idx]) 253 desc = pango.FontDescription('Deja Vu Sans bold ' + fsize) 254 layout.set_font_description(desc) 255 256 # figure out how big that makes us 257 width, height = layout.get_pixel_size() 258 259 # check if that font size fits OK 260 # I don't know why, but we have to also test the line count. 261 # Sometimes it wraps even though we never set a width to wrap at. 262 if width < max_width and layout.get_line_count() == 1: 263 break 264 idx = idx + 1 265 225 266 def get_text_layout(self, text, size): 226 267 if self.size >= 170: 227 268 font_sizes = [80, 60, 46, 36, 28, 22, 18, 12] … … class SvgCard(gtk.DrawingArea): 232 273 elif self.size >= 50: 233 274 font_sizes = [30, 24, 18, 16, 14, 12, 12, 10] 234 275 else: 235 font_sizes = [16, 12, 10, 8, 8, 8, 8, 8] 236 237 # Set font size considering string length 238 if len(text) <= 8: 239 font_size = font_sizes[len(text)-1] 240 else: 241 font_size = 10 276 font_sizes = [16, 12, 10, 8] 242 277 243 278 # Set Pango context and Pango layout 244 279 context = self.create_pango_context() 245 280 layout = self.create_pango_layout(text) 246 desc = pango.FontDescription('Deja Vu Sans bold '+str(font_size))247 layout.set_font_description(desc)248 281 layout.set_alignment(pango.ALIGN_CENTER) 249 layout.set_width(size*1000)282 self._fit_to_layout(layout, len(text), size, font_sizes) 250 283 return layout 251 284 252 285 def set_background(self, color):