Page 1 of 1

TextWidth() with Format Tags

Posted: Fri Nov 04, 2011 5:50 am
by PEB
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].

Re: TextWidth() with Format Tags

Posted: Fri Nov 04, 2011 9:01 am
by Allanon
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 :)

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
returns the text width without format tags and the original text without the format tags too

Re: TextWidth() with Format Tags

Posted: Fri Nov 04, 2011 5:14 pm
by PEB
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.

Re: TextWidth() with Format Tags

Posted: Fri Nov 04, 2011 9:10 pm
by airsoftsoftwair
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

Posted: Fri Nov 04, 2011 9:51 pm
by Allanon
@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