It is currently October 21st, 2021, 2:41 pm

Getting width of text strings in pixels

Help with creating, editing & fixing problems with skins
FlightlessAz
Posts: 23
Joined: September 14th, 2021, 10:27 am

Getting width of text strings in pixels

Post by FlightlessAz »

Hello beautiful people of the rainmeter community! Pardon me if my post format is wrong, I have never tried to post in a forum before, let alone be in any other forums in the past. If you haven't figured out, I am new to the community, though I started using rainmeter approximately 2 weeks ago. :welcome:

I'd like to know if there is any way to measure the width of a string in pixels? I'm using a font that is almost monospace, except for the spaces. Also I'll use this in the future if I ever decide to use a non-monospace font. If it isn't possible, then is there any way to at least find out what and how many characters a string contains? If there's an existing thread discussing my concern, then I'd appreciate it if you would link me to the said thread. Thanks in advance, and have a good day! :rosegift:
User avatar
jsmorley
Developer
Posts: 22372
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting width of text strings in pixels

Post by jsmorley »

FlightlessAz wrote: September 14th, 2021, 11:12 am Hello beautiful people of the rainmeter community! Pardon me if my post format is wrong, I have never tried to post in a forum before, let alone be in any other forums in the past. If you haven't figured out, I am new to the community, though I started using rainmeter approximately 2 weeks ago. :welcome:

I'd like to know if there is any way to measure the width of a string in pixels? I'm using a font that is almost monospace, except for the spaces. Also I'll use this in the future if I ever decide to use a non-monospace font. If it isn't possible, then is there any way to at least find out what and how many characters a string contains? If there's an existing thread discussing my concern, then I'd appreciate it if you would link me to the said thread. Thanks in advance, and have a good day! :rosegift:
https://docs.rainmeter.net/manual/variables/section-variables/#WH

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeterString]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Hello World

[MeterSize]
Meter=String
Y=3R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=[MeterString:W]
DynamicVariables=1

1.png


That section variable can be used pretty much anywhere that supports section variables. So it might be in a (formula), in a Calc measure, a String Measure, whatever. Getting the number of "characters" is actually more complex, and while Lua can do it pretty easily for ASCII characters, it's going to be difficult if the string contains Unicode characters that are more than one byte per character. There might be a Substitute that could do it.
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22372
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting width of text strings in pixels

Post by jsmorley »

So like this:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeterString]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Hello World

[MeterSize]
Meter=String
Y=3R
W=[MeterString:W]
X=([MeterString:W]/2)
StringAlign=Center
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=0,5,0,5
AntiAlias=1
Text=[MeterString:W]
DynamicVariables=1

2.png
You do not have the required permissions to view the files attached to this post.
FlightlessAz
Posts: 23
Joined: September 14th, 2021, 10:27 am

Re: Getting width of text strings in pixels

Post by FlightlessAz »

Thank you very much! Although, I have another question. What does R mean in "Y=3R"?
User avatar
jsmorley
Developer
Posts: 22372
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting width of text strings in pixels

Post by jsmorley »

FlightlessAz wrote: September 14th, 2021, 11:31 am Thank you very much! Although, I have another question. What does R mean in "Y=3R"?
It means "relative" to the meter immediately preceding it.

https://docs.rainmeter.net/manual/meters/general-options/#XY

So in this case, it means "set the Y of this meter 3 pixels below the bottom of the preceding meter".
FlightlessAz
Posts: 23
Joined: September 14th, 2021, 10:27 am

Re: Getting width of text strings in pixels

Post by FlightlessAz »

jsmorley wrote: September 14th, 2021, 11:33 am It means "relative" to the meter immediately preceding it.

https://docs.rainmeter.net/manual/meters/general-options/#XY

So in this case, it means "set the Y of this meter 3 pixels below the bottom of the preceding meter".
Ahh, I see. I've been trying to figure this out for days on my own. Again, thank you very much for the help!
User avatar
jsmorley
Developer
Posts: 22372
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting width of text strings in pixels

