tdf#170396: implement SVG export support for dashed lines

Commit b71d9a6d15 (do not apply line dashing
in drawinglayer (tdf#136957), 2021-04-29) made sure that metafiles pass
dashing information in LineInfo of MetaPolyLineAction, instead of altering
the polyline itself. Since SVG export ignored dashing information in
MetaPolyLineAction, the exported lines were solid since that patch.

This patch introduces respective export support, emitting stroke-dasharray
attribute. As far as I see, it has exactly the same semantics, and renders
correctly in chrome and firefox. It also imports OK into Draw.

Ref: https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/stroke-dasharray

Change-Id: Ifb634a4f3608ee5c2c76727ef4416b80f8f90709
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/197618
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski
2026-01-20 10:13:30 +01:00
parent f10eb0f12e
commit c103d337bd
3 changed files with 54 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" office:version="1.4" office:mimetype="application/vnd.oasis.opendocument.graphics">
<office:styles>
<draw:stroke-dash draw:name="DoubleDashDotDot" draw:style="rect" draw:dots1="1" draw:dots1-length="800%" draw:dots2="2" draw:dots2-length="90%" draw:distance="300%"/>
<style:style style:name="standard" style:family="graphic">
<style:graphic-properties draw:stroke="solid" svg:stroke-width="1mm" svg:stroke-color="#008000" draw:fill="none" draw:shadow="hidden"/>
</style:style>
</office:styles>
<office:automatic-styles>
<style:page-layout style:name="PM0">
<style:page-layout-properties fo:margin-top="1cm" fo:margin-bottom="1cm" fo:margin-left="1cm" fo:margin-right="1cm" fo:page-width="14cm" fo:page-height="4cm"/>
</style:page-layout>
<style:style style:name="gr1" style:family="graphic" style:parent-style-name="standard">
<style:graphic-properties draw:stroke="dash" draw:stroke-dash="DoubleDashDotDot" draw:textarea-vertical-align="middle"/>
</style:style>
</office:automatic-styles>
<office:master-styles>
<style:master-page style:name="Default" style:page-layout-name="PM0"/>
</office:master-styles>
<office:body>
<office:drawing>
<draw:page draw:name="page1" draw:master-page-name="Default">
<draw:line draw:style-name="gr1" svg:x1="2cm" svg:y1="2cm" svg:x2="12cm" svg:y2="2cm"/>
</draw:page>
</office:drawing>
</office:body>
</office:document>
+14
View File
@@ -397,6 +397,20 @@ CPPUNIT_TEST_FIXTURE(SvgFilterTest, testTdf166789)
CPPUNIT_ASSERT_DOUBLES_EQUAL(7000, length.toInt32(), 70); // allow 1% for rounding errors
}
CPPUNIT_TEST_FIXTURE(SvgFilterTest, testDashedLine)
{
// A dashed line
loadFromFile(u"dashedline.fodg");
save(TestFilter::SVG_DRAW);
xmlDocUniquePtr pXmlDoc = parseExportedFile();
CPPUNIT_ASSERT(pXmlDoc);
// The result must include 'stroke-dasharray' attribute
assertXPath(pXmlDoc, "/svg:svg/svg:g//svg:path", "stroke-dasharray", u"800,300,90,300,90,300");
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
+12
View File
@@ -2142,6 +2142,18 @@ void SVGActionWriter::ImplAddLineAttr( const LineInfo &rAttrs )
}
}
if (rAttrs.GetStyle() == LineStyle::Dash)
{
OUStringBuffer aDashArrayStr;
for (double x : rAttrs.GetDotDashArray())
{
if (!aDashArrayStr.isEmpty())
aDashArrayStr.append(",");
aDashArrayStr.append(x);
}
if (!aDashArrayStr.isEmpty())
mrExport.AddAttribute(u"stroke-dasharray"_ustr, aDashArrayStr.makeStringAndClear());
}
}