Canvas: pass fill+stroke for drawing text
This commit is contained in:
parent
a8641ce535
commit
fa62602826
@ -33,9 +33,13 @@ public class AndroidCanvas implements Canvas {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawText(String string, float x, float y, Paint stroke) {
|
||||
if (string != null)
|
||||
public void drawText(String string, float x, float y, Paint fill, Paint stroke) {
|
||||
if (string != null) {
|
||||
if (stroke != null)
|
||||
canvas.drawText(string, x, y, ((AndroidPaint) stroke).mPaint);
|
||||
|
||||
canvas.drawText(string, x, y, ((AndroidPaint) fill).mPaint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -33,9 +33,13 @@ public class AndroidCanvas implements Canvas {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawText(String string, float x, float y, Paint stroke) {
|
||||
if (string != null)
|
||||
public void drawText(String string, float x, float y, Paint fill, Paint stroke) {
|
||||
if (string != null) {
|
||||
if (stroke != null)
|
||||
canvas.drawText(string, x, y, ((AndroidPaint) stroke).mPaint);
|
||||
|
||||
canvas.drawText(string, x, y, ((AndroidPaint) fill).mPaint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -20,6 +20,7 @@ package org.oscim.awt;
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.Shape;
|
||||
import java.awt.font.TextLayout;
|
||||
import java.awt.geom.AffineTransform;
|
||||
|
||||
@ -49,60 +50,47 @@ public class AwtCanvas implements Canvas {
|
||||
|
||||
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_ANTIALIASING, RenderingHints.VALUE_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);
|
||||
|
||||
}
|
||||
|
||||
private final AffineTransform tx = new AffineTransform();
|
||||
|
||||
@Override
|
||||
public void drawText(String text, float x, float y, Paint paint) {
|
||||
public void drawText(String text, float x, float y, Paint fill, Paint stroke) {
|
||||
|
||||
// if (paint.isTransparent()) {
|
||||
// return;
|
||||
// }
|
||||
AwtPaint fillPaint = (AwtPaint) fill;
|
||||
|
||||
AwtPaint awtPaint = (AwtPaint) paint;
|
||||
|
||||
//AwtPaint awtPaint = AwtGraphicFactory.getAwtPaint(paint);
|
||||
|
||||
if (awtPaint.stroke == null) {
|
||||
canvas.setColor(awtPaint.color);
|
||||
canvas.setFont(awtPaint.font);
|
||||
canvas.drawString(text, x + 2, y);
|
||||
if (stroke == null) {
|
||||
canvas.setColor(fillPaint.color);
|
||||
canvas.setFont(fillPaint.font);
|
||||
canvas.drawString(text, x + AwtPaint.TEXT_OFFSET, y);
|
||||
} else {
|
||||
setColorAndStroke(awtPaint);
|
||||
TextLayout tl = new TextLayout(text, awtPaint.font, canvas.getFontRenderContext());
|
||||
AffineTransform tx = new AffineTransform();
|
||||
tx.translate(x + 2, y);
|
||||
canvas.draw(tl.getOutline(tx));
|
||||
canvas.drawString(text, x + 2, y);
|
||||
}
|
||||
}
|
||||
AwtPaint strokePaint = (AwtPaint) stroke;
|
||||
|
||||
private void setColorAndStroke(AwtPaint awtPaint) {
|
||||
canvas.setColor(awtPaint.color);
|
||||
canvas.setColor(strokePaint.color);
|
||||
canvas.setStroke(strokePaint.stroke);
|
||||
|
||||
if (awtPaint.stroke != null) {
|
||||
canvas.setStroke(awtPaint.stroke);
|
||||
TextLayout tl = new TextLayout(text, fillPaint.font,
|
||||
canvas.getFontRenderContext());
|
||||
tx.setToIdentity();
|
||||
tx.translate(x, y);
|
||||
|
||||
Shape s = tl.getOutline(tx);
|
||||
|
||||
canvas.draw(s);
|
||||
canvas.setColor(fillPaint.color);
|
||||
canvas.fill(s);
|
||||
}
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void drawText(String string, float x, float y, Paint stroke) {
|
||||
// AwtPaint p = (AwtPaint)stroke;
|
||||
//
|
||||
// canvas.setFont(p.font);
|
||||
// canvas.setColor(p.color);
|
||||
//
|
||||
// canvas.drawString(string, (int)x, (int)y);
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void drawBitmap(Bitmap bitmap, float x, float y) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnknownError("not implemented");
|
||||
}
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ import org.oscim.backend.canvas.Paint;
|
||||
|
||||
public class AwtPaint implements Paint {
|
||||
|
||||
final static float TEXT_OFFSET = 2;
|
||||
|
||||
private static int getCap(Cap cap) {
|
||||
switch (cap) {
|
||||
case BUTT:
|
||||
@ -85,7 +87,7 @@ public class AwtPaint implements Paint {
|
||||
|
||||
@Override
|
||||
public void setStrokeWidth(float width) {
|
||||
strokeWidth = width;
|
||||
strokeWidth = width + 1;
|
||||
createStroke();
|
||||
|
||||
// int size = font.getSize();
|
||||
@ -108,7 +110,7 @@ public class AwtPaint implements Paint {
|
||||
|
||||
@Override
|
||||
public void setTextSize(float textSize) {
|
||||
font = font.deriveFont(textSize - 2);
|
||||
font = font.deriveFont(textSize);
|
||||
|
||||
}
|
||||
|
||||
|
@ -39,13 +39,13 @@ public class GwtCanvas implements org.oscim.backend.canvas.Canvas {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawText(String string, float x, float y, Paint paint) {
|
||||
public void drawText(String string, float x, float y, Paint fill, Paint stroke) {
|
||||
if (bitmap == null) {
|
||||
//log.debug("no bitmap set");
|
||||
return;
|
||||
}
|
||||
|
||||
GwtPaint p = (GwtPaint) paint;
|
||||
GwtPaint p = (GwtPaint) fill;
|
||||
|
||||
if (p.stroke && GwtGdxGraphics.NO_STROKE_TEXT)
|
||||
return;
|
||||
|
@ -36,8 +36,9 @@ public interface Canvas {
|
||||
* @param x
|
||||
* @param y
|
||||
* @param stroke the stroke
|
||||
* @param
|
||||
*/
|
||||
void drawText(String string, float x, float y, Paint stroke);
|
||||
void drawText(String string, float x, float y, Paint fill, Paint stroke);
|
||||
|
||||
/**
|
||||
* Draw Bitmap to Canvas.
|
||||
|
@ -127,13 +127,9 @@ public class TextBucket extends TextureBucket {
|
||||
}
|
||||
}
|
||||
|
||||
//yy = y + (height - 1) - it.text.fontDescent - mFontPadY;
|
||||
yy = y + height - it.text.fontDescent; // - mFontPadY;
|
||||
yy = y + height - it.text.fontDescent;
|
||||
|
||||
if (it.text.stroke != null)
|
||||
mCanvas.drawText(it.string, x, yy, it.text.stroke);
|
||||
|
||||
mCanvas.drawText(it.string, x, yy, it.text.paint);
|
||||
mCanvas.drawText(it.string, x, yy, it.text.paint, it.text.stroke);
|
||||
|
||||
// FIXME !!!
|
||||
if (width > TEXTURE_WIDTH)
|
||||
|
Loading…
x
Reference in New Issue
Block a user