如何从 golang 中的 .ttf 文件获取字体系列名称?
来源:stackoverflow
时间:2024-04-18 12:24:38 318浏览 收藏
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学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习