desktop: improve text rendering
This commit is contained in:
parent
7ade22d917
commit
440ab0e3cc
@ -1,6 +1,7 @@
|
|||||||
package org.oscim.awt;
|
package org.oscim.awt;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
@ -18,12 +19,16 @@ public class AwtBitmap implements Bitmap {
|
|||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
|
|
||||||
|
boolean internal;
|
||||||
|
|
||||||
public AwtBitmap(int width, int height, int format) {
|
public AwtBitmap(int width, int height, int format) {
|
||||||
bitmap = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
bitmap = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
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 {
|
AwtBitmap(InputStream inputStream) throws IOException {
|
||||||
@ -31,8 +36,9 @@ public class AwtBitmap implements Bitmap {
|
|||||||
this.bitmap = ImageIO.read(inputStream);
|
this.bitmap = ImageIO.read(inputStream);
|
||||||
this.width = this.bitmap.getWidth();
|
this.width = this.bitmap.getWidth();
|
||||||
this.height = this.bitmap.getHeight();
|
this.height = this.bitmap.getHeight();
|
||||||
if (!this.bitmap.isAlphaPremultiplied() && this.bitmap.getType() == BufferedImage.TYPE_INT_ARGB)
|
// if (!this.bitmap.isAlphaPremultiplied()
|
||||||
this.bitmap.coerceData(true);
|
// && this.bitmap.getType() == BufferedImage.TYPE_INT_ARGB)
|
||||||
|
// this.bitmap.coerceData(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -57,6 +63,9 @@ public class AwtBitmap implements Bitmap {
|
|||||||
private static IntBuffer tmpBuffer = BufferUtils.newIntBuffer(512 * 256);
|
private static IntBuffer tmpBuffer = BufferUtils.newIntBuffer(512 * 256);
|
||||||
private static int[] tmpPixel = new int[512 * 256];
|
private static int[] tmpPixel = new int[512 * 256];
|
||||||
|
|
||||||
|
private final static boolean WRITE_TEX = false;
|
||||||
|
private int dbgCnt;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int uploadToTexture(boolean replace) {
|
public int uploadToTexture(boolean replace) {
|
||||||
int[] pixels;
|
int[] pixels;
|
||||||
@ -74,11 +83,30 @@ public class AwtBitmap implements Bitmap {
|
|||||||
// FIXME dont convert to argb when there data is greyscale
|
// FIXME dont convert to argb when there data is greyscale
|
||||||
bitmap.getRGB(0, 0, width, height, pixels, 0, width);
|
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];
|
int c = pixels[i];
|
||||||
|
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
|
// flip blue with red - silly Java
|
||||||
pixels[i] = (c & 0xff00ff00) | (c & 0x00ff0000) >>> 16 | (c & 0x000000ff) << 16;
|
pixels[i] = (c & 0xff00ff00) | (c & 0x00ff0000) >>> 16 | (c & 0x000000ff) << 16;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
buffer.put(pixels, 0, width * height);
|
buffer.put(pixels, 0, width * height);
|
||||||
buffer.flip();
|
buffer.flip();
|
||||||
|
|||||||
@ -43,22 +43,17 @@ public class AwtCanvas implements Canvas {
|
|||||||
AwtBitmap awtBitamp = (AwtBitmap)bitmap;
|
AwtBitmap awtBitamp = (AwtBitmap)bitmap;
|
||||||
|
|
||||||
canvas = awtBitamp.bitmap.createGraphics();
|
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.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0));
|
||||||
canvas.fillRect(0,0,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.SRC_OVER, 1.0f));
|
canvas.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
|
||||||
|
|
||||||
canvas.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
|
canvas.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
|
||||||
//canvas.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
//canvas.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
||||||
//canvas.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
canvas.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||||
//canvas.setRenderingHint(RenderingHints.KEY_KERNING, 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) {
|
if (awtPaint.stroke == null) {
|
||||||
canvas.setColor(awtPaint.color);
|
canvas.setColor(awtPaint.color);
|
||||||
canvas.setFont(awtPaint.font);
|
canvas.setFont(awtPaint.font);
|
||||||
canvas.drawString(text, x, y);
|
canvas.drawString(text, x + 2, y);
|
||||||
} else {
|
} else {
|
||||||
setColorAndStroke(awtPaint);
|
setColorAndStroke(awtPaint);
|
||||||
|
|
||||||
TextLayout textLayout = new TextLayout(text, awtPaint.font, canvas.getFontRenderContext());
|
TextLayout textLayout = new TextLayout(text, awtPaint.font, canvas.getFontRenderContext());
|
||||||
AffineTransform affineTransform = new AffineTransform();
|
AffineTransform affineTransform = new AffineTransform();
|
||||||
affineTransform.translate(x, y);
|
affineTransform.translate(x + 2, y);
|
||||||
canvas.draw(textLayout.getOutline(affineTransform));
|
canvas.draw(textLayout.getOutline(affineTransform));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,8 +47,8 @@ public class AwtPaint implements Paint {
|
|||||||
static {
|
static {
|
||||||
Map<Attribute, Object> textAttributes = new HashMap<Attribute, Object>();
|
Map<Attribute, Object> textAttributes = new HashMap<Attribute, Object>();
|
||||||
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
|
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
|
||||||
textAttributes.put(TextAttribute.FAMILY, "SansSerif");
|
textAttributes.put(TextAttribute.FAMILY, "Arial");
|
||||||
textAttributes.put(TextAttribute.SIZE, 13);
|
textAttributes.put(TextAttribute.SIZE, 14);
|
||||||
|
|
||||||
defaultFont = Font.getFont(textAttributes);
|
defaultFont = Font.getFont(textAttributes);
|
||||||
}
|
}
|
||||||
@ -126,7 +126,7 @@ public class AwtPaint implements Paint {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setTextSize(float textSize) {
|
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);
|
float w = AwtGraphics.getTextWidth(fm, text);
|
||||||
//Gdx.app.log("text width:", text + " " + w);
|
//Gdx.app.log("text width:", text + " " + w);
|
||||||
return w;
|
return w + 4;
|
||||||
// return fm.getStringBounds(text, A).getWidth();
|
// return fm.getStringBounds(text, A).getWidth();
|
||||||
// return AwtGraphics.getTextWidth(fm, text);
|
// return AwtGraphics.getTextWidth(fm, text);
|
||||||
// return fm.stringWidth(text);
|
// return fm.stringWidth(text);
|
||||||
@ -173,6 +173,6 @@ public class AwtPaint implements Paint {
|
|||||||
if (strokeWidth <= 0) {
|
if (strokeWidth <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stroke = new BasicStroke(strokeWidth, cap, BasicStroke.JOIN_ROUND, 0, null, 0);
|
stroke = new BasicStroke(strokeWidth, cap, BasicStroke.JOIN_MITER, 1, null, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user