Why is GDIP removing leading zeros from text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
The_One_True_Rick
Posts: 65
Joined: 25 May 2023, 17:14

Why is GDIP removing leading zeros from text

Post by The_One_True_Rick » 18 Jan 2024, 16:50

when using text to graphics to display a number it is removing leading zeros. I really need those zeros to be there. Does anyone know why? It does not appear to be doing it with dates in 01/11/2022 format so that is also strange. Just happens with numbers like 00000001234. The number of zeros does not appear to matter. I included the Text to graphics functions i'm using in this version of GDIP. I only know enough about GDIP to use it. Have no idea how these functions actually work.

I am using " Center vCenter Bold NoWrap" for the options. They seem to work fine but I'm including just in case it matters.

Side note: if it matters I actually do want most of the zeros removed just not the last one. In the end I would like to end up with 000001234 looking like 01234 but I can handle that part on my own if I can just get it to leave 01234 how it is instead of converting it to 1234.

Code: Select all

Gdip_TextToGraphics( pGraphics, Text, Options, Font="Arial", Width="", Height="", Measure=0 ){
    IWidth := Width, IHeight:= Height

    RegExMatch(Options, "i)X([\-\d\.]+)(p*)", xpos)
    RegExMatch(Options, "i)Y([\-\d\.]+)(p*)", ypos)
    RegExMatch(Options, "i)W([\-\d\.]+)(p*)", Width)
    RegExMatch(Options, "i)H([\-\d\.]+)(p*)", Height)
    RegExMatch(Options, "i)C(?!(entre|enter))([a-f\d]+)", Colour)
    RegExMatch(Options, "i)Top|Up|Bottom|Down|vCentre|vCenter", vPos)
    RegExMatch(Options, "i)NoWrap", NoWrap)
    RegExMatch(Options, "i)R(\d)", Rendering)
    RegExMatch(Options, "i)S(\d+)(p*)", Size)

    if !Gdip_DeleteBrush(Gdip_CloneBrush(Colour2))
        PassBrush := 1, pBrush := Colour2

    if !(IWidth && IHeight) && (xpos2 || ypos2 || Width2 || Height2 || Size2)
        return -1

    Style := 0, Styles := "Regular|Bold|Italic|BoldItalic|Underline|Strikeout"
    Loop, Parse, Styles, |
    {
        if RegExMatch(Options, "\b" A_loopField)
        Style |= (A_LoopField != "StrikeOut") ? (A_Index-1) : 8
    }

    Align := 0, Alignments := "Near|Left|Centre|Center|Far|Right"
    Loop, Parse, Alignments, |
    {
        if RegExMatch(Options, "\b" A_loopField)
            Align |= A_Index//2.1      ; 0|0|1|1|2|2
    }

    xpos := (xpos1 != "") ? xpos2 ? IWidth*(xpos1/100) : xpos1 : 0
    ypos := (ypos1 != "") ? ypos2 ? IHeight*(ypos1/100) : ypos1 : 0
    Width := Width1 ? Width2 ? IWidth*(Width1/100) : Width1 : IWidth
    Height := Height1 ? Height2 ? IHeight*(Height1/100) : Height1 : IHeight
    if !PassBrush
        Colour := "0x" (Colour2 ? Colour2 : "ff000000")
    Rendering := ((Rendering1 >= 0) && (Rendering1 <= 5)) ? Rendering1 : 4
    Size := (Size1 > 0) ? Size2 ? IHeight*(Size1/100) : Size1 : 12

    hFamily := Gdip_FontFamilyCreate(Font)
    hFont := Gdip_FontCreate(hFamily, Size, Style)
    FormatStyle := NoWrap ? 0x4000 | 0x1000 : 0x4000
    hFormat := Gdip_StringFormatCreate(FormatStyle)
    pBrush := PassBrush ? pBrush : Gdip_BrushCreateSolid(Colour)
    if !(hFamily && hFont && hFormat && pBrush && pGraphics)
        return !pGraphics ? -2 : !hFamily ? -3 : !hFont ? -4 : !hFormat ? -5 : !pBrush ? -6 : 0

    CreateRectF(RC, xpos, ypos, Width, Height)
    Gdip_SetStringFormatAlign(hFormat, Align)
    Gdip_SetTextRenderingHint(pGraphics, Rendering)
    ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)

    if vPos {
        StringSplit, ReturnRC, ReturnRC, |

        if (vPos = "vCentre") || (vPos = "vCenter")
            ypos += (Height-ReturnRC4)//2
        else if (vPos = "Top") || (vPos = "Up")
            ypos := 0
        else if (vPos = "Bottom") || (vPos = "Down")
            ypos := Height-ReturnRC4

        CreateRectF(RC, xpos, ypos, Width, ReturnRC4)
        ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
    }

    if !Measure
        E := Gdip_DrawString(pGraphics, Text, hFont, hFormat, pBrush, RC)

    if !PassBrush
        Gdip_DeleteBrush(pBrush)
    Gdip_DeleteStringFormat(hFormat)
    Gdip_DeleteFont(hFont)
    Gdip_DeleteFontFamily(hFamily)
    return E ? E : ReturnRC
}

