Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / docx / to / graphics / graphics.xsl @ 187

History | View | Annotate | Download (17.9 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2
<xsl:stylesheet xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
3
                xmlns:cals="http://www.oasis-open.org/specs/tm9901"
4
                xmlns:contypes="http://schemas.openxmlformats.org/package/2006/content-types"
5
                xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
6
                xmlns:dc="http://purl.org/dc/elements/1.1/"
7
                xmlns:dcmitype="http://purl.org/dc/dcmitype/"
8
                xmlns:dcterms="http://purl.org/dc/terms/"
9
                xmlns:html="http://www.w3.org/1999/xhtml"
10
                xmlns:iso="http://www.iso.org/ns/1.0"
11
                xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
12
                xmlns:mml="http://www.w3.org/1998/Math/MathML"
13
                xmlns:o="urn:schemas-microsoft-com:office:office"
14
                xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
15
                xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
16
		xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
17
                xmlns:tbx="http://www.lisa.org/TBX-Specification.33.0.html"
18
                xmlns:tei="http://www.tei-c.org/ns/1.0"
19
                xmlns:teidocx="http://www.tei-c.org/ns/teidocx/1.0"
20
                xmlns:teix="http://www.tei-c.org/ns/Examples"
21
                xmlns:v="urn:schemas-microsoft-com:vml"
22
                xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
23
                xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
24
                xmlns:w10="urn:schemas-microsoft-com:office:word"
25
                xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
26
                xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
27
                xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
28
                
29
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
30
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
31
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
32
                version="2.0"
33
                exclude-result-prefixes="cp ve o r m v wp w10 w wne
34
					 mml tbx iso  tei a xs
35
					 pic fn xsi dc dcterms
36
					 dcmitype rel contypes teidocx teix html cals">
37
    
38

    
39
    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
40
      <desc>
41
         <p> TEI stylesheet for making Word docx files from TEI XML
42
         </p>
43
         <p>This software is dual-licensed:
44

    
45
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
46
Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
47

    
48
2. http://www.opensource.org/licenses/BSD-2-Clause
49
		
50
All rights reserved.
51

    
52
Redistribution and use in source and binary forms, with or without
53
modification, are permitted provided that the following conditions are
54
met:
55

    
56
* Redistributions of source code must retain the above copyright
57
notice, this list of conditions and the following disclaimer.
58

    
59
* Redistributions in binary form must reproduce the above copyright
60
notice, this list of conditions and the following disclaimer in the
61
documentation and/or other materials provided with the distribution.
62

    
63
This software is provided by the copyright holders and contributors
64
"as is" and any express or implied warranties, including, but not
65
limited to, the implied warranties of merchantability and fitness for
66
a particular purpose are disclaimed. In no event shall the copyright
67
holder or contributors be liable for any direct, indirect, incidental,
68
special, exemplary, or consequential damages (including, but not
69
limited to, procurement of substitute goods or services; loss of use,
70
data, or profits; or business interruption) however caused and on any
71
theory of liability, whether in contract, strict liability, or tort
72
(including negligence or otherwise) arising in any way out of the use
73
of this software, even if advised of the possibility of such damage.
74
</p>
75
         <p>Author: See AUTHORS</p>
76
         <p>Id: $Id: graphics.xsl 9998 2012-01-02 18:27:15Z rahtz $</p>
77
         <p>Copyright: 2008, TEI Consortium</p>
78
      </desc>
79
   </doc>
80

    
81

    
82
    <xsl:key name="W" match="image" use="@url"/>
83
    <xsl:key name="H" match="image" use="@url"/>
84

    
85
    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
86
      <desc>
87
        Guides the identity transformation for graphics
88
    </desc>
89
   </doc>
90
    <xsl:template match="a:blip" mode="iden">
91
        <xsl:variable name="me" select="generate-id()"/>
92
        <a:blip>
93
            <xsl:variable name="rId">
94
                <xsl:for-each select="key('BLIP',1)">
95
                    <xsl:if test="generate-id()=$me">
96
                        <xsl:value-of select="concat('rId', string(200 + position()))"/>
97
                    </xsl:if>
98
                </xsl:for-each>
99
            </xsl:variable>
100
            <xsl:choose>
101
                <xsl:when test="@r:embed">
102
                    <xsl:attribute name="r:embed" select="$rId"/>
103
                </xsl:when>
104
                <xsl:otherwise>
105
                    <xsl:attribute name="r:link" select="$rId"/>
106
                </xsl:otherwise>
107
            </xsl:choose>
108
        </a:blip>
109
    </xsl:template>
110
    
111
    <!-- 
112
        Handle figures 
113
    -->
114
    
115
    <xsl:template match="tei:figure">
116
      <xsl:choose>
117
	<xsl:when test="@rend='inline' or @place='inline'">
118
	  <xsl:apply-templates/>
119
	</xsl:when>
120
	<xsl:otherwise>
121
	  <xsl:call-template name="block-element">
122
            <xsl:with-param name="pPr">
123
	      <w:pPr>
124
		<w:spacing w:before="240"/>
125
		<w:jc w:val="{$alignFigures}"/>
126
	      </w:pPr>
127
            </xsl:with-param>
128
	  </xsl:call-template>
129
	</xsl:otherwise>
130
      </xsl:choose>
131
    </xsl:template>
132

    
133
    <xsl:template match="tei:figure/tei:figDesc"/>
134
    
135
    <xsl:template match="tei:figure/tei:head">
136
      <xsl:variable name="number">
137
	<xsl:number level="any"/>
138
      </xsl:variable>
139
      <xsl:choose>
140
	<xsl:when test="../@xml:id">
141
	    <!-- we want a bookmark for referencing this figure -->
142
	  <xsl:call-template name="block-element">
143
            <xsl:with-param name="style">
144
	      <xsl:choose>
145
		<xsl:when test="ancestor::tei:back">Figuretitleannex</xsl:when>
146
		<xsl:otherwise>Figuretitle</xsl:otherwise>
147
	      </xsl:choose>
148
	    </xsl:with-param>
149
	    <xsl:with-param name="bookmark-id">
150
	      <xsl:value-of select="1000+$number"/>
151
	    </xsl:with-param>
152
	    <xsl:with-param name="bookmark-name">
153
	      <xsl:text>_</xsl:text>
154
	      <xsl:value-of select="../@xml:id"/>
155
	    </xsl:with-param>
156
	  </xsl:call-template>
157
	</xsl:when>
158
	<xsl:otherwise>  
159
	  <xsl:call-template name="block-element">
160
            <xsl:with-param name="style">
161
	      <xsl:choose>
162
		<xsl:when test="ancestor::tei:back">Figuretitleannex</xsl:when>
163
		<xsl:otherwise>Figuretitle</xsl:otherwise>
164
	      </xsl:choose>
165
	    </xsl:with-param>
166
	  </xsl:call-template>
167
	</xsl:otherwise>
168
      </xsl:choose>
169
    </xsl:template>
170
    
171
    <xsl:template match="tei:graphic">
172
        <!-- perform some tests on the graphic -->
173
	<xsl:variable name="maxWidth" select="number(number($pageWidth)*100) cast as xs:integer"/>
174
	<xsl:variable name="maxHeight"
175
		      select="number(number($pageHeight)*100) cast as
176
			      xs:integer"/>
177
	<xsl:variable name="filename">
178
	  <xsl:text>media/resource</xsl:text>
179
	  <xsl:number level="any"/>
180
	  <xsl:text>.</xsl:text>
181
	  <xsl:value-of select="tokenize(@url,'\.')[last()]"/>
182
	</xsl:variable>
183
	<xsl:variable name="origheight">
184
	  <xsl:choose>
185
	    <xsl:when test="@teidocx:height">
186
	      <xsl:value-of select="@teidocx:height"/>
187
	    </xsl:when>
188
	    <xsl:when test="doc-available(concat($wordDirectory,'/image-size-info.xml'))">
189
		<xsl:for-each select="document(concat($wordDirectory,'/image-size-info.xml'))">
190
		  <xsl:value-of select="(number(key('H',$filename)/height) div 72) * 9144"/>
191
		</xsl:for-each>
192
	    </xsl:when>
193
	    <xsl:otherwise>0</xsl:otherwise>
194
	  </xsl:choose>
195
	</xsl:variable>
196

    
197
	<xsl:variable name="origwidth">
198
	  <xsl:choose>
199
	    <xsl:when test="@teidocx:width">
200
	      <xsl:value-of select="@teidocx:width"/>
201
	    </xsl:when>
202
	    <xsl:when test="doc-available(concat($wordDirectory,'/image-size-info.xml'))">
203
		<xsl:for-each select="document(concat($wordDirectory,'/image-size-info.xml'))">
204
		  <xsl:value-of select="(number(key('W',$filename)/width) div 72) * 9144"/>
205
		</xsl:for-each>
206
	    </xsl:when>
207
	    <xsl:otherwise>0</xsl:otherwise>
208
	  </xsl:choose>
209
	</xsl:variable>
210
	<xsl:choose>
211
	  <xsl:when test="$filename and  ( ($origwidth &gt; 0 and $origheight &gt; 0) or (@width and @height))">
212
            
213
            <!--
214
                
215
                is there a number present?
216
                
217
                not(number(substring(@width,0,string-length(@width)-1))=NAN) and 
218
                not(number(substring(@height,0,string-length(@height)-1))=NAN)">
219
                
220
            -->
221
            
222
            <xsl:variable name="Width">
223
	      <!-- remembering that pageWidth is already divided by 100 -->
224
                <xsl:choose>
225
                    <xsl:when test="contains(@width,'%')">
226
                        <xsl:value-of select="number($pageWidth * number(substring-before(@width,'%'))) cast as xs:integer"/>
227
                    </xsl:when>
228
                    <xsl:when test="@width">
229
                        <xsl:value-of select="tei:convert-dim-emu(@width)"/>
230
                    </xsl:when>
231
                    <xsl:when test="@scale and $origwidth">
232
                        <xsl:value-of select="($origwidth *  number(@scale)) cast as xs:integer"/>
233
                    </xsl:when>
234
                    <xsl:when test="@height and $origheight and $origwidth">
235
		      <xsl:variable name="h">
236
			<xsl:choose>
237
			  <xsl:when test="contains(@height,'%')">
238
			    <xsl:value-of select="number($pageHeight * (number(substring-before(@height,'%')))) cast as xs:integer"/>
239
			  </xsl:when>
240
			  <xsl:otherwise>
241
			    <xsl:value-of
242
				select="tei:convert-dim-emu(@height)"/>
243
			  </xsl:otherwise>
244
			</xsl:choose>
245
		      </xsl:variable>
246
		      <xsl:value-of select="number(($h * $origwidth) div $origheight)    cast as xs:integer"/>
247
                    </xsl:when>
248
                    <xsl:when test="$origwidth">
249
                        <xsl:value-of select="$origwidth"/>
250
                    </xsl:when>
251
                    <xsl:otherwise>
252
                        <xsl:message terminate="yes">no way to work out image width for
253
                            <xsl:value-of select="$filename"/>
254
                        </xsl:message>
255
                    </xsl:otherwise>
256
                </xsl:choose>
257
            </xsl:variable>
258
            
259
            <xsl:variable name="Height">
260
                <xsl:choose>
261
                    <xsl:when test="contains(@height,'%')">
262
                        <xsl:value-of select="number($pageHeight * (number(substring-before(@height,'%')))) cast as xs:integer"/>
263
                    </xsl:when>
264
                    <xsl:when test="@height">
265
                        <xsl:value-of select="tei:convert-dim-emu(@height)"/>
266
                    </xsl:when>
267
                    <xsl:when test="@scale and $origheight">
268
                        <xsl:value-of select="($origheight * number(@scale)) cast as xs:integer"/>
269
                    </xsl:when>
270
                    <xsl:when test="@width and $origheight and $origwidth">
271
                        <xsl:value-of select="number(  ($Width *  $origheight) div $origwidth) cast as xs:integer"/>
272
                    </xsl:when>
273
                    <xsl:when test="$origheight">
274
                        <xsl:value-of select="$origheight"/>
275
                    </xsl:when>
276
                    <xsl:otherwise>
277
                        <xsl:message terminate="yes">no way to work out image height for
278
                            <xsl:value-of select="$filename"/>
279
                        </xsl:message>
280
                    </xsl:otherwise>
281
                </xsl:choose>
282
            </xsl:variable>
283
            
284
	    <!-- check for sense -->
285
            <xsl:variable name="imageWidth">
286
	      <xsl:choose>
287
		<xsl:when test="$Width = -1">
288
		  <xsl:value-of select="$maxWidth"/>
289
		</xsl:when>
290
		<xsl:when test="$Width &gt; $maxWidth">
291
		  <xsl:value-of select="$maxWidth"/>
292
		</xsl:when>
293
		<xsl:otherwise>
294
		  <xsl:value-of select="$Width"/>
295
		</xsl:otherwise>
296
	      </xsl:choose>
297
	    </xsl:variable>
298
            <xsl:variable name="imageHeight">
299
	      <xsl:choose>
300
		<xsl:when test="$Height = -1">
301
		  <xsl:value-of select="$maxHeight"/>
302
		</xsl:when>
303
		<xsl:when test="$Height &gt; $maxHeight">
304
		  <xsl:value-of select="$maxHeight"/>
305
		</xsl:when>
306
		<xsl:when test="$Width &gt; $maxWidth">
307
		  <xsl:value-of select="($Height * ($maxWidth div
308
					$Width) ) cast as xs:integer"/>
