svghmi/inline_svg.ysl2
branchsvghmi
changeset 2878 bec552270ad1
parent 2877 682bce953795
child 2904 92d115d8828d
equal deleted inserted replaced
2877:682bce953795 2878:bec552270ad1
       
     1 // inline_svg.ysl2
       
     2 //
       
     3 // Produce Inline SVG element of resulting XHTML page.
       
     4 
       
     5 // Identity template :
       
     6 //  - copy every attributes 
       
     7 //  - copy every sub-elements
       
     8 template "@* | node()", mode="inline_svg" {
       
     9   // use real xsl:copy instead copy-of alias from yslt.yml2
       
    10   if "not(@id = $discardable_elements/@id)"
       
    11       xsl:copy apply "@* | node()", mode="inline_svg";
       
    12 }
       
    13 
       
    14 // replaces inkscape's height and width hints. forces fit
       
    15 template "svg:svg/@width", mode="inline_svg";
       
    16 template "svg:svg/@height", mode="inline_svg";
       
    17 svgtmpl "svg:svg", mode="inline_svg" svg {
       
    18     attrib "preserveAspectRatio" > none
       
    19     attrib "height" > 100vh
       
    20     attrib "width" > 100vw
       
    21     apply "@* | node()", mode="inline_svg";
       
    22 }
       
    23 // ensure that coordinate in CSV file generated by inkscape are in default reference frame
       
    24 template "svg:svg[@viewBox!=concat('0 0 ', @width, ' ', @height)]", mode="inline_svg" {
       
    25     error > ViewBox settings other than X=0, Y=0 and Scale=1 are not supported
       
    26 }
       
    27 // ensure that coordinate in CSV file generated by inkscape match svg default unit
       
    28 template "sodipodi:namedview[@units!='px' or @inkscape:document-units!='px']", mode="inline_svg" {
       
    29     error > All units must be set to "px" in Inkscape's document properties
       
    30 }
       
    31 
       
    32 
       
    33 //////////////// Clone Unlinking
       
    34 
       
    35 // svg:use (inkscape's clones) inside a widgets are
       
    36 // replaced by real elements they refer in order to :
       
    37 //  - allow finding "needle" element in "meter" widget,
       
    38 //    even if "needle" is in a group refered by a svg use.
       
    39 //  - if "needle" is visible through a svg:use for
       
    40 //    each instance of the widget, then needle would show
       
    41 //    the same position in all instances
       
    42 //
       
    43 // For now, clone unlinkink applies to descendants of all widget except HMI:Page
       
    44 // TODO: narrow application of clone unlinking to active elements,
       
    45 //       while keeping static decoration cloned
       
    46 const "to_unlink", "$hmi_elements[not(@id = $hmi_pages)]//svg:use";
       
    47 svgtmpl "svg:use", mode="inline_svg"
       
    48 {
       
    49     choose {
       
    50         when "@id = $to_unlink/@id"
       
    51             call "unlink_clone";
       
    52         otherwise
       
    53             xsl:copy apply "@* | node()", mode="inline_svg";
       
    54     }
       
    55 }
       
    56 
       
    57 // to unlink a clone, an group containing a copy of target element is created
       
    58 // that way, style and transforms can be preserved
       
    59 const "_excluded_use_attrs" {
       
    60     name > href
       
    61     name > width
       
    62     name > height
       
    63     name > x
       
    64     name > y
       
    65 }
       
    66 const "excluded_use_attrs","exsl:node-set($_excluded_use_attrs)";
       
    67 
       
    68 svgfunc "unlink_clone"{
       
    69     g{
       
    70         // include non excluded attributes
       
    71         foreach "@*[not(local-name() = $excluded_use_attrs/name)]"
       
    72             attrib "{name()}" > «.»
       
    73 
       
    74         const "targetid","substring-after(@xlink:href,'#')";
       
    75         apply "//svg:*[@id = $targetid]", mode="unlink_clone"{
       
    76             with "seed","@id";
       
    77         }
       
    78     }
       
    79 }
       
    80 
       
    81 // clone unlinking is really similar to deep-copy
       
    82 // all nodes are sytematically copied
       
    83 svgtmpl "@id", mode="unlink_clone" {
       
    84     param "seed";
       
    85     attrib "id" > «$seed»_«.»
       
    86 }
       
    87 
       
    88 svgtmpl "@*", mode="unlink_clone" xsl:copy;
       
    89 
       
    90 // copying widgets would have unwanted effect
       
    91 // instead widget is refered through a svg:use.
       
    92 svgtmpl "svg:*", mode="unlink_clone" {
       
    93     param "seed";
       
    94     choose {
       
    95         // node recursive copy ends when finding a widget
       
    96         when "@id = $hmi_elements/@id" {
       
    97             // place a clone instead of copying
       
    98             use{
       
    99                 attrib "xlink:href" > «concat('#',@id)»
       
   100             }
       
   101         }
       
   102         otherwise {
       
   103             xsl:copy apply "@* | node()", mode="unlink_clone" {
       
   104                 with "seed","$seed";
       
   105             }
       
   106         }
       
   107     }
       
   108 }
       
   109 
       
   110 /*const "mark" > =HMI=\n*/
       
   111 
       
   112 const "result_svg" apply "/", mode="inline_svg";
       
   113 const "result_svg_ns", "exsl:node-set($result_svg)";
       
   114 
       
   115 function "debug_unlink" {
       
   116     foreach "$to_unlink"{
       
   117         | «@id»
       
   118     }
       
   119 }
       
   120 !debug_output_calls.append("debug_unlink")