Skip to content

Commit

Permalink
release 1.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
misakuo committed Mar 11, 2016
1 parent 883bb44 commit 987090d
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 32 deletions.
13 changes: 11 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,24 @@
#####通过插件仓库安装
在IDE中打开Preferences -> Plugins -> Browse Repositories,搜索SVG2VectorDrawable,安装插件并重启IDE
####图形界面
![img1](https://raw.githubusercontent.com/misakuo/svgtoandroid/master/imgs/1.png)
![img1]+(https://raw.githubusercontent.com/misakuo/svgtoandroid/master/imgs/1.png)
####用法
- 点击`···`按钮,选择SVG源文件
- 在第一个下拉选框中选中要生成xml文件的module,在第二个选框中选择生成VectorDrawable对应的分辨率,在module中已存在的分辨率目录为黑色字体,未存在的目录为灰色字体,如果选中不存在的目录,则插件会自动生成该目录
- 填入生成xml文件的文件名,默认为vector_drawable_ + SVG文件的名称
- 点击`Generate`,插件会生成xml并在编辑器中打开(如果文件名对应的xml已存在,则会覆盖原来的内容)

####更新日志
#####1.4.2
针对Sketch导出的SVG文件做了优化
#####1.4.1
代码优化,更换新Icon
#####1.4.0
支持“transform”属性
#####Early
创建项目,bug fix

欢迎提交Issue和PR
*欢迎提交Issue和PR*
***
Reference: [svg2vectordrawable](https://github.com/Ashung/svg2vectordrawable)

6 changes: 4 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>com.moxun.plugin.s2v</id>
<name>SVG2VectorDrawable</name>
<version>1.4.1</version>
<version>1.4.2</version>
<vendor email="[email protected]">moxun</vendor>

<description><![CDATA[
Expand All @@ -13,11 +13,13 @@
]]></description>

<change-notes><![CDATA[
<b>1.4.2</b><br>
Optimizing for SVG that exported by Sketch.<br>
<b>1.4.1</b><br>
Updating new ICON.<br>
Optimize code structure.<br>
<b>1.4</b><br>
The 'transform' attribute on source SVG is supported now.<br>
The 'transform' attribute on source SVG is supporting now.<br>
<b>1.3</b><br>
Fixed 'NoSuchMethodError' in older Android Studio.<br>
<b>1.2</b><br>
Expand Down
12 changes: 11 additions & 1 deletion src/com/moxun/s2v/GUI.form
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.moxun.s2v.GUI">
<grid id="27dc6" binding="rootPanel" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="rootPanel" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="188" y="450" width="710" height="400"/>
Expand Down Expand Up @@ -103,6 +103,16 @@
<background color="-1"/>
</properties>
</component>
<component id="2e1b5" class="javax.swing.JLabel" binding="statusBar">
<constraints>
<grid row="3" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-4276546"/>
<foreground color="-12009473"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>
23 changes: 19 additions & 4 deletions src/com/moxun/s2v/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.moxun.s2v.utils.Logger;
import com.moxun.s2v.utils.ModulesUtil;
import com.moxun.s2v.utils.MyCellRender;
import com.moxun.s2v.utils.UpdateUtil;

import javax.swing.*;
import java.awt.*;
Expand All @@ -32,6 +33,7 @@ public class GUI {
private JComboBox moduleChooser;
private JButton generateButton;
private JTextField xmlName;
private JLabel statusBar;
private JFrame frame;

private Project project;
Expand All @@ -43,10 +45,11 @@ public class GUI {

public GUI(Project project) {
this.project = project;
frame = new JFrame("SVG to VectorDrawable");
frame = new JFrame("SVG to VectorDrawable (1.4.2)");
modulesUtil = new ModulesUtil(project);
distDirList.clear();
svgPath.setFocusable(false);
statusBar.setVisible(false);
setListener();
initModules();
}
Expand Down Expand Up @@ -123,7 +126,7 @@ public void actionPerformed(ActionEvent e) {
frame.dispose();
}
} else {
ErrorMessage.show(project,"Current project is not an Android project!");
ErrorMessage.show(project, "Current project is not an Android project!");
frame.dispose();
}

Expand All @@ -139,14 +142,24 @@ private void showSVGChooser() {
svg = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile);
//got *.svg file as xml
svgPath.setText(virtualFile.getPath());
xmlName.setText("vector_drawable_" + svg.getName().split("\\.")[0] + ".xml");
xmlName.setText("vector_drawable_" + getValidName(svg.getName().split("\\.")[0]) + ".xml");
} else {
ErrorMessage.show(project,"Please choosing a SVG file.");
ErrorMessage.show(project, "Please choosing a SVG file.");
}
}
frame.setAlwaysOnTop(true);
}

private String getValidName(String s) {
char[] chars = s.toLowerCase().replaceAll("\\s*", "").toCharArray();
for (int i = 0; i < chars.length; i++) {
if (!Character.isLetter(chars[i])) {
chars[i] = '_';
}
}
return String.valueOf(chars);
}

private void showXMLChooser() {
FileChooserDescriptor descriptor = new FileChooserDescriptor(false, true, false, false, false, false);
VirtualFile virtualFile = FileChooser.chooseFile(descriptor, project, null);
Expand Down Expand Up @@ -186,6 +199,8 @@ public void show() {
frame.pack();
frame.setLocationRelativeTo(frame.getParent());
frame.setVisible(true);

//UpdateUtil.checkUpdate(statusBar);
}

private boolean check() {
Expand Down
2 changes: 1 addition & 1 deletion src/com/moxun/s2v/S2V.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class S2V extends AnAction {
@Override
public void actionPerformed(AnActionEvent anActionEvent) {
Logger.init("SVG2VectorDrawable",Logger.INFO);
Logger.init("SVG2VectorDrawable", Logger.INFO);
GUI gui = new GUI(anActionEvent.getProject());
gui.show();
}
Expand Down
51 changes: 44 additions & 7 deletions src/com/moxun/s2v/SVGParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.moxun.s2v.utils.AttrMapper;
import com.moxun.s2v.utils.DensityUtil;
import com.moxun.s2v.utils.Logger;
import com.moxun.s2v.utils.StdColorUtil;
import com.moxun.s2v.utils.*;
import org.apache.http.util.TextUtils;

import java.util.*;

Expand Down Expand Up @@ -89,7 +87,7 @@ public List<XmlTag> getGroups() {
if (rootTag != null) {
for (XmlTag tag : rootTag.getSubTags()) {
if (tag.getName().equals("g")) {
groups.add(trim(tag));
groups.add(trim(tag, null));
}
}
}
Expand Down Expand Up @@ -165,19 +163,58 @@ public Map<String, String> getChildAttrs(XmlTag tag) {
return styles;
}

public XmlTag trim(XmlTag rootTag) {
public XmlTag trim(XmlTag rootTag, List<XmlAttribute> attr) {
XmlTag[] subTags = rootTag.getSubTags();
List<XmlAttribute> attrs = new ArrayList<XmlAttribute>();
if (subTags.length == 1 && subTags[0].getName().equals("g")) {
Logger.debug("Tag" + rootTag + " has only a subTag and the tag is 'g'");
rootTag = trim(subTags[0]);
Collections.addAll(attrs, subTags[0].getAttributes());
if (attr != null) {
attrs.addAll(attr);
}
rootTag = trim(subTags[0], attrs);
} else if (subTags.length > 0 && AttrMapper.isShapeName(subTags[0].getName())) {
Logger.debug(rootTag.getSubTags()[0].getName());
Logger.debug("Tag" + rootTag + " is correct tag.");
if (attr != null) {
for (XmlAttribute attribute : attr) {
Logger.debug(attribute.getName() + ":" + attribute.getValue());
}
return AttrMergeUtil.mergeAttrs((XmlTag) rootTag.copy(), reduceAttrs(attr));
}
return rootTag;
}
return rootTag;
}

//only focusing attr id & transform & fill
private Map<String, String> reduceAttrs(List<XmlAttribute> attributes) {
String uniqueId = "", uniqueFillColor = "", reducedTranslate = "";
for (XmlAttribute attr : attributes) {
if (attr.getName().equals("id")) {
uniqueId = attr.getValue();//后面的覆盖前面的
}
if (attr.getName().equals("fill")) {
uniqueFillColor = attr.getValue();
}
if (attr.getName().equals("transform")) {
reducedTranslate = TranslateReduceUtil.reduce(reducedTranslate, attr.getValue());
}
}
Map<String, String> result = new HashMap<String, String>();
if (!TextUtils.isEmpty(uniqueId)) {
result.put("id", uniqueId);
}
if (!TextUtils.isEmpty(uniqueFillColor)) {
result.put("fill", uniqueFillColor);
}
if (!TextUtils.isEmpty(reducedTranslate)) {
result.put("transform", reducedTranslate);
}
Logger.debug("Reduced Attrs:" + result.toString());
return result;
}

private int getNumber(String src) {
String result = src.split("\\D+")[0];
return Integer.valueOf(result);
Expand Down
28 changes: 16 additions & 12 deletions src/com/moxun/s2v/Transformer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.moxun.s2v;

import com.intellij.codeStyle.CodeStyleFacade;
import com.intellij.icons.AllIcons;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
Expand All @@ -15,19 +13,17 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.formatter.FormatterUtil;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.moxun.s2v.message.InfoMessage;
import com.moxun.s2v.utils.*;
import org.apache.xerces.impl.dtd.XMLAttributeDecl;

import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
* Created by moxun on 15/12/14.
Expand Down Expand Up @@ -76,6 +72,10 @@ public void transforming() {
//set group's attrs
Map<String, String> svgGroupAttrs = svgParser.getChildAttrs(g);
for (String key : svgGroupAttrs.keySet()) {
if (key.equals("fill")) {
//<group> tag not support color attr
continue;
}
if (AttrMapper.getAttrName(key) != null) {
group.setAttribute(AttrMapper.getAttrName(key), svgGroupAttrs.get(key));
}
Expand Down Expand Up @@ -119,8 +119,12 @@ private void parseShapeNode(XmlTag srcTag, XmlTag distTag) {
}
}

if (!childAttrs.keySet().contains("fill")) {
element.setAttribute("android:fillColor", "#000000");
if (element.getAttribute("android:fillColor") == null) {
if (srcTag.getAttribute("fill") != null) {
element.setAttribute("android:fillColor", StdColorUtil.formatColor(srcTag.getAttribute("fill").getValue()));
} else {
element.setAttribute("android:fillColor", "#00000000");
}
}

distTag.addSubTag(element, false);
Expand All @@ -143,19 +147,19 @@ protected void run(Result result) throws Throwable {
PsiDirectory directory = modulesUtil.getOrCreateDrawableDir(moduleName, getDrawableDirName());
PsiFile psiFile = directory.findFile(xmlName);
if (psiFile != null) {
Logger.debug(xmlName + "is existed, delete it.");
Logger.warn(xmlName + "is existed, delete it.");
psiFile.delete();
}
directory.add(file);

Collection<VirtualFile> virtualFiles = FilenameIndex.getVirtualFilesByName(project, xmlName, GlobalSearchScope.allScope(project));
for (VirtualFile vfile : virtualFiles) {
if (dpi.equals("nodpi") && vfile.getPath().contains("/drawable/")) {
Logger.debug("Open file in editor: " + vfile.getPath());
Logger.info("Open file in editor: " + vfile.getPath());
FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, vfile), true);
break;
} else if (vfile.getPath().contains(dpi)) {
Logger.debug("Open file in editor: " + vfile.getPath());
Logger.info("Open file in editor: " + vfile.getPath());
FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, vfile), true);
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/com/moxun/s2v/utils/AttrMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public static boolean isShapeName(String name) {
}

public static String getAttrName(String svgAttrName) {
if (!mapper.containsKey(svgAttrName)) {
if (!mapper.containsKey(svgAttrName) && !svgAttrName.equals("transform")) {
Logger.warn("Skipping attr [" + svgAttrName + "], because it not supported by Android.");
}
return mapper.get(svgAttrName);
}

public static Map<String, String> getTranslateAttrs(String transAttr) {
Map<String, String> result = new HashMap<String, String>();
String tmp = transAttr.replaceAll(" ", ",");
String tmp = transAttr.replaceAll(" ", ",");
String translate = StringUtils.substringBetween(tmp, "translate(", ")");
String scale = StringUtils.substringBetween(tmp, "scale(", ")");
String rotate = StringUtils.substringBetween(tmp, "rotate(", ")");
Expand Down Expand Up @@ -96,7 +96,7 @@ public static Map<String, String> getTranslateAttrs(String transAttr) {

//test case
public static void main(String args[]) {
String s = "translate(100 50),scale(0.5) rotate(30,100,100)";
String s = "translate(100 50),scale(0.5)rotate(30,100,100)";
System.out.println(getTranslateAttrs(s));
}
}
Loading

0 comments on commit 987090d

Please sign in to comment.