sysmodellibs/sysmodelgen/core/legend-module.xsl
changeset 7 3c36c452f013
equal deleted inserted replaced
6:5b32dc297d05 7:3c36c452f013
       
     1 <?xml version="1.0"?>
       
     2  <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"   xmlns:s="http://www.w3.org/2000/svg" xmlns:exslt="http://exslt.org/common" xmlns:m="http://exslt.org/math" exclude-result-prefixes="s m exslt" >
       
     3 	<xsl:key name="ldg-use" match="group" use="@style-id"/>
       
     4 	<xsl:key name="style-ref" match="*[starts-with(name(),'generator-')]/@ref| @*[starts-with(name(),'generator-')]" use="."/>
       
     5 	<xsl:key name="styled" match="group|group/*" use="@style-id"/>	  
       
     6 	<xsl:output method="xml" cdata-section-elements="script s:script" indent="yes"/>
       
     7 
       
     8 <!-- ====== Constants ============= -->
       
     9 <xsl:variable name="cboxWidth" select="15.5"/>	<!-- the width of a sample colour in the legend (mm) -->
       
    10 <xsl:variable name="cboxHeight" select="3.1"/>	<!-- the height of a sample colour in the legend (mm) -->
       
    11 <xsl:variable name="legendDx" select="5"/><!-- the horizontal distance between items in a legend (mm) -->
       
    12 <!-- ====== Computed values ============= -->
       
    13 
       
    14 
       
    15 <xsl:template match="meta[@rel='model-footer' and *]" mode="sizing">
       
    16 	<xsl:copy><xsl:copy-of select="@*"/>
       
    17 		<xsl:attribute name="width">1</xsl:attribute> <!-- virtual width, really spans with of the model -->
       
    18 		<xsl:attribute name="height"><xsl:value-of select="$groupDy + 4.233"/></xsl:attribute>
       
    19 		<xsl:copy-of select="node()"/>
       
    20 	</xsl:copy>
       
    21 </xsl:template>
       
    22 
       
    23 <xsl:template match="meta[@rel='model-logo']" mode="sizing">
       
    24 	<xsl:copy><xsl:copy-of select="@*"/>
       
    25 	<xsl:for-each select="logo">
       
    26 		<xsl:variable name="b" select="normalize-space(@viewBox)"/>
       
    27 		<xsl:attribute name="width">
       
    28 			<xsl:variable name="x0" select="- number(substring-before($b,' '))"/>
       
    29 			<xsl:variable name="x1" select="number(substring-before(substring-after(substring-after($b,' '),' '),' '))"/>
       
    30 			<xsl:choose> <!--  is it really y1+ y0 or should it be - ? -->
       
    31 				<xsl:when test="@width"><xsl:value-of select="@width"/></xsl:when>
       
    32 				<xsl:when test="@height and $b!=''">
       
    33 					<xsl:variable name="y0" select="- number(substring-before(substring-after($b,' '),' '))"/>
       
    34 					<xsl:variable name="y1" select="number(substring-after(substring-after(substring-after($b,' '),' '),' '))"/>
       
    35 					<xsl:value-of select="($x1 + $x0) * number(@height) div ($y1 + $y0)"/>
       
    36 				</xsl:when>
       
    37 				<xsl:when test="$b!=''"><xsl:value-of select="$x1 + $x0"/></xsl:when>
       
    38 				<xsl:otherwise>?</xsl:otherwise>	<!-- cannot be determined -->
       
    39 			</xsl:choose>
       
    40 			</xsl:attribute>
       
    41 			<xsl:attribute name="height">
       
    42 				<xsl:variable name="y0" select="- number(substring-before(substring-after($b,' '),' '))"/>
       
    43 				<xsl:variable name="y1" select="number(substring-after(substring-after(substring-after($b,' '),' '),' '))"/>
       
    44 				<xsl:choose> <!--  is it really y1+ y0 or should it be - ? -->
       
    45 					<xsl:when test="@height"><xsl:value-of select="@height"/></xsl:when>
       
    46 					<xsl:when test="@width and $b!=''">
       
    47 						<xsl:variable name="x0" select="- number(substring-before($b,' '))"/>
       
    48 						<xsl:variable name="x1" select="number(substring-before(substring-after(substring-after($b,' '),' '),' '))"/>
       
    49 						<xsl:value-of select="($y1 + $y0) * number(@width) div ($x1 + $x0)"/>
       
    50 					</xsl:when>
       
    51 					<xsl:when test="$b!=''"><xsl:value-of select="$y1 + $y0"/></xsl:when>
       
    52 					<xsl:otherwise>?</xsl:otherwise>	<!-- cannot be determined -->
       
    53 				</xsl:choose>
       
    54 			</xsl:attribute>
       
    55 		</xsl:for-each>
       
    56 		<xsl:copy-of select="node()"/>
       
    57 	</xsl:copy>
       
    58 </xsl:template>
       
    59 
       
    60 <xsl:template match="meta[@rel='model-legend']" mode="sizing">
       
    61 	<xsl:copy><xsl:copy-of select="@*"/>
       
    62 		<xsl:variable name="content">		
       
    63 			<xsl:apply-templates select="legend" mode="legend-copy"/>
       
    64 		</xsl:variable>
       
    65 		<xsl:variable name="height">
       
    66 			<xsl:choose>
       
    67 				<xsl:when test="exslt:node-set($content)/legend/@height"><xsl:value-of select="exslt:node-set($content)/legend/@height"/></xsl:when>
       
    68 				<xsl:when test="exslt:node-set($content)/legend/@title-height &gt; exslt:node-set($content)/legend/@min-height"><xsl:value-of select="exslt:node-set($content)/legend/@title-height"/></xsl:when>
       
    69 				<xsl:when test="exslt:node-set($content)/legend/@min-height"><xsl:value-of select="exslt:node-set($content)/legend/@min-height"/></xsl:when>
       
    70 				<xsl:when test="exslt:node-set($content)/legend[not(@min-height) and @title-height]">
       
    71 					<xsl:value-of select="exslt:node-set($content)/legend/@title-height"/>
       
    72 				</xsl:when>
       
    73 				<xsl:otherwise>0</xsl:otherwise>
       
    74 			</xsl:choose>
       
    75 		</xsl:variable>
       
    76 		<xsl:variable name="width">
       
    77 			<xsl:apply-templates mode="actual-width" select="exslt:node-set($content)/legend">
       
    78 				<xsl:with-param name="h" select="(exslt:node-set($content)/legend/@height| exslt:node-set($content)/legend/@min-height) - sum(exslt:node-set($content)/legend/@ipad)"/>
       
    79 			</xsl:apply-templates>
       
    80 		</xsl:variable>
       
    81 		<xsl:attribute name="height"><xsl:value-of select="$height"/></xsl:attribute> <!-- externally-visible height = internal +padding-->
       
    82 		<xsl:attribute name="width"><xsl:value-of select="$width + sum(exslt:node-set($content)/legend/@title-width)"/></xsl:attribute> <!-- externally-visible width = internal + padding-->
       
    83 		<xsl:copy-of select="$content"/>
       
    84 	</xsl:copy>
       
    85 </xsl:template>
       
    86 
       
    87 <!-- legend -->
       
    88 
       
    89 <xsl:template match="*[@width]" mode="actual-width" priority="2">
       
    90 	<xsl:value-of select="@width"/>
       
    91 </xsl:template>
       
    92 
       
    93 <xsl:template match="legend[@min-width]" mode="actual-width"><xsl:param name="h"/>
       
    94 	<xsl:call-template name="sum-list">
       
    95 		<xsl:with-param name="list">
       
    96 			<xsl:value-of select="concat(@min-width - sum(*/@min-width) ,' ')"/>
       
    97 			<xsl:for-each select="*[@min-width]">
       
    98 				<xsl:apply-templates select="." mode="actual-width">
       
    99 					<xsl:with-param name="h" select="$h"/>
       
   100 				</xsl:apply-templates>
       
   101 				<xsl:text> </xsl:text>
       
   102 			</xsl:for-each>
       
   103 		</xsl:with-param> 
       
   104 	</xsl:call-template>
       
   105 </xsl:template>
       
   106 
       
   107 <xsl:template match="*[@min-width]" mode="actual-width" priority="-1"><xsl:message terminate="yes">Not supported</xsl:message>
       
   108 </xsl:template>
       
   109 
       
   110 
       
   111 <xsl:template match="legend[cbox]" priority="1" mode="actual-width"><xsl:param name="h"/>
       
   112 	<xsl:variable name="height">
       
   113 		<xsl:choose>
       
   114 			<xsl:when test="$h &lt; @min-height"><xsl:value-of select="@min-height"/></xsl:when>
       
   115 			<xsl:otherwise><xsl:value-of select="$h"/></xsl:otherwise>
       
   116 		</xsl:choose>
       
   117 	</xsl:variable>
       
   118 	<xsl:value-of select="sum(@label-width)  + $cboxWidth * (ceiling(   count(cbox)  div floor(($height) div $cboxHeight)) )"/>
       
   119 </xsl:template>
       
   120 
       
   121 
       
   122 <xsl:template match="node()" mode="legend-copy" priority="-2">
       
   123 	<xsl:copy><xsl:copy-of select="@*"/>
       
   124 		<xsl:apply-templates  mode="legend-copy"/>
       
   125 	</xsl:copy>
       
   126 </xsl:template>
       
   127 <xsl:template match="text()[normalize-space(.)='']" mode="legend-copy" priority="-1"/>
       
   128 
       
   129 <xsl:template match="layer" mode="legend-copy" priority="-1">
       
   130 	<xsl:copy-of select="."/>
       
   131 </xsl:template>
       
   132 
       
   133 
       
   134 <!-- template to ensure that only the desired attributes get copied, and that width gets renamed --> 
       
   135 <xsl:template match="@*" mode="legend-root-attr"><xsl:copy-of select="."/></xsl:template>
       
   136 <xsl:template match="@label|@literal|@label-ref" mode="legend-root-attr"/>
       
   137 <xsl:template match="@width" mode="legend-root-attr">
       
   138 	<xsl:if test="contains(.,'%')">
       
   139 		<xsl:attribute name="percent-width"><xsl:value-of select="substring-before(.,'%')"/></xsl:attribute>
       
   140 	</xsl:if>
       
   141 </xsl:template>
       
   142 
       
   143 
       
   144 <xsl:template match="meta/legend" mode="legend-copy" priority="4">
       
   145 	<xsl:variable name="content">		
       
   146 		<xsl:apply-templates  mode="legend-copy"/>
       
   147 	</xsl:variable>
       
   148 	<xsl:copy>
       
   149 		<xsl:apply-templates mode="legend-root-attr" select="@*"/>
       
   150 		<xsl:if test="@label"><xsl:apply-templates select="." mode="make-label"/></xsl:if>
       
   151 		<xsl:call-template name="legend-sizing">
       
   152 			<xsl:with-param name="content" select="exslt:node-set($content)"/>
       
   153 		</xsl:call-template>
       
   154 		<xsl:variable name="line-height">
       
   155 			<xsl:choose>
       
   156 				<xsl:when test="number(@title-scale)">
       
   157 					<xsl:value-of select="4.3 * @title-scale"/> <!-- scale 12pt by specified factor-->
       
   158 				</xsl:when>
       
   159 				<xsl:when test="@width or @maxscale">4.233</xsl:when> <!-- 12pt -->
       
   160 			<!-- 	<xsl:when test="$full-width &gt; $large-width">6.3495</xsl:when> 18 pt -->
       
   161 				<xsl:otherwise>4.3</xsl:otherwise> <!-- 12pt -->
       
   162 			</xsl:choose>		
       
   163 		</xsl:variable>
       
   164 		<xsl:attribute name="title-width">
       
   165 			<xsl:apply-templates select="/SystemDefinition" mode="legend-title-width">
       
   166 				<xsl:with-param name="h" select="$line-height"/>
       
   167 			</xsl:apply-templates>
       
   168 		</xsl:attribute>
       
   169 		<xsl:attribute name="title-height">
       
   170 			<xsl:value-of select="$line-height * 3"/>
       
   171 		</xsl:attribute>
       
   172 		<xsl:copy-of select="$content"/>		
       
   173 	</xsl:copy>
       
   174 </xsl:template>
       
   175 
       
   176 
       
   177 <xsl:template match="legend/@use" mode="legend-copy">
       
   178 	<xsl:apply-templates select="key('ldg-use',.)/*" mode="legend-copy">
       
   179 		<xsl:with-param name="show-unused" select="../@show-unused"/>
       
   180 	</xsl:apply-templates>
       
   181 </xsl:template>
       
   182 
       
   183 <xsl:template match="@label" mode="width"><xsl:value-of select="string-length(.) *  4"/></xsl:template>
       
   184 <xsl:template match="@label[../@label-ref]" mode="width">
       
   185 	<xsl:variable name="text"><xsl:apply-templates mode="name" select=".."/></xsl:variable>
       
   186 	<xsl:value-of select="string-length($text) *  4"/>
       
   187 </xsl:template>
       
   188 
       
   189 <xsl:template name="label-width">
       
   190 	<xsl:variable name="node">
       
   191 		<t>
       
   192 			<xsl:choose>
       
   193 				<xsl:when test="@label"><xsl:apply-templates select="." mode="make-label"/></xsl:when>
       
   194 				<xsl:otherwise><xsl:apply-templates select="key('ldg-use',@use)" mode="make-label"/></xsl:otherwise>
       
   195 			</xsl:choose>
       
   196 		</t>
       
   197 	</xsl:variable>
       
   198 	<xsl:variable name="text" select="normalize-space(exslt:node-set($node)/*/@label)"/>
       
   199 	<xsl:choose>
       
   200 		<xsl:when test="$text=''">0</xsl:when> <!-- no wrapping big text -->
       
   201 		<xsl:when test="parent::meta"><xsl:value-of select="string-length($text) *  4"/></xsl:when> <!-- no wrapping big text -->
       
   202 		<!-- small text is half as big (approx) -->
       
   203 		<xsl:when test="contains($text,' ') and string-length($text) &gt; 15">15</xsl:when> <!-- wrap so it's no more than 20 wide -->
       
   204 		<xsl:otherwise><xsl:value-of select="string-length($text)"/></xsl:otherwise> <!-- don't wrap -->
       
   205 	</xsl:choose>	
       
   206 </xsl:template>
       
   207 
       
   208 <xsl:template name="legend-sizing"><xsl:param name="content"/>
       
   209 	<xsl:variable name="h">
       
   210 		<xsl:call-template name="max-from-list">
       
   211 			<xsl:with-param name="list">
       
   212 				<xsl:for-each select="$content/*/@height |$content/*/@min-height">
       
   213 					<xsl:value-of select="concat(.,' ')"/>
       
   214 				</xsl:for-each>
       
   215 				<xsl:if test="$content/cbox">
       
   216 					<xsl:value-of select="concat($cboxHeight,' ')"/>
       
   217 				</xsl:if>
       
   218 			</xsl:with-param>
       
   219 		</xsl:call-template>
       
   220 	</xsl:variable>
       
   221 	<xsl:variable name="label-w">
       
   222 		<xsl:call-template name="label-width"/>
       
   223 	</xsl:variable>
       
   224 	<xsl:if test="$label-w != 0">
       
   225 	<xsl:attribute name="label-width"><xsl:value-of select="$label-w"/></xsl:attribute>
       
   226 	</xsl:if>
       
   227 	<xsl:variable name="ipad" select=" (1 - count(parent::legend)) *$legendDx"/>
       
   228 	<xsl:if test="$ipad">
       
   229 		<xsl:attribute name="ipad"><xsl:value-of select="$ipad"/></xsl:attribute>
       
   230 	</xsl:if>
       
   231 	<xsl:choose>	
       
   232 		<xsl:when test="$content/cbox">
       
   233 			<xsl:attribute name="min-width"><xsl:value-of select="$label-w + $cboxWidth"/></xsl:attribute>
       
   234 			<xsl:attribute name="max-width"><xsl:value-of select="$label-w + $cboxWidth * count($content/cbox)"/></xsl:attribute>
       
   235 			<xsl:attribute name="min-height"><xsl:value-of select="$cboxHeight"/></xsl:attribute>					
       
   236 		</xsl:when>
       
   237 		<xsl:when test="not($content/*) and parent::meta">
       
   238 			<xsl:attribute name="width">0</xsl:attribute> <!-- no legend, don't even draw the label -->
       
   239 		</xsl:when>
       
   240 		<xsl:when test="not($content/*[@width or @min-width or @max-width])">
       
   241 			<xsl:attribute name="width">100</xsl:attribute>
       
   242 		</xsl:when>
       
   243 		<xsl:otherwise>
       
   244 			<xsl:variable name="minw">
       
   245 				<xsl:if test="count($content/*/@width) != count($content/*)">min-</xsl:if>
       
   246 			</xsl:variable>
       
   247 			<xsl:variable name="base-w" select="sum($content/*/@width|$content/*[following-sibling::*]/@rpad) + $label-w  + 2* $ipad"/>
       
   248 			<xsl:attribute name="{$minw}width">
       
   249 				<xsl:value-of select="$base-w + sum($content/*/@min-width) "/>
       
   250 			</xsl:attribute>
       
   251 			<xsl:if test="$content/*/@max-width">
       
   252 				<xsl:attribute name="max-width">
       
   253 					<xsl:value-of select="$base-w + sum($content/*/@max-width)"/>
       
   254 				</xsl:attribute>
       
   255 			</xsl:if>					
       
   256 			<xsl:variable name="minh">
       
   257 				<xsl:if test="count($content/*/@height) != count($content/*)">min-</xsl:if>
       
   258 			</xsl:variable>
       
   259 			<xsl:attribute name="{$minh}height"><xsl:value-of select="$h+$ipad"/></xsl:attribute>
       
   260 		</xsl:otherwise>
       
   261 	</xsl:choose>
       
   262 </xsl:template>
       
   263 
       
   264 
       
   265 <xsl:template match="legend" mode="legend-copy">
       
   266 	<xsl:variable name="content">
       
   267 		<xsl:apply-templates mode="legend-copy" select="* | @use"/>
       
   268 	</xsl:variable>
       
   269 	<xsl:if test="exslt:node-set($content)/descendant-or-self::*">
       
   270 		<!-- only show if all the content is present (do we really want to do this? It means the label won't show if nothing is present) -->
       
   271 		<xsl:copy><xsl:copy-of select="@*[name()!='label' and name()!='literal' and name()!='label-ref' and name()!='show-unused' and name()!='use' and name()!='type']"/>
       
   272 <!-- 		<xsl:copy-of select="key('ldg-use',@use)/@*[name()='type' or name()='style-id']"/>-->
       
   273 			<xsl:choose>
       
   274 				<xsl:when test="@label"><xsl:apply-templates select="." mode="make-label"/></xsl:when>
       
   275 				<xsl:otherwise><xsl:apply-templates select="key('ldg-use',@use)" mode="make-label"/></xsl:otherwise>
       
   276 			</xsl:choose>
       
   277 			<xsl:call-template name="legend-sizing">
       
   278 				<xsl:with-param name="content" select="exslt:node-set($content)"/>
       
   279 			</xsl:call-template>
       
   280 			<xsl:if test="following-sibling::*">
       
   281 				<xsl:attribute name="rpad"><xsl:value-of select="$legendDx"/></xsl:attribute>
       
   282 			</xsl:if>			
       
   283 			<xsl:copy-of select="$content"/>
       
   284 		</xsl:copy>
       
   285 	</xsl:if>
       
   286 </xsl:template>
       
   287 
       
   288 <xsl:template match="legend[not(@use or *)]" mode="legend-copy" priority="2">
       
   289 	<xsl:copy><xsl:copy-of select="@*[name()!='label' and name()!='literal' and name()!='label-ref']"/>
       
   290 		<xsl:apply-templates select="." mode="make-label"/>
       
   291 		<xsl:apply-templates select="node()" mode="legend-copy"/>
       
   292 	</xsl:copy>
       
   293 </xsl:template>
       
   294 
       
   295 
       
   296 <xsl:template match="note" mode="legend-copy">
       
   297 	<xsl:copy><xsl:copy-of select="@*[name()!='label' and name()!='literal' and name()!='label-ref' and name()!='width']"/>
       
   298 		<xsl:variable name="text"><xsl:apply-templates select="." mode="make-label"/></xsl:variable>
       
   299 		<xsl:attribute name="width">
       
   300 			<xsl:choose>
       
   301 				<xsl:when test="@width='auto'">
       
   302 					<xsl:variable name="len">
       
   303 						<xsl:call-template name="multiline-width">
       
   304 							<xsl:with-param name="t" select="$text"/>
       
   305 						</xsl:call-template>
       
   306 						</xsl:variable>
       
   307 					<xsl:variable name="h">
       
   308 						<xsl:choose>
       
   309 							<xsl:when test="@class='lgd'">4.233</xsl:when>
       
   310 							<xsl:otherwise>1.94</xsl:otherwise>
       
   311 						</xsl:choose>
       
   312 					</xsl:variable>
       
   313 					<!--  the width is a guess based on the half the (expected) font size + a border of 2.5 on each side-->
       
   314 					<xsl:value-of select="5 + $len * 0.5 * $h"/>
       
   315 				</xsl:when>
       
   316 				<xsl:when test="@width"><xsl:value-of select="@width"/></xsl:when>
       
   317 				<xsl:otherwise>20</xsl:otherwise>
       
   318 			</xsl:choose>
       
   319 		</xsl:attribute>
       
   320 		<xsl:attribute name="height"><xsl:value-of select="3.1 * (1 + string-length($text) - string-length(translate($text,'&#xa;','')))"/></xsl:attribute>
       
   321 		<xsl:if test="following-sibling::*">
       
   322 			<xsl:attribute name="rpad"><xsl:value-of select="$legendDx"/></xsl:attribute>
       
   323 		</xsl:if>
       
   324 		<xsl:copy-of select="$text"/>
       
   325 	</xsl:copy>
       
   326 </xsl:template>
       
   327 
       
   328 <xsl:template match="*" mode="is-showable" priority="-1"/>
       
   329 <xsl:template match="layer | *[/SystemDefinition[@detail=name() or not(@detail) or not(@static='true' or @detail-type='fixed')]]" priority="4" mode="is-showable">1</xsl:template>
       
   330 <xsl:template match="collection[/SystemDefinition[not(@detail) or @detail='component' ]] " mode="is-showable">1</xsl:template>
       
   331 <xsl:template match="package[/SystemDefinition/@detail!='layer' ]" mode="is-showable">1</xsl:template>
       
   332 
       
   333 
       
   334 <xsl:template match="*" mode="show-unused-colorsbackground"/>
       
   335 <xsl:template match="*" mode="show-unused-colorshighlight"/>
       
   336 <xsl:template match="*" mode="show-unused-colorstext-highlight"/>
       
   337 <xsl:template match="*" mode="show-unused-patterns"/>
       
   338 <xsl:template match="*" mode="show-unused-borders"/>
       
   339 <xsl:template match="*" mode="show-unused-styles"/>
       
   340 
       
   341 <xsl:template match="cbox|cmp" mode="legend-copy"><xsl:param name="show-unused"/>
       
   342 	<xsl:variable name="show">
       
   343 		<xsl:choose>
       
   344 			<xsl:when test="self::cbox[@label='']"/> <!-- always leave out -->
       
   345 			<xsl:when test="/SystemDefinition[@detail-type='fixed' or @static='true'] and ((../@detail='component' and /SystemDefinition/@detail!='component') or  
       
   346 			(../@detail='collection' and (/SystemDefinition/@detail='layer' or /SystemDefinition/@detail='package')) or 
       
   347 			 (../@detail='package' and /SystemDefinition/@detail='layer'))"/> <!--  hide stuff outside level of detail -->
       
   348 			<xsl:when test="$show-unused='yes' or ../@show-unused='yes'">1</xsl:when>
       
   349 			<xsl:when test="key('style-ref',@style-id)"> <!-- see if it's referenced by anything -->
       
   350 				<xsl:apply-templates select="key('style-ref',@style-id)" mode="is-showable"/> 
       
   351 			</xsl:when>
       
   352 			<xsl:when test="@lookup"> <!-- anything with lookup attribute can be done fast -->
       
   353 				<xsl:apply-templates select="key(concat('use-',../@style-id),@lookup)" mode="is-showable"/>
       
   354 			</xsl:when>
       
   355 			<xsl:when test="self::cmp[parent::legend]">1<!-- it's an example: always use --></xsl:when>
       
   356 			<!-- everything else is a rule -->
       
   357 			<xsl:when test="self::cbox">
       
   358 				<xsl:apply-templates select="//component|//collection|//package|//layer" mode="show-unused-colorsbackground">
       
   359 					<xsl:with-param name="n" select="@style-id"/>
       
   360 				</xsl:apply-templates>
       
   361 			</xsl:when>
       
   362 			<xsl:when test="self::cmp[../@type='overlay']">
       
   363 				<xsl:apply-templates select="//component|//collection|//package|//layer" mode="show-unused-patterns">
       
   364 					<xsl:with-param name="n" select="@style-id"/>
       
   365 				</xsl:apply-templates>
       
   366 			</xsl:when>
       
   367 			<xsl:when test="self::cmp[../@type='border']">
       
   368 				<xsl:apply-templates select="//component" mode="show-unused-borders">
       
   369 					<xsl:with-param name="n" select="@style-id"/>
       
   370 				</xsl:apply-templates>
       
   371 			</xsl:when>
       
   372 			<xsl:when test="self::cmp[../@type='style']">
       
   373 				<xsl:apply-templates select="//component|//collection|//package|layer" mode="show-unused-styles">
       
   374 					<xsl:with-param name="n" select="@style-id"/>
       
   375 				</xsl:apply-templates>
       
   376 			</xsl:when>
       
   377 			<xsl:when test="self::cmp[../@type='highlight']">
       
   378 				<xsl:apply-templates select="//component|//collection|//package|layer" mode="show-unused-colorshighlight">
       
   379 					<xsl:with-param name="n" select="@style-id"/>
       
   380 				</xsl:apply-templates>
       
   381 			</xsl:when>
       
   382 			<xsl:when test="self::cmp[../@type='text-highlight']">
       
   383 				<xsl:apply-templates select="//component|//collection|//package|layer" mode="show-unused-colorstext-highlight">
       
   384 					<xsl:with-param name="n" select="@style-id"/>
       
   385 				</xsl:apply-templates>
       
   386 			</xsl:when>
       
   387 			<xsl:otherwise>			<xsl:message terminate="yes">[
       
   388 	
       
   389 	<xsl:value-of select="."/>
       
   390 	
       
   391 	
       
   392 	]</xsl:message>
       
   393 			</xsl:otherwise>
       
   394 		</xsl:choose>
       
   395 	</xsl:variable>
       
   396 	<xsl:choose>
       
   397 		<xsl:when test="(@label | @lookup) and $show!='' ">
       
   398 			<xsl:element name="{name()}">
       
   399 				<!-- in some cases this had a label, but otherwise use the lookup value as the label -->
       
   400 				<xsl:attribute name="id"><xsl:value-of select="@style-id"/></xsl:attribute>
       
   401 				<xsl:copy-of select="@font"/> <!-- if any: can only be set via abbrevs file (consider removing this option) -->
       
   402 				<xsl:apply-templates select="." mode="make-label"/>
       
   403 				<xsl:choose>
       
   404 					<xsl:when test="self::cbox">					
       
   405 						<xsl:copy-of select="@value"/>
       
   406 					</xsl:when>
       
   407 					<xsl:otherwise>
       
   408 						<xsl:attribute name="width"><xsl:value-of select="$cSize"/></xsl:attribute>
       
   409 						<xsl:attribute name="height"><xsl:value-of select="$cSize"/></xsl:attribute>						
       
   410 						<xsl:attribute name="generated-{../@type}"><xsl:value-of select="@value"/></xsl:attribute>
       
   411 						<xsl:if test="following-sibling::*"><xsl:attribute name="rpad"><xsl:value-of select="$legendDx"/></xsl:attribute></xsl:if>
       
   412 					</xsl:otherwise>
       
   413 				</xsl:choose>
       
   414 			</xsl:element>
       
   415 		</xsl:when>
       
   416 		<xsl:when test="self::cmp[parent::legend] and normalize-space(text()!='')">
       
   417 			<xsl:copy> <!-- in some cases this had a label, but otherwise use the lookup value as the label -->
       
   418 				<xsl:copy-of select="@font"/> <!-- if any: can only be set via abbrevs file (consider removing this option) -->
       
   419 				<xsl:attribute name="width"><xsl:value-of select="$cSize"/></xsl:attribute>
       
   420 				<xsl:attribute name="height"><xsl:value-of select="$cSize"/></xsl:attribute>
       
   421 				<xsl:if test="following-sibling::*">
       
   422 					<xsl:attribute name="rpad"><xsl:value-of select="$legendDx"/></xsl:attribute>
       
   423 				</xsl:if>
       
   424 				<xsl:apply-templates select="." mode="make-label"/>			
       
   425 				<xsl:for-each select="@*[starts-with(name(),'generator')]">
       
   426 					<xsl:attribute name="generated{substring-after(name(),'generator')}">
       
   427 						<xsl:for-each select="key('styled',.)"><xsl:value-of select="@value | @default"/></xsl:for-each>							
       
   428 					</xsl:attribute>
       
   429 				</xsl:for-each>
       
   430 			</xsl:copy>
       
   431 		</xsl:when>
       
   432 	</xsl:choose>
       
   433 </xsl:template>
       
   434 
       
   435 
       
   436 
       
   437 <xsl:template match="cmp|cbox|legend|note|group|legend-layer" mode="name">
       
   438 	<xsl:choose>
       
   439 		<xsl:when test="@label-ref"><xsl:apply-templates mode="eval-label" select="."/></xsl:when> <!-- evaluated name -->
       
   440 		<xsl:when test="@abbrev"><xsl:value-of select="@abbrev"/></xsl:when> <!-- localisation override-->
       
   441 		<xsl:when test="@label"><xsl:value-of select="@label"/></xsl:when> <!-- label override-->
       
   442 		<xsl:when test="self::legend[@use]"><xsl:apply-templates select="key('ldg-use',@use)" mode="name"/></xsl:when> <!-- for legends -->
       
   443 		<xsl:when test="name"><xsl:value-of select="name"/></xsl:when>
       
   444 		<xsl:when test="@lookup"><xsl:value-of select="@lookup"/></xsl:when>
       
   445 		<xsl:when test="@name"><xsl:value-of select="@name"/></xsl:when>
       
   446 		<xsl:when test="self::cmp"><xsl:value-of select="text()"/></xsl:when>
       
   447 	</xsl:choose>
       
   448 </xsl:template>
       
   449 
       
   450 <xsl:template match="note[not(@label-ref)]" mode="eval-label" priority="5"><xsl:value-of select="."/></xsl:template>
       
   451 <xsl:template match="legend[not(@label-ref)]" mode="eval-label" priority="5"><xsl:value-of select="@label"/></xsl:template>
       
   452 <xsl:template match="cmp|cbox" mode="eval-label" priority="-3"><xsl:value-of select="@label"/></xsl:template>
       
   453 <xsl:template match="cmp[not(@label)] | note" mode="eval-label" priority="-2"><xsl:value-of select="."/></xsl:template>
       
   454 
       
   455 
       
   456 <!-- replace temporary legend items' labels with evaluated ones-->
       
   457 <xsl:template match="legend|cbox|cmp|group|legend-layer" mode="make-label">
       
   458 	<xsl:copy-of select="@font"/>
       
   459 	<xsl:choose>
       
   460 		<xsl:when test="@label-ref"><xsl:attribute name="label"><xsl:apply-templates mode="eval-label" select="."/></xsl:attribute></xsl:when>
       
   461 		<xsl:when test="@label"><xsl:copy-of select="@label"/></xsl:when>
       
   462 		<xsl:when test="@lookup"><xsl:attribute name="label"><xsl:value-of select="@lookup"/></xsl:attribute></xsl:when>
       
   463 		<xsl:when test="self::cmp[text()]"><xsl:attribute name="label"><xsl:value-of select="."/></xsl:attribute></xsl:when>
       
   464 	</xsl:choose>
       
   465 </xsl:template>
       
   466 
       
   467 <xsl:template match="note" mode="make-label">
       
   468 	<xsl:choose>
       
   469 		<xsl:when test="not(@label-ref)"><xsl:copy-of select="node()"/></xsl:when>
       
   470 		<xsl:otherwise><xsl:apply-templates mode="eval-label" select="."/></xsl:otherwise>
       
   471 	</xsl:choose>
       
   472 </xsl:template>
       
   473 
       
   474 
       
   475 <!-- the title -->
       
   476 
       
   477 <xsl:template name="title-line1"> <!--  must call on SystemDefinition element-->
       
   478 	<xsl:value-of select="@name"/>
       
   479 	<xsl:if test="not(@name)"><xsl:value-of select="systemModel/@name"/></xsl:if>
       
   480 	<xsl:if test="@ver and @ver!=''">
       
   481 		<xsl:text> </xsl:text>
       
   482 		<xsl:apply-templates select="@ver" mode="as-text"/>
       
   483 	</xsl:if>
       
   484 </xsl:template>
       
   485 
       
   486 <xsl:template name="title-line2"> <!--  must call on SystemDefinition element-->
       
   487 	<xsl:if test="@label"><xsl:value-of select="@label"/></xsl:if>
       
   488 </xsl:template>
       
   489 
       
   490 <xsl:template name="title-line3"> <!--  must call on SystemDefinition element-->
       
   491 	<xsl:choose> <!-- show nothing if nothing specified, but leave tspan in case later need for it -->
       
   492 		<xsl:when test="@revision">
       
   493 			<xsl:variable name="rt"><xsl:apply-templates select="@revision-type" mode="as-text"/></xsl:variable>
       
   494 			<xsl:if test="$rt!=''">	<!--  space follows if not empty -->
       
   495 				<xsl:value-of select="concat($rt,' ')"/>							
       
   496 			</xsl:if>
       
   497 			<xsl:apply-templates select="@revision" mode="as-text"/>
       
   498 		</xsl:when>
       
   499 		<xsl:when test="@revision-type">
       
   500 			<xsl:apply-templates select="@revision-type" mode="as-text"/>
       
   501 		</xsl:when>
       
   502 	</xsl:choose>
       
   503 </xsl:template>
       
   504 
       
   505 <xsl:template match="SystemDefinition" mode="legend-title-width">
       
   506 	<xsl:param name="h"/>
       
   507 	<xsl:variable name="titleW" select="72.8"/> <!--  min title width -->
       
   508 	<xsl:variable name="len">
       
   509 		<xsl:call-template name="multiline-width">
       
   510 			<xsl:with-param name="t">	
       
   511 				<xsl:call-template name="title-line1"/>
       
   512 				<xsl:text>&#xa;</xsl:text>
       
   513 				<xsl:call-template name="title-line2"/>
       
   514 				<xsl:text>&#xa;</xsl:text>
       
   515 				<xsl:call-template name="title-line3"/>
       
   516 			</xsl:with-param>
       
   517 		</xsl:call-template>
       
   518 	</xsl:variable>
       
   519 		<!--  the width is a guess based on 2/3 of the (expected) bold font size -->
       
   520 	<xsl:choose>
       
   521 		<!-- use min width only if title is not explicitly scaled -->
       
   522 		<xsl:when test="(5 + $len * 0.66 * $h &lt; $titleW) and not(*/meta/legend/@title-scale)"><xsl:value-of select="$titleW"/></xsl:when>
       
   523 		<xsl:otherwise><xsl:value-of select="5 + $len * 0.66 * $h"/></xsl:otherwise>
       
   524 	</xsl:choose>
       
   525 </xsl:template>
       
   526 
       
   527 
       
   528 
       
   529 <xsl:template match="node()|@*" mode="as-text" priority="-1"><xsl:value-of select="."/></xsl:template>
       
   530 <xsl:template match="@copyright" mode="as-text">Copyright &#xa9; <xsl:value-of select="."/></xsl:template>
       
   531 
       
   532 <xsl:template match="@distribution" mode="as-text">
       
   533 	<xsl:choose>
       
   534 		<xsl:when test=".='secret'">SECRET</xsl:when>
       
   535 		<xsl:when test=".='confidential'">CONFIDENTIAL</xsl:when>
       
   536 		<xsl:when test=".='internal'">INTERNAL</xsl:when>
       
   537 		<xsl:when test=".='unrestricted'">UNRESTRICTED</xsl:when>
       
   538 		<xsl:otherwise>
       
   539 			<xsl:value-of select="."/>
       
   540 			<xsl:call-template name="Caller-Note"><xsl:with-param name="text">Warning: unknown security classification: <xsl:value-of select="."/></xsl:with-param></xsl:call-template>
       
   541 		</xsl:otherwise>
       
   542 	</xsl:choose>
       
   543 </xsl:template>
       
   544 
       
   545 
       
   546 <xsl:template match="@revision-type" mode="as-text">
       
   547 	<xsl:choose> <!-- known values are in uppercase -->
       
   548 		<xsl:when test=".='draft'">DRAFT</xsl:when>
       
   549 		<xsl:when test=".='issued'">ISSUED</xsl:when>
       
   550 		<xsl:when test=".='build'">Build</xsl:when>
       
   551 		<xsl:when test=".='date' and ../@revision!=''"/> <!-- don't show word 'date', just show the date -->
       
   552 		<xsl:otherwise><xsl:value-of select="normalize-space(.)"/></xsl:otherwise>
       
   553 	</xsl:choose>
       
   554 </xsl:template>
       
   555 
       
   556 <xsl:template match="@ver" mode="as-text">v<xsl:value-of select="."/></xsl:template>  <!-- normal  -->
       
   557 <xsl:template match="@ver[starts-with(.,'^')]" mode="as-text"><xsl:value-of select="."/></xsl:template>  <!-- to allow SF version notation -->
       
   558 <xsl:template match="@ver[starts-with(.,'tb')]" mode="as-text"> <!-- to allow TB notation -->
       
   559 	<xsl:value-of select="concat('vTB',substring(.,3,string-length(.)-3),'.',substring(.,string-length(.)))"/>
       
   560 </xsl:template> 
       
   561 
       
   562 <!-- /title -->
       
   563 
       
   564 
       
   565 
       
   566 <!-- ============ utilities ============ -->
       
   567 
       
   568 <xsl:template name="multiline-width"><xsl:param name="t" select="."/>
       
   569 	<xsl:choose>
       
   570 		<xsl:when test="contains($t,'&#xa;')">
       
   571 			<xsl:variable name="len" select="string-length(normalize-space(substring-before($t,'&#xa;')))"/>
       
   572 			<xsl:variable name="next">
       
   573 				<xsl:call-template name="multiline-width">
       
   574 					<xsl:with-param name="t" select="substring-after($t,'&#xa;')"/>
       
   575 				</xsl:call-template>
       
   576 			</xsl:variable>
       
   577 		<xsl:choose>
       
   578 			<xsl:when test="$len &lt; $next"><xsl:value-of select="$next"/></xsl:when>
       
   579 			<xsl:otherwise><xsl:value-of select="$len"/></xsl:otherwise>
       
   580 		</xsl:choose>
       
   581 		</xsl:when>
       
   582 		<xsl:otherwise><xsl:value-of select="string-length(normalize-space($t))"/></xsl:otherwise>
       
   583 	</xsl:choose>
       
   584 </xsl:template>
       
   585 
       
   586 
       
   587 </xsl:stylesheet>