コード例 #1
0
        private void parseFontXml(AsXML fontXml)
        {
            float       scale = mTexture.getScale();
            AsRectangle frame = mTexture.getFrame();

            mName       = fontXml.elements("info").attribute("face");
            mSize       = AsGlobal.parseFloat(fontXml.elements("info").attribute("size")) / scale;
            mLineHeight = AsGlobal.parseFloat(fontXml.elements("common").attribute("lineHeight")) / scale;
            mBaseline   = AsGlobal.parseFloat(fontXml.elements("common").attribute("base")) / scale;
            if (fontXml.elements("info").attribute("smooth").ToString() == "0")
            {
                setSmoothing(AsTextureSmoothing.NONE);
            }
            if (mSize <= 0)
            {
                AsGlobal.trace("[Starling] Warning: invalid font size in '" + mName + "' font.");
                mSize = mSize == 0.0f ? 16.0f : mSize * -1.0f;
            }
            AsXMLList __charElements_ = fontXml.elements("chars").elements("_char");

            if (__charElements_ != null)
            {
                foreach (AsXML charElement in __charElements_)
                {
                    int         id       = (int)(AsGlobal.parseInt(charElement.attribute("id")));
                    float       xOffset  = AsGlobal.parseFloat(charElement.attribute("xoffset")) / scale;
                    float       yOffset  = AsGlobal.parseFloat(charElement.attribute("yoffset")) / scale;
                    float       xAdvance = AsGlobal.parseFloat(charElement.attribute("xadvance")) / scale;
                    AsRectangle region   = new AsRectangle();
                    region.x      = AsGlobal.parseFloat(charElement.attribute("x")) / scale + frame.x;
                    region.y      = AsGlobal.parseFloat(charElement.attribute("y")) / scale + frame.y;
                    region.width  = AsGlobal.parseFloat(charElement.attribute("width")) / scale;
                    region.height = AsGlobal.parseFloat(charElement.attribute("height")) / scale;
                    AsTexture    texture    = AsTexture.fromTexture(mTexture, region);
                    AsBitmapChar bitmapChar = new AsBitmapChar(id, texture, xOffset, yOffset, xAdvance);
                    addChar(id, bitmapChar);
                }
            }
            AsXMLList __kerningElements_ = fontXml.elements("kernings").elements("kerning");

            if (__kerningElements_ != null)
            {
                foreach (AsXML kerningElement in __kerningElements_)
                {
                    int   first  = (int)(AsGlobal.parseInt(kerningElement.attribute("first")));
                    int   second = (int)(AsGlobal.parseInt(kerningElement.attribute("second")));
                    float amount = AsGlobal.parseFloat(kerningElement.attribute("amount")) / scale;
                    if (mChars.containsKey(second))
                    {
                        getChar(second).addKerning(first, amount);
                    }
                }
            }
        }
コード例 #2
0
 public AsCharLocation(AsBitmapChar _char)
 {
     this._char = _char;
 }
コード例 #3
0
 public AsCharLocation(AsBitmapChar _char)
 {
     this._char = _char;
 }
コード例 #4
0
 public virtual void addChar(int charID, AsBitmapChar bitmapChar)
 {
     mChars[charID] = bitmapChar;
 }
