Skip to content

Commit

Permalink
Convert SVG into EMF #19
Browse files Browse the repository at this point in the history
  • Loading branch information
jelovirt committed Feb 9, 2017
1 parent c32a990 commit 1f7c584
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 69 deletions.
26 changes: 26 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>

<taskdef resource="net/sf/antcontrib/antlib.xml"/>

<target name="docx.init">
<property name="force-unique" value="true"/>
<property name="docx.dir" location="${dita.plugin.com.elovirta.ooxml.dir}/docx"/>
Expand All @@ -15,6 +17,7 @@
build-init,
preprocess,
docx.image-metadata,
docx.svg2emf,
docx.template,
docx.convert,
docx.package"/>
Expand All @@ -27,6 +30,29 @@
</pipeline>
</target>

<target name="docx.svg2emf" if="inkscape.exec">
<mkdir dir="${dita.temp.dir}/docx/word/media"/>
<for param="svg.file">
<path>
<fileset dir="${user.input.dir}">
<include name="**/*.svg"/>
</fileset>
</path>
<sequential>
<local name="svg.name"/>
<local name="emf.file"/>
<basename property="svg.name" file="@{svg.file}" suffix=".svg"/>
<property name="emf.file" location="${dita.temp.dir}/docx/word/media/${svg.name}.emf"/>
<echo taskname="inkscape">Converting @{svg.file} to ${emf.file}</echo>
<exec executable="${inkscape.exec}" taskname="inkscape">
<arg value="-z" />
<arg value="--export-emf=${emf.file}" />
<arg file="@{svg.file}"/>
</exec>
</sequential>
</for>
</target>

<target name="docx.template">
<unzip src="${dotx.file}" dest="${dita.temp.dir}/dotx">
<patternset>
Expand Down
1 change: 1 addition & 0 deletions docx/[Content_Types].xsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Default Extension="rle" ContentType="image/bmp"/>
<Default Extension="bmz" ContentType="image/bmp"/>
<Default Extension="wmf" ContentType="windows/metafile"/>
<Default Extension="emf" ContentType="image/x-emf"/>
<Default Extension="eps" ContentType="application/postscript"/>
<Default Extension="pct" ContentType="image/x-pict"/>
</xsl:variable>
Expand Down
15 changes: 13 additions & 2 deletions docx/word/_rels/document.xml.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,19 @@
<xsl:comment>images and links</xsl:comment>
<xsl:for-each select="//@x:image-number">
<Relationship Id="rId{.}"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
Target="media/{../@href}"/>
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image">
<xsl:attribute name="Target">
<xsl:text>media/</xsl:text>
<xsl:choose>
<xsl:when test="ends-with(../@href, '.svg')">
<xsl:value-of select="replace(../@href, '\.svg$', '.emf')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="../@href"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</Relationship>
</xsl:for-each>