309
		</xsl:when>
310
		<xsl:otherwise>
311
		  <xsl:value-of select="$Height"/>
312
		</xsl:otherwise>
313
	      </xsl:choose>
314
	    </xsl:variable>
315
	    <!--
316
		
317
		<xsl:message>
318
		========================
319
		<xsl:for-each select="@*">
320
		- @<xsl:value-of select="name(.)"/>: <xsl:value-of select="."/>
321
		</xsl:for-each>
322
		
323
		- maxWidth: <xsl:value-of select="$maxWidth"/>
324
		- maxHeight: <xsl:value-of select="$maxHeight"/>
325
		- Width: <xsl:value-of select="$Width"/>
326
		- Height: <xsl:value-of select="$Height"/>
327
		* imageWidth: <xsl:value-of select="$imageWidth"/>
328
		* imageHeight: <xsl:value-of select="$imageHeight"/>
329
		</xsl:message>
330
	    -->
331
	    <!-- prepare actual graphic -->
332
	    <xsl:variable name="generatedID">
333
	      <xsl:number level="any"/>
334
	    <!--
335
		<xsl:choose>
336
		<xsl:when test="@n">
337
		  <xsl:value-of select="@n"/>
338
		</xsl:when>
339
		<xsl:otherwise>
340
		  <xsl:number level="any"/>
