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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