PRINTWIDTH: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_PRINTWIDTH}}{{DISPLAYTITLE:}} The _PRINTWIDTH function returns the width in pixels of the text string specified. {{PageSyntax}} : {{Parameter|pixelWidth%}} = _PRINTWIDTH({{Parameter|textToPrint$}}[, {{Parameter|destinationHandle&}}]) {{PageDescription}} * {{Parameter|textToPrint$}} is any literal or variable STRING value. * If the {{Parameter|destinationHandle&}} is omitted, the current destination image or screen page is used....")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:_PRINTWIDTH}}{{DISPLAYTITLE:}}
{{DISPLAYTITLE:_PRINTWIDTH}}
The [[_PRINTWIDTH]] function returns the width in pixels of the text [[STRING|string]] specified.
The [[_PRINTWIDTH]] function returns the width in pixels of the text [[STRING|string]] specified.


Line 17: Line 17:
{{PageExamples}}
{{PageExamples}}
''Example:'' SUB returns font or screen mode's text block size using _PRINTWIDTH and [[_FONTHEIGHT]] without a handle parameter.
''Example:'' SUB returns font or screen mode's text block size using _PRINTWIDTH and [[_FONTHEIGHT]] without a handle parameter.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DO}}
{{Cl|DO}}
   {{Cl|INPUT}} "Enter Screen mode 1, 2 or 7 to 13: ", scr$
   {{Cl|INPUT}} "Enter Screen mode 1, 2 or 7 to 13: ", scr$
   mode% = {{Cl|VAL}}(scr$)
   mode% = {{Cl|VAL}}(scr$)
{{Cl|LOOP}} {{Cl|UNTIL}} mode% > 0  
{{Cl|LOOP}} {{Cl|UNTIL}} mode% > 0
{{Cl|SCREEN (statement)|SCREEN}} mode%
{{Cl|SCREEN}} mode%
{{Cl|INPUT}} "Enter first name of TTF font to use or hit enter for text size: ", TTFont$
{{Cl|INPUT}} "Enter first name of TTF font to use or hit enter for text size: ", TTFont$
{{Cl|IF}} {{Cl|LEN}}(TTFont$) {{Cl|THEN}} {{Cl|INPUT (file mode)|INPUT}} "Enter font height: ", hi$
{{Cl|IF}} {{Cl|LEN}}(TTFont$) {{Cl|THEN}} {{Cl|INPUT (file mode)|INPUT}} "Enter font height: ", hi$
Line 36: Line 36:
{{Cl|SUB}} TextSize (TextWidth&, TextHeight&)
{{Cl|SUB}} TextSize (TextWidth&, TextHeight&)
TextWidth& = {{Cl|_PRINTWIDTH}}("W")    'measure width of one font or text character
TextWidth& = {{Cl|_PRINTWIDTH}}("W")    'measure width of one font or text character
TextHeight& = {{Cl|_FONTHEIGHT}}        'can measure normal text block heights also  
TextHeight& = {{Cl|_FONTHEIGHT}}        'can measure normal text block heights also
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
<center>'''Note:''' The SUB procedure does not need the font handle for font sizes after [[_FONT]] enables one.</center>
<center>'''Note:''' The SUB procedure does not need the font handle for font sizes after [[_FONT]] enables one.</center>



Latest revision as of 22:43, 11 February 2023

The _PRINTWIDTH function returns the width in pixels of the text string specified.


Syntax

pixelWidth% = _PRINTWIDTH(textToPrint$[, destinationHandle&])


Description

  • textToPrint$ is any literal or variable STRING value.
  • If the destinationHandle& is omitted, the current destination image or screen page is used.
  • 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.
  • In SCREEN 0, _PRINTWIDTH returns the character length of a text string, exactly as LEN(textToPrint$) (version 1.000 and up).


Examples

Example: SUB returns font or screen mode's text block size using _PRINTWIDTH and _FONTHEIGHT without a handle parameter.

DO
  INPUT "Enter Screen mode 1, 2 or 7 to 13: ", scr$
  mode% = VAL(scr$)
LOOP UNTIL mode% > 0
SCREEN mode%
INPUT "Enter first name of TTF font to use or hit enter for text size: ", TTFont$
IF LEN(TTFont$) THEN INPUT "Enter font height: ", hi$
height& = VAL(hi$)
IF height& > 0 THEN _FONT _LOADFONT("C:\Windows\Fonts\" + TTFont$ + ".ttf", height&, style$)

TextSize wide&, high&       'get the font or current screen mode's text block pixel size

_PRINTSTRING (20, 100), CHR$(1) + STR$(wide&) + " X" + STR$(high&) + " " + CHR$(2)

END

SUB TextSize (TextWidth&, TextHeight&)
TextWidth& = _PRINTWIDTH("W")     'measure width of one font or text character
TextHeight& = _FONTHEIGHT         'can measure normal text block heights also
END SUB
Code by Ted Weissgerber
Note: The SUB procedure does not need the font handle for font sizes after _FONT enables one.


See also



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