Graphics API: implement IosCanvas.drawLine(), IosCanvas.fillColor() and IosPaint.getTextHeight(), #92

This commit is contained in:
Longri
2016-08-01 17:52:25 +02:00
committed by Emux
parent f736530049
commit a9bb6e3788
4 changed files with 114 additions and 43 deletions

View File

@@ -19,6 +19,7 @@ import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Canvas;
import org.oscim.backend.canvas.Paint;
import org.robovm.apple.coregraphics.CGBitmapContext;
import org.robovm.apple.coregraphics.CGBlendMode;
import org.robovm.apple.coregraphics.CGRect;
/**
@@ -26,6 +27,28 @@ import org.robovm.apple.coregraphics.CGRect;
*/
public class IosCanvas implements Canvas {
static void setFillColor(CGBitmapContext bctx, int color) {
float blue = (color & 0xFF) / 255f;
color >>= 8;
float green = (color & 0xFF) / 255f;
color >>= 8;
float red = (color & 0xFF) / 255f;
color >>= 8;
float alpha = (color & 0xFF) / 255f;
bctx.setRGBFillColor(red, green, blue, alpha);
}
static void setStrokeColor(CGBitmapContext bctx, int color) {
float blue = (color & 0xFF) / 255f;
color >>= 8;
float green = (color & 0xFF) / 255f;
color >>= 8;
float red = (color & 0xFF) / 255f;
color >>= 8;
float alpha = (color & 0xFF) / 255f;
bctx.setRGBStrokeColor(red, green, blue, alpha);
}
CGBitmapContext cgBitmapContext;
@Override
@@ -54,6 +77,7 @@ public class IosCanvas implements Canvas {
IosPaint iosStroke = (IosPaint) stroke;
iosFill.setStrokeWidth(iosStroke.strokeWidth);
iosFill.setStrokeColor(iosStroke.getColor());
iosStroke.drawLine(this.cgBitmapContext, string, x, y);
}
iosFill.drawLine(this.cgBitmapContext, string, x, y);
}
@@ -69,12 +93,32 @@ public class IosCanvas implements Canvas {
@Override
public void drawLine(int x1, int y1, int x2, int y2, Paint paint) {
// TODO
//flip Y-axis
y1 = (int) (this.cgBitmapContext.getHeight() - y1);
y2 = (int) (this.cgBitmapContext.getHeight() - y2);
// set Stroke properties
this.cgBitmapContext.setLineWidth(((IosPaint) paint).strokeWidth);
this.cgBitmapContext.setLineCap(((IosPaint) paint).getIosStrokeCap());
this.cgBitmapContext.setLineJoin(((IosPaint) paint).getIosStrokeJoin());
setStrokeColor(this.cgBitmapContext, (paint.getColor()));
//draw line
this.cgBitmapContext.beginPath();
this.cgBitmapContext.moveToPoint(x1, y1);
this.cgBitmapContext.addLineToPoint(x2, y2);
this.cgBitmapContext.strokePath();
}
@Override
public void fillColor(int color) {
// TODO
CGRect rect = new CGRect(0, 0, this.cgBitmapContext.getWidth(), this.cgBitmapContext.getHeight());
setFillColor(this.cgBitmapContext, (color));
this.cgBitmapContext.setBlendMode(CGBlendMode.Clear);
this.cgBitmapContext.fillRect(rect);
this.cgBitmapContext.setBlendMode(CGBlendMode.Normal);
this.cgBitmapContext.fillRect(rect);
}
@Override

View File

@@ -114,11 +114,19 @@ public class IosPaint implements Paint {
return new UIColor(colorR, colorG, colorB, colorA);
}
public CGLineCap getIosStrokeCap() {
return this.cap;
}
@Override
public void setStrokeCap(Cap cap) {
this.cap = getLineCap(cap);
}
public CGLineJoin getIosStrokeJoin() {
return this.join;
}
@Override
public void setStrokeJoin(Join join) {
this.join = getLineJoin(join);
@@ -163,7 +171,6 @@ public class IosPaint implements Paint {
}
}
@Override
public float measureText(String text) {
if (ctLineIsDirty || !text.equals(lastText)) {
@@ -173,7 +180,6 @@ public class IosPaint implements Paint {
return (float) ctLine.getWidth();
}
private void createCTLine(String text) {
if (ctLineIsDirty) {
synchronized (attribs) {
@@ -198,9 +204,7 @@ public class IosPaint implements Paint {
}
}
private void createIosFont() {
/*
DEVICE_DEFAULT = [iOS == getDeviceDefault()], [Android == 'Roboto']
MONOSPACE = [iOS == 'Courier'], [Android == 'Droid Sans Mono']
@@ -208,7 +212,6 @@ public class IosPaint implements Paint {
SERIF = [iOS == 'Georgia'], [Android == 'Droid Serif']
*/
String fontname = DEFAULT_FONT_NAME;
switch (this.fontFamily) {
case DEFAULT:
@@ -309,7 +312,6 @@ public class IosPaint implements Paint {
}
}
public void drawLine(CGBitmapContext cgBitmapContext, String text, float x, float y) {
if (ctLineIsDirty || !text.equals(lastText)) {
ctLineIsDirty = true;
@@ -318,7 +320,7 @@ public class IosPaint implements Paint {
cgBitmapContext.saveGState();
cgBitmapContext.setShouldAntialias(true);
cgBitmapContext.setTextPosition(x, y + descent);
cgBitmapContext.setBlendMode(CGBlendMode.Overlay);
cgBitmapContext.setBlendMode(CGBlendMode.Normal);
ctLine.draw(cgBitmapContext);
@@ -337,8 +339,7 @@ public class IosPaint implements Paint {
@Override
public float getTextHeight(String text) {
// TODO
return 0;
return this.fontHeight;
}
@Override