From 44492e2ea4506be8e02f8f9672e9394849f2a27e Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Tue, 20 Nov 2012 14:36:05 -0600 Subject: [PATCH 1/5] add line_height property to label --- kivy/core/text/__init__.py | 7 ++++--- kivy/core/text/markup.py | 4 ++-- kivy/uix/label.py | 12 +++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/kivy/core/text/__init__.py b/kivy/core/text/__init__.py index e2dfca6fe..caa7c82a8 100644 --- a/kivy/core/text/__init__.py +++ b/kivy/core/text/__init__.py @@ -84,12 +84,12 @@ class LabelBase(object): def __init__(self, text='', font_size=12, font_name=DEFAULT_FONT, bold=False, italic=False, halign='left', valign='bottom', shorten=False, text_size=None, mipmap=False, color=None, - **kwargs): + line_height=1.0, **kwargs): options = {'text': text, 'font_size': font_size, 'font_name': font_name, 'bold': bold, 'italic': italic, 'halign': halign, 'valign': valign, 'shorten': shorten, - 'mipmap': mipmap} + 'mipmap': mipmap, 'line_height': line_height} options['color'] = color or (1, 1, 1, 1) options['padding'] = kwargs.get('padding', 0) @@ -245,6 +245,7 @@ class LabelBase(object): if uw is None: for line in self.text.split('\n'): lw, lh = get_extents(line) + lh = lh * options['line_height'] if real: x = 0 if halign == 'center': @@ -292,6 +293,7 @@ class LabelBase(object): gw, gh = cache[glyph] ww += gw wh = max(gh, wh) + wh = wh * options['line_height'] # is the word fit on the uw ? if ww > uw: @@ -301,7 +303,6 @@ class LabelBase(object): # get the maximum height for this line lh = max(wh, lh) - # is the word fit on the line ? if (word == '\n' or x + ww > uw) and lw != 0: # no, push actuals glyph diff --git a/kivy/core/text/markup.py b/kivy/core/text/markup.py index abd41384f..ab2c58bce 100644 --- a/kivy/core/text/markup.py +++ b/kivy/core/text/markup.py @@ -222,7 +222,7 @@ class MarkupLabel(MarkupLabelBase): uw, uh = self.text_size # split the word - default_line_height = get_extents(' ')[1] + default_line_height = get_extents(' ')[1] * self.options['line_height'] for part in re.split(r'( |\n)', word): if part == '': @@ -244,7 +244,7 @@ class MarkupLabel(MarkupLabelBase): pg = [cache[g] for g in part] pw = get_extents(part)[0] ph = max([g[1] for g in pg]) - + ph = ph * self.options['line_height'] options = copy(options) # check if the part can be put in the line diff --git a/kivy/uix/label.py b/kivy/uix/label.py index f70a0f47a..abbfba25a 100644 --- a/kivy/uix/label.py +++ b/kivy/uix/label.py @@ -117,7 +117,7 @@ class Label(Widget): _font_properties = ('text', 'font_size', 'font_name', 'bold', 'italic', 'halign', 'valign', 'padding_x', 'padding_y', 'text_size', 'shorten', - 'mipmap', 'markup') + 'mipmap', 'markup', 'line_height') def __init__(self, **kwargs): self._trigger_texture = Clock.create_trigger(self.texture_update, -1) @@ -284,6 +284,16 @@ class Label(Widget): 12dp. ''' + line_height = NumericProperty(1.0) + '''Line Height for the text. e.g. line_height = 2 will cause the spacing + between lines to be twice the size. + + :data:`line_height` is a :class:`~kivy.properties.NumericProperty`, default to + 1.0. + ''' + + + bold = BooleanProperty(False) '''Indicates use of the bold version of your font. From c08e79070fa4eb01016ad10de3b9b5f2eeeb7546 Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Tue, 20 Nov 2012 14:36:22 -0600 Subject: [PATCH 2/5] add example to show text_size and line_height property of label --- examples/widgets/label_text_size.py | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/widgets/label_text_size.py diff --git a/examples/widgets/label_text_size.py b/examples/widgets/label_text_size.py new file mode 100644 index 000000000..402fa68b7 --- /dev/null +++ b/examples/widgets/label_text_size.py @@ -0,0 +1,34 @@ + +''' +Label textsize +============ + +This example shows how the textsize and line_height property are used +to format label widget +''' + +import kivy +kivy.require('1.0.7') + +from kivy.app import App +from kivy.uix.label import Label + + +_long_text = """ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus odio nisi, pellentesque molestie adipiscing vitae, aliquam at tellus. Fusce quis est ornare erat pulvinar elementum ut sed felis. Donec vel neque mauris. In sit amet nunc sit amet diam dapibus lacinia. In sodales placerat mauris, ut euismod augue laoreet at. Integer in neque non odio fermentum volutpat nec nec nulla. Donec et risus non mi viverra posuere. Phasellus cursus augue purus, eget volutpat leo. Phasellus sed dui vitae ipsum mattis facilisis vehicula eu justo. + +Quisque neque dolor, egestas sed venenatis eget, porta id ipsum. Ut faucibus, massa vitae imperdiet rutrum, sem dolor rhoncus magna, non lacinia nulla risus non dui. Nulla sit amet risus orci. Nunc libero justo, interdum eu pulvinar vel, pulvinar et lectus. Phasellus sed luctus diam. Pellentesque non feugiat dolor. Cras at dolor velit, gravida congue velit. Aliquam erat volutpat. Nullam eu nunc dui, quis sagittis dolor. Ut nec dui eget odio pulvinar placerat. Pellentesque mi metus, tristique et placerat ac, pulvinar vel quam. Nam blandit magna a urna imperdiet molestie. Nullam ut nisi eget enim laoreet sodales sit amet a felis. +""" + + +class LabelTextSizeTest(App): + def build(self): + l = Label( + text=_long_text, + text_size = (600,None), + line_height=1.5 + ) + return l + +if __name__ == '__main__': + LabelTextSizeTest().run() From d4ef9c3e92a26789462b237a8852ca872f9ea54c Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Mon, 26 Nov 2012 18:59:03 +0100 Subject: [PATCH 3/5] add versionadded --- kivy/core/text/__init__.py | 3 +++ kivy/uix/label.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/kivy/core/text/__init__.py b/kivy/core/text/__init__.py index caa7c82a8..c63cd5f8a 100644 --- a/kivy/core/text/__init__.py +++ b/kivy/core/text/__init__.py @@ -5,6 +5,9 @@ Text Abstraction of text creation. Depending of the selected backend, the text rendering can be more or less accurate. +.. versionchanged:: 1.5.0 + :data:`LabelBase.line_height` added. + .. versionchanged:: 1.0.7 The :class:`LabelBase` don't generate any texture is the text have a width <= 1. diff --git a/kivy/uix/label.py b/kivy/uix/label.py index abbfba25a..ce1de6195 100644 --- a/kivy/uix/label.py +++ b/kivy/uix/label.py @@ -290,10 +290,10 @@ class Label(Widget): :data:`line_height` is a :class:`~kivy.properties.NumericProperty`, default to 1.0. + + .. versionadded:: 1.5.0 ''' - - bold = BooleanProperty(False) '''Indicates use of the bold version of your font. From a9db7f4014bbaeaacbf87daef5e4f372baab8f82 Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Mon, 26 Nov 2012 16:09:23 -0600 Subject: [PATCH 4/5] core.text: fix line_height of last line in label (dont add extra space below label on last line) --- kivy/core/text/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kivy/core/text/__init__.py b/kivy/core/text/__init__.py index c63cd5f8a..a97641374 100644 --- a/kivy/core/text/__init__.py +++ b/kivy/core/text/__init__.py @@ -331,6 +331,9 @@ class LabelBase(object): if not real: self._internal_height = sum([size[1] for size, glyphs in lines]) + ll_h = lines[-1][0][1] + lh_offset = ll_h - (ll_h / self.options['line_height']) + self._internal_height = self._internal_height - lh_offset h = self._internal_height if uh is None else uh w = uw else: From 5f6ee4f9cf282186e0feb9e6b3f17943556696d7 Mon Sep 17 00:00:00 2001 From: tshirtman Date: Tue, 27 Nov 2012 00:06:35 +0100 Subject: [PATCH 5/5] add **kwargs to super().__init__ call in App.__init__ --- kivy/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kivy/app.py b/kivy/app.py index b05ccd9a5..dda745360 100644 --- a/kivy/app.py +++ b/kivy/app.py @@ -286,7 +286,7 @@ class App(EventDispatcher): self._app_name = None self._app_settings = None self._app_window = None - super(App, self).__init__() + super(App, self).__init__(**kwargs) self.register_event_type('on_start') self.register_event_type('on_stop') self.register_event_type('on_pause')