Your Digital Media Has Never Looked So Good

 
shess
Topic Author
Posts: 9
Joined: Sat Sep 09, 2017 9:55 pm

Get Font from Label in BrightScript?

Mon Oct 09, 2017 9:50 pm

In XML, if I define a label to use a custom font:

<Label>
  id="label"
  <Font role = "font" uri = "pkg:/fonts/font.ttf" size = "24" />
</Label>


In BrightScript, how do I get to the actual ifFont object so that I can make calls like GetOneLineWidth?
 
User avatar
squirreltown
Posts: 752
Joined: Sun Apr 21, 2013 2:20 pm

Re: Get Font from Label in BrightScript?

Tue Oct 10, 2017 7:20 am

https://sdkdocs.roku.com/display/sdkdoc/roFont
for custom:
fontreg = createobject("rofontregistry")
fontreg.Register("pkg:/images/fonts/customfont.tff")
Kinetics Screensaver (kineticsscreensaver), Kinetics³ Screensaver(kinetics3), Kinetics Painter Screensaver (kineticspainter), Kinetics Splash Screensaver (kineticssplash)
 
shess
Topic Author
Posts: 9
Joined: Sat Sep 09, 2017 9:55 pm

Re: Get Font from Label in BrightScript?

Tue Oct 10, 2017 9:24 pm

So - I can't query the node for the font - I have to look in the XML and copy out the font name and the size?  I guess that works, but means every time the XML changes, you have to find any reference in code and update it, too.   :(
 
rymawby
Posts: 16
Joined: Fri Jan 16, 2015 9:03 am
Contact:

Re: Get Font from Label in BrightScript?

Wed Oct 11, 2017 2:21 am

You could actually get the values from the font object using brightscript and then use these values in the getFont call.

So if you had your label like:

<Label
  id="myLabel"
  text="Awesome"
  translation="[300,300]" >
  <Font role = "font" uri = "pkg:/components/fonts/Helvetica.otf" size = "24" />
</Label>


You could get the values of this font by doing something like:

myLabel = m.top.findNode("myLabel")
myFont = myLabel.font
fontURI = myFont.uri
fontSize = myFont.size


You could then use fontURI and fontSize in your roFontRegistry getFont calls.

Hope this helps.
---
Blogging Roku stuff at http://rymawby.com
---
 
joetesta
Posts: 600
Joined: Wed Apr 20, 2011 11:48 am

Re: Get Font from Label in BrightScript?

Wed Oct 11, 2017 10:26 am

Be warned:
A) getOneLineWidth works only in task node
B) Due to firmware upscaling on lower devices such as Roku 3 you will have incorrect values for dimensions not divisible by 3

If you are able to use boundingRect() you will have much easier time and better success.
hope it helps,
Joe
aspiring
 
shess
Topic Author
Posts: 9
Joined: Sat Sep 09, 2017 9:55 pm

Re: Get Font from Label in BrightScript?

Tue Oct 24, 2017 3:59 pm

Please pardon my slow reply, I got pulled away for a while.

Thank you both for your replies.

At the end of the day, what I'm trying to do is to work around not being able to set the width for a RowList row label.  I have a RowList that is fairly narrow, but might have a long row title.  That label doesn't seem to pay attention to ItemSize.  The only way I can think to work around it is to manually truncate the string if it is going to be too long for that RowList row label - unless there is a better way?

Thanks!
 
joetesta
Posts: 600
Joined: Wed Apr 20, 2011 11:48 am

Re: Get Font from Label in BrightScript?

Tue Oct 24, 2017 5:44 pm

I have a few ideas but not sure what would actually work or best suit your needs.
1) Probably easiest to code is a brute-forcish method, check the label's boundingRect().width and compare to the rowlist width.  If it's wider, peel letters off the end until it's not wider.
2) If you'd prefer to have the letters actually chopped in half at the boundary, you could wrap the label in a group with clippingRect 3rd array value equal to the rowlist width.
3) You could also probably update the label fields "ellipsisText" to get rid of the dots and set the width equal to the rowlist, then it won't have a chopped off character and would be slightly less "hack-y" than the first option

hope this helps,
Joe
aspiring
 
shess
Topic Author
Posts: 9
Joined: Sat Sep 09, 2017 9:55 pm

Re: Get Font from Label in BrightScript?

Wed Oct 25, 2017 11:25 pm

Hi Joe,

Do we have access to the RowList row label directly?
 
joetesta
Posts: 600
Joined: Wed Apr 20, 2011 11:48 am

Re: Get Font from Label in BrightScript?

Thu Oct 26, 2017 8:16 pm

shess wrote:
Hi Joe,
Do we have access to the RowList row label directly?


looks like yes - rowlist has a field "rowTitleComponentName" that you can use to specify a custom component that is used for each row's label.
In that custom component you can add the code to do the trimming.
aspiring
 
shess
Topic Author
Posts: 9
Joined: Sat Sep 09, 2017 9:55 pm

Re: Get Font from Label in BrightScript?

Fri Oct 27, 2017 1:05 pm

Hey Joe,

Huh - that seems like an easy fix - I like it!  Is there any way that could be dynamic?  If I create a custom node that inherits from label and set it as that rowlist label component, I'm going to have to hardcode that width into that component, yes?

Thanks, I appreciate your time!
 
joetesta
Posts: 600
Joined: Wed Apr 20, 2011 11:48 am

Re: Get Font from Label in BrightScript?

Fri Oct 27, 2017 1:31 pm

easy? easy for you to say :)
for sure there's more than one way to make it dynamic but probably the way to go is add a field to the custom component (that as you said, extends label) add a new field like
<field id="parentWidth" type="float" onChange="trimLabel" alwaysNotify="true"/>


then in your custom component's brs code you have the function "trimLabel" reference m.top.parentWidth to know what its desired target width is.  make sense?
oh yeah, you'll still need some way to set this field from the parent...  Will be a little tricky but believe it can be done via content, add a parentWidth field to the contentNode.... or maybe there's an easier way to reference those items... the custom component itself might calculate the width (use boundingRect().width) given the content?
aspiring

Who is online

Users browsing this forum: No registered users and 3 guests