Search in sources :

Example 1 with GlyphRun

use of com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun in project libgdx by libgdx.

the class BitmapFontCache method addToCache.

private void addToCache(GlyphLayout layout, float x, float y) {
    // Check if the number of font pages has changed.
    int pageCount = font.regions.size;
    if (pageVertices.length < pageCount) {
        float[][] newPageVertices = new float[pageCount][];
        System.arraycopy(pageVertices, 0, newPageVertices, 0, pageVertices.length);
        pageVertices = newPageVertices;
        int[] newIdx = new int[pageCount];
        System.arraycopy(idx, 0, newIdx, 0, idx.length);
        idx = newIdx;
        IntArray[] newPageGlyphIndices = new IntArray[pageCount];
        int pageGlyphIndicesLength = 0;
        if (pageGlyphIndices != null) {
            pageGlyphIndicesLength = pageGlyphIndices.length;
            System.arraycopy(pageGlyphIndices, 0, newPageGlyphIndices, 0, pageGlyphIndices.length);
        }
        for (int i = pageGlyphIndicesLength; i < pageCount; i++) newPageGlyphIndices[i] = new IntArray();
        pageGlyphIndices = newPageGlyphIndices;
        tempGlyphCount = new int[pageCount];
    }
    layouts.add(layout);
    requireGlyphs(layout);
    for (int i = 0, n = layout.runs.size; i < n; i++) {
        GlyphRun run = layout.runs.get(i);
        Array<Glyph> glyphs = run.glyphs;
        FloatArray xAdvances = run.xAdvances;
        float color = run.color.toFloatBits();
        float gx = x + run.x, gy = y + run.y;
        for (int ii = 0, nn = glyphs.size; ii < nn; ii++) {
            Glyph glyph = glyphs.get(ii);
            gx += xAdvances.get(ii);
            addGlyph(glyph, gx, gy, color);
        }
    }
    // Cached glyphs have changed, reset the current tint.
    currentTint = whiteTint;
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) IntArray(com.badlogic.gdx.utils.IntArray) GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun) Glyph(com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)

Example 2 with GlyphRun

use of com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun in project libgdx by libgdx.

the class BitmapFontTest method render.

