1<?xml version="1.0" encoding="UTF-8"?>
2<!--***********************************************************
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements.  See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership.  The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License.  You may obtain a copy of the License at
11 *
12 *   http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied.  See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 *
21 ***********************************************************-->
22
23
24<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" exclude-result-prefixes="office table style text draw svg   dc config xlink meta oooc dom ooo chart math dr3d form script ooow draw">
25    <xsl:template name="page-background">
26        <xsl:choose>
27            <xsl:when test="/office:document/office:automatic-styles/style:page-layout/style:page-layout-properties/style:background-image[string-length(office:binary-data/text()) &gt; 0]">
28                <w:bgPict>
29                    <xsl:apply-templates select="/office:document/office:automatic-styles/style:page-layout/style:page-layout-properties/style:background-image[string-length(office:binary-data/text()) &gt; 0]" mode="bgPict"/>
30                </w:bgPict>
31            </xsl:when>
32            <xsl:when test="/office:document/office:automatic-styles/style:page-layout/style:page-layout-properties[string-length(@fo:background-color) &gt; 0]">
33                <w:bgPict>
34                    <xsl:apply-templates select="/office:document/office:automatic-styles/style:page-layout/style:page-layout-properties[string-length(@fo:background-color) &gt; 0]" mode="bgPict"/>
35                </w:bgPict>
36            </xsl:when>
37        </xsl:choose>
38    </xsl:template>
39    <xsl:template match="style:background-image" mode="bgPict">
40        <xsl:variable name="binName" select="concat('wordml://',generate-id(.))"/>
41        <w:binData w:name="{$binName}">
42            <xsl:value-of select="translate(office:binary-data/text(),'&#9;&#10;&#13;&#32;','' ) "/>
43        </w:binData>
44        <w:background w:bgcolor="{parent::style:page-layout-propertie/@fo:background-color}" w:background="{$binName}"/>
45    </xsl:template>
46    <xsl:template match="style:page-layout-properties" mode="bgPict">
47        <w:background w:bgcolor="{@fo:background-color}"/>
48    </xsl:template>
49    <xsl:template match="style:master-page">
50        <xsl:apply-templates select="key( 'page-layout', @style:page-layout-name)"/>
51        <xsl:if test="style:header">
52            <w:hdr w:type="odd">
53                <xsl:apply-templates select="style:header/text:p | style:header/table:table"/>
54                <!-- change style:header//text:p to style:header/text:p and  add table:table here, fix for  Issue 32035 -->
55            </w:hdr>
56        </xsl:if>
57        <xsl:if test="style:header-left">
58            <w:hdr w:type="even">
59                <xsl:apply-templates select="style:header-left/text:p | style:header-left/table:table"/>
60                <!-- change style:header//text:p to style:header/text:p and  add table:table here, fix for  Issue 32035 -->
61            </w:hdr>
62        </xsl:if>
63        <xsl:if test="style:footer">
64            <w:ftr w:type="odd">
65                <xsl:apply-templates select="style:footer/text:p | style:footer/table:table"/>
66                <!-- change style:header//text:p to style:header/text:p and  add table:table here, fix for  Issue 32035 -->
67            </w:ftr>
68        </xsl:if>
69        <xsl:if test="style:footer-left">
70            <w:ftr w:type="even">
71                <xsl:apply-templates select="style:footer-left/text:p | style:footer-left/table:table"/>
72                <!-- change style:header//text:p to style:header/text:p and  add table:table here, fix for  Issue 32035 -->
73            </w:ftr>
74        </xsl:if>
75    </xsl:template>
76    <xsl:template match="style:page-layout">
77        <xsl:choose>
78            <xsl:when test="@style:page-usage = 'left'">
79                <w:type w:val="even-page"/>
80            </xsl:when>
81            <xsl:when test="@style:page-usage = 'right'">
82                <w:type w:val="odd-page"/>
83            </xsl:when>
84            <xsl:when test="@style:page-usage = 'all'">
85                <w:type w:val="next-page"/>
86            </xsl:when>
87            <!-- for mirrored, and default -->
88            <xsl:otherwise>
89                <w:type w:val="next-page"/>
90            </xsl:otherwise>
91        </xsl:choose>
92        <xsl:variable name="page-width">
93            <xsl:if test="style:page-layout-properties/@fo:page-width">
94                <xsl:call-template name="convert2twip">
95                    <xsl:with-param name="value" select="style:page-layout-properties/@fo:page-width"/>
96                </xsl:call-template>
97            </xsl:if>
98        </xsl:variable>
99        <xsl:variable name="margin-left">
100            <xsl:if test="style:page-layout-properties/@fo:margin-left">
101                <xsl:call-template name="convert2twip">
102                    <xsl:with-param name="value" select="style:page-layout-properties/@fo:margin-left"/>
103                </xsl:call-template>
104            </xsl:if>
105        </xsl:variable>
106        <xsl:variable name="margin-right">
107            <xsl:if test="style:page-layout-properties/@fo:margin-right">
108                <xsl:call-template name="convert2twip">
109                    <xsl:with-param name="value" select="style:page-layout-properties/@fo:margin-right"/>
110                </xsl:call-template>
111            </xsl:if>
112        </xsl:variable>
113        <w:pgSz>
114            <xsl:if test="style:page-layout-properties/@fo:page-width">
115                <xsl:attribute name="w:w">
116                    <xsl:value-of select="$page-width"/>
117                </xsl:attribute>
118            </xsl:if>
119            <xsl:if test="style:page-layout-properties/@fo:page-height">
120                <xsl:attribute name="w:h">
121                    <xsl:call-template name="convert2twip">
122                        <xsl:with-param name="value" select="style:page-layout-properties/@fo:page-height"/>
123                    </xsl:call-template>
124                </xsl:attribute>
125            </xsl:if>
126            <xsl:if test="style:page-layout-properties/@style:print-orientation">
127                <xsl:attribute name="w:orient">
128                    <xsl:value-of select="style:page-layout-properties/@style:print-orientation"/>
129                </xsl:attribute>
130            </xsl:if>
131        </w:pgSz>
132        <w:pgMar>
133            <xsl:if test="style:page-layout-properties/@fo:margin-top">
134                <xsl:variable name="top-margin">
135                    <xsl:call-template name="convert2twip">
136                        <xsl:with-param name="value" select="style:page-layout-properties/@fo:margin-top"/>
137                    </xsl:call-template>
138                </xsl:variable>
139                <xsl:attribute name="w:top">
140                    <xsl:value-of select="$top-margin"/>
141                </xsl:attribute>
142                <xsl:if test="style:header-style/style:page-layout-properties/@fo:min-height">
143                    <xsl:variable name="header-height">
144                        <xsl:call-template name="convert2twip">
145                            <xsl:with-param name="value" select="style:header-style/style:page-layout-properties/@fo:min-height"/>
146                        </xsl:call-template>
147                    </xsl:variable>
148                    <xsl:attribute name="w:header">
149                        <xsl:value-of select="$top-margin - $header-height"/>
150                    </xsl:attribute>
151                </xsl:if>
152            </xsl:if>
153            <xsl:if test="style:page-layout-properties/@fo:margin-bottom">
154                <xsl:variable name="bottom-margin">
155                    <xsl:call-template name="convert2twip">
156                        <xsl:with-param name="value" select="style:page-layout-properties/@fo:margin-bottom"/>
157                    </xsl:call-template>
158                </xsl:variable>
159                <xsl:attribute name="w:bottom">
160                    <xsl:value-of select="$bottom-margin"/>
161                </xsl:attribute>
162                <xsl:if test="style:footer-style/style:page-layout-properties/@fo:min-height">
163                    <xsl:variable name="footer-height">
164                        <xsl:call-template name="convert2twip">
165                            <xsl:with-param name="value" select="style:footer-style/style:page-layout-properties/@fo:min-height"/>
166                        </xsl:call-template>
167                    </xsl:variable>
168                    <xsl:attribute name="w:footer">
169                        <xsl:value-of select="$bottom-margin - $footer-height"/>
170                    </xsl:attribute>
171                </xsl:if>
172            </xsl:if>
173            <xsl:if test="style:page-layout-properties/@fo:margin-left">
174                <xsl:attribute name="w:left">
175                    <xsl:value-of select="$margin-left"/>
176                </xsl:attribute>
177                <xsl:attribute name="w:gutter">
178                    <xsl:value-of select="'0'"/>
179                </xsl:attribute>
180            </xsl:if>
181            <xsl:if test="style:page-layout-properties/@fo:margin-right">
182                <xsl:attribute name="w:right">
183                    <xsl:value-of select="$margin-right"/>
184                </xsl:attribute>
185            </xsl:if>
186        </w:pgMar>
187        <xsl:variable name="border-top" select="style:page-layout-properties/@fo:border-top | style:page-layout-properties/@fo:border"/>
188        <xsl:variable name="border-bottom" select="style:page-layout-properties/@fo:border-bottom | style:page-layout-properties/@fo:border"/>
189        <xsl:variable name="border-left" select="style:page-layout-properties/@fo:border-left | style:page-layout-properties/@fo:border"/>
190        <xsl:variable name="border-right" select="style:page-layout-properties/@fo:border-right | style:page-layout-properties/@fo:border"/>
191        <xsl:variable name="border-line-width-top" select="style:page-layout-properties/@style:border-line-width-top | style:page-layout-properties/@style:border-line-width "/>
192        <xsl:variable name="border-line-width-bottom" select="style:page-layout-properties/@style:border-line-width-bottom | style:page-layout-properties/@style:border-line-width"/>
193        <xsl:variable name="border-line-width-left" select="style:page-layout-properties/@style:border-line-width-left | style:page-layout-properties/@style:border-line-width"/>
194        <xsl:variable name="border-line-width-right" select="style:page-layout-properties/@style:border-line-width-right | style:page-layout-properties/@style:border-line-width"/>
195        <xsl:variable name="padding-top" select="style:page-layout-properties/@fo:padding-top | style:page-layout-properties/@fo:padding"/>
196        <xsl:variable name="padding-bottom" select="style:page-layout-properties/@fo:padding-bottom | style:page-layout-properties/@fo:padding"/>
197        <xsl:variable name="padding-left" select="style:page-layout-properties/@fo:padding-left | style:page-layout-properties/@fo:padding"/>
198        <xsl:variable name="padding-right" select="style:page-layout-properties/@fo:padding-right | style:page-layout-properties/@fo:padding"/>
199        <w:pgBorders w:offset-from="text">
200            <xsl:if test="$border-top">
201                <xsl:element name="w:top">
202                    <xsl:call-template name="get-border">
203                        <xsl:with-param name="so-border" select="$border-top"/>
204                        <xsl:with-param name="so-border-line-width" select="$border-line-width-top"/>
205                        <xsl:with-param name="so-border-position" select=" 'top' "/>
206                    </xsl:call-template>
207                    <xsl:attribute name="w:space">
208                        <xsl:call-template name="convert2pt">
209                            <xsl:with-param name="value" select="$padding-top"/>
210                        </xsl:call-template>
211                    </xsl:attribute>
212                    <xsl:if test="style:page-layout-properties/@style:shadow!='none'">
213                        <xsl:attribute name="w:shadow">on</xsl:attribute>
214                    </xsl:if>
215                </xsl:element>
216            </xsl:if>
217            <xsl:if test="$border-bottom">
218                <xsl:element name="w:bottom">
219                    <xsl:call-template name="get-border">
220                        <xsl:with-param name="so-border" select="$border-bottom"/>
221                        <xsl:with-param name="so-border-line-width" select="$border-line-width-bottom"/>
222                        <xsl:with-param name="so-border-position" select=" 'bottom' "/>
223                    </xsl:call-template>
224                    <xsl:attribute name="w:space">
225                        <xsl:call-template name="convert2pt">
226                            <xsl:with-param name="value" select="$padding-bottom"/>
227                        </xsl:call-template>
228                    </xsl:attribute>
229                    <xsl:if test="style:page-layout-properties/@style:shadow!='none'">
230                        <xsl:attribute name="w:shadow">on</xsl:attribute>
231                    </xsl:if>
232                </xsl:element>
233            </xsl:if>
234            <xsl:if test="$border-left">
235                <xsl:element name="w:left">
236                    <xsl:call-template name="get-border">
237                        <xsl:with-param name="so-border" select="$border-left"/>
238                        <xsl:with-param name="so-border-line-width" select="$border-line-width-left"/>
239                        <xsl:with-param name="so-border-position" select=" 'left' "/>
240                    </xsl:call-template>
241                    <xsl:attribute name="w:space">
242                        <xsl:call-template name="convert2pt">
243                            <xsl:with-param name="value" select="$padding-left"/>
244                        </xsl:call-template>
245                    </xsl:attribute>
246                    <xsl:if test="style:page-layout-properties/@style:shadow!='none'">
247                        <xsl:attribute name="w:shadow">on</xsl:attribute>
248                    </xsl:if>
249                </xsl:element>
250            </xsl:if>
251            <xsl:if test="$border-right">
252                <xsl:element name="w:right">
253                    <xsl:call-template name="get-border">
254                        <xsl:with-param name="so-border" select="$border-right"/>
255                        <xsl:with-param name="so-border-line-width" select="$border-line-width-right"/>
256                        <xsl:with-param name="so-border-position" select=" 'right' "/>
257                    </xsl:call-template>
258                    <xsl:attribute name="w:space">
259                        <xsl:call-template name="convert2pt">
260                            <xsl:with-param name="value" select="$padding-right"/>
261                        </xsl:call-template>
262                    </xsl:attribute>
263                    <xsl:if test="style:page-layout-properties/@style:shadow!='none'">
264                        <xsl:attribute name="w:shadow">on</xsl:attribute>
265                    </xsl:if>
266                </xsl:element>
267            </xsl:if>
268        </w:pgBorders>
269        <xsl:variable name="valid-width">
270            <xsl:value-of select="$page-width - $margin-left - $margin-right"/>
271        </xsl:variable>
272        <xsl:apply-templates select="style:page-layout-properties/style:columns">
273            <xsl:with-param name="page-width" select="$valid-width"/>
274        </xsl:apply-templates>
275        <xsl:apply-templates select="/office:document/office:styles/text:linenumbering-configuration"/>
276    </xsl:template>
277    <xsl:template match="text:linenumbering-configuration">
278        <xsl:if test="not(@text:number-lines = 'false')">
279            <xsl:element name="w:lnNumType">
280                <xsl:if test="@text:increment">
281                    <xsl:attribute name="w:count-by">
282                        <xsl:value-of select="@text:increment"/>
283                    </xsl:attribute>
284                </xsl:if>
285                <xsl:if test="@text:offset">
286                    <xsl:attribute name="w:distance">
287                        <xsl:call-template name="convert2twip">
288                            <xsl:with-param name="value" select="@text:offset"/>
289                        </xsl:call-template>
290                    </xsl:attribute>
291                </xsl:if>
292                <xsl:attribute name="w:restart">continuous</xsl:attribute>
293            </xsl:element>
294        </xsl:if>
295    </xsl:template>
296    <xsl:template match="style:style" mode="section">
297        <xsl:param name="master-page"/>
298        <xsl:variable name="page-width">
299            <xsl:call-template name="convert2twip">
300                <xsl:with-param name="value" select="$master-page/style:page-layout-properties/@fo:page-width"/>
301            </xsl:call-template>
302        </xsl:variable>
303        <xsl:variable name="margin-left">
304            <xsl:call-template name="convert2twip">
305                <xsl:with-param name="value" select="$master-page/style:page-layout-properties/@fo:margin-left"/>
306            </xsl:call-template>
307        </xsl:variable>
308        <xsl:variable name="margin-right">
309            <xsl:call-template name="convert2twip">
310                <xsl:with-param name="value" select="$master-page/style:page-layout-properties/@fo:margin-right"/>
311            </xsl:call-template>
312        </xsl:variable>
313        <xsl:variable name="valid-width">
314            <xsl:value-of select="$page-width - $margin-left - $margin-right"/>
315        </xsl:variable>
316        <w:type w:val="continuous"/>
317        <xsl:apply-templates select="style:section-properties/style:columns">
318            <xsl:with-param name="page-width" select="$valid-width"/>
319        </xsl:apply-templates>
320    </xsl:template>
321    <xsl:template match="style:columns">
322        <xsl:param name="page-width"/>
323        <w:cols w:num="{@fo:column-count}">
324            <xsl:if test="@fo:column-gap">
325                <xsl:attribute name="w:space">
326                    <xsl:call-template name="convert2twip">
327                        <xsl:with-param name="value" select="@fo:column-gap"/>
328                    </xsl:call-template>
329                </xsl:attribute>
330            </xsl:if>
331            <xsl:if test="style:column-sep">
332                <xsl:attribute name="w:sep">on</xsl:attribute>
333            </xsl:if>
334            <xsl:choose>
335                <xsl:when test="not(style:column)">
336                    <xsl:attribute name="w:equalWidth">on</xsl:attribute>
337                </xsl:when>
338                <xsl:otherwise>
339                    <xsl:attribute name="w:equalWidth">off</xsl:attribute>
340                    <xsl:variable name="column-relative-width">
341                        <xsl:call-template name="get-sum-column-width">
342                            <xsl:with-param name="current-column" select="style:column[1]"/>
343                            <xsl:with-param name="current-width" select="'0'"/>
344                        </xsl:call-template>
345                    </xsl:variable>
346                    <xsl:for-each select="style:column">
347                        <xsl:element name="w:col">
348                            <xsl:attribute name="w:w">
349                                <xsl:value-of select="floor(substring-before(@style:rel-width,'*') * $page-width div $column-relative-width)"/>
350                            </xsl:attribute>
351                            <xsl:if test="@fo:margin-right">
352                                <xsl:variable name="margin-right">
353                                    <xsl:call-template name="convert2twip">
354                                        <xsl:with-param name="value" select="@fo:margin-right"/>
355                                    </xsl:call-template>
356                                </xsl:variable>
357                                <xsl:choose>
358                                    <xsl:when test="following-sibling::style:column">
359                                        <xsl:variable name="margin-left">
360                                            <xsl:call-template name="convert2twip">
361                                                <xsl:with-param name="value" select="@fo:margin-left"/>
362                                            </xsl:call-template>
363                                        </xsl:variable>
364                                        <xsl:attribute name="w:space">
365                                            <xsl:value-of select="$margin-right + $margin-left"/>
366                                        </xsl:attribute>
367                                    </xsl:when>
368                                    <xsl:otherwise>
369                                        <xsl:attribute name="w:space">
370                                            <xsl:value-of select="$margin-right"/>
371                                        </xsl:attribute>
372                                    </xsl:otherwise>
373                                </xsl:choose>
374                            </xsl:if>
375                        </xsl:element>
376                    </xsl:for-each>
377                </xsl:otherwise>
378            </xsl:choose>
379        </w:cols>
380    </xsl:template>
381    <xsl:template name="get-sum-column-width">
382        <xsl:param name="current-column"/>
383        <xsl:param name="current-width"/>
384        <xsl:variable name="new-width" select="$current-width + substring-before($current-column/@style:rel-width,'*')"/>
385        <xsl:choose>
386            <xsl:when test="$current-column/following-sibling::style:column">
387                <xsl:call-template name="get-sum-column-width">
388                    <xsl:with-param name="current-column" select="$current-column/following-sibling::style:column[1]"/>
389                    <xsl:with-param name="current-width" select="$new-width"/>
390                </xsl:call-template>
391            </xsl:when>
392            <xsl:otherwise>
393                <xsl:value-of select="$new-width"/>
394            </xsl:otherwise>
395        </xsl:choose>
396    </xsl:template>
397</xsl:stylesheet>
398