SVG symbols: allow custom creation, #74

This commit is contained in:
Emux 2016-07-19 15:50:54 +03:00
parent 21ea5a7842
commit 3d6cad1a0c
4 changed files with 164 additions and 65 deletions

View File

@ -28,35 +28,59 @@ import java.io.InputStream;
public class AndroidSvgBitmap extends AndroidBitmap { public class AndroidSvgBitmap extends AndroidBitmap {
/** /**
* Default size is 20x20px at baseline mdpi (160dpi). * Default size is 20x20px (400px) at baseline mdpi (160dpi).
*/ */
public static float DEFAULT_SIZE = 400f; public static float DEFAULT_SIZE = 400f;
private static android.graphics.Bitmap getResourceBitmap(InputStream inputStream) throws IOException { public static android.graphics.Bitmap getResourceBitmap(InputStream inputStream, float scaleFactor, float defaultSize, int width, int height, int percent) throws IOException {
synchronized (SVG.getVersion()) { try {
try { SVG svg = SVG.getFromInputStream(inputStream);
SVG svg = SVG.getFromInputStream(inputStream); Picture picture = svg.renderToPicture();
Picture picture = svg.renderToPicture();
float scaleFactor = CanvasAdapter.dpi / 160; double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize);
double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / DEFAULT_SIZE);
float bitmapWidth = (float) (picture.getWidth() * scale); float bitmapWidth = (float) (picture.getWidth() * scale);
float bitmapHeight = (float) (picture.getHeight() * scale); float bitmapHeight = (float) (picture.getHeight() * scale);
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth), float aspectRatio = (1f * picture.getWidth()) / picture.getHeight();
(int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight));
return bitmap; if (width != 0 && height != 0) {
} catch (Exception e) { // both width and height set, override any other setting
throw new IOException(e); bitmapWidth = width;
bitmapHeight = height;
} else if (width == 0 && height != 0) {
// only width set, calculate from aspect ratio
bitmapWidth = height * aspectRatio;
bitmapHeight = height;
} else if (width != 0 && height == 0) {
// only height set, calculate from aspect ratio
bitmapHeight = width / aspectRatio;
bitmapWidth = width;
} }
if (percent != 100) {
bitmapWidth *= percent / 100f;
bitmapHeight *= percent / 100f;
}
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth),
(int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight));
return bitmap;
} catch (Exception e) {
throw new IOException(e);
} }
} }
AndroidSvgBitmap(InputStream inputStream) throws IOException { private static android.graphics.Bitmap getResourceBitmapImpl(InputStream inputStream) throws IOException {
super(getResourceBitmap(inputStream)); synchronized (SVG.getVersion()) {
return getResourceBitmap(inputStream, CanvasAdapter.dpi / 160, DEFAULT_SIZE, 0, 0, 100);
}
}
public AndroidSvgBitmap(InputStream inputStream) throws IOException {
super(getResourceBitmapImpl(inputStream));
} }
} }

View File

@ -28,35 +28,59 @@ import java.io.InputStream;
public class AndroidSvgBitmap extends AndroidBitmap { public class AndroidSvgBitmap extends AndroidBitmap {
/** /**
* Default size is 20x20px at baseline mdpi (160dpi). * Default size is 20x20px (400px) at baseline mdpi (160dpi).
*/ */
public static float DEFAULT_SIZE = 400f; public static float DEFAULT_SIZE = 400f;
private static android.graphics.Bitmap getResourceBitmap(InputStream inputStream) throws IOException { public static android.graphics.Bitmap getResourceBitmap(InputStream inputStream, float scaleFactor, float defaultSize, int width, int height, int percent) throws IOException {
synchronized (SVG.getVersion()) { try {
try { SVG svg = SVG.getFromInputStream(inputStream);
SVG svg = SVG.getFromInputStream(inputStream); Picture picture = svg.renderToPicture();
Picture picture = svg.renderToPicture();
float scaleFactor = CanvasAdapter.dpi / 160; double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize);
double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / DEFAULT_SIZE);
float bitmapWidth = (float) (picture.getWidth() * scale); float bitmapWidth = (float) (picture.getWidth() * scale);
float bitmapHeight = (float) (picture.getHeight() * scale); float bitmapHeight = (float) (picture.getHeight() * scale);
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth), float aspectRatio = (1f * picture.getWidth()) / picture.getHeight();
(int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight));
return bitmap; if (width != 0 && height != 0) {
} catch (Exception e) { // both width and height set, override any other setting
throw new IOException(e); bitmapWidth = width;
bitmapHeight = height;
} else if (width == 0 && height != 0) {
// only width set, calculate from aspect ratio
bitmapWidth = height * aspectRatio;
bitmapHeight = height;
} else if (width != 0 && height == 0) {
// only height set, calculate from aspect ratio
bitmapHeight = width / aspectRatio;
bitmapWidth = width;
} }
if (percent != 100) {
bitmapWidth *= percent / 100f;
bitmapHeight *= percent / 100f;
}
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth),
(int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight));
return bitmap;
} catch (Exception e) {
throw new IOException(e);
} }
} }
AndroidSvgBitmap(InputStream inputStream) throws IOException { private static android.graphics.Bitmap getResourceBitmapImpl(InputStream inputStream) throws IOException {
super(getResourceBitmap(inputStream)); synchronized (SVG.getVersion()) {
return getResourceBitmap(inputStream, CanvasAdapter.dpi / 160, DEFAULT_SIZE, 0, 0, 100);
}
}
public AndroidSvgBitmap(InputStream inputStream) throws IOException {
super(getResourceBitmapImpl(inputStream));
} }
} }

View File

@ -28,38 +28,62 @@ import java.net.URI;
public class AwtSvgBitmap extends AwtBitmap { public class AwtSvgBitmap extends AwtBitmap {
/** /**
* Default size is 20x20px. * Default size is 20x20px (400px).
*/ */
public static float DEFAULT_SIZE = 400f; public static float DEFAULT_SIZE = 400f;
private static BufferedImage getResourceBitmap(InputStream inputStream) throws IOException { public static BufferedImage getResourceBitmap(InputStream inputStream, float scaleFactor, float defaultSize, int width, int height, int percent) throws IOException {
synchronized (SVGCache.getSVGUniverse()) { try {
try { URI uri = SVGCache.getSVGUniverse().loadSVG(inputStream, Integer.toString(inputStream.hashCode()));
URI uri = SVGCache.getSVGUniverse().loadSVG(inputStream, Integer.toString(inputStream.hashCode())); SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri);
SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri);
float scaleFactor = CanvasAdapter.dpi / 240; double scale = scaleFactor / Math.sqrt((diagram.getHeight() * diagram.getWidth()) / defaultSize);
double scale = scaleFactor / Math.sqrt((diagram.getHeight() * diagram.getWidth()) / DEFAULT_SIZE);
float bitmapWidth = (float) (diagram.getWidth() * scale); float bitmapWidth = (float) (diagram.getWidth() * scale);
float bitmapHeight = (float) (diagram.getHeight() * scale); float bitmapHeight = (float) (diagram.getHeight() * scale);
SVGIcon icon = new SVGIcon(); float aspectRatio = diagram.getWidth() / diagram.getHeight();
icon.setAntiAlias(true);
icon.setPreferredSize(new Dimension((int) bitmapWidth, (int) bitmapHeight));
icon.setScaleToFit(true);
icon.setSvgURI(uri);
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
icon.paintIcon(null, bufferedImage.createGraphics(), 0, 0);
return bufferedImage; if (width != 0 && height != 0) {
} catch (Exception e) { // both width and height set, override any other setting
throw new IOException(e); bitmapWidth = width;
bitmapHeight = height;
} else if (width == 0 && height != 0) {
// only width set, calculate from aspect ratio
bitmapWidth = height * aspectRatio;
bitmapHeight = height;
} else if (width != 0 && height == 0) {
// only height set, calculate from aspect ratio
bitmapHeight = width / aspectRatio;
bitmapWidth = width;
} }
if (percent != 100) {
bitmapWidth *= percent / 100f;
bitmapHeight *= percent / 100f;
}
SVGIcon icon = new SVGIcon();
icon.setAntiAlias(true);
icon.setPreferredSize(new Dimension((int) bitmapWidth, (int) bitmapHeight));
icon.setScaleToFit(true);
icon.setSvgURI(uri);
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
icon.paintIcon(null, bufferedImage.createGraphics(), 0, 0);
return bufferedImage;
} catch (Exception e) {
throw new IOException(e);
} }
} }
AwtSvgBitmap(InputStream inputStream) throws IOException { private static BufferedImage getResourceBitmapImpl(InputStream inputStream) throws IOException {
super(getResourceBitmap(inputStream)); synchronized (SVGCache.getSVGUniverse()) {
return getResourceBitmap(inputStream, CanvasAdapter.dpi / 240, DEFAULT_SIZE, 0, 0, 100);
}
}
public AwtSvgBitmap(InputStream inputStream) throws IOException {
super(getResourceBitmapImpl(inputStream));
} }
} }

