SVG 使用其他的中文字型

今天在HTML5的測試網頁中加入一段語法,試著讓SVG可以使用免費的王漢宗中文字型。
語法主要是以CSS宣告使用的字型,再以CSS重設SVG的Tag「text」
<style type="text/css"><![CDATA[
           @font-face {
      自訂一個字型名稱
               font-family: "chinese_font";
  wt028.ttf是字型的檔案名稱,與HTML5的網頁放在同一個目錄下
               src: url(wt028.ttf) format("truetype");
           }
         
           text {
      字型改為我們自訂的字型名稱
              font-family: "chinese_font", serif;                
  font-size:40px;
  font-style:normal;
  font-weight:normal;line-height:125%;
  letter-spacing:0px;
  word-spacing:0px;
  fill:#000000;
  fill-opacity:1;
  stroke:none;  
           }
]]></style>

完整的HTML5網頁原始碼如下:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <title>Hello World</title>
    </head>
    <body>
        <img src="button.png" id="imgBtn1" />
          <br />
        <svg
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:cc="http://creativecommons.org/ns#"
            xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:svg="http://www.w3.org/2000/svg"
            xmlns="http://www.w3.org/2000/svg"
            version="1.1"
            width="252.80942"
            height="100.89236"
        id="svg2">

        <style type="text/css"><![CDATA[
           @font-face {
               font-family: "chinese_font";
               src: url(wt028.ttf) format("truetype");
           }

           text {
              font-family: "chinese_font", serif;                
  font-size:40px;
  font-style:normal;
  font-weight:normal;line-height:125%;
  letter-spacing:0px;
  word-spacing:0px;
  fill:#000000;
  fill-opacity:1;
  stroke:none;  
           }
        ]]></style>

        <defs id="defs4" />
        <metadata  id="metadata7">
        <rdf:RDF>
        <cc:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
        </cc:Work>
        </rdf:RDF>
        </metadata>
        <g
          transform="translate(-105.26811,-111.91601)"
          id="layer1">
        <rect
          width="251.91707"
          height="100"
          rx="5.5981574"
          ry="50"
          x="105.71429"
          y="112.36218"
          id="rect2985"
          style="fill:#008000;stroke:#000000;stroke-width:0.89235312;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />
        <text
          x="151.42857"
          y="178.07648"
          id="text2987"
          xml:space="preserve"
          >
            <tspan
            x="151.42857"
            y="178.07648"
            id="tspan2989">文字測試</tspan></text>
        </g>
        </svg>        
       
    </body>
</html>

<script src="jquery-2.1.1.min.js"></script>
       
<script type="text/javascript">  
     $("#imgBtn1").click(function() {
         alert(SVG_Check());
        $('#tspan2989').text("改變成功");  
     });  
       
     //判斷瀏覽器是否支援SVG
    function SVG_Check(){
        SVG_NS = 'http://www.w3.org/2000/svg' ;
         return !!document.createElementNS &&  !!document.createElementNS(SVG_NS, 'svg').createSVGRect;
    }
   
</script>

結果如下,中文字變成空心的王漢宗字型,而且按下小房子的圖示,還可以用jQuery來改變中文字:

Windows 11安裝時跳過網路連線