[SOLVED] Splitting text over multiple lines - (and some spout?)

Discussion in 'Plugin Development' started by randomman159, Nov 8, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    randomman159

    See posts 5 and 6 for the answer!


    Heres the original post:
     
  2. Offline

    Afforess

    I do not see any label.getWidth() in the javadocs. But there is a static GenericLabel.getStringWidget(String) you can use.
     
  3. Offline

    randomman159

    I can't seem to find that... ?

    Ok... well i'd going into it quite a bit... I found an image file in the minecraft.jar of spout, so i looked into the font there.

    Every symbol is down in the image with a width of 8 pixels. so i am assuming it works by checking how much of those 8 pixels wide the letter really is. Then in between letters, there is a 1 pixel gap. Space bar is a 4 pixel gap (excluding the spaces on either side of the character)

    more info coming

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  4. I dont know if this can help you but Bukkit already made a list of all characher widths here.
     
  5. Offline

    randomman159

    i am saved! ok thanks, looking through it now

    Thanks so much, i came up with a slightly modified version, simply removing the chat character limit, and made the width a parameter set when the function is called.



    THIS VERSION WILL SPLIT WORDS HALF WAY, INSTEAD OF MOVING THE WHOLE WORD TO THE NEXT LINE...

    Code:
    package modified.org.bukkit.craftbukkit;
    
    public class TextWrapper
    {
        private static final int[] characterWidths = new int[] {
            1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9,
            8, 8, 8, 8, 9, 9, 8, 9, 8, 8, 8, 8, 8, 9, 9, 9,
            4, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6,
            7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6,
            3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6,
            6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6,
            6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2, 6, 6,
            8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 9, 5, 9, 9,
            8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7,
            7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1
        };
    
        private static final String allowedChars = net.minecraft.server.FontAllowedCharacters.allowedCharacters;
    
        public static String[] wrapText(final String text, int CHAT_WINDOW_WIDTH)
        {
            final StringBuilder out = new StringBuilder();
            int lineWidth = 0;
    
            // Go over the message char by char.
            for (int i = 0; i < text.length(); i++)
            {
                char ch = text.charAt(i);
    
                // Figure out if it's allowed
                int index = allowedChars.indexOf(ch);
                if (index == -1)
                {
                    // Invalid character .. skip it.
                    continue;
                } else
                {
                    // Sadly needed as the allowedChars string misses the first
                    index += 32;
                }
    
                // Find the width
                final int width = characterWidths[index];
    
                // See if we need a linebreak
                if (lineWidth + width >= CHAT_WINDOW_WIDTH)
                {
                    out.append('\n');
    
                    lineWidth = width;
                }
                else
                {
                    lineWidth += width;
                }
                out.append(ch);
            }
    
            // Return it split
            return out.toString().split("\n");
        }
    }



    THIS VERSION WILL NICELY KEEP WORDS ON THE SAME LINE, UNLESS THE WORD IS LONGER THAN THE WIDTH OF THE BOX OF COURSE.


    Code:
    package randomman159.utils;
    
    public class TextWrapper
    {
        private static final int[] characterWidths = new int[] {
            1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9,
            8, 8, 8, 8, 9, 9, 8, 9, 8, 8, 8, 8, 8, 9, 9, 9,
            4, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6,
            7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6,
            3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6,
            6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6,
            6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2, 6, 6,
            8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 9, 5, 9, 9,
            8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7,
            7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1
        };
    
        private static final String allowedChars = net.minecraft.server.FontAllowedCharacters.allowedCharacters;
    
        public static String[] wrapText(final String text, int CHAT_WINDOW_WIDTH)
        {
            final StringBuilder out = new StringBuilder();
            int lineWidth = 0;
    
            String[] words = text.split(" ");
    
            for (String w : words)
            {
                int wordWidth = 0;
                String word = "";
                for (int i = 0; i < w.length(); i++)
                {
                    char ch = w.charAt(i);
    
                    int index = allowedChars.indexOf(ch);
    
                    if (index == -1){
                        continue;
                    }
                    else{
                        index += 32;
                    }
    
                    int width = characterWidths[index];
                    wordWidth += width;
    
                    word += ch;
                }
    
                if (wordWidth < CHAT_WINDOW_WIDTH)
                {
                    if (lineWidth + wordWidth >= CHAT_WINDOW_WIDTH)
                    {
                        out.append('\n');
                        lineWidth = wordWidth;
                    }
                    else
                    {
                        lineWidth += wordWidth;
                    }
                    out.append(word);
                }
                else
                {
                    for (int i = 0; i < w.length(); i++)
                    {
                        char ch = w.charAt(i);
    
                        // Figure out if it's allowed
                        int index = allowedChars.indexOf(ch);
                        if (index == -1){
                            continue;
                        }
                        else{
                            index += 32;
                        }
    
                        // Find the width
                        final int width = characterWidths[index];
    
                        // See if we need a linebreak
                        if (lineWidth + width >= CHAT_WINDOW_WIDTH)
                        {
                            out.append('\n');
    
                            lineWidth = width;
                        }
                        else
                        {
                            lineWidth += width;
                        }
                        out.append(ch);
                    }
                }
                out.append(" ");
            }
    
            System.out.println(out.toString());
            return out.toString().split("\n");
        }
    }
     
  6. Offline

    Afforess

    It's there. Likely your API is out of date.
     
  7. Offline

    randomman159

    wouldn't doubt it, ill update now then :)
     
Thread Status:
Not open for further replies.

Share This Page