341
		</xsl:otherwise>
342
	      </xsl:choose>
343
	      -->
344
	    </xsl:variable>
345

    
346
            <xsl:variable name="graphic-element">
347
                <a:graphic>
348
                    <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
349
                        <pic:pic>
350
                            <pic:nvPicPr>
351
                                <pic:cNvPr name="{tokenize($filename, '/')[last()]}">
352
                                    <xsl:attribute name="id">
353
                                        <xsl:number level="any"/>
354
                                    </xsl:attribute>
355
                                </pic:cNvPr>
356
                                <pic:cNvPicPr/>
357
                            </pic:nvPicPr>
358
                            <pic:blipFill>
359
                                <a:blip>
360
				  <xsl:attribute name="r:embed">
361
				    <xsl:choose>
362
				      <xsl:when test="$isofreestanding='true'">
363
					<xsl:text>rId</xsl:text>
364
					<xsl:value-of
365
					    select="number($generatedID)
366
						    + 300"/>
367
				      </xsl:when>
368
				      <xsl:otherwise>
369
					<xsl:value-of select="document(concat($wordDirectory,'/word/_rels/document.xml.rels'))//rel:Relationship[@Target=$filename]/@Id"/>
370
				      </xsl:otherwise>
371
				    </xsl:choose>
372
				  </xsl:attribute>