コード例 #5
0
 private void parseFontXml(AsXML fontXml)
 {
     float scale = mTexture.getScale();
     AsRectangle frame = mTexture.getFrame();
     mName = fontXml.elements("info").attribute("face");
     mSize = AsGlobal.parseFloat(fontXml.elements("info").attribute("size")) / scale;
     mLineHeight = AsGlobal.parseFloat(fontXml.elements("common").attribute("lineHeight")) / scale;
     mBaseline = AsGlobal.parseFloat(fontXml.elements("common").attribute("base")) / scale;
     if(fontXml.elements("info").attribute("smooth").ToString() == "0")
     {
         setSmoothing(AsTextureSmoothing.NONE);
     }
     if(mSize <= 0)
     {
         AsGlobal.trace("[Starling] Warning: invalid font size in '" + mName + "' font.");
         mSize = mSize == 0.0f ? 16.0f : mSize * -1.0f;
     }
     AsXMLList __charElements_ = fontXml.elements("chars").elements("_char");
     if (__charElements_ != null)
     {
         foreach (AsXML charElement in __charElements_)
         {
             int id = (int)(AsGlobal.parseInt(charElement.attribute("id")));
             float xOffset = AsGlobal.parseFloat(charElement.attribute("xoffset")) / scale;
             float yOffset = AsGlobal.parseFloat(charElement.attribute("yoffset")) / scale;
             float xAdvance = AsGlobal.parseFloat(charElement.attribute("xadvance")) / scale;
             AsRectangle region = new AsRectangle();
             region.x = AsGlobal.parseFloat(charElement.attribute("x")) / scale + frame.x;
             region.y = AsGlobal.parseFloat(charElement.attribute("y")) / scale + frame.y;
             region.width = AsGlobal.parseFloat(charElement.attribute("width")) / scale;
             region.height = AsGlobal.parseFloat(charElement.attribute("height")) / scale;
             AsTexture texture = AsTexture.fromTexture(mTexture, region);
             AsBitmapChar bitmapChar = new AsBitmapChar(id, texture, xOffset, yOffset, xAdvance);
             addChar(id, bitmapChar);
         }
     }
     AsXMLList __kerningElements_ = fontXml.elements("kernings").elements("kerning");
     if (__kerningElements_ != null)
     {
         foreach (AsXML kerningElement in __kerningElements_)
         {
             int first = (int)(AsGlobal.parseInt(kerningElement.attribute("first")));
             int second = (int)(AsGlobal.parseInt(kerningElement.attribute("second")));
             float amount = AsGlobal.parseFloat(kerningElement.attribute("amount")) / scale;
             if(mChars.containsKey(second))
             {
                 getChar(second).addKerning(first, amount);
             }
         }
     }
 }
