Graphics API enhancements, closes #92
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
|
||||
* Copyright 2013 Hannes Janetzek
|
||||
* Copyright 2016 devemux86
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
*
|
||||
@@ -19,21 +20,30 @@ package org.oscim.awt;
|
||||
|
||||
import org.oscim.backend.canvas.Bitmap;
|
||||
import org.oscim.backend.canvas.Canvas;
|
||||
import org.oscim.backend.canvas.Color;
|
||||
import org.oscim.backend.canvas.Paint;
|
||||
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Composite;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.Shape;
|
||||
import java.awt.font.TextLayout;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class AwtCanvas implements Canvas {
|
||||
|
||||
Graphics2D canvas;
|
||||
private static final java.awt.Color TRANSPARENT = new java.awt.Color(0, 0, 0, 0);
|
||||
|
||||
private BufferedImage bitmap;
|
||||
public Graphics2D canvas;
|
||||
|
||||
public AwtCanvas() {
|
||||
}
|
||||
|
||||
public AwtCanvas(BufferedImage bitmap) {
|
||||
this.bitmap = bitmap;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,26 +51,51 @@ public class AwtCanvas implements Canvas {
|
||||
if (canvas != null)
|
||||
canvas.dispose();
|
||||
|
||||
AwtBitmap awtBitamp = (AwtBitmap) bitmap;
|
||||
AwtBitmap awtBitmap = (AwtBitmap) bitmap;
|
||||
|
||||
canvas = awtBitamp.bitmap.createGraphics();
|
||||
this.bitmap = awtBitmap.bitmap;
|
||||
canvas = awtBitmap.bitmap.createGraphics();
|
||||
|
||||
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_FRACTIONALMETRICS,
|
||||
// RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
||||
canvas.setRenderingHint(RenderingHints.KEY_RENDERING,
|
||||
RenderingHints.VALUE_RENDER_QUALITY);
|
||||
canvas.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
canvas.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_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_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
canvas.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
|
||||
}
|
||||
|
||||
private final AffineTransform tx = new AffineTransform();
|
||||
|
||||
@Override
|
||||
public void drawText(String text, float x, float y, Paint paint) {
|
||||
|
||||
AwtPaint awtPaint = (AwtPaint) paint;
|
||||
|
||||
if (awtPaint.stroke == null) {
|
||||
canvas.setColor(awtPaint.color);
|
||||
canvas.setFont(awtPaint.font);
|
||||
canvas.drawString(text, x, y);
|
||||
} else {
|
||||
canvas.setColor(awtPaint.color);
|
||||
canvas.setStroke(awtPaint.stroke);
|
||||
|
||||
TextLayout tl = new TextLayout(text, awtPaint.font,
|
||||
canvas.getFontRenderContext());
|
||||
tx.setToIdentity();
|
||||
tx.translate(x, y);
|
||||
|
||||
Shape s = tl.getOutline(tx);
|
||||
|
||||
canvas.draw(s);
|
||||
canvas.setColor(awtPaint.color);
|
||||
canvas.fill(s);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawText(String text, float x, float y, Paint fill, Paint stroke) {
|
||||
|
||||
@@ -69,7 +104,7 @@ public class AwtCanvas implements Canvas {
|
||||
if (stroke == null) {
|
||||
canvas.setColor(fillPaint.color);
|
||||
canvas.setFont(fillPaint.font);
|
||||
canvas.drawString(text, x + AwtPaint.TEXT_OFFSET, y);
|
||||
canvas.drawString(text, x, y);
|
||||
} else {
|
||||
AwtPaint strokePaint = (AwtPaint) stroke;
|
||||
|
||||
@@ -91,6 +126,35 @@ public class AwtCanvas implements Canvas {
|
||||
|
||||
@Override
|
||||
public void drawBitmap(Bitmap bitmap, float x, float y) {
|
||||
throw new UnknownError("not implemented");
|
||||
this.canvas.drawImage(((AwtBitmap) bitmap).bitmap, (int) x, (int) y, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(int x1, int y1, int x2, int y2, Paint paint) {
|
||||
AwtPaint awtPaint = (AwtPaint) paint;
|
||||
this.canvas.setColor(awtPaint.color);
|
||||
if (awtPaint.stroke != null)
|
||||
this.canvas.setStroke(awtPaint.stroke);
|
||||
this.canvas.drawLine(x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillColor(int color) {
|
||||
java.awt.Color awtColor = color == Color.TRANSPARENT ? TRANSPARENT : new java.awt.Color(color);
|
||||
Composite originalComposite = this.canvas.getComposite();
|
||||
this.canvas.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
|
||||
this.canvas.setColor(awtColor);
|
||||
this.canvas.fillRect(0, 0, getWidth(), getHeight());
|
||||
this.canvas.setComposite(originalComposite);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return this.bitmap != null ? this.bitmap.getHeight() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return this.bitmap != null ? this.bitmap.getWidth() : 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,10 +66,12 @@ public class AwtGraphics extends CanvasAdapter {
|
||||
static {
|
||||
image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
|
||||
canvas = image.createGraphics();
|
||||
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_ANTIALIASING, RenderingHints.VALUE_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_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
canvas.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
|
||||
}
|
||||
|
||||
static synchronized FontMetrics getFontMetrics(Font font) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
|
||||
* Copyright 2013 Hannes Janetzek
|
||||
* Copyright 2016 devemux86
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
*
|
||||
@@ -23,16 +24,16 @@ import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Stroke;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.text.AttributedCharacterIterator.Attribute;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AwtPaint implements Paint {
|
||||
|
||||
final static float TEXT_OFFSET = 2;
|
||||
|
||||
private static int getCap(Cap cap) {
|
||||
switch (cap) {
|
||||
case BUTT:
|
||||
@@ -46,10 +47,54 @@ public class AwtPaint implements Paint {
|
||||
throw new IllegalArgumentException("unknown cap: " + cap);
|
||||
}
|
||||
|
||||
private static String getFontName(FontFamily fontFamily) {
|
||||
switch (fontFamily) {
|
||||
case MONOSPACE:
|
||||
return Font.MONOSPACED;
|
||||
case DEFAULT:
|
||||
case DEFAULT_BOLD:
|
||||
return null;
|
||||
case SANS_SERIF:
|
||||
return Font.SANS_SERIF;
|
||||
case SERIF:
|
||||
return Font.SERIF;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("unknown fontFamily: " + fontFamily);
|
||||
}
|
||||
|
||||
private static int getFontStyle(FontStyle fontStyle) {
|
||||
switch (fontStyle) {
|
||||
case BOLD:
|
||||
return Font.BOLD;
|
||||
case BOLD_ITALIC:
|
||||
return Font.BOLD | Font.ITALIC;
|
||||
case ITALIC:
|
||||
return Font.ITALIC;
|
||||
case NORMAL:
|
||||
return Font.PLAIN;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("unknown fontStyle: " + fontStyle);
|
||||
}
|
||||
|
||||
private static int getJoin(Join join) {
|
||||
switch (join) {
|
||||
case ROUND:
|
||||
return BasicStroke.JOIN_ROUND;
|
||||
case BEVEL:
|
||||
return BasicStroke.JOIN_BEVEL;
|
||||
case MITER:
|
||||
return BasicStroke.JOIN_MITER;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("unknown cap: " + join);
|
||||
}
|
||||
|
||||
static final Font defaultFont;
|
||||
|
||||
static {
|
||||
Map<Attribute, Object> textAttributes = new HashMap<Attribute, Object>();
|
||||
Map<Attribute, Object> textAttributes = new HashMap<>();
|
||||
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
|
||||
textAttributes.put(TextAttribute.FAMILY, "Arial");
|
||||
textAttributes.put(TextAttribute.SIZE, 14);
|
||||
@@ -57,15 +102,19 @@ public class AwtPaint implements Paint {
|
||||
defaultFont = Font.getFont(textAttributes);
|
||||
}
|
||||
|
||||
Color color = new Color(0.1f, 0.1f, 0.1f, 1);
|
||||
FontMetrics fm;
|
||||
Font font = defaultFont; // new Font("Default", Font.PLAIN, 13);
|
||||
Stroke stroke;
|
||||
FontMetrics fm;
|
||||
Color color = new Color(0.1f, 0.1f, 0.1f, 1);
|
||||
|
||||
private int cap;
|
||||
Style style = Style.FILL;
|
||||
private int cap = getCap(Cap.BUTT);
|
||||
private String fontName = defaultFont.getFontName();
|
||||
private int fontStyle = defaultFont.getStyle();
|
||||
private int join = getJoin(Join.MITER);
|
||||
private float strokeWidth;
|
||||
private float textSize = defaultFont.getSize();
|
||||
|
||||
//private Align mAlign;
|
||||
private final BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
@Override
|
||||
public int getColor() {
|
||||
@@ -86,39 +135,45 @@ public class AwtPaint implements Paint {
|
||||
createStroke();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStrokeJoin(Join join) {
|
||||
this.join = getJoin(join);
|
||||
createStroke();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStrokeWidth(float width) {
|
||||
strokeWidth = width + 1;
|
||||
strokeWidth = width;
|
||||
createStroke();
|
||||
|
||||
// int size = font.getSize();
|
||||
// font = font.deriveFont(size + width * 4);
|
||||
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStyle(Style style) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
this.style = style;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTextAlign(Align align) {
|
||||
//mAlign = align;
|
||||
// Align text in text layer
|
||||
//this.align = align;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTextSize(float textSize) {
|
||||
font = font.deriveFont(textSize);
|
||||
|
||||
this.textSize = textSize;
|
||||
this.font = this.font.deriveFont(textSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTypeface(FontFamily fontFamily, FontStyle fontStyle) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
this.fontName = getFontName(fontFamily);
|
||||
this.fontStyle = getFontStyle(fontStyle);
|
||||
Map<Attribute, Object> textAttributes = new HashMap<>();
|
||||
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
|
||||
this.font = new Font(this.fontName, this.fontStyle, (int) this.textSize).deriveFont(textAttributes);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -158,6 +213,22 @@ public class AwtPaint implements Paint {
|
||||
if (strokeWidth <= 0) {
|
||||
return;
|
||||
}
|
||||
stroke = new BasicStroke(strokeWidth, cap, BasicStroke.JOIN_MITER, 1, null, 0);
|
||||
stroke = new BasicStroke(strokeWidth, cap, join, join == BasicStroke.JOIN_MITER ? 1.0f : 0, null, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getTextHeight(String text) {
|
||||
Graphics2D graphics2d = bufferedImage.createGraphics();
|
||||
FontMetrics fontMetrics = graphics2d.getFontMetrics(this.font);
|
||||
graphics2d.dispose();
|
||||
return (float) this.font.createGlyphVector(fontMetrics.getFontRenderContext(), text).getVisualBounds().getHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getTextWidth(String text) {
|
||||
Graphics2D graphics2d = bufferedImage.createGraphics();
|
||||
FontMetrics fontMetrics = graphics2d.getFontMetrics(this.font);
|
||||
graphics2d.dispose();
|
||||
return fontMetrics.stringWidth(text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user