373
				</a:blip>
374
				<a:stretch>
375
				  <a:fillRect/>
376
                                </a:stretch>
377
                            </pic:blipFill>
378
                            <pic:spPr>
379
                                <a:xfrm>
380
                                    <a:off x="0" y="0"/>
381
                                    <a:ext cx="{$imageWidth}00" cy="{$imageHeight}00"/>
382
                                </a:xfrm>
383
                                <a:prstGeom prst="rect">
384
                                    <a:avLst/>
385
                                </a:prstGeom>
386
				<xsl:if test="$shadowGraphics='true'
387
					      and parent::tei:figure">
388
				  <a:effectLst>
389
				    <a:outerShdw blurRad="50800" dist="88900" dir="2700000" algn="tl" rotWithShape="0">
390
				      <a:schemeClr val="tx1">
391
					<a:alpha val="49000"/>
392
				      </a:schemeClr>
393
				    </a:outerShdw>
394
				  </a:effectLst>
395
				</xsl:if>
396
                            </pic:spPr>
397
                        </pic:pic>
398
                    </a:graphicData>
399
                </a:graphic>
400
            </xsl:variable>
401
            <!-- end graphic element -->
402
            
403
            <w:r>
404
                <w:drawing>
405
                    <!-- choose between inline and block -->
