dongtanliefang8765 2018-09-23 03:13
浏览 196
已采纳

模板中的Golang ttf字体

I'm trying to get a TTF font to work in a golang template, but it wont render the font. It shows up as regular Times New Roman. I can change fonts using the standard font-family fonts (ex verdana or 'helvetica'), but I cant import a TTF.

All I can seem to find about TTF fonts is libraries to add text to images, but I want to change web fonts. How can I achieve this?

Structure of project is

  • /html_templates/portal.html
  • /html_teplates/Comfortaa-Regular.ttf
  • main.go

Here is the relevant golang code:

import (
    "fmt"
    "net/http"
    "text/template"
)
type Portal struct{
    Title string
}
func main(){
    //Create MUX Routing handlers
    http.HandleFunc("/", portal)

    //Start WebServer
    if err := http.ListenAndServe(":1234", nil); err != nil{ panic(err) }
}
func portal(w http.ResponseWriter, r *http.Request){
    //Create template
    tmpl, _ := template.ParseFiles("./html_templates/portal.html")

    //Populate struct
    portal := Portal{
        Title: "title",
    }

    //Execute template with struct data
    tmpl.Execute(w, portal)
}

And the relevant HTML:

<html>
<head>
    <title>{{ .Title }}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        @font-face {
            font-family: 'comfortaaRegular';
            src: url('Comfortaa-Regular.ttf');
            src: local('comfortaaRegular'), 
                 local('Comfortaa-Regular.ttf'), 
                 url('Comfortaa-Regular.ttf') format('truetype'),
        }
        body{ font-family: 'comfortaaRegular' }
    </style>
</head>
<body>
    <p>test/p>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • drema2014 2018-09-23 10:22
    关注

    You need process static files, add this to main func and set url to /static/Comfortaa-Regular.ttf in your template

    //Create MUX Routing for static
    fs := http.FileServer(http.Dir("./static"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))
    

    Here is complete working code

    package main
    
    import (
        "net/http"
        "text/template"
    )
    
    type Portal struct{
        Title string
    }
    
    func main(){
        //Create MUX Routing handlers
        http.HandleFunc("/", portal)
    
        //Create MUX Routing for static
        fs := http.FileServer(http.Dir("./static"))
        http.Handle("/static/", http.StripPrefix("/static/", fs))
    
        //Start WebServer
        if err := http.ListenAndServe(":1234", nil); err != nil{ panic(err) }
    }
    
    func portal(w http.ResponseWriter, r *http.Request){
        //Create template
        tmpl, _ := template.ParseFiles("./html_templates/portal.html")
    
        //Populate struct
        portal := Portal{
            Title: "title",
        }
    
        //Execute template with struct data
        tmpl.Execute(w, portal)
    }
    

    And template

    <head>
        <title>{{ .Title }}</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
        <style>
            @font-face {
                font-family: 'comfortaaRegular';
                src: url('/static/Comfortaa-Regular.ttf');
                src: local('comfortaaRegular'),
                     local('Comfortaa-Regular.ttf'),
                     url('/static/Comfortaa-Regular.ttf') format('truetype'),
            }
            body{ font-family: 'comfortaaRegular' }
        </style>
    </head>
    <body>
        <p>test</p>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题