コード例 #1
0
        void CreateLabelTextureWrapped()
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            // set a local maxWidth that compensates for textScale
            int maxWidth = (int)(this.maxWidth / textScale);

            // First pass encodes ASCII and calculates final dimensions
            int width = 0;
            int greatestWidthFound = 0;
            int lastEndOfRowByte   = 0;

            asciiBytes = Encoding.ASCII.GetBytes(text);
            List <byte[]> rows     = new List <byte[]>();
            List <int>    rowWidth = new List <int>();

            for (int i = 0; i < asciiBytes.Length; i++)
            {
                // Invalid ASCII bytes are cast to a space character
                if (!font.HasGlyph(asciiBytes[i]))
                {
                    asciiBytes[i] = PixelFont.SpaceASCII;
                }

                // Calculate total width
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);

                // If maxWidth is set, don't allow the label texture to exceed it
                if ((maxWidth <= 0) || ((width + glyph.width + font.GlyphSpacing) <= maxWidth))
                {
                    width += glyph.width + font.GlyphSpacing;
                }
                else
                {
                    int rowLength;
                    if (wrapWords)
                    {
                        int j;
                        for (j = i; j >= lastEndOfRowByte; j--)
                        {
                            glyph = font.GetGlyph(asciiBytes[j]);
                            if (j < i) // glyph i has not been added to width
                            {
                                width -= glyph.width + font.GlyphSpacing;
                                if (width <= maxWidth && asciiBytes[j] == PixelFont.SpaceASCII)
                                {
                                    break;
                                }
                            }
                        }

                        if (j > lastEndOfRowByte)             // space found in row at position that is not exceeding maxWidth for all summed glyph's widths before this position
                        {
                            i         = j;                    // set new processing position (j is the space position, i will be increased on next loop iteration to point to next chat after the space char)
                            rowLength = j - lastEndOfRowByte; // set length from row start to position before space
                        }
                        else
                        {
                            rowLength = i - lastEndOfRowByte;
                        }

                        // compute width of text-wrapped line
                        width = 0;
                        for (int k = lastEndOfRowByte; k < j; k++)
                        {
                            if (k < j - 1 || (k == j - 1 && asciiBytes[k] != PixelFont.SpaceASCII)) // all expect last character if it is a space
                            {
                                glyph  = font.GetGlyph(asciiBytes[k]);
                                width += glyph.width + font.GlyphSpacing;
                            }
                        }
                    }
                    else
                    {
                        rowLength = i - lastEndOfRowByte;
                    }
                    // The row of glyphs exceeded maxWidth. Add it to the list of rows and start
                    // counting width again with the remainder of the ASCII bytes.
                    List <byte> content = new List <byte>(asciiBytes).GetRange(lastEndOfRowByte, rowLength);
                    if (content[content.Count - 1] == PixelFont.SpaceASCII)
                    {
                        content.RemoveAt(content.Count - 1);
                    }
                    byte[] trimmed = content.ToArray();

                    rows.Add(trimmed);
                    rowWidth.Add(width);

                    // update greatest width found so far
                    if (greatestWidthFound < width)
                    {
                        greatestWidthFound = width;
                    }

                    // reset width for next line
                    width = 0;

                    // Resume interation over remainder of ASCII bytes
                    //asciiBytes = new List<byte>(asciiBytes).GetRange(i, asciiBytes.Length - i).ToArray();
                    //i = 0;

                    lastEndOfRowByte = i + 1; // position after space for next line, note: i will be increased on next loop iteration anyway - so no need to increase i
                }
            }

            if (lastEndOfRowByte > 0)
            {
                asciiBytes = new List <byte>(asciiBytes).GetRange(lastEndOfRowByte, asciiBytes.Length - lastEndOfRowByte).ToArray();
            }

            // also get width of last line
            width = 0;
            for (int i = 0; i < asciiBytes.Length; i++)
            {
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                width += glyph.width + font.GlyphSpacing;
            }

            // update greatest width found so far
            if (width <= maxWidth && greatestWidthFound < width) // width should always be <= maxWidth here
            {
                greatestWidthFound = width;
            }

            rows.Add(asciiBytes);
            rowWidth.Add(width);

            //width = greatestWidthFound;

            // Destroy old texture
            if (labelTexture)
            {
                UnityEngine.Object.Destroy(labelTexture);
            }

            // Create target label texture
            totalWidth   = maxWidth;
            totalHeight  = (int)(rows.Count * font.GlyphHeight);
            numTextLines = rows.Count;
            labelTexture = CreateLabelTexture(totalWidth, totalHeight);
            if (labelTexture == null)
            {
                throw new Exception("TextLabel failed to create labelTexture.");
            }

            // Second pass adds glyphs to label texture
            int xpos = 0;
            int ypos = totalHeight - font.GlyphHeight;

            //foreach (byte[] row in rows)
            for (int r = 0; r < rows.Count; r++)
            {
                byte[] row = rows[r];
                float  alignmentOffset;
                switch (horizontalTextAlignment)
                {
                default:
                case HorizontalTextAlignmentSetting.None:
                case HorizontalTextAlignmentSetting.Left:
                case HorizontalTextAlignmentSetting.Justify:
                    alignmentOffset = 0.0f;
                    break;

                case HorizontalTextAlignmentSetting.Center:
                    alignmentOffset = (totalWidth - rowWidth[r]) * 0.5f;
                    break;

                case HorizontalTextAlignmentSetting.Right:
                    alignmentOffset = totalWidth - rowWidth[r];
                    break;
                }

                int numSpaces = 0;              // needed to compute extra offset between words for HorizontalTextAlignmentSetting.Justify
                //int widthTextCharacters = 0; // needed to compute extra offset between words for HorizontalTextAlignmentSetting.Justify
                int extraSpaceToDistribute = 0; // needed to compute extra offset between words for HorizontalTextAlignmentSetting.Justify
                if (horizontalTextAlignment == HorizontalTextAlignmentSetting.Justify)
                {
                    for (int i = 0; i < row.Length; i++)
                    {
                        if (row[i] == PixelFont.SpaceASCII)
                        {
                            numSpaces++;
                        }
                        //else
                        //widthTextCharacters += font.GetGlyph(row[i]).width + font.GlyphSpacing;
                    }

                    extraSpaceToDistribute = maxWidth - rowWidth[r]; // +numSpaces * font.GetGlyph(PixelFont.SpaceASCII).width + font.GlyphSpacing;
                }

                xpos = (int)alignmentOffset;
                for (int i = 0; i < row.Length; i++)
                {
                    PixelFont.GlyphInfo glyph = font.GetGlyph(row[i]);
                    if (xpos + glyph.width > totalWidth)
                    {
                        break;
                    }

                    if (row[i] == PixelFont.SpaceASCII)
                    {
                        if (numSpaces > 1)
                        {
                            int currentPortionExtraSpaceToDistribute = (int)Mathf.Round((float)extraSpaceToDistribute / (float)numSpaces);
                            xpos += currentPortionExtraSpaceToDistribute;
                            extraSpaceToDistribute -= currentPortionExtraSpaceToDistribute;
                            numSpaces--;
                        }
                        else if (numSpaces == 1)
                        {
                            xpos += extraSpaceToDistribute;
                        }
                    }

                    labelTexture.SetPixels32(xpos, ypos, glyph.width, font.GlyphHeight, glyph.colors);
                    xpos += glyph.width + font.GlyphSpacing;
                }
                ypos -= font.GlyphHeight;
            }

            labelTexture.Apply(false, makeTextureNoLongerReadable);
            labelTexture.filterMode = font.FilterMode;
            this.Size = new Vector2(totalWidth * textScale, totalHeight * textScale);
        }
