Returns the exact width of rendered text string in pixels.
Takes text (string to measure, any UTF-8 text), font (font handle from b2dLoadFont, positive integer).
Returns the exact width of rendered text string in pixels.
2D Overlay
Parameters & Returns
Parameters
textString
fontInt
Returns
Int
Quick Summary
Returns the exact width of rendered text string in pixels.
Takes text (string to measure, any UTF-8 text), font (font handle from b2dLoadFont, positive integer).
Returns the exact width of rendered text string in pixels.
Technical Exegesis...
Returns the exact width of rendered text string in pixels. Takes text (string to measure, any UTF-8 text), font (font handle from b2dLoadFont, positive integer). Returns integer width (horizontal space required for text in pixels, 0 if invalid font). Calculates precise rendering width by summing individual character advances and kerning adjustments.
Returns the exact width of rendered text string in pixels. Takes text (string to measure, any UTF-8 text), font (font handle from b2dLoadFont, positive integer). Returns integer width (horizontal space required for text in pixels, 0 if invalid font). Calculates precise rendering width by summing individual character advances and kerning adjustments.
Width calculation: sums character advances (horizontal space each character occupies), includes kerning pairs (spacing adjustments between specific characters like "AV"), exact rendering width (matches actual drawn text), handles proportional fonts (variable character widths).
Use cases: (1) Text centering (calculate offset for centered alignment), (2) Text box sizing (size UI elements to fit text precisely), (3) Text wrapping (determine line breaks for word wrap), (4) Cursor positioning (calculate click positions in text), (5) Dynamic layout (adjust spacing based on text width).
Common patterns: center text = x=(screenWidth-b2dGetStringWidth(text, font))/2: b2dDrawText(text, x, y, 0, font); right align = x=rightEdge-b2dGetStringWidth(text, font): b2dDrawText(text, x, y, 0, font); text box = boxWidth=b2dGetStringWidth(text, font)+padding*2; wrap check = If b2dGetStringWidth(line, font)>maxWidth Then (wrap to next line) EndIf.
Performance: fast calculation (~0.001ms for typical strings), linear with string length (each character queried), cached font metrics (no file I/O), suitable for per-frame calls.
Kerning: includes kerning pair adjustments (e.g., "AV" narrower than "A" + "V"), depends on font design (some fonts have extensive kerning, others minimal), matches actual rendering (same width as drawn text).
Related: b2dGetStringHeight measures text height, b2dGetFontWidth queries average character width, b2dGetFontHeight queries line height, b2dDrawText renders text with same width.