View File

@ -33,7 +33,10 @@ import svg.SVGRenderer;
public class IosSvgBitmap extends IosBitmap { public class IosSvgBitmap extends IosBitmap {
private static final Logger log = LoggerFactory.getLogger(IosSvgBitmap.class); private static final Logger log = LoggerFactory.getLogger(IosSvgBitmap.class);
private static final float DEFAULT_SIZE = 400f; /**
* Default size is 20x20px (400px).
*/
public static float DEFAULT_SIZE = 400f;
private static String getStringFromInputStream(InputStream is) { private static String getStringFromInputStream(InputStream is) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -52,21 +55,45 @@ public class IosSvgBitmap extends IosBitmap {
return sb.toString(); return sb.toString();
} }
private static UIImage getUIImage(InputStream inputStream) { public static UIImage getResourceBitmap(InputStream inputStream, float scaleFactor, float defaultSize, int width, int height, int percent) {
String svg = getStringFromInputStream(inputStream); String svg = getStringFromInputStream(inputStream);
SVGRenderer renderer = new SVGRenderer(svg); SVGRenderer renderer = new SVGRenderer(svg);
CGRect viewRect = renderer.getViewRect(); CGRect viewRect = renderer.getViewRect();
float scaleFactor = CanvasAdapter.dpi / 240; double scale = scaleFactor / Math.sqrt((viewRect.getHeight() * viewRect.getWidth()) / defaultSize);
double scale = scaleFactor / Math.sqrt((viewRect.getHeight() * viewRect.getWidth()) / DEFAULT_SIZE);
float bitmapWidth = (float) (viewRect.getWidth() * scale); float bitmapWidth = (float) (viewRect.getWidth() * scale);
float bitmapHeight = (float) (viewRect.getHeight() * scale); float bitmapHeight = (float) (viewRect.getHeight() * scale);
float aspectRatio = (float) (viewRect.getWidth() / viewRect.getHeight());
if (width != 0 && height != 0) {
// both width and height set, override any other setting
bitmapWidth = width;
bitmapHeight = height;
} else if (width == 0 && height != 0) {
// only width set, calculate from aspect ratio
bitmapWidth = height * aspectRatio;
bitmapHeight = height;
} else if (width != 0 && height == 0) {
// only height set, calculate from aspect ratio
bitmapHeight = width / aspectRatio;
bitmapWidth = width;
}
if (percent != 100) {
bitmapWidth *= percent / 100f;
bitmapHeight *= percent / 100f;
}
return renderer.asImageWithSize(new CGSize(bitmapWidth, bitmapHeight), 1); return renderer.asImageWithSize(new CGSize(bitmapWidth, bitmapHeight), 1);
} }
private static UIImage getResourceBitmapImpl(InputStream inputStream) {
return getResourceBitmap(inputStream, CanvasAdapter.dpi / 240, DEFAULT_SIZE, 0, 0, 100);
}
public IosSvgBitmap(InputStream inputStream) throws IOException { public IosSvgBitmap(InputStream inputStream) throws IOException {
super(getUIImage(inputStream)); super(getResourceBitmapImpl(inputStream));
} }
} }