Skip to content

Commit 3851a70

Browse files
committed
fix stripping for wrapped text
1 parent b81d873 commit 3851a70

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

kivy/core/text/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,22 @@ def render(self, real=False):
500500
options['halign'][-1] == 'y')
501501
uw, uh = options['text_size'] = self._text_size
502502
text = self.text
503-
if strip:
504-
text = text.strip()
503+
if not strip:
504+
# all text will be stripped by default. unicode NO-BREAK SPACE
505+
# characters will be preserved, so we replace the leading and
506+
# trailing spaces with \u00a0
507+
text = text.decode('utf8') if isinstance(text, bytes) else text
508+
lspace = len(text) - len(text.lstrip())
509+
rspace = len(text) - len(text.rstrip())
510+
text = (u'\u00a0' * lspace) + text.strip() + (u'\u00a0' * rspace)
505511
if uw is not None and options['shorten']:
506512
text = self.shorten(text)
507513
self._cached_lines = lines = []
508514
if not text:
509515
return 0, 0
510516

517+
ostrip = options['strip']
518+
strip = options['strip'] = True
511519
if uh is not None and options['valign'][-1] == 'e': # middle
512520
center = -1 # pos of newline
513521
if len(text) > 1:
@@ -533,6 +541,7 @@ def render(self, real=False):
533541
else: # top or bottom
534542
w, h, clipped = layout_text(text, lines, (0, 0), (uw, uh), options,
535543
self.get_cached_extents(), options['valign'][-1] == 'p', True)
544+
options['strip'] = ostrip
536545
self._internal_size = w, h
537546
if uw:
538547
w = uw

kivy/core/text/text_layout.pyx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ An internal module for laying out text according to options and constraints.
66
This is not part of the API and may change at any time.
77
'''
88

9+
import string
910

1011
__all__ = ('layout_text', 'LayoutWord', 'LayoutLine')
1112

@@ -147,7 +148,7 @@ cdef inline void final_strip(LayoutLine line):
147148
line.w -= last_word.lw # likely 0
148149
continue
149150

150-
stripped = last_word.text.rstrip() # ends with space
151+
stripped = last_word.text.rstrip(string.whitespace) # ends with space
151152
# subtract ending space length
152153
diff = ((len(last_word.text) - len(stripped)) *
153154
last_word.options['space_width'])
@@ -187,10 +188,10 @@ cdef inline layout_text_unrestricted(object text, list lines, int w, int h,
187188
k = n - 1
188189
if strip:
189190
if not _line.w: # no proceeding text: strip leading
190-
line = line.lstrip()
191+
line = line.lstrip(string.whitespace)
191192
# ends this line so right strip
192193
if complete or (dwn and n > 1 or not dwn and pos > 1):
193-
line = line.rstrip()
194+
line = line.rstrip(string.whitespace)
194195
lw, lh = get_extents(line)
195196

196197
old_lh = _line.h
@@ -217,9 +218,9 @@ cdef inline layout_text_unrestricted(object text, list lines, int w, int h,
217218
# the last line is only stripped from left
218219
if strip:
219220
if complete or (dwn and i < n - 1 or not dwn and i > s):
220-
line = line.strip()
221+
line = line.strip(string.whitespace)
221222
else:
222-
line = line.lstrip()
223+
line = line.lstrip(string.whitespace)
223224
lw, lh = get_extents(line)
224225
lhh = int(lh * line_height)
225226
if uh != -1 and h + lhh > uh and pos: # too high
@@ -424,9 +425,9 @@ def layout_text(object text, list lines, tuple size, tuple text_size,
424425
line = new_lines[i]
425426
if strip:
426427
if not _line.w: # there's no proceeding text, so strip leading
427-
line = line.lstrip()
428+
line = line.lstrip(string.whitespace)
428429
if ends_line:
429-
line = line.rstrip()
430+
line = line.rstrip(string.whitespace)
430431
k = len(line)
431432
if not k: # just add empty line if empty
432433
_line.is_last_line = ends_line # nothing will be appended
@@ -474,7 +475,7 @@ def layout_text(object text, list lines, tuple size, tuple text_size,
474475
if s != m:
475476
_do_last_line = 1
476477
if strip and line[m - 1] == ' ':
477-
ln = line[s:m].rstrip()
478+
ln = line[s:m].rstrip(string.whitespace)
478479
lww, lhh = get_extents(ln)
479480
else:
480481
ln = line[s:m]
@@ -494,7 +495,7 @@ def layout_text(object text, list lines, tuple size, tuple text_size,
494495
# try to fit word on new line, if it doesn't fit we'll
495496
# have to break the word into as many lines needed
496497
if strip:
497-
s = e - len(line[s:e].lstrip())
498+
s = e - len(line[s:e].lstrip(string.whitespace))
498499
if s == e: # if it was only a stripped space, move on
499500
m = s
500501
continue

0 commit comments

Comments
 (0)