Post by jsmorley »

FlightlessAz wrote: September 14th, 2021, 11:35 am Ahh, I see. I've been trying to figure this out for days on my own. Again, thank you very much for the help!
You are quite welcome.
User avatar
sl23
Posts: 889
Joined: February 17th, 2011, 7:45 pm
Location: a Galaxy S7 far far away

Re: Getting width of text strings in pixels

Post by sl23 »

Wow never knew you could do that! Handy thing to know :great:
FlightlessAz
Posts: 23
Joined: September 14th, 2021, 10:27 am

Re: Getting width of text strings in pixels

Post by FlightlessAz »

Hey, so I tried using the function, but it doesn't seem to work.... Here is the code, It's quite long, but I'll point out the part where it doesn't work

Code: Select all

[Rainmeter]
Update=#Update#
;BackgroundMode=2
AccurateText=1
SolidColor=200, 200, 200, 50
DynamicWindowSize=1
SkinWidth=673

[Variables]
Update=15
;673
OriginalX=673
TotalMove=2272
Y1=60
Subject=General Mathematics

[MeterLength]
;DynamicVariables=1
FontSize=75
FontFace=KH Dot Hibiya 32
FontWeight=900
FontColor=0,0,255,255
Meter=String
Text=General Mathematics

[MeasureMove]
;DynamicVariables=1
Measure=Calc
Formula=#OriginalX#-((((Counter)*1.5))%#TotalMove#)
AverageSize=1

[MeasureMove2]
Measure=Calc
Formula=#OriginalX#-((((((Counter)*1.5))%#TotalMove#)+[MeterLength:W])%#TotalMove#)

