Sign in to follow this  
Followers 0
E1M1

Need calculating help

9 posts in this topic

#1 ·  Posted (edited)

Could anyone help me making function that would take max height, max width and stringlen as arguments and then return max fontsize that fits with in given size?

I have no idea where to start.

My solution for width is something like that.

$MaxFontSize = $Width/$StringLen

But with this method I may still get font which height is too much.

What calcus I could do for height?

Edited by E1M1

edited

Share this post


Link to post
Share on other sites



E1M1,

Look at the final posts in the thread JohnOne linked to - they are exactly what you are looking for. :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

That's much code. I found that max char height is 1.29 or 1.25 * font size. But i am not sure if my idea works.

This didn't work.

$fontsizex = $rndw /StringLen($word)
            $fontsizey = $rndh/$fontsizex
            If $fontsizey > 1.2 Then
                $fontsizey = $fontsizey - ($fontsizey/1.3)
            EndIf
            $fontsize =4
            If $fontsizey > $fontsizex Then
                $fontsize = $fontsizex
            Else
                $fontsize = $fontsizey
            EndIf

Your's code is nice, but i am looking for something that would return fontsize as int.

Edited by E1M1

edited

Share this post


Link to post
Share on other sites

E1M1,

That's much code

But it already written for you! :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

;                  |$array[2] = Width of rectangle required to hold formatted string
;                  |$array[3] = Height of rectangle required to hold formatted string

That does return w,h; but i have w,h given.


edited

Share this post


Link to post
Share on other sites

E1M1,

So use a little initiative and run a loop to see which font size will fit within those dimensions: :(

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StringSize.au3>

Global $hlabel = 9999

$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("Enter required text", 10, 10, 200, 20)
$hEdit = GUICtrlCreateEdit("", 10, 30, 480, 200, $ES_WANTRETURN)

GUICtrlCreateLabel("Enter available width", 10, 250, 200, 20)
$hWidth = GUICtrlCreateInput("", 10, 270, 230, 20)
GUICtrlCreateLabel("Enter available height", 260, 250, 200, 20)
$hHeight = GUICtrlCreateInput("", 260, 270, 230, 20)

$hButton = GUICtrlCreateButton("Go", 10, 300, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            $sText = GUICtrlRead($hEdit)
            $iWidth = Int(Number(GUICtrlRead($hWidth)))
            $iHeight = Int(Number(GUICtrlRead($hHeight)))

            For $iTextSize = 100 To 1 Step -1
                $aSize = _StringSize($sText, $iTextSize, Default, Default, "Tahoma")
                ; Allow for the label margins or you might loose the edges of italic fonts
                If $aSize[2] < $iWidth + 8 And $aSize[3] < $iHeight + 8 Then ExitLoop
            Next

            MsgBox(0, "Max Font Size", $iTextSize)
            _Demo()

    EndSwitch

WEnd

Func _Demo()

    $hLabel_1 = GUICtrlCreateLabel($sText, 10, 350, $iWidth, $iHeight)
    GUICtrlSetBkColor(-1, 0xC0FFC0)
    GUICtrlSetFont(-1, $iTextSize, Default, Default, "Tahoma")

    $hLabel_2 = GUICtrlCreateLabel($sText, 260, 350, $iWidth, $iHeight)
    GUICtrlSetBkColor(-1, 0xFFC0C0)
    GUICtrlSetFont(-1, $iTextSize + 1, Default, Default, "Tahoma")

    Sleep(5000)

    GUICtrlDelete($hLabel_1)
    GUICtrlDelete($hLabel_2)

EndFunc

The green label uses the calculated max font size - the red one the one above. Some fonts might still appear to fit at the larger size, but you must allow for the possibility of using italic letters - because these are sloped, you risk losing the edges if you do not allow a small margin (look for the comment in the script). :D

Does that do what you want? :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

For $i = 1 To 100
            $sText = "0"
            $iWidth = 13
            $iHeight = 11

            For $iTextSize = 100 To 1 Step -1
                $aSize = _StringSize($sText, $iTextSize, Default, Default, "Tahoma")
                ; Allow for the label margins or you might loose the edges of italic fonts
                If $aSize[2] < $iWidth + 8 And $aSize[3] < $iHeight + 8 Then ExitLoop
            Next

Next

that takes some cpu.

This Get's it dont faster, but might not be so nice.

$fontsizex = $rndw /StringLen($word)
            $fontsizey = max($fontsizex,$rndh*.80)
            GUICtrlSetFont(-1, $fontsizey, Random(0, 800, 1), Random(0, 14, 1), $aFonts[Random(1, $aFonts[0], 1)])

edited

Share this post


Link to post
Share on other sites

E1M1,

that takes some cpu

What is your machine powered by? A hamster in a wheel? :(

But if you are happy with your solution..... :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0