UPRINTWIDTH: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Add _UCHARPOS to "See Also")
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:_UPRINTWIDTH}}
{{DISPLAYTITLE:_UPRINTWIDTH}}
The '''_UPRINTWIDTH''' function returns the width in pixels of the text [[STRING|string]] specified. The function supports Unicode encoded text.
The '''_UPRINTWIDTH''' function returns the width in pixels of the text [[STRING|string]] specified. The function supports Unicode encoded text.


{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|pixelWidth&}} = [[_UPRINTWIDTH]]({{Parameter|text$}}[, {{Parameter|utfEncoding&}}][, {{Parameter|fontHandle&}}])
: {{Parameter|pixelWidth&}} = [[_UPRINTWIDTH]]({{Parameter|text$}}[, {{Parameter|utfEncoding&}}][, {{Parameter|fontHandle&}}])


{{PageParameters}}
{{PageParameters}}
Line 9: Line 11:
* {{Parameter|utfEncoding&}} is an optional UTF encoding of {{Parameter|text$}}. This can be 0 for ASCII, 8 for UTF-8, 16 for UTF-16 or 32 for UTF-32.
* {{Parameter|utfEncoding&}} is an optional UTF encoding of {{Parameter|text$}}. This can be 0 for ASCII, 8 for UTF-8, 16 for UTF-16 or 32 for UTF-32.
* {{Parameter|fontHandle&}} is an optional font handle.
* {{Parameter|fontHandle&}} is an optional font handle.


{{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.
* 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.
Line 18: 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.


{{PageAvailability}}
{{PageAvailability}}
Line 29: Line 33:
File:Osx.png|'''yes'''
File:Osx.png|'''yes'''
</gallery>
</gallery>
<!-- additional availability notes go below here -->


{{PageExamples}}
{{PageExamples}}
Line 76: Line 82:
{{Cl|END FUNCTION}}
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}


{{PageSeeAlso}}
{{PageSeeAlso}}
* [https://qb64phoenix.com/forum/showthread.php?tid=2782 Featured in our "Keyword of the Day" series]
* [[_UFONTHEIGHT]], [[_ULINESPACING]], [[_UPRINTSTRING]], [[_UCHARPOS]]
* [[_UFONTHEIGHT]], [[_ULINESPACING]], [[_UPRINTSTRING]], [[_UCHARPOS]]
* [[_FONTWIDTH]], [[_FONTHEIGHT]], [[_PRINTWIDTH]]
* [[_FONTWIDTH]], [[_FONTHEIGHT]], [[_PRINTWIDTH]]
Line 83: Line 91:
* [[_PRINTSTRING]], [[_FONT]]
* [[_PRINTSTRING]], [[_FONT]]
* [[Text Using Graphics]]
* [[Text Using Graphics]]


{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 10:41, 7 June 2024

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.


Availability


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



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link