[Motor]
Measure=Calc
Formula=(Counter%2)
;IfCondition=(Motor < 2)
;IfTrueAction=!Execute [!MoveMeter [MeasureMove] #Y1# ScrollingText]
IfBelowValue=1
IfBelowAction=!Execute [!MoveMeter [MeasureMove] #Y1# ScrollingText]
IfEqualValue=1
IfEqualAction=!Execute [!MoveMeter [MeasureMove] #Y1# ScrollingText]
DynamicVariables=1

[Motor2]
Measure=Calc
Formula=(Counter%2)
IfBelowValue=1
IfBelowAction=!Execute [!RainmeterMoveMeter [MeasureMove2] #Y1# ScrollingText2]
IfEqualValue=1
IfEqualAction=!Execute [!RainmeterMoveMeter [MeasureMove2] #Y1# ScrollingText2]
DynamicVariables=1

[MeterBackground]
Meter=Image
X=0
Y=0
W=673
H=120
SolidColor=80,80,80,0

[ScrollingText]
Meter=String
X=0
Y=60
FontColor=0,255,0,255
FontFace=KH Dot Hibiya 32
FontWeight=900
Fontsize=75
StringAlign=LeftCenter
Text=#Subject#

[ScrollingText2]
DynamicVariables=1
Meter=String
X=0
Y=60
FontColor=255,0,0,200
FontFace=KH Dot Hibiya 32
FontWeight=900
Fontsize=75
StringAlign=LeftCenter
Text=#Subject#

;[MeterBackground]
Meter=Image
X=0
Y=0
W=673
H=120
SolidColor=80,80,80,0

[MeterString]
DynamicVariables=1
Meter=String
Text=#Leength#
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1


;[MeterSize]
;Meter=String
;Y=50
;FontSize=11
;FontWeight=400
;FontColor=255,255,255,255
;SolidColor=47,47,47,255
;Padding=5,5,5,5
;AntiAlias=1
;Text=[MeterLength:W]
;DynamicVariables=1

[MeasureMove6]
Measure=Calc
Formula=[MeasureMove:W]
I know the code is kinda long, and kinda messy, but the code is still a WIP, so please bear with me.
This line in [MeasureMove2] doesn't seem to be working: Formula=#OriginalX#-((((((Counter)*1.5))%#TotalMove#)+[MeterLength:W])%#TotalMove#)

The weird thing though is that it works in [MeterString], it displays the width value properly, here is an image;
https://prnt.sc/1s618k1

I don't know why it doesn't work. Help is greatly appreciated!
User avatar
jsmorley
Developer
Posts: 22372
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting width of text strings in pixels

Post by jsmorley »

FlightlessAz wrote: September 14th, 2021, 1:03 pm Hey, so I tried using the function, but it doesn't seem to work.... Here is the code, It's quite long, but I'll point out the part where it doesn't work

Code: Select all

[Rainmeter]
Update=#Update#
;BackgroundMode=2
AccurateText=1
SolidColor=200, 200, 200, 50
DynamicWindowSize=1
SkinWidth=673

[Variables]
Update=15
;673
OriginalX=673
TotalMove=2272
Y1=60
Subject=General Mathematics

[MeterLength]
;DynamicVariables=1
FontSize=75
FontFace=KH Dot Hibiya 32
FontWeight=900
FontColor=0,0,255,255
Meter=String
Text=General Mathematics

[MeasureMove]
;DynamicVariables=1
Measure=Calc
Formula=#OriginalX#-((((Counter)*1.5))%#TotalMove#)
AverageSize=1

[MeasureMove2]
Measure=Calc
Formula=#OriginalX#-((((((Counter)*1.5))%#TotalMove#)+[MeterLength:W])%#TotalMove#)

[Motor]
Measure=Calc
Formula=(Counter%2)
;IfCondition=(Motor < 2)
;IfTrueAction=!Execute [!MoveMeter [MeasureMove] #Y1# ScrollingText]
IfBelowValue=1
IfBelowAction=!Execute [!MoveMeter [MeasureMove] #Y1# ScrollingText]
IfEqualValue=1
IfEqualAction=!Execute [!MoveMeter [MeasureMove] #Y1# ScrollingText]
DynamicVariables=1

[Motor2]
Measure=Calc
Formula=(Counter%2)
IfBelowValue=1
IfBelowAction=!Execute [!RainmeterMoveMeter [MeasureMove2] #Y1# ScrollingText2]
IfEqualValue=1
IfEqualAction=!Execute [!RainmeterMoveMeter [MeasureMove2] #Y1# ScrollingText2]
DynamicVariables=1

[MeterBackground]
Meter=Image
X=0
Y=0
W=673
H=120
SolidColor=80,80,80,0

[ScrollingText]
Meter=String
X=0
Y=60
FontColor=0,255,0,255
FontFace=KH Dot Hibiya 32
FontWeight=900
Fontsize=75
StringAlign=LeftCenter
Text=#Subject#

[ScrollingText2]
DynamicVariables=1
Meter=String
X=0
Y=60
FontColor=255,0,0,200
FontFace=KH Dot Hibiya 32
FontWeight=900
Fontsize=75
StringAlign=LeftCenter
Text=#Subject#

;[MeterBackground]
Meter=Image
X=0
Y=0
W=673
H=120
SolidColor=80,80,80,0

[MeterString]
DynamicVariables=1
Meter=String
Text=#Leength#
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1


;[MeterSize]
;Meter=String
;Y=50
;FontSize=11
;FontWeight=400
;FontColor=255,255,255,255
;SolidColor=47,47,47,255
;Padding=5,5,5,5
;AntiAlias=1
;Text=[MeterLength:W]
;DynamicVariables=1

[MeasureMove6]
Measure=Calc
Formula=[MeasureMove:W]
I know the code is kinda long, and kinda messy, but the code is still a WIP, so please bear with me.
This line in [MeasureMove2] doesn't seem to be working: Formula=#OriginalX#-((((((Counter)*1.5))%#TotalMove#)+[MeterLength:W])%#TotalMove#)

The weird thing though is that it works in [MeterString], it displays the width value properly, here is an image;
https://prnt.sc/1s618k1

I don't know why it doesn't work. Help is greatly appreciated!
You need DynamicVariables=1 anywhere you use a [SectionVariable], including [MeasureMove2]