/** * Creates a text block with the supplied {@code lines}. */ public TextBlock (TextLayout[] lines) { this.lines = lines; this.bounds = getBounds(lines, new Rectangle()); }
@Override public void init() { String text = "The quick brown fox jumped over the lazy dog."; TextFormat format = new TextFormat(new Font("Helvetica", 18)); TextBlock block = new TextBlock(game.graphics.layoutText(text, format, new TextWrap(100))); float x = 5; for (float scale : new float[] { 1f, 2f, 3f }) { float swidth = block.bounds.width() * scale, sheight = block.bounds.height() * scale; Canvas canvas = game.graphics.createCanvas(swidth, sheight); canvas.setStrokeColor(0xFFFFCCCC).strokeRect(0, 0, swidth-0.5f, sheight-0.5f); canvas.scale(scale, scale); canvas.setFillColor(0xFF000000); block.fill(canvas, TextBlock.Align.RIGHT, 0, 0); game.rootLayer.addAt(new ImageLayer(canvas.toTexture()), x, 5); addInfo(canvas, x + swidth/2, sheight + 10); x += swidth + 5; } }
/** * Creates a canvas image large enough to accommodate this text block and renders the lines into * it. The image will include padding around the edge to ensure that antialiasing has a bit of * extra space to do its work. */ public Canvas toCanvas(Graphics gfx, Align align, int fillColor) { float pad = 1/gfx.scale().factor; Canvas canvas = gfx.createCanvas(bounds.width()+2*pad, bounds.height()+2*pad); canvas.setFillColor(fillColor); fill(canvas, align, pad, pad); return canvas; } }
/** * Fills {@code lines} into {@code canvas} at the specified coordinates, using the specified * alignment. */ public void fill(Canvas canvas, Align align, float x, float y) { float sy = y + bounds.y(); for (TextLayout line : lines) { float sx = x + bounds.x() + align.getX(line.size.width(), textWidth()); canvas.fillText(line, sx, sy); sy += line.ascent() + line.descent() + line.leading(); } }
protected Texture makeTextImage() { TextFormat format = new TextFormat(new Font(font.value(), style.value(), 24)); float wrapWidth = wrap.value() == 0 ? Float.MAX_VALUE : game.graphics.viewSize.width()*wrap.value()/100; TextBlock block = new TextBlock( game.graphics.layoutText(sample, format, new TextWrap(wrapWidth))); float awidth = adjustWidth(block.bounds.width()), aheight = adjustHeight(block.bounds.height()); float pad = 1/game.graphics.scale().factor; Canvas canvas = game.graphics.createCanvas(awidth+2*pad, aheight+2*pad); canvas.translate(pad, pad); canvas.setStrokeColor(0xFFFFCCCC).strokeRect(0, 0, awidth, aheight); render(canvas, block, align.value(), lineBounds.value()); return canvas.toTexture(); }
/** * Creates a canvas image large enough to accommodate this text block and renders the lines into * it. The image will include padding around the edge to ensure that antialiasing has a bit of * extra space to do its work. */ public Canvas toCanvas(Graphics gfx, Align align, int fillColor) { float pad = 1/gfx.scale().factor; Canvas canvas = gfx.createCanvas(bounds.width()+2*pad, bounds.height()+2*pad); canvas.setFillColor(fillColor); fill(canvas, align, pad, pad); return canvas; } }
/** * Strokes {@code lines} into {@code canvas} at the specified coordinates, using the specified * alignment. */ public void stroke(Canvas canvas, Align align, float x, float y) { float sy = y + bounds.y(); for (TextLayout line : lines) { float sx = x + bounds.x() + align.getX(line.size.width(), textWidth()); canvas.strokeText(line, sx, sy); sy += line.ascent() + line.descent() + line.leading(); } }
/** * Creates a text block with the supplied {@code lines}. */ public TextBlock (TextLayout[] lines) { this.lines = lines; this.bounds = getBounds(lines, new Rectangle()); }
/** * Strokes {@code lines} into {@code canvas} at the specified coordinates, using the specified * alignment. */ public void stroke(Canvas canvas, Align align, float x, float y) { float sy = y + bounds.y(); for (TextLayout line : lines) { float sx = x + bounds.x() + align.getX(line.size.width(), textWidth()); canvas.strokeText(line, sx, sy); sy += line.ascent() + line.descent() + line.leading(); } }
public Block (Graphics gfx, String text, TextStyle style, TextWrap wrap, TextBlock.Align align) { super(gfx, text, style); assert wrap != null && align != null; this.wrap = wrap; this.align = align; _layouts = gfx.layoutText(text, style, wrap); _bounds = TextBlock.getBounds(_layouts, new Rectangle()); _bounds.width = style.effect.adjustWidth(_bounds.width); _bounds.height = style.effect.adjustHeight(_bounds.height); }
/** * Fills {@code lines} into {@code canvas} at the specified coordinates, using the specified * alignment. */ public void fill(Canvas canvas, Align align, float x, float y) { float sy = y + bounds.y(); for (TextLayout line : lines) { float sx = x + bounds.x() + align.getX(line.size.width(), textWidth()); canvas.fillText(line, sx, sy); sy += line.ascent() + line.descent() + line.leading(); } }