<xsl:for-each select="//*[@x:external-link-number]">
Expand Down
161 changes: 94 additions & 67 deletions docx/word/document.topic.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -437,73 +437,100 @@
<xsl:param name="styles" as="element()*" tunnel="yes">
<xsl:apply-templates select="." mode="inline-style"/>
</xsl:param>
<!-- Units are English metric units: 1 EMU = 1 div 914400 in = 1 div 360000 cm -->
<xsl:variable name="width" as="xs:integer?">
<xsl:if test="@dita-ot:image-width">
<xsl:sequence select="x:to-emu(@dita-ot:image-width, @dita-ot:horizontal-dpi)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="height" as="xs:integer?">
<xsl:if test="@dita-ot:image-height">
<xsl:sequence select="x:to-emu(@dita-ot:image-height, @dita-ot:vertical-dpi)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="size" as="xs:integer*"
select="if (exists($width) and exists($height))
then x:scale-to-max-box($width, $height)
else ()"/>
<w:r>
<xsl:if test="exists($styles)">
<w:rPr>
<xsl:copy-of select="$styles"/>
</w:rPr>
</xsl:if>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<xsl:if test="exists($size[1]) and exists($size[2])">
<wp:extent cx="{$size[1]}" cy="{$size[2]}"/>
</xsl:if>
<wp:effectExtent l="0" t="0" r="0" b="0"/>
<wp:docPr id="1" name="Picture 1"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks noChangeAspect="1"/>
</wp:cNvGraphicFramePr>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic>
<pic:nvPicPr>
<pic:cNvPr id="0" name="media/{@href}"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId{@x:image-number}">
<a:extLst>
<a:ext uri="{{28A0092B-C50C-407E-A947-70E740481C1C}}">
<a14:useLocalDpi val="0"/>
</a:ext>
</a:extLst>
</a:blip>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<xsl:if test="exists($width) and exists($height)">
<a:ext cx="{$width}" cy="{$height}"/>
</xsl:if>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
<xsl:variable name="image.url" select="concat($input.dir.url, @href)" as="xs:string"/>
<xsl:message select="$image.url, $image.dir, unparsed-text-available($image.url)"></xsl:message>
<xsl:choose>
<xsl:when test="unparsed-text-available($image.url)">
<!-- Units are English metric units: 1 EMU = 1 div 914400 in = 1 div 360000 cm -->
<xsl:variable name="width" as="xs:integer?">
<xsl:if test="@dita-ot:image-width">
<xsl:sequence select="x:to-emu(@dita-ot:image-width, @dita-ot:horizontal-dpi)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="height" as="xs:integer?">
<xsl:if test="@dita-ot:image-height">
<xsl:sequence select="x:to-emu(@dita-ot:image-height, @dita-ot:vertical-dpi)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="size" as="xs:integer*"
select="if (exists($width) and exists($height))
then x:scale-to-max-box($width, $height)
else ()"/>
<w:r>
<xsl:if test="exists($styles)">
<w:rPr>
<xsl:copy-of select="$styles"/>
</w:rPr>
</xsl:if>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<xsl:if test="exists($size[1]) and exists($size[2])">
<wp:extent cx="{$size[1]}" cy="{$size[2]}"/>
</xsl:if>
<wp:effectExtent l="0" t="0" r="0" b="0"/>
<wp:docPr id="1" name="Picture 1"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks noChangeAspect="1"/>
</wp:cNvGraphicFramePr>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic>
<pic:nvPicPr>
<pic:cNvPr id="0">
<xsl:attribute name="name">
<xsl:text>media/</xsl:text>
<xsl:choose>
<xsl:when test="ends-with(@href, '.svg')">
<xsl:value-of select="replace(@href, '\.svg$', '.emf')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@href"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</pic:cNvPr>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId{@x:image-number}">
<a:extLst>
<a:ext uri="{{28A0092B-C50C-407E-A947-70E740481C1C}}">
<a14:useLocalDpi val="0"/>
</a:ext>
</a:extLst>
</a:blip>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<xsl:if test="exists($width) and exists($height)">
<a:ext cx="{$width}" cy="{$height}"/>
</xsl:if>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
</xsl:when>
<xsl:otherwise>
<w:r>
<w:t>
<xsl:text>Image </xsl:text>
<xsl:value-of select="@href"/>
<xsl:text> missing</xsl:text>
</w:t>
</w:r>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:function name="x:scale-to-max-box" as="xs:integer+">
Expand Down
Binary file added lib/ant-contrib-0.6.jar
Binary file not shown.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<feature extension="dita.conductor.transtype.check" value="docx"/>
<feature extension="dita.transtype.print" value="docx"/>
<feature extension="dita.conductor.target.relative" file="integrator.xml"/>
<feature extension="dita.conductor.lib.import" file="lib/ant-contrib-0.6.jar"/>
</plugin>

1 comment on commit 1f7c584

@stefan-jung
Copy link

@stefan-jung stefan-jung commented on 1f7c584 Feb 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for coding this 👍
I tried to code and PR this, but failed. 🤕

I tested this, but I could not get it working. I set inkscape.exec, but SVGs are not converted. Tried settings inkscape.exec to inkscape, C\:/Program Files/Inkscape/inkscape and C\:/Program Files/Inkscape/inkscape.exe.

Please sign in to comment.