[Snippet] StringSize

Scripts

13 years
edited 8 years
This library will calculate the width of a string in pixels. This is used in word wrapping.

I have updated my old library a bit.

For those who don't know how to use it, try making a paragraph written in a multiboard. If you want that text to be dynamic, it is going to be very painful, and using |n will have a lot of problems. StringLength() is an option but it is unreliable when trying to word wrap since not all characters are the same width. It will end up looking pretty sloppy if you use all skinny characters and may bug out if you use all massive letters.

Enjoy, I'll update my word wrap library and post it later. Requires Ascii by Bribe, link is in the code.

Code (jass) Select
library StringSize /* v2.1.0.0
********************************************************************
*
*  This library can calculate the width of a string in pixels.
*  Useful for word wrapping in multiboards and texttags.
*
*  The sizes might not be 100% accurate but are far more reliable
*  than using just StringLength().
*
*  Note that actual sizes may very depending on resolution.
*
*********************************************************************
*
*   */uses/*
*   
*       */ Ascii /*       https://wc3modding.info/4472/snippet-ascii/
*
*********************************************************************
*
*  function MeasureString takes string source returns real
*
*      - Measures the string and returns the calculated width.
*
*  function MeasureCharacter takes string char returns real
*
*      - Returns the width of an individual character.
*
*********************************************************************
*
*  struct StringSize
*
*      static method measure takes string source returns real
*
*      static method measureChar takes string char returns real
*
*********************************************************************
*
*  Credits
*
*    - Bob666 aka N-a-z-g-u-l for the character widths.
*    - Tukki for pointing out an error in the system.   
*
*********************************************************************/

    globals
        private real array size
    endglobals
   
    private module StringSizeModule
        static method onInit takes nothing returns nothing
            set size[124]  =  3
            set size[39]   =  4
            set size[58]   =  4
            set size[59]   =  4
            set size[46]   =  4
            set size[44]   =  4
            set size[49]   =  5
            set size[105]  =  5
            set size[33]   =  5
            set size[108]  =  6
            set size[73]   =  6
            set size[106]  =  6
            set size[40]   =  6
            set size[91]   =  6
            set size[93]   =  6
            set size[123]  =  6
            set size[125]  =  6
            set size[32]   =  7
            set size[34]   =  7
            set size[41]   =  7
            set size[74]   =  7
            set size[114]  =  8
            set size[102]  =  8
            set size[96]   =  8
            set size[116]  =  9
            set size[45]   =  9
            set size[92]   =  9
            set size[42]   =  9
            set size[70]   = 10
            set size[115]  = 11
            set size[47]   = 11
            set size[63]   = 11
            set size[69]   = 12
            set size[76]   = 12
            set size[55]   = 12
            set size[43]   = 12
            set size[61]   = 12
            set size[60]   = 12
            set size[62]   = 12
            set size[36]   = 12
            set size[97]   = 12
            set size[107]  = 13
            set size[84]   = 13
            set size[99]   = 13
            set size[83]   = 13
            set size[110]  = 13
            set size[122]  = 13
            set size[80]   = 13
            set size[51]   = 13
            set size[53]   = 13
            set size[95]   = 13
            set size[126]  = 13
            set size[94]   = 13
            set size[98]   = 14
            set size[66]   = 14
            set size[54]   = 14
            set size[118]  = 14
            set size[101]  = 14
            set size[120]  = 14
            set size[121]  = 14
            set size[50]   = 14
            set size[57]   = 14
            set size[104]  = 14
            set size[117]  = 14
            set size[111]  = 15
            set size[100]  = 15
            set size[48]   = 15
            set size[103]  = 15
            set size[56]   = 15
            set size[52]   = 15
            set size[113]  = 15
            set size[112]  = 15
            set size[115]  = 15
            set size[67]   = 16
            set size[82]   = 16
            set size[90]   = 16
            set size[86]   = 16
            set size[89]   = 16
            set size[68]   = 16
            set size[75]   = 16
            set size[85]   = 16
            set size[35]   = 16
            set size[78]   = 17
            set size[72]   = 17
            set size[37]   = 17
            set size[71]   = 18
            set size[88]   = 18
            set size[64]   = 18
            set size[65]   = 19
            set size[119]  = 20
            set size[79]   = 20
            set size[109]  = 21
            set size[81]   = 21
            set size[38]   = 21
            set size[77]   = 25
            set size[87]   = 26
        endmethod
    endmodule

    struct StringSize extends array
        static method measureChar takes string char returns real
            return size[Char2Ascii(char)]
        endmethod
       
        static method measure takes string s returns real
            local integer i = 0
            local integer l = StringLength(s)
            local real result = 0
            local string sub = ""
            if l == 0 then
                return 0.
            elseif l == 1 then
                return size[Char2Ascii(s)]
            endif
            loop
                exitwhen i >= l
                set sub = SubString(s, i, i+1)
                if sub == "|" then
                    set sub = SubString(s, i+1, i+2)
                    if sub == "c" then
                        set i = i + 9
                    elseif sub == "r" then
                        set i = i + 1
                    else
                        set result = result + size[124]
                    endif
                else
                    set result = result + size[Char2Ascii(sub)]
                endif
                set i = i + 1
            endloop
            return result
        endmethod
       
        implement StringSizeModule
    endstruct

    function MeasureString takes string s returns real
        return StringSize.measure(s)
    endfunction

    function MeasureCharacter takes string s returns real
        return StringSize.measureChar(s)
    endfunction

endlibrary



Some notes:
- I'm not really sure which is faster, hashtable saving or ascii conversion. However, I use ascii and I don't remember which characters are for which values since the old wc3jass went down.

EDIT: Updated with optimizations and fixed the compile errors.
13 years
I don't know why I haven't seen this library... hmmm, anyways, checked and nothing was away from usual so approved!!
13 years
Char2Ascii("|") will return a constant value :p
Replace it with 124:

Code (jass) Select
result = result + size[124 /* Char2Ascii("|") */]
13 years
Thanks, I forgot about that. Updated.

In fact, I can replace size[124] with the direct value since it is constant, but I'll do that later since I'm not on my modding comp.