It is currently September 24th, 2020, 5:30 am

String size in pixels

Help with creating, editing & fixing problems with skins
User avatar
Mirage
Posts: 19
Joined: September 11th, 2012, 3:36 am

String size in pixels

Post by Mirage »

I know this has been asked before, (I several posts on reddit), but I thought I'd come to the definitive source for a real answer. Is there a way to determine the length of a string in pixels?

I don't have the actual meters in fron tof me, but what I'm trying to accomplish is (what should be) a simple Month /day/suffix// year string, but when I try aligning them from the right I get the S in September cut in half, like I've run out of window space, and it makes no difference what I set the window size to. If I align to the Left, when the month changes to say July, I end up with a huge gap between the Month and the date. I know I'm doing something wrong, but I can't figure out what. But I know knowing pixel positions would really be a help.

Any ideas?
User avatar
SilverAzide
Posts: 955
Joined: March 23rd, 2015, 5:26 pm

Re: String size in pixels

Post by SilverAzide »

Do you really need to display these in different meters? Unless you want different vertical spacing, you can display all these in a single meter, using inline options if needed. That way this issue isn't a problem. If you DO need different meters, then DON'T set the meter width (let it default), then use relative positioning on the meters (not absolute positioning).

Code: Select all

[MeterMonth]
MeasureName=YourMonthNameMeasure
Text=%1
X=0
Y=0

[MeterDay]
MeasureName=YourDayMeasure
Text=%1
X=2R
Y=0

[MeterYear]
MeasureName=YourYearMeasure
Text=%1
X=2R
Y=0
This will place the day 2 pixels after the month, and the year 2 pixels after the day, with no need to know how wide anything is.

Otherwise, just put everything in a single meter, like so:

Code: Select all

[MeterMonth]
MeasureName=YourMonthNameMeasure
MeasureName=YourDayMeasure
MeasureName=YourYearMeasure
Text=%1 %2 %3
X=0
Y=0
Inline Options are very powerful, so if you need something fancy, try that instead of either of the above.

If you really really want to know the width of a string, you can create a hidden string meter, set the text to the value you want, and then get the width using syntax like [HiddenMeter:W], which gets you the width of the meter HiddenMeter. See Section Variables.
Gadgets Wiki GitHub More Gadgets...
User avatar
Mirage
Posts: 19
Joined: September 11th, 2012, 3:36 am

Re: String size in pixels

Post by Mirage »

SilverAzide, You're right, I'm making this much more complicated than I have to. But I don't set the width at all in this skin, and I almost certain the first example you showed is what I tried doing and i didn't work. I must be doing something wrong. Probably some simple syntax error. I'll check it out again. Thanks.
User avatar
balala
Rainmeter Sage
Posts: 11390
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: String size in pixels

Post by balala »

Mirage wrote: September 12th, 2020, 12:14 am But I know knowing pixel positions would really be a help.

Any ideas?
Yep, there is a solution.
If besides SilverAzide's reply you still want to know the width (size) of the meter, you can use the :W (and :H for height) parameter of the section variables. Since you didn't post a code, here is an example:

Code: Select all

[Rainmeter]
DynamicWindowSize=1

[MeasureTime]
Measure=Time
Format=%#c

[MeterTime]
Meter=String
MeasureName=MeasureTime
Fontsize=15
FontFace=Segoe UI
FontColor=220,220,220
AntiAlias=1 
StringAlign=Left
x=0
y=0
Text=%1
SolidColor=0,0,0,150
Padding=15,5,15,5

[MeterWidth]
Meter=String
MeasureName=MeasureTime
Fontsize=15
FontFace=Segoe UI
FontColor=220,220,220
AntiAlias=1 
StringAlign=Left
x=0r
y=5R
Text=[MeterTime:W]
DynamicVariables=1
SolidColor=0,0,0,150
Padding=15,5,15,5
See that [MeterWidth] shows the width of [MeterTime] meter, through the [MeterTime:W] section variable.
What you have to know here is that you absolutely need the DynamicVariables=1 option on all measures or meters where you do want to use the width returned by the [MeterTime:W] section variable (because section variables require to set on the dynamic variables).
User avatar
Yincognito
Posts: 2593
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: String size in pixels

