Search in sources :

Example 1 with Glyph

use of maspack.render.TextImageStore.Glyph in project artisynth_core by artisynth.

the class TextureTextRenderer method draw.

/**
 * Draws a set of glyphs directly, without modifying the underlying text
 * storage texture
 *
 * @param queue
 * glyphs to draw
 * @return unfinished queues
 */
protected ArrayList<GlyphQueue> draw(ArrayList<GlyphQueue> queue) {
    ArrayList<GlyphQueue> remaining = new ArrayList<GlyphQueue>(queue.size());
    // first ones we can render without changing the texture
    for (GlyphQueue gq : queue) {
        int s = gq.remaining;
        Glyph[] glyphs = gq.glyphqueue;
        int skipped = 0;
        for (int i = 0; i < s; ++i) {
            GlyphStore store = textstore.get(glyphs[i]);
            if (store == null) {
                // add back to queue
                glyphs[skipped++] = glyphs[i];
            } else {
                render(store, gq.origin, gq.scale);
            }
        }
        gq.remaining = skipped;
        if (skipped > 0) {
            remaining.add(gq);
        }
    }
    return remaining;
}
Also used : GlyphStore(maspack.render.TextImageStore.GlyphStore) Glyph(maspack.render.TextImageStore.Glyph) ArrayList(java.util.ArrayList)

Example 2 with Glyph

use of maspack.render.TextImageStore.Glyph in project artisynth_core by artisynth.

the class TextureTextRenderer method drawText.

/**
 * Renders text with a given font at a provided baseline location.
 * @param font font to use.  Note that the font's size controls the
 * resolution of the glyphs in the backing texture.  The final drawn
 * font size can be controlled using {@code emsize}.  Rendering
 * might not take place immediately.  This class creates a queue
 * to minimize the number of required changes to a backing texture.
 * To force a render, use {@link #flush()}.
 *
 * @param text text to draw
 * @param loc baseline location to start drawing
 * @param emsize size of an 'em' unit
 * @return the advance distance in the x-direction (width of text)
 */
@Override
public float drawText(Font font, String text, float[] loc, float emsize) {
    GlyphVector vec = textstore.createGlyphVector(font, text);
    Glyph[] glyphs = textstore.createGlyphs(vec, null);
    // determine origin and scale
    float[] origin = new float[3];
    origin[0] = loc[0];
    origin[1] = loc[1];
    if (loc.length > 2) {
        origin[2] = loc[2];
    }
    // scale distances by this number, font-size independent
    float scale = emsize / font.getSize();
    queue.add(new GlyphQueue(glyphs, origin, scale));
    return (float) (vec.getGlyphPosition(vec.getNumGlyphs()).getX() * scale);
}
Also used : GlyphVector(java.awt.font.GlyphVector) Glyph(maspack.render.TextImageStore.Glyph)

Example 3 with Glyph

use of maspack.render.TextImageStore.Glyph in project artisynth_core by artisynth.

the class TextureTextRenderer method flush.

/**
 * Draw characters to screen now
 */
public void flush() {
    queue = draw(queue);
    // now try to upload any that were missing
    while (queue.size() > 0) {
        // upload as many as we can before we start drawing
        for (GlyphQueue gq : queue) {
            int s = gq.remaining;
            Glyph[] glyphs = gq.glyphqueue;
            for (int i = 0; i < s; ++i) {
                textstore.upload(glyphs[i]);
            }
        }
        // update texture
        if (textureUploaded) {
            Rectangle dirty = textstore.getDirty();
            ByteBuffer buff = BufferUtilities.newNativeByteBuffer(dirty.area() * 4);
            textstore.getData(dirty, buff);
            buff.flip();
            replaceSubTexture(dirty.x(), dirty.y(), dirty.width(), dirty.height(), buff);
            textstore.markClean();
            buff = BufferUtilities.freeDirectBuffer(buff);
        } else {
            ByteBuffer buff = BufferUtilities.newNativeByteBuffer(textstore.getWidth() * textstore.getHeight() * 4);
            textstore.getData(buff);
            buff.flip();
            replaceTexture(textstore.getWidth(), textstore.getHeight(), buff);
            textstore.markClean();
            buff = BufferUtilities.freeDirectBuffer(buff);
            textureUploaded = true;
        }
        // draw
        queue = draw(queue);
        // again
        if (queue.size() > 0) {
            setPreferredSize(textstore.getWidth() * 2, textstore.getHeight() * 2);
            textstore.clear();
        }
    }
}
Also used : Glyph(maspack.render.TextImageStore.Glyph) Rectangle(maspack.util.Rectangle) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Glyph (maspack.render.TextImageStore.Glyph)3 GlyphVector (java.awt.font.GlyphVector)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 GlyphStore (maspack.render.TextImageStore.GlyphStore)1 Rectangle (maspack.util.Rectangle)1