コード例 #2
0
        void CreateLabelTextureSingleLine()
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            // set a local maxWidth that compensates for textScale
            int maxWidth = (int)(this.maxWidth / textScale);

            // First pass encodes ASCII and calculates final dimensions
            int width = 0;

            asciiBytes = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("ISO-8859-1"), Encoding.Default.GetBytes(text));
            for (int i = startCharacterIndex; i < asciiBytes.Length; i++)
            {
                // Invalid ASCII bytes are cast to a space character
                if (!font.HasGlyph(asciiBytes[i]))
                {
                    asciiBytes[i] = PixelFont.SpaceASCII;
                }

                // Calculate total width
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                width += glyph.width + font.GlyphSpacing;
            }

            if (maxWidth > 0 && width > maxWidth)
            {
                width = maxWidth;
            }

            // Destroy old texture
            if (labelTexture)
            {
                UnityEngine.Object.Destroy(labelTexture);
            }

            // Create target label texture
            totalWidth   = width;
            totalHeight  = (int)(font.GlyphHeight);
            numTextLines = 1;
            labelTexture = CreateLabelTexture(totalWidth, totalHeight);
            if (labelTexture == null)
            {
                throw new Exception("TextLabel failed to create labelTexture.");
            }

            float alignmentOffset;

            switch (horizontalTextAlignment)
            {
            default:
            case HorizontalTextAlignmentSetting.None:
            case HorizontalTextAlignmentSetting.Left:
            case HorizontalTextAlignmentSetting.Justify:
                alignmentOffset = 0.0f;
                break;

            case HorizontalTextAlignmentSetting.Center:
                alignmentOffset = (totalWidth - width) * 0.5f;
                break;

            case HorizontalTextAlignmentSetting.Right:
                alignmentOffset = totalWidth - width;
                break;
            }

            // Second pass adds glyphs to label texture
            int xpos = (int)alignmentOffset;

            for (int i = startCharacterIndex; i < asciiBytes.Length; i++)
            {
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                if (xpos + glyph.width >= totalWidth)
                {
                    break;
                }

                labelTexture.SetPixels32(xpos, 0, glyph.width, totalHeight, glyph.colors);
                xpos += glyph.width + font.GlyphSpacing;
            }
            labelTexture.Apply(false, makeTextureNoLongerReadable);
            labelTexture.filterMode = font.FilterMode;
            this.Size = new Vector2(totalWidth * textScale, totalHeight * textScale);
        }