UPRINTWIDTH: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 14: | Line 14: | ||
{{PageDescription}} | {{PageDescription}} | ||
* If the {{Parameter|utfEncoding&}} is omitted, then it is assumed to be 0. | * If the {{Parameter|utfEncoding&}} is omitted, then it is assumed to be 0 (ASCII). | ||
* If {{Parameter|fontHandle&}} is omitted, then the current write page font is used. | |||
* All multi-byte UTF encodings are expected in little-endian. | * All multi-byte UTF encodings are expected in little-endian. | ||
* Unicode byte order mark (BOM) is not processed and must be handled by user code. | * Unicode byte order mark (BOM) is not processed and must be handled by user code. | ||
Line 20: | Line 21: | ||
* Can be used with variable-width fonts or built-in fonts, unlike [[_FONTWIDTH]] which requires a MONOSPACE font handle. | * Can be used with variable-width fonts or built-in fonts, unlike [[_FONTWIDTH]] which requires a MONOSPACE font handle. | ||
* Unlike [[_PRINTWIDTH]], [[_UPRINTWIDTH]] always returns the width of the text string in pixels. | * Unlike [[_PRINTWIDTH]], [[_UPRINTWIDTH]] always returns the width of the text string in pixels. | ||
;* {{Text|Note|red}}: If UTF-8 is specified, but the given {{Parameter|text$}} contains invalid bytes such as ASCII values > 127 which are not part of a valid UTF-8 multibyte character encoding, then this function returns zero(0). | |||
:* This behavior can be used to validate any UTF-8 text. | |||
Line 32: | Line 35: | ||
File:Osx.png|'''yes''' | File:Osx.png|'''yes''' | ||
</gallery> | </gallery> | ||
<!-- additional availability notes go below here --> | |||
* Since '''QB64-PE v3.14.0''' it supports both, little-endian and big-endian UTF-16 encodings and also correctly handles the UTF-16 byte order mark (BOM) now. | |||
Line 83: | Line 88: | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[_UFONTHEIGHT]], [[_ULINESPACING]], [[_UPRINTSTRING]] | * [https://qb64phoenix.com/forum/showthread.php?tid=2782 Featured in our "Keyword of the Day" series] | ||
* [[_UFONTHEIGHT]], [[_ULINESPACING]], [[_UPRINTSTRING]], [[_UCHARPOS]] | |||
* [[_FONTWIDTH]], [[_FONTHEIGHT]], [[_PRINTWIDTH]] | * [[_FONTWIDTH]], [[_FONTHEIGHT]], [[_PRINTWIDTH]] | ||
* [[_NEWIMAGE]], [[_LOADFONT]] | * [[_NEWIMAGE]], [[_LOADFONT]] |
Latest revision as of 16:10, 4 May 2025
The _UPRINTWIDTH function returns the width in pixels of the text string specified. The function supports Unicode encoded text.
Syntax
- pixelWidth& = _UPRINTWIDTH(text$[, utfEncoding&][, fontHandle&])
Parameters
- text$ is any literal or variable STRING value.
- utfEncoding& is an optional UTF encoding of text$. This can be 0 for ASCII, 8 for UTF-8, 16 for UTF-16 or 32 for UTF-32.
- fontHandle& is an optional font handle.
Description
- If the utfEncoding& is omitted, then it is assumed to be 0 (ASCII).
- If fontHandle& is omitted, then the current write page font is used.
- All multi-byte UTF encodings are expected in little-endian.
- Unicode byte order mark (BOM) is not processed and must be handled by user code.
- This can be useful to find the width of the font print string before actually printing it.
- Can be used with variable-width fonts or built-in fonts, unlike _FONTWIDTH which requires a MONOSPACE font handle.
- Unlike _PRINTWIDTH, _UPRINTWIDTH always returns the width of the text string in pixels.
- Note
- If UTF-8 is specified, but the given text$ contains invalid bytes such as ASCII values > 127 which are not part of a valid UTF-8 multibyte character encoding, then this function returns zero(0).
- This behavior can be used to validate any UTF-8 text.
Availability
- Since QB64-PE v3.14.0 it supports both, little-endian and big-endian UTF-16 encodings and also correctly handles the UTF-16 byte order mark (BOM) now.
Examples
- Example
- Centers and prints a Russian text on a graphics screen.
OPTION _EXPLICIT SCREEN _NEWIMAGE(800, 600, 32) DIM fh AS LONG: fh = _LOADFONT("cyberbit.ttf", 21) IF fh <= 0 THEN PRINT "Failed to load font file!" END END IF _FONT fh RESTORE text_data DIM myString AS STRING: myString = LoadUData$ _UPRINTSTRING (_WIDTH \ 2 - _UPRINTWIDTH(myString, 8, fh) \ 2, _HEIGHT \ 2 - _UFONTHEIGHT \ 2), myString, _WIDTH, 8 END text_data: DATA 6F,D0,91,D1,8B,D1,81,D1,82,D1,80,D0,B0,D1,8F,20,D0,BA,D0,BE,D1,80,D0,B8,D1 DATA 87,D0,BD,D0,B5,D0,B2,D0,B0,D1,8F,20,D0,BB,D0,B8,D1,81,D0,B0,20,D0,BF,D0,B5 DATA D1,80,D0,B5,D0,BF,D1,80,D1,8B,D0,B3,D0,B8,D0,B2,D0,B0,D0,B5,D1,82,20,D1,87 DATA D0,B5,D1,80,D0,B5,D0,B7,20,D0,BB,D0,B5,D0,BD,D0,B8,D0,B2,D1,83,D1,8E,20,D1 DATA 81,D0,BE,D0,B1,D0,B0,D0,BA,D1,83,2E FUNCTION LoadUData$ DIM AS _UNSIGNED LONG i, s DIM d AS STRING DIM buffer AS STRING READ d s = VAL("&h" + d) buffer = SPACE$(s) FOR i = 1 TO s READ d ASC(buffer, i) = VAL("&h" + d) NEXT LoadUData = buffer END FUNCTION |
See also
- Featured in our "Keyword of the Day" series
- _UFONTHEIGHT, _ULINESPACING, _UPRINTSTRING, _UCHARPOS
- _FONTWIDTH, _FONTHEIGHT, _PRINTWIDTH
- _NEWIMAGE, _LOADFONT
- _PRINTSTRING, _FONT
- Text Using Graphics