From e7b074e4df8d2bf9a31dbbf46a98c6b72431b591 Mon Sep 17 00:00:00 2001 From: qua-non Date: Sat, 9 Mar 2013 17:48:00 +0530 Subject: [PATCH] core:text change `justified` to `justify` for consistency --- examples/widgets/textalign.py | 5 ++--- kivy/core/text/__init__.py | 14 +++++++----- kivy/core/text/markup.py | 40 ++++++++++++++++++++--------------- kivy/uix/label.py | 9 ++++---- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/examples/widgets/textalign.py b/examples/widgets/textalign.py index 429e74867..16f08fb6a 100644 --- a/examples/widgets/textalign.py +++ b/examples/widgets/textalign.py @@ -16,11 +16,10 @@ class Selector(FloatLayout): class TextAlignApp(App): def select(self, case): - grid = GridLayout(rows=3, cols=6, spacing=10, size_hint=(None, None), + grid = GridLayout(rows=3, cols=3, spacing=10, size_hint=(None, None), pos_hint={'center_x': .5, 'center_y': .5}) for valign in ('bottom', 'middle', 'top'): - for halign in ('left', 'center', 'right', 'left_justified', - 'center_justified', 'right_justified'): + for halign in ('left', 'center', 'right'): label = BoundedLabel(text='V: %s\nH: %s' % (valign, halign), size_hint=(None, None), size=(150, 150), diff --git a/kivy/core/text/__init__.py b/kivy/core/text/__init__.py index 277edbfcf..91b15c621 100644 --- a/kivy/core/text/__init__.py +++ b/kivy/core/text/__init__.py @@ -253,9 +253,11 @@ class LabelBase(object): lh = lh * options['line_height'] if real: x = 0 - if halign[0] == 'center': + if halign[0] == 'c': + # center x = int((self.width - lw) / 2.) - elif halign == 'right': + elif halign[0] == 'r': + # right x = int(self.width - lw) if len(line): render_text(line, x, y) @@ -347,14 +349,16 @@ class LabelBase(object): # really render now. for size, last_line, glyphs in lines: x = 0 - if halign.startswith('center'): + if halign[0] == 'c': + # center x = int((self.width - size[0]) / 2.) - elif halign.startswith('right'): + elif halign[0] == 'r': + # right x = int(self.width - size[0]) # justification just_space = 0 - if halign[-1] == 'd': + if halign[-1] == 'y': # justified if glyphs and not last_line: x = 0 diff --git a/kivy/core/text/markup.py b/kivy/core/text/markup.py index f664e0845..86bb81f3e 100644 --- a/kivy/core/text/markup.py +++ b/kivy/core/text/markup.py @@ -219,9 +219,9 @@ class MarkupLabel(MarkupLabelBase): if len(lines): line = lines[-1] else: - # line-> line width, line height, is_last_line, words + # line-> line width, line height, is_last_line, is_first_line, words # words -> (w, h, word)... - line = [0, 0, 0, []] + line = [0, 0, 0, 1, []] lines.append(line) # extract user limitation @@ -236,7 +236,7 @@ class MarkupLabel(MarkupLabelBase): if part == '\n': # put a new line! - line = [0, default_line_height, 0, []] + line = [0, default_line_height, 0, 1, []] # skip last line for justification. if lines: lines[-1][2] = 1 @@ -261,13 +261,13 @@ class MarkupLabel(MarkupLabelBase): if uw is None or lw + pw < uw: # no limitation or part can be contained in the line # then append the part to the line - line[3].append((pw, ph, part, options)) + line[4].append((pw, ph, part, options)) # and update the line size line[0] += pw line[1] = max(line[1], ph) else: # part can't be put in the line, do a new one... - line = [pw, ph, 0, [(pw, ph, part, options)]] + line = [pw, ph, 0, 0, [(pw, ph, part, options)]] lines.append(line) # set last_line to be skipped for justification lines[-1][2] = 1 @@ -281,8 +281,7 @@ class MarkupLabel(MarkupLabelBase): # convert halign/valign to int, faster comparison av = {'top': 0, 'middle': 1, 'bottom': 2}[self.options['valign']] ah = {'left': 0, 'center': 1, 'right': 2, - 'left_justified': 3, 'center_justified': 4, - 'right_justified': 5}[self.options['halign']] + 'justify': 3,}[self.options['halign']] y = 0 w, h = self._size @@ -294,12 +293,13 @@ class MarkupLabel(MarkupLabelBase): lh = line[1] lw = line[0] last_line = line[2] + first_line = line[3] # horizontal alignement if ah in (0, 3): # left x = 0 - elif ah in (1, 4): + elif ah == 1: # center x = int((w - lw) / 2) else: @@ -316,28 +316,34 @@ class MarkupLabel(MarkupLabelBase): # justification just_space = 0 + first_space = 0 if ah > 2: # justified - if line[3] and not last_line: - last_word = line[3][-1][2] + if line[4] and not last_line: + last_word = line[4][-1][2] - x = last_space = space_width = _spaces = 0 - for pw, ph, word, options in line[3]: - _spaces += 1if word == ' ' else 0 + x = first_space = last_space = space_width = _spaces = 0 + for pw, ph, word, options in line[4]: + _spaces += 1 if word == ' ' else 0 if word == ' ': last_space = 1 space_width = pw - _spaces -= last_space + if line[4][0][2][0] == ' ': + first_space = 1 + space_width += pw + _spaces -= last_space + first_space # divide left over space between `spaces` if _spaces: - just_space = (((w - lw + space_width) *1.) + just_space = (((w - lw + space_width) * 1.) /(_spaces*1.)) - - for pw, ph, part, options in line[3]: + for pw, ph, part, options in line[4]: self.options = options + if not first_line and first_space: + first_line = 1 + continue if part == ' ': x += just_space r(part, x, y + (lh - ph) / 1.25) diff --git a/kivy/uix/label.py b/kivy/uix/label.py index e7b6541fa..5bf974357 100644 --- a/kivy/uix/label.py +++ b/kivy/uix/label.py @@ -340,8 +340,7 @@ class Label(Widget): ''' halign = OptionProperty('left', options=['left', 'center', 'right', - 'left_justified', 'right_justified', - 'center_justified']) + 'justify']) '''Horizontal alignment of the text. :data:`halign` is a :class:`~kivy.properties.OptionProperty`, default to @@ -354,10 +353,10 @@ class Label(Widget): want to bind the size of the Label to the :data:`texture_size` or set a :data:`text_size`. - .. versionchanged:: 1.0.6 + .. versionchanged:: 1.6.0 - Starting version 1.0.6 two new options were added to :data:`halign` - namely `left_justified` and `right_justified`. + Starting version 1.6.0 a new option was added to :data:`halign` + namely `justify` ''' valign = OptionProperty('bottom', options=['bottom', 'middle', 'top'])