c# – 如何以编程方式压缩SVG文件中的变换?
我需要编写一些C#代码,以编程方式对任何SVG文件进行变换.
此选项在SVG编辑器中可用,如Affinity Designer(?$40 / Mac)或Inkscape(FOSS): 但是你如何以编程方式执行此操作?即使是好的SVG c# libraries也没有提供这样做的方法. 在SO上有很多讨论(即here,here,here和here).在那些建议一些工具,但没有人给出代码(除了一些对我来说没用的javascript片段,因为它使用浏览器API来获取转换后的坐标). 我需要在C#代码中执行此操作,因为要操纵svg元素,我需要那些独立于父变换的元素,包括椭圆弧,渐变,文本和tspan元素. 编辑澄清: 解决方法
这是我的问题的初步答案.但它仍然存在一些元素的问题(带有剪辑路径,蒙版或滤镜属性的元素,或者使用网址或defs,在展平后似乎已经破坏了坐标).任何进一步的帮助和改进表示赞赏.
public void FlattenNodeRecursive(XElement item) { var children_elements = item.Elements(); if ( IsSVGElem(item,"g") && HasAttr(item,"transform") && !item.IsEmpty && item.Elements().Any((XElement inner) => { return IsSVGElem(inner,"path") || IsSVGElem(inner,"text") || IsSVGElem(inner,"g"); }) && item.Elements().Any((XElement inner) => { return !IsSVGElem(inner,"tspan"); }) ) { XAttribute parent_attribute = item.Attribute("transform"); foreach (var inner in children_elements) { if (HasAttr(inner,"transform")) { inner.Attribute("transform").SetValue(ConcatenateTransforms((parent_attribute.Value.Trim() + " " + inner.Attribute("transform").Value.Trim())).Trim()); } else { XAttribute attribute = new XAttribute("transform",parent_attribute.Value.Trim()); inner.Add(attribute); } } parent_attribute.Remove(); } foreach(XElement xelem in children_elements){ if(xelem.Elements() != null) { FlattenNodeRecursive(xelem); } } } 注意:我没有发布HasAttr方法或ConcatenateTransforms方法源代码,因为它们非常简单,但如果有人需要那些我会发布它们. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |