
此选项在SVG编辑器中可用,如Affinity Designer(〜$40 / Mac)或Inkscape(FOSS):
但是你如何以编程方式执行此 *** 作?即使是好的SVG c# libraries也没有提供这样做的方法.
在SO上有很多讨论(即here,here,here和here).在那些建议一些工具,但没有人给出代码(除了一些对我来说没用的JavaScript片段,因为它使用浏览器API来获取转换后的坐标).
我需要在C#代码中执行此 *** 作,因为要 *** 纵svg元素,我需要那些独立于父变换的元素,包括椭圆弧,渐变,文本和tspan元素.
编辑澄清:
我不需要转换元素.我只需要在最深的子节点中存在转换矩阵,所以我不再需要考虑父母了.为此,我需要在单个矩阵中连接所有父矩阵,并将其作为单个转换属性分配给svg元素.我不一定需要转换路径或弧,例如将椭圆转换为路径,或重新采样位图.我只需要能够为每个元素计算一个矩阵变换,它自己,作为变换属性分配给它,而不是每次都要考虑所有父组变换.
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方法源代码,因为它们非常简单,但如果有人需要那些我会发布它们.
总结以上是内存溢出为你收集整理的c# – 如何以编程方式压缩SVG文件中的变换?全部内容,希望文章能够帮你解决c# – 如何以编程方式压缩SVG文件中的变换?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)