javaFX 处理SVG的方法,从SVG文件中读取path,转换为SVGPath,以及封装为Region. svg来自阿里矢量图

1 篇文章 0 订阅
import javafx.scene.layout.Region;
import javafx.scene.shape.SVGPath;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.*;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author guolinyuan
 */
public class SvgUtil
{
    static
    {
        svgs = new HashMap<>();
        regions = new HashMap<>();
        loadGlyphToMM("svg/iconfont.svg");
        loadPathToMM("svg/edit-filling.svg");
    }

    private final static Map<String, SVGPath> svgs;
    private final static Map<String, Region> regions;
    private static final String defaultStyle = "-fx-background-color: #333333;-fx-pref-width: 15;-fx-pref-height: 15;-fx-scale-y: -1";

    /**
     * 将目标文件加载到内存中,并且转化为SVGPath对象
     * 可以通过{@link SvgUtil#getSvg(String)}访问到
     * 适用于glyph标签,"svg/iconfont.svg"
     */
    public static void loadGlyphToMM(String pathName)
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try
        {
            DocumentBuilder builder = factory.newDocumentBuilder();
            //禁止DTD验证,防止网络阻塞
            builder.setEntityResolver(
                    (publicId, systemId) -> new InputSource(new StringReader(""))
            );
            Document d = builder.parse(SvgUtil.class.getClassLoader().getResourceAsStream(pathName));
            NodeList list = d.getElementsByTagName("glyph");
            for (int i = 0; i < list.getLength(); i++)
            {
                Node node = list.item(i);
                String content = node.getAttributes().getNamedItem("d").getNodeValue();
                String name = node.getAttributes().getNamedItem("glyph-name").getNodeValue();
                SVGPath path = new SVGPath();
                path.setContent(content);
                svgs.put(name, path);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 从单个svg文件的svg/path中读取path,
     * 文件名作为svg的key,需要拓展名为.svg结尾
     *
     * @param pathName
     */
    public static void loadPathToMM(String pathName)
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try
        {
            DocumentBuilder builder = factory.newDocumentBuilder();
            //禁止DTD验证,防止网络阻塞
            builder.setEntityResolver(
                    (publicId, systemId) -> new InputSource(new StringReader(""))
            );
            Document d = builder.parse(SvgUtil.class.getClassLoader().getResourceAsStream(pathName));
            Node node = d.getElementsByTagName("path").item(0);
            String content = node.getAttributes().getNamedItem("d").getNodeValue();
            String name = pathName.substring(pathName.lastIndexOf("/")+1, pathName.length() - 4);
            SVGPath path = new SVGPath();
            path.setContent(content);
            svgs.put(name, path);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static SVGPath getSvg(String name)
    {
        return svgs.get(name);
    }

    /**
     * 获取到默认的样式的region包裹的svg
     * 默认样式包含背景色,Y轴翻转,20x20的大小
     * 样式有缺陷时,可以调用重载方法完成
     *
     * @param name
     * @return
     */
    public static Region getRegion(String name)
    {
        Region region = regions.get(name);
        if (region == null)
        {
            SVGPath path = getSvg(name);
            if (path != null)
            {
                region = new Region();
                region.setShape(path);
                region.setStyle(defaultStyle);
                return region;
            }
            return null;
        }
        return region;
    }

    public static Region getRegion(String name, String style)
    {
        Region region = getRegion(name);
        if (region != null)
        {
            region.setStyle(style);
            return region;
        }
        return null;
    }

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值