Extended KotD #16: _UPRINTWIDTH - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://qb64phoenix.com/forum/forumdisplay.php?fid=49) +---- Thread: Extended KotD #16: _UPRINTWIDTH (/showthread.php?tid=2782) |
Extended KotD #16: _UPRINTWIDTH - SMcNeill - 06-07-2024 Now, since my last couple of entries was for _UPrintString, I imagine that everyone could guess that _UPrintWidth is probably some sort of helper function for that command. Right? I also imagine that since most folks already know that _PrintWidth will tell you how many pixels wide a text string is, most can probably hazard a guess as to what _UPrintWidth does. Right? Well, for those who aren't brave enough to guess, let me tell you! _UPrintWidth tells you the pixel width of a text string that will be printed to the screen! And, I can hear some of you guys muttering under your breath, saying stuff like, "And we needed a new command for this? Why?! We already have _PrintWidth!" . . . And, I hope the basic answer to that was already stressed via my last couple of posts. _UPrintString tends to print wider and taller characters than _PrintString and PRINT does, as they both can tend to "clip" characters off with various fonts and font sizes. _PRINTWIDTH returns the width of text that is going to be printed to the screen using _PRINTSCREEN, or plain PRINT. _UPRINTWIDTH returns the width of text that is going to be printed to the screen using _UPRINTSCREEN. ^ That is the basic -- and most important -- difference in the two commands. If you're used to using _PrintWidth, then there should be zero issue with you swapping over to _UPrintWidth -- with a few syntax changes which I'll go over below. For starters, let me share the relevant wiki page for each command: https://qb64phoenix.com/qb64wiki/index.php/UPRINTWIDTH https://qb64phoenix.com/qb64wiki/index.php/PRINTWIDTH And their syntax: pixelWidth& = _UPRINTWIDTH(text$[, utfEncoding&][, fontHandle&]) pixelWidth% = _PRINTWIDTH(textToPrint$[, destinationHandle&]) Now, as you can see, the REQUIRED portions of the commands are both the same, in their simplest form: width = _command(text$) <whereas command is whichever of those two commands you want to substitute in there> But, there ARE a few differences in the optional parameters, which I'll cover below: _PRINSTSTRING has only one optional parameter -- destinationHandle&. Basically, you can point it to the SCREEN that you want to get the width for, and it'll return the size for any text that you'd print to that screen. _UPRINTSTRING has two optional parameters: 1) utfEncoding& --- this is a parameter which you can use to tell it what encoding you're using with the chosen font. Are you wanting the width for ASCII/ANSI formatted text? UTF-8 formatted text? UTF-16? UTF-32? Like I mentioned yesterday, *MOST* folks can basically just skip this parameter completely. Unless you're working with UniCode text strings (which UPrintString does, but PrintString can't), then you can just skip messing with this parameter completely. fontHandle& -- and here you can specify the FONT HANDLE that you want, when you're getting the width of that text to the screen. Note that this is NOT the same as the destinationHandle&, that _PrintString uses. Don't let that old habit catch you unaware and cause issues for you. _PrintString returns the width for whatever font is currently loaded on the screen you specify. _UPrintString returns the width for whatever font you specify. It doesn't have to be loaded on the screen whatsoever, for it to work for you. This difference can save you from having to write code such as: Code: (Select All) F = _FONT 'get the old font Instead, you can just do something as simple as: Code: (Select All) upw = _UPrintWidth("Hello World", ,newfonthandle) 'get the uprintwidth of the text in the chosen font. The most important thing is just to remember: When using _UPRINTSTRING, then use _UPrintWidth with it If you're using _PrintString or just PRINT, then use _PrintWidth with those commands. There's a few slight differences in things here, between _PrintWidth and _UPrintWidth, but they're subtle enough that I don't think anyone will have any trouble adjusting to them once they're aware of them. And since you guys all read this post, you're all aware of those minute differences now, and shouldn't have any issues whatsoever making using of _UPrintWidth properly. Right? |