@Override
public void render() {
    // red.a = (red.a + Gdx.graphics.getDeltaTime() * 0.1f) % 1;
    int viewHeight = Gdx.graphics.getHeight();
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    // Test wrapping or truncation with the font directly.
    if (!true) {
        // BitmapFont font = label.getStyle().font;
        BitmapFont font = this.font;
        font.getRegion().getTexture().setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
        font.getData().setScale(2f);
        renderer.begin(ShapeRenderer.ShapeType.Line);
        renderer.setColor(0, 1, 0, 1);
        float w = Gdx.input.getX();
        // w = 855;
        renderer.rect(10, 10, w, 500);
        renderer.end();
        spriteBatch.begin();
        String text = "your new";
        text = "How quickly [RED]daft jumping zebras vex.";
        // text = "Another font wrap is-sue, this time with    multiple whitespace characters.";
        text = "test with AGWlWi      AGWlWi issue";
        if (true) {
            // Test wrap.
            layout.setText(font, text, 0, text.length(), font.getColor(), w, Align.center, true, null);
        } else {
            // Test truncation.
            layout.setText(font, text, 0, text.length(), font.getColor(), w, Align.center, false, "...");
        }
        float meowy = (500 / 2 + layout.height / 2 + 5);
        font.draw(spriteBatch, layout, 10, 10 + meowy);
        spriteBatch.end();
        renderer.begin(ShapeRenderer.ShapeType.Line);
        renderer.setColor(0, 1, 0, 1);
        for (int i = 0, n = layout.runs.size; i < n; i++) {
            GlyphRun r = layout.runs.get(i);
            renderer.rect(10 + r.x, 10 + meowy + r.y, r.width, -font.getLineHeight());
        }
        renderer.end();
        font.getData().setScale(1f);
        return;
    }
    // Test wrapping with label.
    if (false) {
        label.debug();
        label.getStyle().font = font;
        label.setStyle(label.getStyle());
        label.setText("How quickly [RED]daft[] jumping zebras vex.");
        label.setWrap(true);
        //			label.setEllipsis(true);
        label.setAlignment(Align.center, Align.right);
        label.setWidth(Gdx.input.getX() - label.getX());
        label.setHeight(label.getPrefHeight());
    } else {
        // Test various font features.
        spriteBatch.begin();
        String text = "Sphinx of black quartz, judge my vow.";
        font.setColor(Color.RED);
        float x = 100, y = 20;
        float alignmentWidth;
        if (false) {
            alignmentWidth = 0;
            font.draw(spriteBatch, text, x, viewHeight - y, alignmentWidth, Align.right, false);
        }
        if (true) {
            alignmentWidth = 280;
            font.draw(spriteBatch, text, x, viewHeight - y, alignmentWidth, Align.right, true);
        }
        font.draw(spriteBatch, "[", 50, 60, 100, Align.left, true);
        font.getData().markupEnabled = true;
        font.draw(spriteBatch, "[", 100, 60, 100, Align.left, true);
        font.getData().markupEnabled = false;
        // 'R' and 'p' are in different pages
        String txt2 = "this font uses " + multiPageFont.getRegions().size + " texture pages: RpRpRpRpRpNM";
        spriteBatch.renderCalls = 0;
        // regular draw function
        multiPageFont.setColor(Color.BLUE);
        multiPageFont.draw(spriteBatch, txt2, 10, 100);
        // expert usage.. drawing with bitmap font cache
        BitmapFontCache cache = multiPageFont.getCache();
        cache.clear();
        cache.setColor(Color.BLACK);
        cache.setText(txt2, 10, 50);
        cache.setColors(Color.PINK, 3, 6);
        cache.setColors(Color.ORANGE, 9, 12);
        cache.setColors(Color.GREEN, 16, txt2.length());
        cache.draw(spriteBatch, 5, txt2.length() - 5);
        cache.clear();
        cache.setColor(Color.BLACK);
        float textX = 10;
        textX += cache.setText("[black] ", textX, 150).width;
        multiPageFont.getData().markupEnabled = true;
        textX += cache.addText("[[[PINK]pink[]] ", textX, 150).width;
        textX += cache.addText("[PERU][[peru] ", textX, 150).width;
        cache.setColor(Color.GREEN);
        textX += cache.addText("green ", textX, 150).width;
        textX += cache.addText("[#A52A2A]br[#A52A2ADF]ow[#A52A2ABF]n f[#A52A2A9F]ad[#A52A2A7F]in[#A52A2A5F]g o[#A52A2A3F]ut ", textX, 150).width;
        multiPageFont.getData().markupEnabled = false;
        cache.draw(spriteBatch);
        // tinting
        cache.tint(new Color(1f, 1f, 1f, 0.3f));
        cache.translate(0f, 40f);
        cache.draw(spriteBatch);
        spriteBatch.end();
        // System.out.println(spriteBatch.renderCalls);
        renderer.begin(ShapeType.Line);
        renderer.setColor(Color.BLACK);
        renderer.rect(x, viewHeight - y - 200, alignmentWidth, 200);
        renderer.end();
    }
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}
Also used : GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun) Color(com.badlogic.gdx.graphics.Color) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) BitmapFontCache(com.badlogic.gdx.graphics.g2d.BitmapFontCache)

Example 3 with GlyphRun

use of com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun in project libgdx by libgdx.

the class BitmapFontCache method tint.

/** Tints all text currently in the cache. Does not affect subsequently added text. */
public void tint(Color tint) {
    float newTint = tint.toFloatBits();
    if (currentTint == newTint)
        return;
    currentTint = newTint;
    int[] tempGlyphCount = this.tempGlyphCount;
    for (int i = 0, n = tempGlyphCount.length; i < n; i++) tempGlyphCount[i] = 0;
    for (int i = 0, n = layouts.size; i < n; i++) {
        GlyphLayout layout = layouts.get(i);
        for (int ii = 0, nn = layout.runs.size; ii < nn; ii++) {
            GlyphRun run = layout.runs.get(ii);
            Array<Glyph> glyphs = run.glyphs;
            float colorFloat = tempColor.set(run.color).mul(tint).toFloatBits();
            for (int iii = 0, nnn = glyphs.size; iii < nnn; iii++) {
                Glyph glyph = glyphs.get(iii);
                int page = glyph.page;
                int offset = tempGlyphCount[page] * 20 + 2;
                tempGlyphCount[page]++;
                float[] vertices = pageVertices[page];
                for (int v = 0; v < 20; v += 5) vertices[offset + v] = colorFloat;
            }
        }
    }
}
Also used : GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun) Glyph(com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)