406
                    <xsl:choose>
407
		      <xsl:when test="parent::tei:figure[@place='left'
408
				      or @place='centre' or @place='right' or @place='center']">
409
			<wp:anchor simplePos="0" relativeHeight="10" behindDoc="0" locked="0" layoutInCell="1"
410
				   allowOverlap="1">
411
			  <wp:simplePos x="0" y="0"/>
412
			  <wp:positionH relativeFrom="margin">
413
			    <wp:align>
414
			      <xsl:value-of select="parent::tei:figure/@place"/>
415
			    </wp:align>
416
			  </wp:positionH>
417
			  <wp:positionV relativeFrom="paragraph">
418
			    <wp:align>center</wp:align>
419
			  </wp:positionV>
420
			  <wp:extent cx="{$imageWidth}00" cy="{$imageHeight}00"/>
421
			  <xsl:if test="$shadowGraphics='true'">
422
			    <wp:effectExtent l="50800" t="25400" r="101600" b="63500"/>
423
			  </xsl:if>
424
			  <wp:wrapSquare wrapText="bothSides"/>
425
			  <wp:docPr  name="{tokenize($filename, '/')[last()]}">
426
			    <xsl:attribute name="id">
427
			      <xsl:value-of select="$generatedID"/>
428
			    </xsl:attribute>
429
			  </wp:docPr>                                
430
			  <xsl:if test="$shadowGraphics='true'">
431
			    <wp:cNvGraphicFramePr/>
432
			  </xsl:if>
433
			  <xsl:copy-of select="$graphic-element"/>
434
			</wp:anchor>
435
		      </xsl:when>
436
		      <xsl:otherwise>
437
			<wp:inline distT="0" distB="0" distL="0" distR="0">
438
			  <wp:extent cx="{$imageWidth}00" cy="{$imageHeight}00"/>
439
			  <xsl:if test="$shadowGraphics='true'">
440
			    <wp:effectExtent l="50800" t="25400" r="101600" b="63500"/>
441
			  </xsl:if>
442
			  <wp:docPr  name="{tokenize($filename, '/')[last()]}">
443
			    <xsl:attribute name="id">
444
			      <xsl:value-of select="$generatedID"/>
445
			    </xsl:attribute>
446
			  </wp:docPr>                                
447
			  <xsl:if test="$shadowGraphics='true'">
448
			    <wp:cNvGraphicFramePr/>
449
			  </xsl:if>
450
			  <xsl:copy-of select="$graphic-element"/>
451
			</wp:inline>
452
		      </xsl:otherwise>
453
                    </xsl:choose>
454
                </w:drawing>
455
            </w:r>
456
	  </xsl:when>
457
	  <xsl:otherwise>
458
	    <xsl:message terminate="yes">ERROR. no image size info for  <xsl:value-of select="$filename"/>, cannot proceed</xsl:message>
459

    
460
	  </xsl:otherwise>
461
	</xsl:choose>
462
    </xsl:template>
463
    
464
</xsl:stylesheet>