2

Just a quick question that might be obvious (sorry in advance!), does Kivy have support for Light and Condensed fonts?

I'm aware that Kivy has markup for things such as italics and bold, however is there something that allows you to render light or condensed fonts in KvLang? I haven't found anything online showing you how to use font family or font context (if these are even relevant) to do this or any alternative methods.

For Example, I'm currently wanting to use Bahnschrift Light SemiCondensed as a font - its part of the font family of Bahnschrift but I have no idea how to access the Light SemiCondensed version specifically as it is in the Bahnschrift.ttf file and throws an error if I try to put Bahnschrift Light SemiCondensed in the place of standard Bahnschrift(Ive tried spaces,dashes,no dashes, underscores etc but I'm probably doing this wrong since the semicondensed version isn't a standalone ttf file).

Inside the Bahnschrift ttf file there is all the different font weights/types but I've got no clue how to address one specifically.

(A quick note that I do have the font installed, and Kivy does recognize and render the regular version of Bahnschrift)

If there's any methods on how to do this I'd be really grateful to know how to do it!

test.py

from kivy.app import App
from kivy.uix.widget import Widget 
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout

class Testkv(FloatLayout):
    pass

class testApp(App):
    def build(self):
        return Testkv()

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

test.kv

#:kivy 2.1.0

<Testkv>:
    Label:
        markup: True
        text:'[font=Bahnschrift]hello[/font]' #Do I add something to the markup here or is there something completely different?
        font_size: 44

Chene
  • 21
  • 4
  • the answers may depend on the platform you are building on and what you are targeting. is this just for Windows ? Linux? etc. – Mark Dec 01 '22 at 13:53
  • @Mark - Hi, I'm using Windows so the directories would be correct to what you've shown - I've clarified my problem a bit more in the reply below! – Chene Dec 01 '22 at 19:12

1 Answers1

0

I have used different fonts in Kivy by copying them into the kivy install directories.

<python or virtual environment directory>\Lib\site-packages\kivy\data\fonts

in that location there should be some standard fonts and I have also copied new ones to this location.

You can reference the fonts by using the Kivy config object in the program or else change them in config.ini

this location varies by platform by on my Windows but it could be here:

C:\Users\<your_username>\.kivy\config.ini

and there is a section there for default fonts. my entry just as an example is this:

default_font = ['Microsoft YaHei', 'data/fonts/msyh.ttc', 'data/fonts/msyhl.ttc', 'data/fonts/msyhbd.ttc']

Finally, one can change the location of the kivy configuration files using the environment variable KIVY_HOME. I do this in my projects so that I can have the configuration files located in my repository and under git control.

Mark
  • 532
  • 1
  • 4
  • 9
  • Hi - Just to clarify I've been able to get different fonts in using similar methods (ie using labelbase and pointing to a specific directory like yourself - I've got a problem with this specific font - Fonts such as Roboto have separate ttf files for each font weight (ie Roboto-Light.ttf and Roboto-Regular.ttf that I can call on and import separately) however for Bahnschrift specifically, it only has Bahnschrift.ttf, and all the font weights such as the light version aren't separate .ttf files and are all inside - Do you have any idea how to call these specific fonts inside the one ttf file? – Chene Dec 01 '22 at 19:11
  • unfortunately, I don't know how to do that. I know that you can set markup: True on a widget and this gives you access to Text Markup and you can do inline formatting of various kinds. I tried some of these and failed but I didn't try exhaustively. – Mark Dec 01 '22 at 20:08
  • the Text Markup documentation shows additional options such as font_context, font_family, font_features, sub,sup and so on. but I could not find a way to trigger the special types within that file. – Mark Dec 01 '22 at 20:29
  • 1
    Thank you for your help regardless - I'll update the post if I find out! – Chene Dec 01 '22 at 20:45