Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add font rendering options #3896

Merged
merged 6 commits into from Jan 25, 2016
Merged

add font rendering options #3896

merged 6 commits into from Jan 25, 2016

Conversation

ghost
Copy link

@ghost ghost commented Jan 7, 2016

On low resolution LCD, with the right font type and the hinting method, there is a better looking result if antialiasing is disabled. With retina display, this is not an issue, but not all kivy users have retina.

Kivy is hardwired to enable antialiasing because it always calls TTF_RenderUTF8_Blended.
With this change Kivy could optionally call the TTF_RenderUTF8_Solid (which does not use antialiasing).

Beside that the change allows setting the hinting level.

Additional info:
http://www.sdltutorials.com/sdl-ttf

Further thinking: Maybe kivy should automatically detect if display is low res and font type is such, and switch between antialising enabled/disabled automatically? For simplicity it is better just leave control to the programmer.

@dessant
Copy link
Contributor

dessant commented Jan 15, 2016

Nice features. 👍
I have a minor reservation about the hinting api though, could you make it an OptionProperty?
https://kivy.org/docs/api-kivy.properties.html#kivy.properties.OptionProperty

The options could be: None, 'mono', 'light', 'normal'.

The new api endpoints will also need a bit of docs, please follow this as a template:
https://github.com/udiboy1209/kivy/blob/styling/kivy/uix/label.py#L641.

Also add a note to them about sdl2, like this:

.

@ghost
Copy link
Author

ghost commented Jan 15, 2016

Shortly I will correct the issues, you have noticed.
I am glad, not only I think; this feature would be nice to add.

@dessant
Copy link
Contributor

dessant commented Jan 17, 2016

Thanks a lot for your work. Yes, absolutely, we intend to merge it. 😉

@matham
Copy link
Member

matham commented Jan 23, 2016

Since you're still making fixes, it may be nice if you explain in the docs what kerning, hinting and blending do. Since I for one don't know what hinting or blending does. It should just be a short one or two sentences.


.. note::
This feature requires a SDL2 window provider and is currently only
supported on desktop platforms.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SDL2 is now being supported everywhere even on mobiles.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..versionadded::1.9.2

@ghost
Copy link
Author

ghost commented Jan 25, 2016

Demo here:

from kivy.app import App
from kivy.uix.button import Button
from kivy import __version__ as kivy_version
from kivy.uix.boxlayout import BoxLayout
import os


#font_name = "/usr/share/fonts/truetype/msttcorefonts/arial.ttf"
# font_name = "/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf"
# font_name = "/usr/share/fonts/truetype/roboto/Roboto-Regular.ttf"
font_name = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"
fname = os.path.basename(font_name)
fsize = '13pt'


class TestApp(App):
    def build(self):
        layout = BoxLayout(orientation='vertical')
        layout.add_widget(
            Button(
                text="kivy_version: {}".format(kivy_version),
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='normal',
                font_blended=False,
                )
            )
        layout.add_widget(
            Button(
                text="Kerning On: AV",
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='normal',
                font_kerning=True,
                font_blended=False,
                )
            )
        layout.add_widget(
            Button(
                text="Kerning Off: AV",
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='normal',
                font_kerning=False,
                font_blended=False,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' solid none',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='none',
                font_blended=False,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' solid light',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='light',
                font_blended=False,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' solid normal',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='normal',
                font_blended=False,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' solid mono',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='mono',
                font_blended=False,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' blended none',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='none',
                font_blended=True,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' blended light',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='light',
                font_blended=True,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' blended normal',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='normal',
                font_blended=True,
                )
            )
        layout.add_widget(
            Button(
                text=fname + ' blended mono',
                background_normal='',
                color=[0, 0, 0, 1],
                font_name=font_name,
                font_size=fsize,
                font_hinting='mono',
                font_blended=True,
                )
            )
        return layout


def _main():
    TestApp().run()

if __name__ == '__main__':
    _main()

It would be nice to add this demo somehow to kivy examples. Any suggestions?

@dessant
Copy link
Contributor

dessant commented Jan 25, 2016

@Ali65, a label example with the new options could be added here, in a separate pr: https://github.com/ali65/kivy/tree/master/examples/widgets.

akshayaurora added a commit that referenced this pull request Jan 25, 2016
add font rendering options
@akshayaurora akshayaurora merged commit 9d07f45 into kivy:master Jan 25, 2016
@akshayaurora
Copy link
Member

@Ali65 thanks nice work !

@ghost
Copy link
Author

ghost commented Jan 25, 2016

Soon I will follow up with the merge request of the cleaned-up example of using the new font rendering options. Thanks for merging.

@dessant
Copy link
Contributor

dessant commented Jan 26, 2016

@akshayaurora 💯

11

from kivy.app import App
from kivy.lang import Builder

kv = """
Label:
    text: 'OFFICIALLY TESTED'
    font_size: '20dp'
"""


class TestApp(App):
    def build(self):
        return Builder.load_string(kv)

if __name__ == '__main__':
    TestApp().run()

@dessant
Copy link
Contributor

dessant commented Jan 26, 2016

This might also need to be sorted out:

matham: if TTF_STYLE_XXX is defined in the external headers, we should just do cdef int TTF_STYLE_XXX not cdef int TTF_STYLE_XXX = ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants