core:text change `justified` to `justify` for consistency

This commit is contained in:
qua-non 2013-03-09 17:48:00 +05:30
parent 0eaf0a46bf
commit e7b074e4df
4 changed files with 38 additions and 30 deletions

View File

@ -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),

View File

@ -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

View File

@ -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)

View File

@ -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'])