Example #1
0
        public virtual AsSprite createSprite(float width, float height, String text, float fontSize, uint color, String hAlign, String vAlign, bool autoScale, bool kerning)
        {
            AsVector <AsCharLocation> charLocations = arrangeChars(width, height, text, fontSize, hAlign, vAlign, autoScale, kerning);
            int      numChars = (int)(charLocations.getLength());
            AsSprite sprite   = new AsSprite();
            int      i        = 0;

            for (; i < numChars; ++i)
            {
                AsCharLocation charLocation = charLocations[i];
                AsImage        _char        = charLocation._char.createImage();
                _char.setX(charLocation.x);
                _char.setY(charLocation.y);
                _char.setScaleX(_char.setScaleY(charLocation.scale));
                _char.setColor(color);
                sprite.addChild(_char);
            }
            return(sprite);
        }
Example #2
0
        public virtual void fillQuadBatch(AsQuadBatch quadBatch, float width, float height, String text, float fontSize, uint color, String hAlign, String vAlign, bool autoScale, bool kerning)
        {
            AsVector <AsCharLocation> charLocations = arrangeChars(width, height, text, fontSize, hAlign, vAlign, autoScale, kerning);
            int numChars = (int)(charLocations.getLength());

            mHelperImage.setColor(color);
            if (numChars > 8192)
            {
                throw new AsArgumentError("Bitmap Font text is limited to 8192 characters.");
            }
            int i = 0;

            for (; i < numChars; ++i)
            {
                AsCharLocation charLocation = charLocations[i];
                mHelperImage.setTexture(charLocation._char.getTexture());
                mHelperImage.readjustSize();
                mHelperImage.setX(charLocation.x);
                mHelperImage.setY(charLocation.y);
                mHelperImage.setScaleX(mHelperImage.setScaleY(charLocation.scale));
                quadBatch.addImage(mHelperImage);
            }
        }
Example #3
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);
        }