Search in sources :

Example 1 with FontException

use of org.andengine.opengl.font.exception.FontException in project AndEngine by nicolasgramlich.

the class Font method createLetter.

private Letter createLetter(final char pCharacter) throws FontException {
    final String characterAsString = String.valueOf(pCharacter);
    final float textureWidth = this.mTextureWidth;
    final float textureHeight = this.mTextureHeight;
    this.updateTextBounds(characterAsString);
    final int letterLeft = this.mTextBounds.left;
    final int letterTop = this.mTextBounds.top;
    final int letterWidth = this.mTextBounds.width();
    final int letterHeight = this.mTextBounds.height();
    final Letter letter;
    final float advance = this.getLetterAdvance(characterAsString);
    final boolean whitespace = Character.isWhitespace(pCharacter) || (letterWidth == 0) || (letterHeight == 0);
    if (whitespace) {
        letter = new Letter(pCharacter, advance);
    } else {
        if ((this.mCurrentTextureX + Font.LETTER_TEXTURE_PADDING + letterWidth) >= textureWidth) {
            this.mCurrentTextureX = 0;
            this.mCurrentTextureY += this.mCurrentTextureYHeightMax + (2 * Font.LETTER_TEXTURE_PADDING);
            this.mCurrentTextureYHeightMax = 0;
        }
        if ((this.mCurrentTextureY + letterHeight) >= textureHeight) {
            throw new FontException("Not enough space for " + Letter.class.getSimpleName() + ": '" + pCharacter + "' on the " + this.mTexture.getClass().getSimpleName() + ". Existing Letters: " + SparseArrayUtils.toString(this.mManagedCharacterToLetterMap));
        }
        this.mCurrentTextureYHeightMax = Math.max(letterHeight, this.mCurrentTextureYHeightMax);
        this.mCurrentTextureX += Font.LETTER_TEXTURE_PADDING;
        final float u = this.mCurrentTextureX / textureWidth;
        final float v = this.mCurrentTextureY / textureHeight;
        final float u2 = (this.mCurrentTextureX + letterWidth) / textureWidth;
        final float v2 = (this.mCurrentTextureY + letterHeight) / textureHeight;
        letter = new Letter(pCharacter, this.mCurrentTextureX - Font.LETTER_TEXTURE_PADDING, this.mCurrentTextureY - Font.LETTER_TEXTURE_PADDING, letterWidth, letterHeight, letterLeft, letterTop - this.getAscent(), advance, u, v, u2, v2);
        this.mCurrentTextureX += letterWidth + Font.LETTER_TEXTURE_PADDING;
    }
    return letter;
}
Also used : FontException(org.andengine.opengl.font.exception.FontException) Paint(android.graphics.Paint)

Example 2 with FontException

use of org.andengine.opengl.font.exception.FontException in project AndEngine by nicolasgramlich.

the class BitmapFont method parseCharacters.

private void parseCharacters(final int pCharacterCount, final BufferedReader pBufferedReader) throws IOException {
    for (int i = pCharacterCount - 1; i >= 0; i--) {
        final String character = pBufferedReader.readLine();
        final String[] charAttributes = TextUtils.SPLITPATTERN_SPACES.split(character, BitmapFont.TAG_CHAR_ATTRIBUTECOUNT + 1);
        if ((charAttributes.length - 1) != BitmapFont.TAG_CHAR_ATTRIBUTECOUNT) {
            throw new FontException("Expected: '" + BitmapFont.TAG_CHAR_ATTRIBUTECOUNT + "' " + BitmapFont.TAG_CHAR + " attributes, found: '" + (charAttributes.length - 1) + "'.");
        }
        if (!charAttributes[0].equals(BitmapFont.TAG_CHAR)) {
            throw new FontException("Expected: '" + BitmapFont.TAG_CHAR + "' attributes.");
        }
        final char id = BitmapFont.getCharAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_ID_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_ID);
        final int x = this.mBitmapFontOptions.mTextureOffsetX + BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_X_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_X);
        final int y = this.mBitmapFontOptions.mTextureOffsetY + BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_Y_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_Y);
        final int width = BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_WIDTH_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_WIDTH);
        final int height = BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_HEIGHT_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_HEIGHT);
        final int xOffset = BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_XOFFSET_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_XOFFSET);
        final int yOffset = BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_YOFFSET_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_YOFFSET);
        final int xAdvance = BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_XADVANCE_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_XADVANCE);
        final int page = BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_PAGE_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_PAGE);
        // final int channel = BitmapFont.getIntAttribute(charAttributes, BitmapFont.TAG_CHAR_ATTRIBUTE_CHANNEL_INDEX, BitmapFont.TAG_CHAR_ATTRIBUTE_CHANNEL);
        final ITexture bitmapFontPageTexture = this.mBitmapFontPages[page].getTexture();
        final float textureWidth = bitmapFontPageTexture.getWidth();
        final float textureHeight = bitmapFontPageTexture.getHeight();
        final float u = x / textureWidth;
        final float v = y / textureHeight;
        final float u2 = (x + width) / textureWidth;
        final float v2 = (y + height) / textureHeight;
        this.mCharacterToLetterMap.put(id, new Letter(id, x, y, width, height, xOffset, yOffset, xAdvance, u, v, u2, v2));
    }
}
Also used : FontException(org.andengine.opengl.font.exception.FontException) ITexture(org.andengine.opengl.texture.ITexture)

Aggregations

FontException (org.andengine.opengl.font.exception.FontException)2 Paint (android.graphics.Paint)1 ITexture (org.andengine.opengl.texture.ITexture)1