OK, here's a new one:
Can TextWidth()/TextExtent() be made to return the accurately rendered width when Format Tags are used?
It gets thrown off with , but is especially bad with something like this [shadow=$99000000,2,#SHDWSOUTHEAST].
TextWidth() with Format Tags
Re: TextWidth() with Format Tags
In the meanwhile I've made a simple routine to calculate the exact width without these tags, its part of my GFX library, just rename the function name
returns the text width without format tags and the original text without the format tags too
Code: Select all
Function GFX.Text.GetWidth(txt)
Local real_txt = txt
If FindStr(txt, "[") = -1 Then Return(TextWidth(txt), txt)
; BOLD
real_txt = ReplaceStr(real_txt, "[b]", "")
real_txt = ReplaceStr(real_txt, "[B]", "")
real_txt = ReplaceStr(real_txt, "[/b]", "")
real_txt = ReplaceStr(real_txt, "[/B]", "")
; ITALIC
real_txt = ReplaceStr(real_txt, "[i]", "")
real_txt = ReplaceStr(real_txt, "[I]", "")
real_txt = ReplaceStr(real_txt, "[/i]", "")
real_txt = ReplaceStr(real_txt, "[/I]", "")
; UNDERLINE
real_txt = ReplaceStr(real_txt, "[u]", "")
real_txt = ReplaceStr(real_txt, "[U]", "")
real_txt = ReplaceStr(real_txt, "[/u]", "")
real_txt = ReplaceStr(real_txt, "[/U]", "")
; SHADOW
Local s = 0
While s <> -1
s = FindStr(real_txt, "[shadow", False)
If s <> -1
Local e = FindStr(real_txt, "]", False, s)
If e <> -1
real_txt = UnMidStr(real_txt, s, e-s +1)
EndIf
EndIf
Wend
real_txt = ReplaceStr(real_txt, "[/shadow]", "")
real_txt = ReplaceStr(real_txt, "[/SHADOW]", "")
; EDGE
Local s = 0
While s <> -1
s = FindStr(real_txt, "[edge", False)
If s <> -1
Local e = FindStr(real_txt, "]", False, s)
If e <> -1
real_txt = UnMidStr(real_txt, s, e-s +1)
EndIf
EndIf
Wend
real_txt = ReplaceStr(real_txt, "[/edge]", "")
real_txt = ReplaceStr(real_txt, "[/EDGE]", "")
; COLOR
Local s = 0
While s <> -1
s = FindStr(real_txt, "[color", False)
If s <> -1
Local e = FindStr(real_txt, "]", False, s)
If e <> -1
real_txt = UnMidStr(real_txt, s, e-s +1)
EndIf
EndIf
Wend
real_txt = ReplaceStr(real_txt, "[/color]", "")
real_txt = ReplaceStr(real_txt, "[/COLOR]", "")
Return(TextWidth(real_txt), real_txt)
EndFunction
----------------------------
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆
Re: TextWidth() with Format Tags
That kind of solution works fine for , but with tags such as the rendered (bold in this case) text is wider than simply the normal text minus the tags.
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: TextWidth() with Format Tags
This might be supported in the future. As a workaround, you can write a function which breaks down a formatted string into its constituents and then call TextWidth() for each constituent. As both TextWidth() and TextExtent() take the style set using SetFontStyle() into account you can just set the style you need before each call to TextWidth() or TextExtent(). Shouldn't be too complicated.
Re: TextWidth() with Format Tags
@PEB
yes you are right it's not precise with the bold tag, but is better then calculating the width, anyway as suggested by Andreas I'll modify this routine to calculate exaclty the text width applying the font style step by step, tag by tag... even if I think it's a bit cpu time expensive for heavy text rendering
yes you are right it's not precise with the bold tag, but is better then calculating the width, anyway as suggested by Andreas I'll modify this routine to calculate exaclty the text width applying the font style step by step, tag by tag... even if I think it's a bit cpu time expensive for heavy text rendering
----------------------------
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