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