Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
renojim
Level 21

Font metrics of built-in fonts

All the examples I've seen using the built-in fonts "Large", "Small", etc. use hard-coded values for TargetRect to display the text. Is there any way to get the size of an arbitrary string?

-JT
0 Kudos
6 Replies
RokuKevin
Level 9

Re: Font metrics of built-in fonts

The component reference guide has a usage example in Section 5.3. The idea is that the fontMetrics size() method returns the target rect size for a passed in string rendered in the specified font.

--Kevin

Here's the sample code:

helloString = "Hello ImageCanvas"
fontReg = CreateObject("roFontRegistry")
fontReg.Register("pkg:/fonts/LCDMono.ttf")
font = fontReg.Get("LCDMono",36,50,false) ' 36pt, 50 is normal
' weight, no italics
fontMetrics = CreateObject("roFontMetrics", font)
stringSize = fontMetrics.size(helloString)
canvasItem = { Text:helloString
TextAttrs:{Color:"#FFCCCCCC", Font:font,
HAlign:"HCenter",
VAlign:"VCenter", Direction:"LeftToRight"}
TargetRect:{x:390,y:357,
w:stringSize.w,h:stringSize.h}
}
canvas = CreateObject("roImageCanvas")
port = CreateObject("roMessagePort")
canvas.SetMessagePort(m.port)
'Set opaque background
canvas.SetLayer(0, {Color:"#FF000000", CompositionMode:"Source"})
canvas.SetRequireAllImagesToDraw(true)
canvas.SetLayer(1, canvasItem)
canvas.Show()
0 Kudos
renojim
Level 21

Re: Font metrics of built-in fonts

Right. I know how to do it for my own fonts, but I'm talking about the built-in fonts: "Large", "Medium", "Small", etc. What would I pass to the roFontRegistry Register() function? The example uses a font that's part of the package, not a built-in font, like "Medium", that's used elsewhere in the examples.

-JT
0 Kudos
RokuKevin
Level 9

Re: Font metrics of built-in fonts

fontMetrics only works with fonts that are exposed through the font registry and only on the roImageCanvas screen. It does not work with the built in system fonts or on any other screen.

--Kevin
0 Kudos
renojim
Level 21

Re: Font metrics of built-in fonts

Thanks. That's too bad. It would be nice to be able to get the size of an arbitrary string from a built-in font so it can be positioned on an roImageCanvas. Otherwise, I guess the developer has to always use his own fonts or "guess" at the size of the TargetRect required.

-JT
0 Kudos
jlfreund
Level 7

Re: Font metrics of built-in fonts

"renojim" wrote:
Thanks. That's too bad. It would be nice to be able to get the size of an arbitrary string from a built-in font so it can be positioned on an roImageCanvas. Otherwise, I guess the developer has to always use his own fonts or "guess" at the size of the TargetRect required.

-JT


I was also trying to get the TargetRect of a string drawn using the default font into an roImageCanvas, but not having any luck.

    
fontReg = CreateObject("roFontRegistry")
font=fontReg.GetDefaultFont()
REM "font" does not contain any keys, and the next line crashes with Type mismatch
fontMetrics = CreateObject("roFontMetrics", font)


I guess GetDefaultFont() only refers to some app-installed default font, but cannot get the Roku default system font?

Jason
0 Kudos
renojim
Level 21

Re: Font metrics of built-in fonts

As far as I know, roFontRegistry only works when using roScreen. You're trying to mix roScreen fonts with roImageCanvas fonts. The call to CreateObject("roFontMetrics", font) takes a String for the font, not a font created from roFontRegistry. If you want to use built-in fonts and calculate the size on the screen of an arbitrary string you'll have to switch to roScreen. By the way, the built-in, or default, font is a different font from roImageCanvas to roScreen (take a look at the "J" that you get on an roScreen).

-JT
0 Kudos