Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

Letter width

Custom scripts
Forum rules
By using the forum you agree to the following rules.
Post Reply
android7
Fighter
Fighter
Posts: 21
Joined: Sat Aug 27, 2016 11:54 am

Letter width

Post by android7 » Fri Mar 04, 2022 10:40 pm

Hi
In case anyone needs it, here's a piece of code that will tell you how wide a piece of text is. Does NOT account for newlines.
Image

Code: Select all

        Dictionary<char, int> letter = new Dictionary<char, int>() { { ' ', 2 }, { '!', 2 }, { '"', 4 }, { '#', 6 }, { '$', 6 }, { '%', 6 }, { '&', 6 }, { '\'', 2 }, { '(', 3 }, { ')', 3 }, { '*', 6 }, { '+', 6 }, { ',', 3 }, { '-', 5 }, { '.', 2 }, { '/', 6 }, { '0', 5 }, { '1', 3 }, { '2', 5 }, { '3', 5 }, { '4', 5 }, { '5', 5 }, { '6', 5 }, { '7', 4 }, { '8', 5 }, { '9', 5 }, { ':', 2 }, { ';', 3 }, { '<', 3 }, { '=', 3 }, { '>', 3 }, { '?', 4 }, { '@', 5 }, { 'A', 5 }, { 'B', 5 }, { 'C', 5 }, { 'D', 5 }, { 'E', 5 }, { 'F', 5 }, { 'G', 5 }, { 'H', 5 }, { 'I', 4 }, { 'J', 4 }, { 'K', 5 }, { 'L', 4 }, { 'M', 6 }, { 'N', 5 }, { 'O', 5 }, { 'P', 5 }, { 'Q', 5 }, { 'R', 5 }, { 'S', 5 }, { 'T', 4 }, { 'U', 5 }, { 'V', 4 }, { 'W', 6 }, { 'X', 5 }, { 'Y', 4 }, { 'Z', 5 }, { '[', 3 }, { '\\', 6 }, { ']', 3 }, { '^', 4 }, { '_', 4 }, { '`', 3 }, { 'a', 4 }, { 'b', 4 }, { 'c', 4 }, { 'd', 4 }, { 'e', 4 }, { 'f', 3 }, { 'g', 4 }, { 'h', 4 }, { 'i', 2 }, { 'j', 3 }, { 'k', 4 }, { 'l', 2 }, { 'm', 6 }, { 'n', 4 }, { 'o', 4 }, { 'p', 4 }, { 'q', 4 }, { 'r', 3 }, { 's', 4 }, { 't', 4 }, { 'u', 4 }, { 'v', 4 }, { 'w', 6 }, { 'x', 4 }, { 'y', 4 }, { 'z', 4 }, { '{', 4 }, { '|', 2 }, { '}', 4 }, { '~', 5 }, { '', 1 }, { '€', 2 }, { '', 2 }, { '‚', 2 }, { 'ƒ', 6 }, { '„', 2 }, { '†', 4 }, { '‡', 4 }, { 'ˆ', 3 }, { '‰', 8 }, { 'Š', 5 }, { '‹', 3 }, { 'Œ', 9 }, { '', 2 }, { 'Ž', 2 }, { '', 2 }, { '', 2 }, { '‘', 2 }, { '’', 2 }, { '“', 4 }, { '”', 4 }, { '•', 2 }, { '–', 4 }, { '—', 9 }, { '˜', 3 }, { '™', 9 }, { 'š', 3 }, { '›', 3 }, { 'œ', 6 }, { '¡', 2 }, { '¢', 5 }, { '£', 6 }, { '¤', 7 }, { '¥', 6 }, { '¦', 2 }, { '§', 4 }, { '¨', 4 }, { '©', 5 }, { 'ª', 4 }, { '«', 6 }, { '¬', 5 }, { '­', 2 }, { '®', 6 }, { '¯', 3 }, { '°', 4 }, { '±', 4 }, { '²', 4 }, { '³', 4 }, { '´', 3 }, { 'µ', 6 }, { '¶', 6 }, { '·', 2 }, { '¸', 3 }, { '¹', 2 }, { 'º', 4 }, { '»', 6 }, { '¼', 8 }, { '½', 8 }, { '¾', 9 }, { '¿', 5 }, { 'À', 5 }, { 'Á', 5 }, { 'Â', 5 }, { 'Ã', 5 }, { 'Ä', 5 }, { 'Å', 5 }, { 'Æ', 6 }, { 'Ç', 5 }, { 'È', 5 }, { 'É', 5 }, { 'Ê', 5 }, { 'Ë', 5 }, { 'Ì', 4 }, { 'Í', 4 }, { 'Î', 4 }, { 'Ï', 4 }, { 'Ð', 5 }, { 'Ñ', 5 }, { 'Ò', 5 }, { 'Ó', 5 }, { 'Ô', 5 }, { 'Õ', 5 }, { 'Ö', 5 }, { '×', 4 }, { 'Ø', 6 }, { 'Ù', 5 }, { 'Ú', 5 }, { 'Û', 5 }, { 'Ü', 5 }, { 'Ý', 4 }, { 'Þ', 5 }, { 'ß', 5 }, { 'à', 4 }, { 'á', 4 }, { 'â', 4 }, { 'ã', 4 }, { 'ä', 4 }, { 'å', 4 }, { 'æ', 6 }, { 'ç', 5 }, { 'è', 4 }, { 'é', 4 }, { 'ê', 4 }, { 'ë', 4 }, { 'ì', 2 }, { 'í', 2 }, { 'î', 3 }, { 'ï', 3 }, { 'ð', 4 }, { 'ñ', 4 }, { 'ò', 4 }, { 'ó', 4 }, { 'ô', 4 }, { 'õ', 4 }, { 'ö', 4 }, { '÷', 4 }, { 'ø', 5 }, { 'ù', 4 }, { 'ú', 4 }, { 'û', 4 }, { 'ü', 4 }, { 'ý', 4 }, { 'þ', 4 }, { 'ÿ', 4 } };
        int GetTextWidth(string text)
        {
            int result = 0;
            foreach (char c in text)
            {
                result += letter[c];
            }
            return result;
        }
        
1 x

Post Reply