Example 4 with GlyphRun

use of com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun in project libgdx by libgdx.

the class TextField method updateDisplayText.

void updateDisplayText() {
    BitmapFont font = style.font;
    BitmapFontData data = font.getData();
    String text = this.text;
    int textLength = text.length();
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < textLength; i++) {
        char c = text.charAt(i);
        buffer.append(data.hasGlyph(c) ? c : ' ');
    }
    String newDisplayText = buffer.toString();
    if (passwordMode && data.hasGlyph(passwordCharacter)) {
        if (passwordBuffer == null)
            passwordBuffer = new StringBuilder(newDisplayText.length());
        if (passwordBuffer.length() > textLength)
            passwordBuffer.setLength(textLength);
        else {
            for (int i = passwordBuffer.length(); i < textLength; i++) passwordBuffer.append(passwordCharacter);
        }
        displayText = passwordBuffer;
    } else
        displayText = newDisplayText;
    layout.setText(font, displayText);
    glyphPositions.clear();
    float x = 0;
    if (layout.runs.size > 0) {
        GlyphRun run = layout.runs.first();
        FloatArray xAdvances = run.xAdvances;
        fontOffset = xAdvances.first();
        for (int i = 1, n = xAdvances.size; i < n; i++) {
            glyphPositions.add(x);
            x += xAdvances.get(i);
        }
    } else
        fontOffset = 0;
    glyphPositions.add(x);
    if (selectionStart > newDisplayText.length())
        selectionStart = textLength;
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 5 with GlyphRun

use of com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun in project libgdx by libgdx.

the class UnicodeFont method drawBitmap.

private void drawBitmap(String text, int startIndex, int endIndex) {
    BitmapFontData data = bitmapFont.getData();
    int padY = paddingTop + paddingBottom + paddingAdvanceY;
    data.setLineHeight(data.lineHeight + padY);
    layout.setText(bitmapFont, text);
    data.setLineHeight(data.lineHeight - padY);
    for (GlyphRun run : layout.runs) for (int i = 0, n = run.xAdvances.size; i < n; i++) run.xAdvances.incr(i, paddingAdvanceX + paddingLeft + paddingRight);
    cache.setText(layout, paddingLeft, paddingRight);
    Array<TextureRegion> regions = bitmapFont.getRegions();
    for (int i = 0, n = regions.size; i < n; i++) {
        regions.get(i).getTexture().bind();
        GL11.glBegin(GL11.GL_QUADS);
        float[] vertices = cache.getVertices(i);
        for (int ii = 0, nn = vertices.length; ii < nn; ii += 20) {
            GL11.glTexCoord2f(vertices[ii + U], vertices[ii + V]);
            GL11.glVertex3f(vertices[ii + X], vertices[ii + Y], 0);
            GL11.glTexCoord2f(vertices[ii + U], vertices[ii + V2]);
            GL11.glVertex3f(vertices[ii + X], vertices[ii + Y2], 0);
            GL11.glTexCoord2f(vertices[ii + U2], vertices[ii + V2]);
            GL11.glVertex3f(vertices[ii + X2], vertices[ii + Y2], 0);
            GL11.glTexCoord2f(vertices[ii + U2], vertices[ii + V]);
            GL11.glVertex3f(vertices[ii + X2], vertices[ii + Y], 0);
        }
        GL11.glEnd();
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun)

Aggregations

GlyphRun (com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun)5 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)2 Glyph (com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)2 FloatArray (com.badlogic.gdx.utils.FloatArray)2 Color (com.badlogic.gdx.graphics.Color)1 BitmapFontCache (com.badlogic.gdx.graphics.g2d.BitmapFontCache)1 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)1 IntArray (com.badlogic.gdx.utils.IntArray)1