From 440ab0e3cc43c667ededd07fa9d0f8d588c27e80 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Wed, 24 Jul 2013 01:36:58 +0200 Subject: [PATCH] desktop: improve text rendering --- .../src/org/oscim/awt/AwtBitmap.java | 44 +++++++++++++++---- .../src/org/oscim/awt/AwtCanvas.java | 20 +++------ .../src/org/oscim/awt/AwtPaint.java | 10 ++--- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/vtm-gdx-desktop/src/org/oscim/awt/AwtBitmap.java b/vtm-gdx-desktop/src/org/oscim/awt/AwtBitmap.java index fb30e3df..811da75b 100644 --- a/vtm-gdx-desktop/src/org/oscim/awt/AwtBitmap.java +++ b/vtm-gdx-desktop/src/org/oscim/awt/AwtBitmap.java @@ -1,6 +1,7 @@ package org.oscim.awt; import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.IntBuffer; @@ -18,12 +19,16 @@ public class AwtBitmap implements Bitmap { int width; int height; + boolean internal; + public AwtBitmap(int width, int height, int format) { bitmap = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); this.width = width; this.height = height; - if (!this.bitmap.isAlphaPremultiplied()) - this.bitmap.coerceData(true); + + internal = true; + // if (!this.bitmap.isAlphaPremultiplied()) + // this.bitmap.coerceData(true); } AwtBitmap(InputStream inputStream) throws IOException { @@ -31,8 +36,9 @@ public class AwtBitmap implements Bitmap { this.bitmap = ImageIO.read(inputStream); this.width = this.bitmap.getWidth(); this.height = this.bitmap.getHeight(); - if (!this.bitmap.isAlphaPremultiplied() && this.bitmap.getType() == BufferedImage.TYPE_INT_ARGB) - this.bitmap.coerceData(true); + // if (!this.bitmap.isAlphaPremultiplied() + // && this.bitmap.getType() == BufferedImage.TYPE_INT_ARGB) + // this.bitmap.coerceData(true); } @Override @@ -57,6 +63,9 @@ public class AwtBitmap implements Bitmap { private static IntBuffer tmpBuffer = BufferUtils.newIntBuffer(512 * 256); private static int[] tmpPixel = new int[512 * 256]; + private final static boolean WRITE_TEX = false; + private int dbgCnt; + @Override public int uploadToTexture(boolean replace) { int[] pixels; @@ -74,17 +83,36 @@ public class AwtBitmap implements Bitmap { // FIXME dont convert to argb when there data is greyscale bitmap.getRGB(0, 0, width, height, pixels, 0, width); - for (int i = 0, n = width * height; i < n; i++){ + if (WRITE_TEX) { + try { + boolean ok = ImageIO.write(bitmap, "png", new File("texture_" + dbgCnt + ".png")); + System.out.println("write tex " + ok + " " + dbgCnt); + dbgCnt++; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + for (int i = 0, n = width * height; i < n; i++) { int c = pixels[i]; - // flip blue with red - silly Java - pixels[i] = (c & 0xff00ff00) | (c & 0x00ff0000) >>> 16 | (c & 0x000000ff) << 16; + if (internal) { + float alpha = (c >>> 24) / 255f; + int r = (int) ((c & 0x000000ff) * alpha); + int b = (int) (((c & 0x00ff0000) >>> 16) * alpha); + int g = (int) (((c & 0x0000ff00) >>> 8) * alpha); + pixels[i] = (c & 0xff000000) | r << 16 | g << 8 | b; + } else { + // flip blue with red - silly Java + pixels[i] = (c & 0xff00ff00) | (c & 0x00ff0000) >>> 16 | (c & 0x000000ff) << 16; + } } buffer.put(pixels, 0, width * height); buffer.flip(); Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL20.GL_RGBA, width, - height, 0, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, buffer); + height, 0, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, buffer); return 0; } diff --git a/vtm-gdx-desktop/src/org/oscim/awt/AwtCanvas.java b/vtm-gdx-desktop/src/org/oscim/awt/AwtCanvas.java index 816b3a0f..221c4c66 100644 --- a/vtm-gdx-desktop/src/org/oscim/awt/AwtCanvas.java +++ b/vtm-gdx-desktop/src/org/oscim/awt/AwtCanvas.java @@ -43,22 +43,17 @@ public class AwtCanvas implements Canvas { AwtBitmap awtBitamp = (AwtBitmap)bitmap; canvas = awtBitamp.bitmap.createGraphics(); - //awtBitamp.bitmap. - //bitmap.eraseColor(); - canvas.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0)); - //canvas.setBackground(new Color(1,1,1,1)); - canvas.setColor(Color.BLACK); - //Gdx.app.log("set bitmap ", bitmap + " "+ bitmap.getWidth() + " " +bitmap.getHeight()); - canvas.fillRect(0,0,bitmap.getWidth(),bitmap.getHeight()); - //canvas.clearRect(0, 0, bitmap.getWidth(),bitmap.getHeight()); + canvas.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0)); + canvas.fillRect(0, 0, bitmap.getWidth(), bitmap.getHeight()); canvas.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); canvas.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON ); //canvas.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); - //canvas.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); - //canvas.setRenderingHint(RenderingHints.KEY_KERNING, RenderingHints.VALUE_RENDER_QUALITY); + canvas.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + canvas.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + } @@ -76,13 +71,12 @@ public class AwtCanvas implements Canvas { if (awtPaint.stroke == null) { canvas.setColor(awtPaint.color); canvas.setFont(awtPaint.font); - canvas.drawString(text, x, y); + canvas.drawString(text, x + 2, y); } else { setColorAndStroke(awtPaint); - TextLayout textLayout = new TextLayout(text, awtPaint.font, canvas.getFontRenderContext()); AffineTransform affineTransform = new AffineTransform(); - affineTransform.translate(x, y); + affineTransform.translate(x + 2, y); canvas.draw(textLayout.getOutline(affineTransform)); } } diff --git a/vtm-gdx-desktop/src/org/oscim/awt/AwtPaint.java b/vtm-gdx-desktop/src/org/oscim/awt/AwtPaint.java index 58b24565..eafa121b 100644 --- a/vtm-gdx-desktop/src/org/oscim/awt/AwtPaint.java +++ b/vtm-gdx-desktop/src/org/oscim/awt/AwtPaint.java @@ -47,8 +47,8 @@ public class AwtPaint implements Paint { static { Map textAttributes = new HashMap(); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); - textAttributes.put(TextAttribute.FAMILY, "SansSerif"); - textAttributes.put(TextAttribute.SIZE, 13); + textAttributes.put(TextAttribute.FAMILY, "Arial"); + textAttributes.put(TextAttribute.SIZE, 14); defaultFont = Font.getFont(textAttributes); } @@ -126,7 +126,7 @@ public class AwtPaint implements Paint { @Override public void setTextSize(float textSize) { - font = font.deriveFont(textSize - 4); + font = font.deriveFont(textSize - 2); } @@ -143,7 +143,7 @@ public class AwtPaint implements Paint { float w = AwtGraphics.getTextWidth(fm, text); //Gdx.app.log("text width:", text + " " + w); - return w; + return w + 4; // return fm.getStringBounds(text, A).getWidth(); // return AwtGraphics.getTextWidth(fm, text); // return fm.stringWidth(text); @@ -173,6 +173,6 @@ public class AwtPaint implements Paint { if (strokeWidth <= 0) { return; } - stroke = new BasicStroke(strokeWidth, cap, BasicStroke.JOIN_ROUND, 0, null, 0); + stroke = new BasicStroke(strokeWidth, cap, BasicStroke.JOIN_MITER, 1, null, 0); } }