Example #1
0
    private CreateTextGeometryResult createTextGeometry(Vector3 position, string text, int startIndex, int length, int quadStartIndex, bool lineIsIssuedFromLineCut, float scaleFactor, float lineMaxWidth, bool preserveWords)
    {
        CreateTextGeometryResult result = new CreateTextGeometryResult();

        if (lineIsIssuedFromLineCut)
        {
            // trim spaces at start
            while ((startIndex < startIndex + length) && ((text[startIndex] == 0x20) || (text[startIndex] == 0xA0)))
            {
                startIndex++;
                length--;
                result.processedCharCount++;
                result.spaceRemovedAtBeginning++;
            }
        }

        //      float pageWidth = font.getRegion ().getRegionWidth ();
        //      float pageHeight = font.getRegion ().getRegionHeight ();
        //      Glyph glyph = null;
        CharacterInfo glyph;
        // Font.fontChar letterStruct = null;
        Vector3 letterPosition      = new Vector3();
        Vector2 letterDimension     = new Vector2();
        Vector2 letterTopLeftUV     = new Vector2();
        Vector2 letterTopRightUV    = new Vector2();
        Vector2 letterBottomLeftUV  = new Vector2();
        Vector2 letterBottomRightUV = new Vector2();
        float   advance             = 0;
        int     lastSpaceIndex      = startIndex;

        for (int i = startIndex; i < startIndex + length; i++)
        {
            char charToRender = text[i];
            if (charToRender == 0xA0)
            {
                charToRender = (char)0x20;                 // convert unbreakable space into space
            }
            if (!font.GetCharacterInfo(charToRender, out glyph))
            {
#if UNITY_EDITOR
                if (charToRender != 0x0D)
                {
                    if (errorMessage == null)
                    {
                        errorMessage = "";
                    }

                    errorMessage += "Font character unknown : '" + charToRender + "' (0x" + ((int)charToRender).ToString("X2") + "), used in text \"" + text + "\"" + "\n";
                    // Debug.LogWarning ( "Font character unknown : '" + charToRender + "' (0x" + ( (int)charToRender ).ToString ( "X2" ) + "), used in text \"" + text + "\"" );
                    if (!font.GetCharacterInfo(' ', out glyph))
                    {
                        errorMessage += "Font default replacement character [space] not defined.";
                    }
                    // Debug.LogError ( "Font default replacement character [space] not defined." );
                }
#endif
            }

            //          glyph = font.getData ().getGlyph ( charToRender );
            //          if (glyph == null)
            //              // throw new RuntimeException ( "Character '" + charToRender + "' is not defined in font" );
            //              glyph = font.getData ().getGlyph ( ' ' );

            int kerning = 0;             // kerning to add
            // if (i > 0)
            // {
            // char previousCharToRender = text.charAt ( i - 1 );
            // if (previousCharToRender == 0xA0)
            // previousCharToRender = (char) 0x20; // convert unbreakable space into space
            //
            // font.kerningTable.TryGetValue ( new KeyValuePair<char, char> ( previousCharToRender, charToRender ), out kerning );
            // }

            if (charToRender == 0x20)
            {
                lastSpaceIndex = i;                 // keep index of last space
//
//              // space special case
//              letterPosition.x = advance + kerning + glyph.minX;
//              letterPosition.y = ( glyph.minY + glyph.maxY - glyph.minY );
//              letterDimension.x = (float)glyph.maxX - glyph.minX;
//              letterDimension.y = glyph.maxY - glyph.minY; // 0.0f;
//              letterTopLeftUV = glyph.uvTopLeft;
//              letterTopRightUV = glyph.uvTopRight;
//              letterBottomLeftUV = glyph.uvBottomLeft;
//              letterBottomRightUV = glyph.uvBottomRight;
            }
//          else
            {
                letterPosition.x    = advance + kerning + glyph.minX;
                letterPosition.y    = (glyph.minY + glyph.maxY - glyph.minY);
                letterDimension.x   = glyph.maxX - glyph.minX;
                letterDimension.y   = -(glyph.maxY - glyph.minY);
                letterTopLeftUV     = glyph.uvTopLeft;
                letterTopRightUV    = glyph.uvTopRight;
                letterBottomLeftUV  = glyph.uvBottomLeft;
                letterBottomRightUV = glyph.uvBottomRight;
            }

            letterPosition  *= scaleFactor;            // apply scaling
            letterDimension *= scaleFactor;

            // check if text is over the max line width
            if (letterPosition.x + letterDimension.x > lineMaxWidth)
            {
                // generation is not finished, return count of processed characters
                if (preserveWords && (lastSpaceIndex > startIndex))
                {
                    // cut where the last space char was found, will truncate and discard all chars after
                    result.processedCharCount -= (i - lastSpaceIndex);
                    result.renderedCharCount  -= (i - lastSpaceIndex);
                    break;
                }
                else
                {
                    break;                     // line break in the middle of a word
                }
            }

            letterPosition += position;             // then offset by position

            // if ( charToRender != 0x20 )
            quadBatch.setQuad(quadStartIndex + result.renderedCharCount, letterPosition, letterDimension, letterTopLeftUV, letterTopRightUV, letterBottomLeftUV, letterBottomRightUV, color);
//              if ( glyph.flipped )
//                  quadBatch.flipUvs ( quadStartIndex + result.renderedCharCount );

            if (letterSpacingMode == LetterSpacingMode.Add)
            {
                advance += (glyph.advance + letterSpacing.x);
            }
            else
            {
                advance += letterSpacing.x;
            }

            result.processedCharCount++;
            result.renderedCharCount++;
        }

        return(result);
    }