william_ahk
Posts: 512
Joined: 03 Dec 2018, 20:02

Re: Why is GDIP removing leading zeros from text

Post by william_ahk » 19 Jan 2024, 06:05

I cannot reproduce this. It's likely your input text has already got leading zeroes removed, if it's evaluated as a number at some stage. Try displaying the text variable with MsgBox.

The_One_True_Rick
Posts: 65
Joined: 25 May 2023, 17:14

Re: Why is GDIP removing leading zeros from text

Post by The_One_True_Rick » 19 Jan 2024, 08:44

Not sure why it can't be reproduced. I did it with msgbox and my variables are fine. I even tried just using a plain text string number like "000001234" directly into the function and it still happened. Perhaps it is something to do with those options or some other settings. I'll keep looking. Thanks for the help!

It is not the Font options. Still does it even if I remove them all. If I add a "/" or anything else to the beginning or end of the variable it does not remove the leading zeros so has something to do with it just being a number.

william_ahk
Posts: 512
Joined: 03 Dec 2018, 20:02

Re: Why is GDIP removing leading zeros from text

Post by william_ahk » 20 Jan 2024, 01:19

That sounds very much like the text is evaluated as a number, which is strange since passing a literal string to the function would also yield that result. However I've tried replacing with your Gdip_TextToGraphics function but still got the same correct result. Perhaps try using tic's original Gdip library. Also make sure to test it on a new script file to better isolate the problem.

just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Why is GDIP removing leading zeros from text

Post by just me » 20 Jan 2024, 04:53

