Forum Discussion

lisakb140's avatar
lisakb140
Visitor
7 years ago

Using custom fonts (.ttf files) in XML

I am trying to use 2 fonts (I have their ttf files saved in a folder within my project called "fonts"), and am having trouble incorporating them properly in my nodes in my xml files. 

I've tried including this in my nodes:


font="pkg:/fonts/font:Muli-ExtraLight.ttf"


but that doesn't work.

I've read that fonts need to be included in the fonts directory in a package file, following this example: https://sdkdocs.roku.com/display/sdkdoc/Font

Is there something I am doing wrong??

UPDATE:

I've also tried putting this directly in one of my .brs files

[b]font[/b]  = [i]CreateObject[/i]("roSGNode", "Font")
    font.uri = "pkg:/fonts/font:Muli-ExtraLight.ttf"

but I get an Error message saying this is an invalid path!
My fonts directory is top-level, as in it's the same level as my components folder. I don't understand why this path would be invalid!!

9 Replies

  • font  = CreateObject("roSGNode", "Font")
    font.uri = "pkg:/images/optima.otf"
    font.size = 42
    font.color="0xFFFFFFFF"

    I don't use the xml but this works fine in the  init() function in my .brs file.
    • random_user's avatar
      random_user
      Newbie

      you wouldnt believe that using the font file in the images instead of the fonts directory would be the solution for me, isnt the folder of fonts being included to the bundle ? where is that even defined ?

      • renojim's avatar
        renojim
        Community Streaming Expert

        random_user, you can put the font file anywhere you want - you just have to use the proper path in your code/XML.  It sounds like however you're creating your zip file it isn't pulling in the fonts directory.

  • RokuBen's avatar
    RokuBen
    Community Moderator
    Using a colon in the filename might be a problem and prevent the system from opening the file.  There are filename checks in the Roku OS to filter out things that affect FAT filesystems, and colon is a reserved character there.
  • Thanks-- I think I got this working actually-- turns out my node needed to be:


                        <Label
    id="Title"
    translation="[60, 135]"
    color="0xf5f5f5"
    font="font:Muli-ExtraLight">
    <Font role="font"uri = "pkg:/fonts/Muli-ExtraLight.ttf" size="24"/>
                          </Label>

     Which was a little different.

    Thank you for double checking the .brs code-- I have a question, though..I just put a secondary font in my XML file (in a node), just for a section with a different font treatment. However, I didn't put that font in my brs file, and it still works. If that's so, then what is the point of putting it in the brs file? Is that necessary??
    • oa24153's avatar
      oa24153
      Binge Watcher

      It gives me error on role?

      cvc-complex-type.3.2.2: Attribute 'role' is not allowed to appear in element 'Font'.

  • "lisakb140" wrote:
    However, I didn't put that font in my brs file, and it still works. If that's so, then what is the point of putting it in the brs file? Is that necessary??

    If you're asking me,  It only needs to be in one place. I don't use the xml file, so for me everything is in the .brs file.
  • Have you considered something like this?


          label = CreateObject("roSGNode", "Label")
          font  = CreateObject("roSGNode", "Font")
          font.uri = "pkg:/fonts/yourFont.ttf"
          font.size = 20
          label.font = font

    Now all your label fonts will use your font. Assuming you want to reuse your font for the labels and not duplicate font codes.