如何从 golang 中的 .ttf 文件获取字体系列名称?
Golang不知道大家是否熟悉?今天我将给大家介绍《如何从 golang 中的 .ttf 文件获取字体系列名称?》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!
众所周知,.ttf(或otf)字体文件的文件名并不总是与字体系列名称相同,例如,如果我将arial.ttf重命名为abcd.ttf,它的字体系列名称仍然是arial,那么golang中有没有一个包或库来解析.ttf并获取该名称?
我尝试过freetype,但似乎没有方法可以得到它
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/golang/freetype"
)
func main() {
homedir, _ = os.userhomedir()
fontfile := homedir + "/downloads/new folder with items 5/arial.ttf"
fontbytes, err := ioutil.readfile(fontfile)
font, err := freetype.parsefont(fontbytes)
if err == nil {
fmt.println(font)
}
}
*truetype.font的方法很少
name 返回给定 nameid 的字体名称值
哪里可以找到 nameid?
编辑:
原来nameid是一个go常量,代表truetype字体属性的一个field,我发现nameidpostscriptname字段的值会被用作字体唯一名称,例如一个.fcpxml文件就会使用这个值因为它的字体名称
以下代码片段可以获得.ttf文件的唯一字体名称
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
// the original freetype has a bug for utf8: https://github.com/golang/freetype/issues/66
// "github.com/golang/freetype"
// so we use this beta one
"github.com/beta/freetype"
"github.com/beta/freetype/truetype"
)
// getpostscriptname returns the postscriptname of a .ttf font file
func getpostscriptname(fontfilepath string) (postscriptname string, err error) {
postscriptname = ""
fontbytes, err := ioutil.readfile(fontfilepath)
if err == nil {
font, err := freetype.parsefont(fontbytes)
if err == nil {
postscriptname = font.name(truetype.nameidpostscriptname)
}
}
return postscriptname, err
}
func main() {
fontfilerelative := "downloads/new folder with items 5/arial.ttf"
homedir, _ := os.userhomedir()
fontfile := filepath.join(homedir, fontfilerelative)
postscriptname, _ := getpostscriptname(fontfile)
fmt.println(postscriptname)
}
编辑2:
freetype不支持otf文件,谷歌搜索后,我发现我们可以使用sfnt来获取.ttf或.otf字体文件的所有元
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"golang.org/x/image/font/sfnt"
)
func main() {
fontfile := "/path/to/arial.ttf"
nameids, err := getfntnameids(fontfile)
if err == nil {
fmt.println(nameids["nameidpostscript"])
fmt.println("-------------------------")
prettyprint(nameids)
} else {
fmt.println(err)
}
}
// prettyprint print map or slice as formatted json
func prettyprint(v interface{}) (err error) {
b, err := json.marshalindent(v, "", " ")
if err == nil {
fmt.println(string(b))
}
return
}
// getfntnameids returns .ttf or .otf font nameids
// inspired by https://github.com/wayneashleyberry/sfnt
func getfntnameids(fontfile string) (nameidinfo map[string]string, err error) {
b, err := ioutil.readfile(fontfile)
if err != nil {
return nameidinfo, err
}
f, err := sfnt.parse(b)
if err != nil {
return nameidinfo, err
}
nameids := []sfnt.nameid{
sfnt.nameidcopyright,
sfnt.nameidfamily,
sfnt.nameidsubfamily,
sfnt.nameiduniqueidentifier,
sfnt.nameidfull,
sfnt.nameidversion,
sfnt.nameidpostscript,
sfnt.nameidtrademark,
sfnt.nameidmanufacturer,
sfnt.nameiddesigner,
sfnt.nameiddescription,
sfnt.nameidvendorurl,
sfnt.nameiddesignerurl,
sfnt.nameidlicense,
sfnt.nameidlicenseurl,
sfnt.nameidtypographicfamily,
sfnt.nameidtypographicsubfamily,
sfnt.nameidcompatiblefull,
sfnt.nameidsampletext,
sfnt.nameidpostscriptcid,
sfnt.nameidwwsfamily,
sfnt.nameidwwssubfamily,
sfnt.nameidlightbackgroundpalette,
sfnt.nameiddarkbackgroundpalette,
sfnt.nameidvariationspostscriptprefix,
}
labels := []string{
"nameidcopyright",
"nameidfamily",
"nameidsubfamily",
"nameiduniqueidentifier",
"nameidfull",
"nameidversion",
"nameidpostscript",
"nameidtrademark",
"nameidmanufacturer",
"nameiddesigner",
"nameiddescription",
"nameidvendorurl",
"nameiddesignerurl",
"nameidlicense",
"nameidlicenseurl",
"nameidtypographicfamily",
"nameidtypographicsubfamily",
"nameidcompatiblefull",
"nameidsampletext",
"nameidpostscriptcid",
"nameidwwsfamily",
"nameidwwssubfamily",
"nameidlightbackgroundpalette",
"nameiddarkbackgroundpalette",
"nameidvariationspostscriptprefix",
}
nameidinfo = map[string]string{}
for i, nameid := range nameids {
label := labels[i]
value, err := f.name(nil, nameid)
if err != nil {
nameidinfo[label] = ""
continue
}
nameidinfo[label] = value
}
return nameidinfo, err
}
输出
ArialMT
-------------------------
{
"NameIDCompatibleFull": "",
"NameIDCopyright": "© 2006 The Monotype Corporation. All Rights Reserved.",
"NameIDDarkBackgroundPalette": "",
"NameIDDescription": "",
"NameIDDesigner": "Monotype Type Drawing Office - Robin Nicholas, Patricia Saunders 1982",
"NameIDDesignerURL": "",
"NameIDFamily": "Arial",
"NameIDFull": "Arial",
"NameIDLicense": "You may use this font to display and print content as permitted by the license terms for the product in which this font is included. You may only (i) embed this font in content as permitted by the embedding restrictions included in this font; and (ii) temporarily download this font to a printer or other output device to help print content.",
"NameIDLicenseURL": "",
"NameIDLightBackgroundPalette": "",
"NameIDManufacturer": "The Monotype Corporation",
"NameIDPostScript": "ArialMT",
"NameIDPostScriptCID": "",
"NameIDSampleText": "",
"NameIDSubfamily": "Regular",
"NameIDTrademark": "Arial is a trademark of The Monotype Corporation in the United States and/or other countries.",
"NameIDTypographicFamily": "",
"NameIDTypographicSubfamily": "",
"NameIDUniqueIdentifier": "Monotype:Arial Regular:Version 5.01 (Microsoft)",
"NameIDVariationsPostScriptPrefix": "",
"NameIDVendorURL": "",
"NameIDVersion": "Version 5.01.2x",
"NameIDWWSFamily": "",
"NameIDWWSSubfamily": ""
}
实际上,软件将使用 nameidfamily 和 nameidtypgraphicsubfamily 作为 font 属性,而不是 nameidpostscriptname。
正确答案
使用这个go库freetype可以读取ttf文件的数据,你只需要提供字体数据,它就会解析所有数据。还有一篇关于使用 javascipt 手动读取 ttf 文件内容的文章 here。
编辑:如果您使用 freetype 来获取姓氏或其他信息,您可以使用结构体的 name 接收器函数,它接受一个 nameid,它是 uint16 的别名。 (您可以在名称id代码部分找到有效值here的完整表格)
例如,您可以使用以下代码获取字体系列名称:
package main
import (
"fmt"
"io/ioutil"
"github.com/golang/freetype"
)
func main() {
fontFile := "./font.ttf"
fontBytes, err := ioutil.ReadFile(fontFile)
font, err := freetype.ParseFont(fontBytes)
if err == nil {
fmt.Println(font.Name(1))
}
}以上就是《如何从 golang 中的 .ttf 文件获取字体系列名称?》的详细内容,更多关于的资料请关注golang学习网公众号!
LGA-1851 插槽特写,广积展示新嵌入式主板:支持英特尔酷睿 Ultra 200 系列处理器
- 上一篇
- LGA-1851 插槽特写,广积展示新嵌入式主板:支持英特尔酷睿 Ultra 200 系列处理器
- 下一篇
- DynamoDB 按 Scan 函数中的特定字段排序
-
- Golang · Go问答 | 53分钟前 |
- 火焰图里占比最高的函数就一定最值得优化吗
- 282浏览 收藏
-
- Golang · Go问答 | 1小时前 | go · TLS · 长连接 GetCertificate 证书轮换 会话恢复 Go TLS
- 证书轮换时旧长连接会立即失效吗,更新范围怎样判断
- 209浏览 收藏
-
- Golang · Go问答 | 1小时前 | 网络编程 · https · Go问答 · tls Go 证书校验 RootCAs VerifyConnection InsecureSkipVerify
- 为什么 InsecureSkipVerify 不是临时万能解法,安全替代方案有哪些
- 125浏览 收藏
-
- Golang · Go问答 | 2小时前 |
- 动态生成属性和脚本片段为何会被替换为安全占位符
- 176浏览 收藏
-
- Golang · Go问答 | 2小时前 | 错误处理 · go · 模板 · html/template text/template missingkey Go模板 模板错误处理
- 模板执行时出现缺失字段,应报错还是输出空值
- 223浏览 收藏
-
- Golang · Go问答 | 3小时前 |
- html/template 与 text/template 的转义边界有什么不同
- 245浏览 收藏
-
- Golang · Go问答 | 3小时前 | Go问答 · 类型系统 · reflect.Type 缓存键 Go反射 类型名称
- 反射缓存按 Type 还是类型名称做键更可靠
- 194浏览 收藏
-
- Golang · Go问答 | 3小时前 |
- 反射拿到零值后怎样区分无效值、nil 指针和类型零值
- 208浏览 收藏
-
- Golang · Go问答 | 4小时前 | go语言 · 故障排查 · reflect.Value CanSet Go反射 可寻址性
- reflect.Value 为什么会 panic,CanSet 与可寻址性如何理解
- 300浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 378次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 449次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 457次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 400次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 228次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go crypto/rand.Text 的长度为什么不是固定字符数
- 2026-10-04 501浏览
-
- Go strings.ToValidUTF8 清洗日志内容的边界
- 2026-10-03 501浏览
-
- Go tls.GetCertificate 为什么收不到空 ServerName 请求
- 2026-09-27 501浏览