The_One_True_Rick wrote: If I add a "/" or anything else to the beginning or end of the variable it does not remove the leading zeros so has something to do with it just being a number.
See Caching
If a variable contains a numeric string and also the corresponding numeric value the numeric value is passed when used as function parameter. If you add any non-numeric character (even "" it cannot be cached.

User avatar
andymbody
Posts: 994
Joined: 02 Jul 2017, 23:47

Re: Why is GDIP removing leading zeros from text

Post by andymbody » 20 Jan 2024, 05:03

See if this version produces different results (I doubt it)
GDIP 1.96 2023-08-22

The_One_True_Rick
Posts: 65
Joined: 25 May 2023, 17:14

Re: Why is GDIP removing leading zeros from text

Post by The_One_True_Rick » 20 Jan 2024, 20:12

Thanks I'll look into those suggestions. Is there another GDIP function I should be using to display numbers as apposed to text? Still new to GDIP. I've just been using Text To Graphics for everything and its been fine until now but never had to display a number with a zero in front of it before.

just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Why is GDIP removing leading zeros from text

Post by just me » 21 Jan 2024, 06:00

Again, the issue it's most likely not caused by GDIP but by your script. How do you set the value of the variable passed a parameter Text?

The_One_True_Rick
Posts: 65
Joined: 25 May 2023, 17:14

Re: Why is GDIP removing leading zeros from text

Post by The_One_True_Rick » 21 Jan 2024, 13:58

Sorry I misunderstood. I am passing the value as follows:
1. Copy Screen to clipboard
2. Regex match ID number
3. Set this.ID_Number to the reg ex match results
4. Change the ID_Number display to that number using Update_Button_Text()

I suppose what you are looking for is at number 4. Update_Button_Text is passed the number from this.ID_Number through the New_Text variable like below.

Pretty stright forward until you get to _Create_Bitmaps(). Works great its just convoluted. Each button has 3 states that correspond to the window they open. If the window already exists that button is green, if it doesn't exist the button is pink, and if its the active window the button is red. Each of those 3 main states gets 3 alt states that correspond to default, MouseHover, and Pressed which are just different shades of the colors for the window states. So each button has 9 bitmaps associated with it that get flipped though based on some stuff. I guess the problem could be in there but I'm doing the exact same thing with another variable for birthday and it gets passed just fine but like you said it cant be cached so not sure how that plays in. I simplified this but it should give you the general idea of how i'm passing the variable to it at least.

Probably going to need some more functions if you wanted to actually run this but not sure you have to just to figure out how the variable is passed.

Code: Select all


^W::
Get_ID_Number()
Update_Display()
return

Get_ID_Number(){
		SendEvent, {Ctrl Down}{a}{Ctrl Up}
		Clipboard := ""
		SendEvent, {Ctrl Down}{c}{Ctrl Up}
		Clipwait, 1
		Clipboard := Clipboard
		RegExMatch( Clipboard, "iO)(?:Number)(?:\D*)(?:\d{6,})(?:\D*)(\d{6,})", Number_Pat )
		this.ID_Number  := SubStr( Number_Pat .Value(1), -5 )
	}
	
Update_Display(){
	WindowManager.Controls[ 59 ].Update_Button_Text( this.CurrentProfile.ID_Number  )
}

Update_Button_Text( New_Text := "", New_Font_Size := "", New_Font_Color := "" ){
	( New_Text ) ? this.Text := New_Text
	( New_Font_Size ) ? this.FontSize := New_Font_Size
	( New_Font_Color ) ? this.FontColor := New_Font_Color
	this.Create_Button()
	this.Parent.UpdateWindow()
	}
	
 Create_Button(){
     this._CreateBitmaps()
     this.Redraw_Button()
}

        _CreateBitmaps(){
            static StateList := [ "Default", "Exists", "Active" ]			; a list of all the button states
            static Alt_StateList := [ "", "Hover", "Pressed" ]
            for k , State in StateList {
                for index , Alt_State in Alt_StateList {

                    OffsetY := ( alt_state = "Pressed" ) ?  this.FontOffsetY + 5 : this.FontOffsetY
                    this.UseGradient := ( alt_state = "Pressed" ) ?  1 : 0

                    Bitmap_Color := ( alt_state = "Hover" ) ? "0xFF" Format( "{:X}", LightenDarkenColor( This[ State "Color" ], -25 ) ) : This[ State "Color" ]

                    This[ State alt_state "Bitmap" ] := Gdip_CreateBitmap( This.W , This.H )                  ;create a new bitmap for the current state (the canvas to be drawn to for the current state)
                    G := Gdip_GraphicsFromImage( This[ State alt_state "Bitmap" ] )
                    Gdip_SetSmoothingMode( G , This.Smoothing )
                    Brush := ( This.UseGradient ) ? Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , Bitmap_Color , This.GradientColor ) : Gdip_BrushCreateSolid( Bitmap_Color )
                    Gdip_FillRoundedRectangle( G , Brush , 1 , 1 , This.W - 2 , This.H - 2 , This.Roundness )                            ;use the brush to fill a rounded rectangle. (the buttons main surface)
                    Gdip_DeleteBrush( Brush )                                                                                            ;delete the brush to free the memory ( *pointers )

                    if( !This.UseGradient ){                                                                                             ;check if gradient should be used
                        Pen := Gdip_CreatePen( This.BorderColor , This.BorderWidth )        											 ;create a solid pen
                    }else{
                        Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , Bitmap_Color , This.GradientColor )   		 ;create a gradient brush
                        Pen := Gdip_CreatePenFromBrush( Brush , w )                                                                   ;create a pen from the brush
                        Gdip_DeleteBrush( Brush )                                                                                    ;delete the brush to free the memory
                    }
                    Gdip_DrawRoundedRectangle( G , Pen , 1 , 1 , This.W - 2 , This.H - 2 , This.Roundness )            ;draw a rounded rectangle. (Border around the button)
                    Gdip_DeletePen( Pen )                                                                              ;delete the pen to free memory
                    Brush := Gdip_BrushCreateSolid( This.FontColor )                                                   ;create a brush to paint the text with.
                    Gdip_TextToGraphics( G , This.Text , "s" This.FontSize " c" Brush " " This.FontOptions " x" 1 + This.FontOffsetX " y" 1 + OffsetY  , This.FontType , This.W - 2 , This.H - 2 )     	;draw the text onto the bitmap
                    Gdip_DeleteBrush( Brush )       ;delete the brush to free memory
                    Gdip_DeleteGraphics( G )        ;delete the grahics object to free memory
                }
            }
        }
        
          Redraw_Button(){
            static StateList := [ "Default", "Exists", "Active" ]
            static Alt_StateList := [ "", "Hover", "Pressed" ]
            State := stateList[ this.CurrentState ]	Alt_StateList[ this.AltState ]			            ; get the state of the button
            this.DrawBitmap( this[ State "Bitmap" ] , WindowManager.ButtonHandles[ this.Hwnd ], 0, 0 )  ; redraw button in current state
        }
        
        DrawBitmap( pBitmap , obj , dispose := 1 , AutoUpdate := 0 ){
            Gdip_DrawImage( This.Parent.G , pBitmap , obj.X , obj.Y , obj.W , obj.H )
            ( dispose ) ? Gdip_DisposeImage( pBitmap )
            ( Autoupdate ) ? This.Parent.UpdateWindow()
        }

