-
Notifications
You must be signed in to change notification settings - Fork 21
/
simplify-coordinates.xsl
83 lines (77 loc) · 2.99 KB
/
simplify-coordinates.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns:local="local"
xmlns="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="#all"
version="3.0">
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> 2021-11-08</xd:p>
<xd:p><xd:b>Author:</xd:b> [email protected]</xd:p>
<xd:p>evaluate coordinates within @points and turn the bounding rectangle of the polygon</xd:p>
</xd:desc>
</xd:doc>
<xd:doc>
<xd:desc>evaluate coordinates and return bounding rectangle</xd:desc>
</xd:doc>
<xsl:template match="tei:zone/@points" mode="bounding-rectangle">
<xsl:variable name="x" select="local:pointsX(.)" />
<xsl:variable name="y" select="local:pointsY(.)" />
<xsl:attribute name="points">
<xsl:value-of select="min($x) || ',' || min($y)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="max($x) || ',' || min($y)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="max($x) || ',' || max($y)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="min($x) || ',' || max($y)
"/>
</xsl:attribute>
</xsl:template>
<xd:doc>
<xd:desc>for a polygon, return a sequence of X coordinates, ordered ascending from left to right</xd:desc>
<xd:param name="pts">the value of @points</xd:param>
</xd:doc>
<xsl:function name="local:pointsX" as="xs:integer+">
<xsl:param name="pts" />
<xsl:choose>
<xsl:when test="$pts=''">
<xsl:sequence select="(0, 0)"/>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="tokenize($pts, ' ')">
<xsl:value-of select="round(number(substring-before(current(), ',')))" />
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xd:doc>
<xd:desc>for a polygon, return a sequence of Y coordinates, ordered ascending from top to bottom</xd:desc>
<xd:param name="pts">the value of @points</xd:param>
</xd:doc>
<xsl:function name="local:pointsY" as="xs:integer+">
<xsl:param name="pts" />
<xsl:choose>
<xsl:when test="$pts=''">
<xsl:sequence select="(0, 0)"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vals" select="tokenize($pts, ' ')"/>
<xsl:for-each select="$vals">
<xsl:value-of select="round(number(substring-after(current(), ',')))" />
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xd:doc>
<xd:desc>Default</xd:desc>
</xd:doc>
<xsl:template match="@* | node()" mode="bounding-rectangle">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="#current" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>