Render themes: SVG resources on iOS
This commit is contained in:
@@ -122,6 +122,25 @@ public class IosBitmap implements Bitmap {
|
||||
image.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* protected constructor for create IosBitmap from IosSvgBitmap
|
||||
* @param image
|
||||
*/
|
||||
protected IosBitmap(UIImage image){
|
||||
CGImage cgiIimage = image.getCGImage();
|
||||
this.width = (int) cgiIimage.getWidth();
|
||||
this.height = (int) cgiIimage.getHeight();
|
||||
this.cgBitmapContext = CGBitmapContext.create(width, height, 8, 4 * width,
|
||||
CGColorSpace.createDeviceRGB(), CGImageAlphaInfo.PremultipliedLast);
|
||||
|
||||
this.cgBitmapContext.drawImage(new CGRect(0, 0, width, height), cgiIimage);
|
||||
|
||||
// can dispose helper images for release memory
|
||||
image.dispose();
|
||||
cgiIimage.dispose();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
|
||||
@@ -63,8 +63,12 @@ public class IosGraphics extends CanvasAdapter {
|
||||
|
||||
@Override
|
||||
protected Bitmap decodeSvgBitmapImpl(InputStream inputStream) {
|
||||
// TODO
|
||||
return null;
|
||||
try {
|
||||
return new IosSvgBitmap(inputStream);
|
||||
} catch (IOException e) {
|
||||
log.error("decodeSvgImpl", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -269,7 +269,6 @@ public class IosPaint implements Paint {
|
||||
fontHeight = (float) ctFont.getBoundingBox().getHeight();
|
||||
|
||||
font = ctFont.as(UIFont.class);
|
||||
log.debug("Put Font to buffer :" + key);
|
||||
fontHashMap.put(key, font);
|
||||
}
|
||||
|
||||
|
||||
87
vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java
Normal file
87
vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2016 Longri
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.ios.backend;
|
||||
|
||||
|
||||
import org.oscim.backend.CanvasAdapter;
|
||||
import org.robovm.apple.coregraphics.CGRect;
|
||||
import org.robovm.apple.coregraphics.CGSize;
|
||||
import org.robovm.apple.uikit.UIImage;
|
||||
import svg.SVGRenderer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Created by Longri on 17.07.16.
|
||||
*/
|
||||
public class IosSvgBitmap extends IosBitmap {
|
||||
private static final float DEFAULT_SIZE = 400f;
|
||||
|
||||
/**
|
||||
* Constructor<br>
|
||||
* @param inputStream
|
||||
* @throws IOException
|
||||
*/
|
||||
public IosSvgBitmap(InputStream inputStream) throws IOException {
|
||||
super(getUIImage(inputStream));
|
||||
}
|
||||
|
||||
//get UIImage from SVG file
|
||||
private static UIImage getUIImage(InputStream inputStream) {
|
||||
String svg = getStringFromInputStream(inputStream);
|
||||
SVGRenderer renderer = new SVGRenderer(svg);
|
||||
CGRect viewRect = renderer.getViewRect();
|
||||
|
||||
float scaleFactor = CanvasAdapter.dpi / 240;
|
||||
double scale = scaleFactor / Math.sqrt((viewRect.getHeight() * viewRect.getWidth()) / DEFAULT_SIZE);
|
||||
|
||||
float bitmapWidth = (float) (viewRect.getWidth() * scale);
|
||||
float bitmapHeight = (float) (viewRect.getHeight() * scale);
|
||||
|
||||
return renderer.asImageWithSize(new CGSize(bitmapWidth, bitmapHeight), 1);
|
||||
}
|
||||
|
||||
|
||||
// convert InputStream to String
|
||||
private static String getStringFromInputStream(InputStream is) {
|
||||
|
||||
BufferedReader br = null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String line;
|
||||
try {
|
||||
|
||||
br = new BufferedReader(new InputStreamReader(is));
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (br != null) {
|
||||
try {
|
||||
br.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
15
vtm-ios/src/svg/GHRenderable.java
Normal file
15
vtm-ios/src/svg/GHRenderable.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package svg;
|
||||
|
||||
import org.robovm.apple.coregraphics.CGAffineTransform;
|
||||
import org.robovm.apple.foundation.NSObjectProtocol;
|
||||
import org.robovm.objc.annotation.Property;
|
||||
|
||||
public interface GHRenderable extends NSObjectProtocol{
|
||||
@Property(selector = "transform")
|
||||
public CGAffineTransform getTransform();
|
||||
|
||||
@Property(selector = "hidden")
|
||||
public boolean isHidden();
|
||||
|
||||
|
||||
}
|
||||
14
vtm-ios/src/svg/SVGContext.java
Normal file
14
vtm-ios/src/svg/SVGContext.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package svg;
|
||||
|
||||
import org.robovm.apple.foundation.NSObject;
|
||||
import org.robovm.apple.uikit.UIColor;
|
||||
import org.robovm.objc.annotation.Method;
|
||||
|
||||
public interface SVGContext {
|
||||
@Method(selector = "colorForSVGColorString:")
|
||||
public UIColor colorForSVGColorString(String svgColorString);
|
||||
|
||||
@Method(selector = "objectAtURL:")
|
||||
public NSObject objectAtURL(String aLocation);
|
||||
|
||||
}
|
||||
31
vtm-ios/src/svg/SVGParser.java
Normal file
31
vtm-ios/src/svg/SVGParser.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package svg;
|
||||
|
||||
import org.robovm.apple.foundation.NSError;
|
||||
import org.robovm.apple.foundation.NSObject;
|
||||
import org.robovm.objc.ObjCRuntime;
|
||||
import org.robovm.objc.annotation.Method;
|
||||
import org.robovm.objc.annotation.NativeClass;
|
||||
import org.robovm.objc.annotation.Property;
|
||||
import org.robovm.rt.bro.annotation.Library;
|
||||
import org.robovm.rt.bro.annotation.Pointer;
|
||||
import org.robovm.rt.bro.ptr.Ptr;
|
||||
|
||||
@Library(Library.INTERNAL)
|
||||
@NativeClass("SVGParser")
|
||||
public class SVGParser extends NSObject {
|
||||
public static class SVGParserPtr extends Ptr<SVGParser, SVGParserPtr> {}
|
||||
static { ObjCRuntime.bind(SVGParser.class); }/*</bind>*/
|
||||
|
||||
public SVGParser() {};
|
||||
protected SVGParser(long handle) { super(handle); }
|
||||
protected SVGParser(SkipInit skipInit) { super(skipInit); }
|
||||
|
||||
public SVGParser(String utf8String) { super((SkipInit) null); initObject(init(utf8String)); }
|
||||
|
||||
@Method(selector = "initWithString:")
|
||||
protected native @Pointer long init(String utf8String);
|
||||
|
||||
@Property(selector = "parserError")
|
||||
public native NSError getParserError();
|
||||
|
||||
}
|
||||
52
vtm-ios/src/svg/SVGRenderer.java
Normal file
52
vtm-ios/src/svg/SVGRenderer.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package svg;
|
||||
|
||||
import org.robovm.apple.coregraphics.CGAffineTransform;
|
||||
import org.robovm.apple.coregraphics.CGRect;
|
||||
import org.robovm.apple.coregraphics.CGSize;
|
||||
import org.robovm.apple.foundation.NSObject;
|
||||
import org.robovm.apple.uikit.UIColor;
|
||||
import org.robovm.apple.uikit.UIImage;
|
||||
import org.robovm.objc.ObjCRuntime;
|
||||
import org.robovm.objc.annotation.Method;
|
||||
import org.robovm.objc.annotation.NativeClass;
|
||||
import org.robovm.objc.annotation.Property;
|
||||
import org.robovm.rt.bro.annotation.ByVal;
|
||||
import org.robovm.rt.bro.annotation.Library;
|
||||
import org.robovm.rt.bro.annotation.MachineSizedFloat;
|
||||
import org.robovm.rt.bro.annotation.Pointer;
|
||||
import org.robovm.rt.bro.ptr.Ptr;
|
||||
|
||||
@Library(Library.INTERNAL)
|
||||
@NativeClass("SVGRenderer")
|
||||
public class SVGRenderer extends SVGParser implements SVGContext, GHRenderable {
|
||||
public static class SVGRendererPtr extends Ptr<SVGRenderer, SVGRendererPtr> {}
|
||||
static { ObjCRuntime.bind(SVGRenderer.class); }/*</bind>*/
|
||||
|
||||
public SVGRenderer() {};
|
||||
protected SVGRenderer(long handle) { super(handle); }
|
||||
protected SVGRenderer(SkipInit skipInit) { super(skipInit); }
|
||||
|
||||
public SVGRenderer(String utf8String) { super((SkipInit) null); initObject(init(utf8String)); }
|
||||
|
||||
@Method(selector = "initWithString:")
|
||||
protected native @Pointer long init(String utf8String);
|
||||
|
||||
@Property(selector = "viewRect")
|
||||
public native @ByVal CGRect getViewRect();
|
||||
|
||||
@Method(selector = "colorForSVGColorString:")
|
||||
public native UIColor colorForSVGColorString(String svgColorString);
|
||||
|
||||
@Method(selector = "objectAtURL:")
|
||||
public native NSObject objectAtURL(String aLocation);
|
||||
|
||||
@Property(selector = "transform")
|
||||
public native CGAffineTransform getTransform();
|
||||
|
||||
@Property(selector = "hidden")
|
||||
public native boolean isHidden();
|
||||
|
||||
@Method(selector = "asImageWithSize:andScale:")
|
||||
public native UIImage asImageWithSize(@ByVal CGSize maximumSize, @MachineSizedFloat double scale);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user