Jump to content

(SOLVED)_GDIPlus how calculate Font Size to fill a _GDIPlus_RectFCreate?


Recommended Posts

I'm creating an small script that will get Text from User Input or From a File and write that text in an Image for that I'm using _GDIPlus and _GDIPlus_RectFCreate().

The problem is the text length will vary and I would like to adjust the Text font size using _GDIPlus_FontCreate() to make it fit on the _GDIPlus_RectFCreate().

Someone could help me with this? there is a way to test if an specific text with an specific length and specific font size will fit on a predefined _GDIPlus_RectFCreate()?

Here some example code:

#include <GDIPlus.au3>

_GDIPlus_Startup()

$sText = "Example very very long string"
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Clas1.jpg")
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$dimen = _GDIPlus_ImageGetDimension($hImage)

Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF009900)
Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")
Local $hFont = _GDIPlus_FontCreate($hFamily, 20)
Local $hLayout = _GDIPlus_RectFCreate(0, 0, $dimen[0], $dimen[1])
Local $hStringFormat = _GDIPlus_StringFormatCreate()
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sText , $hFont, $hLayout, $hStringFormat, $hBrush)
_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\GDIPlus_ImageWithText.jpg")

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

Regards
Alien.

Edited by alien4u
Link to comment
Share on other sites

You have to measure the font size by testing testing.

#include <GDIPlus.au3>

_GDIPlus_Startup()

$sText = "Example very very long string"
$hImage = _GDIPlus_BitmapCreateFromScan0(300, 200)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$dimen = _GDIPlus_ImageGetDimension($hImage)

Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF009900)
Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")
Local $hLayout = _GDIPlus_RectFCreate(0, 0, $dimen[0], $dimen[1])
Local $hStringFormat = _GDIPlus_StringFormatCreate()

$fSize = 50
Do
    $hFont = _GDIPlus_FontCreate($hFamily, $fSize)
    $aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, $sText, $hFont, $hLayout, $hStringFormat)
    If $aMeasure[2] = 1 Then ExitLoop
    $fSize -= 1
    _GDIPlus_FontDispose($hFont)
Until False
ConsoleWrite("Font size: " & $fSize & @CRLF)

_GDIPlus_GraphicsDrawStringEx($hGraphic, $sText , $hFont, $hLayout, $hStringFormat, $hBrush)
_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\GDIPlus_ImageWithText.jpg")

; Clean up resources
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_Shutdown()

ShellExecute(@ScriptDir & "\GDIPlus_ImageWithText.jpg")

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thank you @UEZ that code show me exactly how to do it.

I modify the code to use $aMeasure[1] - The number of characters that actually fit into the layout rectangle and use StringLen to know the number of characters.

The final code base on your code is this one:

#include <GDIPlus.au3>
#include <String.au3>

_GDIPlus_Startup()

$sText = "Example very very long string then keep and keep and keep and keep writing until the font get reduced this is the way I need it"
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Clas1.jpg")
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$dimen = _GDIPlus_ImageGetDimension($hImage)
$iTextLen = StringLen($sText)
Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF009900)
Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")
Local $hLayout = _GDIPlus_RectFCreate(0, $dimen[1] - 280, $dimen[0], $dimen[1] - ($dimen[1] - 280))
Local $hStringFormat = _GDIPlus_StringFormatCreate()

$fSize = 50
Do
    $hFont = _GDIPlus_FontCreate($hFamily, $fSize)
    $aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, $sText, $hFont, $hLayout, $hStringFormat)
    If $aMeasure[1] = $iTextLen Then ExitLoop
    $fSize -= 1
    _GDIPlus_FontDispose($hFont)
Until False
ConsoleWrite("Font size: " & $fSize & @CRLF)
$fSize = $fSize - 5
$hFont = _GDIPlus_FontCreate($hFamily, $fSize)

_GDIPlus_GraphicsDrawStringEx($hGraphic, $sText , $hFont, $hLayout, $hStringFormat, $hBrush)
_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\GDIPlus_ImageWithText.jpg")

; Clean up resources
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_Shutdown()

ShellExecute(@ScriptDir & "\GDIPlus_ImageWithText.jpg")

Regards
Alien.

GDIPlus_ImageWithText.jpg

Edited by alien4u
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...