Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kivy Label markup for custom fonts

Kivy Labels have a great property markup that allows to customize the font properties like font size, boldness, etc. All of that works only when the font acutally supports it. But what should I do if I've got a font that doesn't?
So, maybe I'm not understanding how .ttf fonts work. I've got a few .ttf files for all the needed text types, but I have no idea how to put them together.
Googling on this case mostly led me to questions about CSS, where it is nicely customizable, which font to treat as bold, italics.

Some more info. This is what I'm talking about:

from kivy.base import runTouchApp
from kivy.uix.label import Label

runTouchApp(Label(font_name = 'my_font.ttf',
                  markup = True,
                  text = '[b]Bold[/b] and [i]italics[/i]'))

Running this code, I get the following output:
enter image description here

So there was no effect from those markup tags. However, text coloring tags do work, for example, so I'm pretty sure it's a font problem.

Is there a way to programmatically change, which font will be used to render bold text? And if not, what steps could I take to change the font? How to merge two fonts that only differ in text style?

This is the font I've used (Regular version, renamed to my_font.ttf)

like image 770
illright Avatar asked Jan 25 '26 05:01

illright


1 Answers

There's a [font=<font .ttf file>] tag in markup. Basically, everything that is here should be usable. I copied Arial and Times New Roman to my desktop where I had this piece of code and it successfully changed the font. In a similar way I'm sure you can even try to use [i] or [b] inside the [font] tag.

from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<Test>:
    Label:
        markup: True
        text: "[font=Arial]test[/font][font=times]test[/font]"
''')
class Test(BoxLayout): pass
runTouchApp(Test())

However I'm not sure how to merge fonts. I even found multiple Times New Roman or Arial files e.g. one for normal, one for bold, etc and the only difference was with a suffix - arial.ttf, ariali.ttf, arialb.ttf, arialbi.ttf, times.ttf, timesi.ttf, etc. So, try to make it the similar way and maybe it'll just recognize the italic/bold part via the suffix.

like image 153
Peter Badida Avatar answered Jan 26 '26 17:01

Peter Badida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!