TextWidth() with Format Tags

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

TextWidth() with Format Tags

Post by PEB » Fri Nov 04, 2011 5:50 am

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: 464
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: TextWidth() with Format Tags

Post by Allanon » Fri Nov 04, 2011 9:01 am

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
----------------------------
[Allanon] Fabio Falcucci
AMC - Creative Development // Docs Site // Support Forum
Support me on Patreon for Hollywood libraries!

PEB
Posts: 269
Joined: Sun Feb 21, 2010 1:28 am

Re: TextWidth() with Format Tags

Post by PEB » Fri Nov 04, 2011 5:14 pm

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: 3309
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: TextWidth() with Format Tags

Post by airsoftsoftwair » Fri Nov 04, 2011 9:10 pm

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: 464
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: TextWidth() with Format Tags

Post by Allanon » Fri Nov 04, 2011 9:51 pm

@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
----------------------------
[Allanon] Fabio Falcucci
AMC - Creative Development // Docs Site // Support Forum
Support me on Patreon for Hollywood libraries!

Post Reply