コード例 #6
0
        private AsVector <AsCharLocation> arrangeChars(float width, float height, String text, float fontSize, String hAlign, String vAlign, bool autoScale, bool kerning)
        {
            if (text == null || text.Length == 0)
            {
                return(new AsVector <AsCharLocation>());
            }
            if (fontSize < 0)
            {
                fontSize = fontSize * -mSize;
            }
            AsVector <AsVector <AsCharLocation> > lines = null;
            bool           finished        = false;
            AsCharLocation charLocation    = null;
            int            numChars        = 0;
            float          containerWidth  = 0;
            float          containerHeight = 0;
            float          scale           = 0;

            while (!finished)
            {
                scale           = fontSize / mSize;
                containerWidth  = width / scale;
                containerHeight = height / scale;
                lines           = new AsVector <AsVector <AsCharLocation> >();
                if (mLineHeight <= containerHeight)
                {
                    int   lastWhiteSpace = -1;
                    int   lastCharID     = -1;
                    float currentX       = 0;
                    float currentY       = 0;
                    AsVector <AsCharLocation> currentLine = new AsVector <AsCharLocation>();
                    numChars = text.Length;
                    int i = 0;
                    for (; i < numChars; ++i)
                    {
                        bool         lineFull = false;
                        int          charID   = (int)(AsString.charCodeAt(text, i));
                        AsBitmapChar _char    = getChar(charID);
                        if (charID == CHAR_NEWLINE || charID == CHAR_CARRIAGE_RETURN)
                        {
                            lineFull = true;
                        }
                        else
                        {
                            if (_char == null)
                            {
                                AsGlobal.trace("[Starling] Missing character: " + charID);
                            }
                            else
                            {
                                if (charID == CHAR_SPACE || charID == CHAR_TAB)
                                {
                                    lastWhiteSpace = i;
                                }
                                if (kerning)
                                {
                                    currentX = currentX + _char.getKerning(lastCharID);
                                }
                                charLocation       = mCharLocationPool.getLength() != 0 ? mCharLocationPool.pop() : new AsCharLocation(_char);
                                charLocation._char = _char;
                                charLocation.x     = currentX + _char.getXOffset();
                                charLocation.y     = currentY + _char.getYOffset();
                                currentLine.push(charLocation);
                                currentX   = currentX + _char.getXAdvance();
                                lastCharID = charID;
                                if (currentLine.getLength() == 1)
                                {
                                    currentX       = currentX - _char.getXOffset();
                                    charLocation.x = charLocation.x - _char.getXOffset();
                                }
                                if (charLocation.x + _char.getWidth() > containerWidth)
                                {
                                    int numCharsToRemove = lastWhiteSpace == -1 ? 1 : i - lastWhiteSpace;
                                    int removeIndex      = (int)(currentLine.getLength() - numCharsToRemove);
                                    currentLine.splice(removeIndex, (uint)(numCharsToRemove));
                                    if (currentLine.getLength() == 0)
                                    {
                                        break;
                                    }
                                    i        = i - numCharsToRemove;
                                    lineFull = true;
                                }
                            }
                        }
                        if (i == numChars - 1)
                        {
                            lines.push(currentLine);
                            finished = true;
                        }
                        else
                        {
                            if (lineFull)
                            {
                                lines.push(currentLine);
                                if (lastWhiteSpace == i)
                                {
                                    currentLine.pop();
                                }
                                if (currentY + 2 * mLineHeight <= containerHeight)
                                {
                                    currentLine    = new AsVector <AsCharLocation>();
                                    currentX       = 0;
                                    currentY       = currentY + mLineHeight;
                                    lastWhiteSpace = -1;
                                    lastCharID     = -1;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                if (autoScale && !finished)
                {
                    fontSize = fontSize - 1;
                    lines.setLength(0);
                }
                else
                {
                    finished = true;
                }
            }
            AsVector <AsCharLocation> finalLocations = new AsVector <AsCharLocation>();
            int   numLines = (int)(lines.getLength());
            float bottom   = currentY + mLineHeight;
            int   yOffset  = 0;

            if (vAlign == AsVAlign.BOTTOM)
            {
                yOffset = (int)(containerHeight - bottom);
            }
            else
            {
                if (vAlign == AsVAlign.CENTER)
                {
                    yOffset = (int)((containerHeight - bottom) / 2);
                }
            }
            int lineID = 0;

            for (; lineID < numLines; ++lineID)
            {
                AsVector <AsCharLocation> line = lines[lineID];
                numChars = (int)(line.getLength());
                if (numChars == 0)
                {
                    continue;
                }
                AsCharLocation lastLocation = line[line.getLength() - 1];
                float          right        = lastLocation.x + lastLocation._char.getWidth();
                int            xOffset      = 0;
                if (hAlign == AsHAlign.RIGHT)
                {
                    xOffset = (int)(containerWidth - right);
                }
                else
                {
                    if (hAlign == AsHAlign.CENTER)
                    {
                        xOffset = (int)((containerWidth - right) / 2);
                    }
                }
                int c = 0;
                for (; c < numChars; ++c)
                {
                    charLocation       = line[c];
                    charLocation.x     = scale * (charLocation.x + xOffset);
                    charLocation.y     = scale * (charLocation.y + yOffset);
                    charLocation.scale = scale;
                    if (charLocation._char.getWidth() > 0 && charLocation._char.getHeight() > 0)
                    {
                        finalLocations.push(charLocation);
                    }
                    mCharLocationPool.push(charLocation);
                }
            }
            return(finalLocations);
        }
コード例 #7
0
 public virtual void addChar(int charID, AsBitmapChar bitmapChar)
 {
     mChars[charID] = bitmapChar;
 }