TextWidth() with Format Tags

Feature requests for future versions of Hollywood can be voiced here
Post Reply
PEB
Posts: 569
Joined: Sun Feb 21, 2010 1:28 am

TextWidth() with Format Tags

Post 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].
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: TextWidth() with Format Tags

Post 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
PEB
Posts: 569
Joined: Sun Feb 21, 2010 1:28 am

Re: TextWidth() with Format Tags

Post 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.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: TextWidth() with Format Tags

Post 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.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: TextWidth() with Format Tags

Post 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
Post Reply