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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user