Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ide.common.vectordrawable;
import com.android.annotations.NonNull;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.awt.geom.AffineTransform;
import java.io.*;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Converts SVG to VectorDrawable's XML
*
* There are 2 major functions:
* 1. parse(file)
* This include parse the .svg file and build an internal tree. The optimize this tree.
*
* 2. writeFile()
* This is traversing the whole tree, and write the group / path info into the XML.
*/
public class Svg2Vector {
private static Logger logger = Logger.getLogger(Svg2Vector.class.getSimpleName());
public static final String SVG_POLYGON = "polygon";
public static final String SVG_POLYLINE = "polyline";
public static final String SVG_RECT = "rect";
public static final String SVG_CIRCLE = "circle";
public static final String SVG_LINE = "line";
public static final String SVG_PATH = "path";
public static final String SVG_ELLIPSE = "ellipse";
public static final String SVG_GROUP = "g";
public static final String SVG_TRANSFORM = "transform";
public static final String SVG_STYLE = "style";
public static final String SVG_DISPLAY = "display";
public static final String SVG_D = "d";
public static final String SVG_STROKE_COLOR = "stroke";
public static final String SVG_STROKE_OPACITY = "stroke-opacity";
public static final String SVG_STROKE_LINEJOINE = "stroke-linejoin";
public static final String SVG_STROKE_LINECAP = "stroke-linecap";
public static final String SVG_STROKE_WIDTH = "stroke-width";
public static final String SVG_FILL_COLOR = "fill";
public static final String SVG_FILL_OPACITY = "fill-opacity";
public static final String SVG_OPACITY = "opacity";
public static final String SVG_CLIP = "clip";
public static final String SVG_POINTS = "points";
public static final ImmutableMap presentationMap =
ImmutableMap.builder()
.put(SVG_STROKE_COLOR, "android:strokeColor")
.put(SVG_STROKE_OPACITY, "android:strokeAlpha")
.put(SVG_STROKE_LINEJOINE, "android:strokeLineJoin")
.put(SVG_STROKE_LINECAP, "android:strokeLineCap")
.put(SVG_STROKE_WIDTH, "android:strokeWidth")
.put(SVG_FILL_COLOR, "android:fillColor")
.put(SVG_FILL_OPACITY, "android:fillAlpha")
.put(SVG_CLIP, "android:clip")
.put(SVG_OPACITY, "android:fillAlpha")
.build();
// List all the Svg nodes that we don't support. Categorized by the types.
private static final HashSet unsupportedSvgNodes = Sets.newHashSet(
// Animation elements
"animate", "animateColor", "animateMotion", "animateTransform", "mpath", "set",
// Container elements
"a", "defs", "glyph", "marker", "mask", "missing-glyph", "pattern", "switch", "symbol",
// Filter primitive elements
"feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix",
"feDiffuseLighting", "feDisplacementMap", "feFlood", "feFuncA", "feFuncB", "feFuncG",
"feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology",
"feOffset", "feSpecularLighting", "feTile", "feTurbulence",
// Font elements
"font", "font-face", "font-face-format", "font-face-name", "font-face-src", "font-face-uri",
"hkern", "vkern",
// Gradient elements
"linearGradient", "radialGradient", "stop",
// Graphics elements
"ellipse", "text",
// Light source elements
"feDistantLight", "fePointLight", "feSpotLight",
// Structural elements
"defs", "symbol", "use",
// Text content elements
"altGlyph", "altGlyphDef", "altGlyphItem", "glyph", "glyphRef", "textPath", "text", "tref",
"tspan",
// Text content child elements
"altGlyph", "textPath", "tref", "tspan",
// Uncategorized elements
"clipPath", "color-profile", "cursor", "filter", "foreignObject", "script", "view");
@NonNull
private static SvgTree parse(File f) throws Exception {
SvgTree svgTree = new SvgTree();
Document doc = svgTree.parse(f);
NodeList nSvgNode;
// Parse svg elements
nSvgNode = doc.getElementsByTagName("svg");
if (nSvgNode.getLength() != 1) {
throw new IllegalStateException("Not a proper SVG file");
}
Node rootNode = nSvgNode.item(0);
for (int i = 0; i < nSvgNode.getLength(); i++) {
Node nNode = nSvgNode.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
svgTree.parseDimension(nNode);
}
}
if (svgTree.getViewBox() == null) {
svgTree.logErrorLine("Missing \"viewBox\" in