14 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.
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.
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.
1 library StringSize /* v2.1.0.0
2 ********************************************************************3 *4 * This library can calculate the width of a string in pixels.5 * Useful for word wrapping in multiboards and texttags. 6 *7 * The sizes might not be 100% accurate but are far more reliable8 * than using just StringLength().9 *10 * Note that actual sizes may very depending on resolution.11 *12 *********************************************************************13 *14 * */uses/*
15 * 16 * */ Ascii /* https://wc3modding.info/4472/snippet-ascii/
17 *18 *********************************************************************19 *20 * function MeasureString takes string source returns real21 *22 * - Measures the string and returns the calculated width.23 *24 * function MeasureCharacter takes string char returns real25 *26 * - Returns the width of an individual character.27 *28 *********************************************************************29 *30 * struct StringSize31 *32 * static method measure takes string source returns real33 *34 * static method measureChar takes string char returns real35 *36 *********************************************************************37 *38 * Credits39 *40 * - Bob666 aka N-a-z-g-u-l for the character widths.41 * - Tukki for pointing out an error in the system. 42 *43 *********************************************************************/44 45 globals46 private real array size
47 endglobals48
49 private module StringSizeModule
50 static method onInit takes nothing returns nothing
51 set size[124] = 3
52 set size[39] = 4
53 set size[58] = 4
54 set size[59] = 4
55 set size[46] = 4
56 set size[44] = 4
57 set size[49] = 5
58 set size[105] = 5
59 set size[33] = 5
60 set size[108] = 6
61 set size[73] = 6
62 set size[106] = 6
63 set size[40] = 6
64 set size[91] = 6
65 set size[93] = 6
66 set size[123] = 6
67 set size[125] = 6
68 set size[32] = 7
69 set size[34] = 7
70 set size[41] = 7
71 set size[74] = 7
72 set size[114] = 8
73 set size[102] = 8
74 set size[96] = 8
75 set size[116] = 9
76 set size[45] = 9
77 set size[92] = 9
78 set size[42] = 9
79 set size[70] = 10
80 set size[115] = 11
81 set size[47] = 11
82 set size[63] = 11
83 set size[69] = 12
84 set size[76] = 12
85 set size[55] = 12
86 set size[43] = 12
87 set size[61] = 12
88 set size[60] = 12
89 set size[62] = 12
90 set size[36] = 12
91 set size[97] = 12
92 set size[107] = 13
93 set size[84] = 13
94 set size[99] = 13
95 set size[83] = 13
96 set size[110] = 13
97 set size[122] = 13
98 set size[80] = 13
99 set size[51] = 13
100 set size[53] = 13
101 set size[95] = 13
102 set size[126] = 13
103 set size[94] = 13
104 set size[98] = 14
105 set size[66] = 14
106 set size[54] = 14
107 set size[118] = 14
108 set size[101] = 14
109 set size[120] = 14
110 set size[121] = 14
111 set size[50] = 14
112 set size[57] = 14
113 set size[104] = 14
114 set size[117] = 14
115 set size[111] = 15
116 set size[100] = 15
117 set size[48] = 15
118 set size[103] = 15
119 set size[56] = 15
120 set size[52] = 15
121 set size[113] = 15
122 set size[112] = 15
123 set size[115] = 15
124 set size[67] = 16
125 set size[82] = 16
126 set size[90] = 16
127 set size[86] = 16
128 set size[89] = 16
129 set size[68] = 16
130 set size[75] = 16
131 set size[85] = 16
132 set size[35] = 16
133 set size[78] = 17
134 set size[72] = 17
135 set size[37] = 17
136 set size[71] = 18
137 set size[88] = 18
138 set size[64] = 18
139 set size[65] = 19
140 set size[119] = 20
141 set size[79] = 20
142 set size[109] = 21
143 set size[81] = 21
144 set size[38] = 21
145 set size[77] = 25
146 set size[87] = 26
147 endmethod148 endmodule149 150 struct StringSize extends array
151 static method measureChar takes string char returns real
152 return size[Char2Ascii(char)]
153 endmethod154
155 static method measure takes string s returns real
156 local integer i = 0
157 local integer l = StringLength(s)
158 local real result = 0
159 local string sub = ""
160 if l == 0 then
161 return 0.
162 elseif l == 1 then
163 return size[Char2Ascii(s)]
164 endif165 loop166 exitwhen i >= l
167 set sub = SubString(s, i, i+1)
168 if sub == "|" then
169 set sub = SubString(s, i+1, i+2)
170 if sub == "c" then
171 set i = i + 9
172 elseif sub == "r" then
173 set i = i + 1
174 else175 set result = result + size[124]
176 endif177 else178 set result = result + size[Char2Ascii(sub)]
179 endif180 set i = i + 1
181 endloop182 return result183 endmethod184
185 implement StringSizeModule186 endstruct187 188 function MeasureString takes string s returns real
189 return StringSize.measure(s)
190 endfunction191 192 function MeasureCharacter takes string s returns real
193 return StringSize.measureChar(s)
194 endfunction195 196 endlibrary197 198
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.