Post by Yincognito »

SilverAzide wrote: September 12th, 2020, 12:46 am If you really really want to know the width of a string, you can create a hidden string meter, set the text to the value you want, and then get the width using syntax like [HiddenMeter:W], which gets you the width of the meter HiddenMeter. See Section Variables.
This. To avoid confusion though, "hidden" should be replaced by "invisible", since, strictly speaking, a hidden meter has its width and height set to 0, as per the manual.

Invisible meters are a great (and only, as far as I know) "automated" way of setting a subsequent meter's dimensions to be an exact fit of the largest possible value the latter could take, according to the font's characteristics.
Last edited by Yincognito on September 12th, 2020, 8:40 am, edited 1 time in total.
User avatar
Mirage
Posts: 19
Joined: September 11th, 2012, 3:36 am

Re: String size in pixels

Post by Mirage »

Balala / Yincognito, this may be the answer I was looking for, thanks! I'm going to play around and see what works best. Thanks again.
User avatar
Yincognito
Posts: 2593
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: String size in pixels

Post by Yincognito »

Mirage wrote: September 12th, 2020, 12:47 pm Balala / Yincognito, this may be the answer I was looking for, thanks! I'm going to play around and see what works best. Thanks again.
Yeah, I was on mobile at that time and couldn't post a basic sample code, so here it is (notice the Alpha value of 0 in [MeterMonthMaxW]'s FontColor option, which makes it invisible):

Code: Select all

[Variables]

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

[MeasureMonth]
Measure=String
String=May

---Meters---

[MeterMonthMaxW]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,0
FontSize=10
AntiAlias=1
Text="September"
UpdateDivider=-1

[MeterMonth]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,255
FontSize=10
AntiAlias=1
X=0r
Y=0r
MeasureName=MeasureMonth
Text="%1"
This is just for reference, you can adjust it afterwards. If you align [MeterMonth]'s content to the right, just set its:

Code: Select all

StringAlign=Right
X=0R
No absolute need to set W=[MeterMonthMaxW:W] in [MeterMonth] in this case, since the meters follow one after the other and X and Y are set using relative positioning, but you can obviously use it (along with DynamicVariables=1), in order to be consistent across your skin and have the width of the meters set to their maximum potential extent. You can use a similar approach for the meters' height, of course.

Also, assuming you don't know which of "December" and "September" strings is the longest in pixels (or you don't want to bother finding out), you can use something like this:

Code: Select all

[Variables]

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

[MeasureMonth]
Measure=String
String=May

---Meters---

[MeterMonthMaxH]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,0
FontSize=10
AntiAlias=1
Text="bp"

[MeterMonthMaxW]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,0
FontSize=10
AntiAlias=1
X=0r
Y=0r
H=[MeterMonthMaxH:H]
Text="September#CRLF#December"
DynamicVariables=1

[MeterMonth]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,255
FontSize=10
AntiAlias=1
X=0r
Y=0r
W=[MeterMonthMaxW:W]
H=[MeterMonthMaxH:H]
MeasureName=MeasureMonth
Text="%1"
DynamicVariables=1
where you find out the meter's maximum height first in [MeterMonthMaxH], then use this height to set the height of [MeterMonthMaxW] where you'll have both "September" and "December" separated by #CRLF# aka the new line character. The latter operation is made in order to keep the [MeterMonthMaxW] just one line tall, considering that now it's technically 2 lines tall. Then, in [MeterMonth], use both previous (invisible) meters to set the desired W and H.
User avatar
balala
Rainmeter Sage
Posts: 11390
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: String size in pixels

Post by balala »

Mirage wrote: September 12th, 2020, 12:47 pm Balala / Yincognito, this may be the answer I was looking for, thanks! I'm going to play around and see what works best.
Alright, I'm gladif you found the answer. If any other question arises, fell free to come back with it.