just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Why is GDIP removing leading zeros from text

Post by just me » 21 Jan 2024, 17:02

Gdip_TextToGraphics( G , This.Text , ...

Object -> Keys
  • The string format of integer values is not retained. For example, AutoHotkey v1 produces two different strings for 0x10 and 16 (unlike AutoHotkey v2), but x[0x10], x[16] and x[00016] are equivalent as only the numeric value is used.
To store a numeric string you can try:

Code: Select all

Update_Button_Text( New_Text := "", New_Font_Size := "", New_Font_Color := "" ){
	( New_Text ) ? this.Text := New_Text . ""

The_One_True_Rick
Posts: 65
Joined: 25 May 2023, 17:14

Re: Why is GDIP removing leading zeros from text

Post by The_One_True_Rick » 21 Jan 2024, 21:18

OH my god that worked. I will try to understand why tomorrow but thanks you!!!
Cache me outside

just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Why is GDIP removing leading zeros from text

Post by just me » 22 Jan 2024, 05:07

It works, because New_Text . "" always creates a key of type string.

The_One_True_Rick
Posts: 65
Joined: 25 May 2023, 17:14

Re: Why is GDIP removing leading zeros from text

Post by The_One_True_Rick » 01 Feb 2024, 17:49

Ok new issue. NewText . "" works great unless I am trying to to update a display with an empty variable. Setting NewText = " " seems to clear the display but if a variable returns blank, instead of clearing the display it is not updated at all and just retains the previous value which isn't great. I got around this by manually clearing them all with a " " every time before they are updated but if anyone knows a way around that it would be helpful. Thanks!

User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: Why is GDIP removing leading zeros from text

Post by lmstearn » 01 Feb 2024, 23:11

In the event this.text changes outside of the function, does the following help?

Code: Select all

Update_Button_Text( New_Text := "", New_Font_Size := "", New_Font_Color := "" ){
	static Old_text := ""
	( New_Text  || Old_text == this.text) ? this.Text := New_Text . ""
	Old_text := this.text
Edit: Forget that, - sorry not with you entirely, how about just this instead:

Code: Select all

Update_Button_Text( New_Text := "", New_Font_Size := "", New_Font_Color := "" ){
	( New_Text) ? this.Text := New_Text . "": this[Text] :=""
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

The_One_True_Rick
Posts: 65
Joined: 25 May 2023, 17:14

Re: Why is GDIP removing leading zeros from text

Post by The_One_True_Rick » 02 Feb 2024, 10:47

Yeah I should I have thought of that. if New_Text is blank it just does nothing because that is how the function works. My fault. Thanks! I'll just change the function since that parameter should really not even be optional. Makes more sense to just do this I think.

Code: Select all

		Update_Button_Text( New_Text, New_Font_Size := "", New_Font_Color := "" ){
			 this.Text := New_Text . ""
			( New_Font_Size ) ? this.FontSize := New_Font_Size
			( New_Font_Color ) ? this.FontColor := New_Font_Color
			this.Create_Button()
			this.Parent.UpdateWindow()
		}
Edit:: Yep this works, I'm dumb. Thanks!

Post Reply

Return to “Ask for Help (v1)”