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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line