[Library] TextDraw String Width - Printable Version + open.mp forum (https://forum.open.mp) -- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3) --- Forum: Releases (https://forum.open.mp/forumdisplay.php?fid=13) ---- Forum: Libraries (https://forum.open.mp/forumdisplay.php?fid=31) ---- Thread: [Library] TextDraw String Width (/showthread.php?tid=293) |
TextDraw String Width - kristo - 2019-04-16 SA-MP TextDraw String Width About two years ago an user called Alcatrik came up with a concept of calculating the width of textdraw strings in this thread. In those two years a few functions using this concept appeared in that thread, but all of them had major flaws. The first one by Crayder didn?t include the first 32 characters of the ASCII table. The next one by Freaksten fixed that flaw, but neither of the functions also didn?t take into account that there are four different textdraw fonts and just used the first array in fonts.dat. About two or three months ago I needed a correct version of that function so I started digging into the data in fonts.dat. The first discovery I made is that fonts 2 and 3 are actually subfonts of fonts 0 and 1 (fun fact: you can actually access the characters of fonts 2 and 3 from fonts 0 and 1 by using values larger than 175 as characters). I manually reassembled the data into four arrays, but soon after that I temporarily abandoned the project I collected this data for. A few days ago I mentioned the data I had collected and Y_Less sent me a repository that contained a function that generated the same dataset automatically. I decided to compare the two datasets and found out that they had different widths for 16 characters, which I re-measured manually. Both got about half of the 16 widths right and half of them wrong. Another mistake that all existing functions did was presuming that characters 1-31 are always rendered as regular whitespace. In reality, the widths of the invisible characters differed in font 2 and the characters in font 3 actually have two different widths. One of them is the visual width: most of the characters don?t actually render as whitespace, instead of this you see either one or two stripes with varying widths. The other width is the ?physical? width, which is either 0 (the stripes overlap with the next character or two) or a relatively large number, up to 255. Now, after multiple days of work that contained manually reassembling data, manually measuring almost 100 characters and a lot more, this library is ready. What could it be used for? I personally needed this to calculate the width of buttons based on their strings. The previously mentioned repository uses code similar to this to achieve full colouring of textdraws. A few more usages can be found from the original topic explaining the concept. Installation Simply install to your project: Code: sampctl package install kristoisberg/samp-td-string-width Include in your code and begin using the library: PHP Code: #include <td-string-width> Usage
To test, simply run the package: Code: sampctl package run RE: TextDraw String Width - Markski - 2019-04-16 This is great, I was looking for something like this to display large blocks of text in a limited space not too long ago. RE: TextDraw String Width - mr_sacrimoni - 2019-04-17 This is absolutely great. Scripting for a guy on my community (before it was mine) I had an assignment creating a function doing something similair to this and I can't tell you how much hours I've lost trying to get all possible cases. Thank you, rep up |