精华内容
下载资源
问答
  • FontTools的使用
    千次阅读
    2020-12-25 14:11:37

    一、FontTools的安装
    pip install fontTools
    1
    二、FontTools的基本操作
    1、字体读取
    from fontTools.ttLib import TTFont

    加载字体文件:

    font = TTFont(‘maoyan.woff’)

    转为xml文件:

    font.saveXML(‘maoyan.xml’)
    1
    2
    3
    4
    5
    6
    7
    2、各节点名称:font.keys()
    from fontTools.ttLib import TTFont

    加载字体文件:

    font = TTFont(‘XRXV3I6Li01BKofINeaB.woff2’)

    kv = font.keys()

    print(kv)
    1
    2
    3
    4
    5
    6
    7
    8
    运行结果:

    [‘GlyphOrder’, ‘head’, ‘hhea’, ‘maxp’, ‘OS/2’, ‘hmtx’, ‘cmap’, ‘fpgm’, ‘prep’, 'cvt ', ‘loca’, ‘glyf’, ‘name’, ‘post’, ‘gasp’, ‘GDEF’, ‘GPOS’, ‘GSUB’]
    1
    3、获取getGlyphOrder节点name值
    font.getGlyphOrder()
    1
    运行结果:

    [’.notdef’, ‘cid00001’, ‘cid00017’, ‘cid00018’, ‘cid00019’, ‘cid00020’, ‘cid00021’, ‘cid00022’, ‘cid00023’, ‘cid00024’, ‘cid00025’, ‘cid00026’]
    1
    4、获取cmap节点code与name值映射
    print(font.getGlyphOrder())
    1
    运行结果:

    {48: ‘cid00026’, 49: ‘cid00017’, 50: ‘cid00019’, 51: ‘cid00024’, 52: ‘cid00022’, 53: ‘cid00021’, 54: ‘cid00020’, 55: ‘cid00018’, 56: ‘cid00025’, 57: ‘cid00023’}

    1
    2
    5、获取字体坐标信息
    #font[‘glyf’][字体编码].coordinates
    font[‘glyf’][‘one’].coordinates
    1
    2
    运行结果:

    GlyphCoordinates([(510, 69),(530, 51),(530, 35),(530, 18),(511, 0),(492, 0),(161, 0),(142, 0),(123, 18),(123, 35),(123, 51),(143, 69),(161, 69),(285, 69),(285, 605),(176, 534),(168, 529),(160, 529),(146, 529),(125, 555),(125, 570),(125, 591),(141, 600),(293, 699),(310, 710),(328, 710),(345, 710),(367, 688),(367, 668),(367, 69),(492, 69)])
    1
    6、获取坐标的0或10表示弧形区域 1表示矩形
    #font[‘glyf’][字体’].flags
    font[‘glyf’][‘one’].flags
    1
    2
    运行结果:

    array(‘B’, [0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1])
    1
    三、扩展
    1、一个最基本的字体文件一定会包含以下的表:

    cmap: Char­ac­ter to glyph map­ping unicode跟 Name的映射关系
    head: Font header 字体全局信息
    hhea: Hor­i­zon­tal header 定义了水平header
    hmtx: Hor­i­zon­tal met­rics 定义了水平metric
    maxp: Max­i­mum pro­file 用于为字体分配内存
    name: Nam­ing ta­ble 定义字体名称、风格名以及版权说明等
    glyf: 字形数据即轮廓定义和调整指令
    OS__2: OS__2 and Win­dows spe­cific met­rics
    post: Post­Script in­for­ma­tion
    2、高级书法特性的字体包含表:

    BASE: Base­line data
    GDEF: Glyph de­f­i­n­i­tion data
    GPOS: Glyph po­si­tion­ing data
    GSUB: Glyph sub­sti­tu­tion data
    JSTF: Jus­ti­fi­ca­tion data
    MATH: Math lay­out data

    更多相关内容
  • fontTools是一个使用Python编写的用于处理字体的库。 该项目包括TTX工具,该工具可以将TrueType和OpenType字体与XML文本格式(也称为TTX)相互转换。 它支持TrueType,OpenType,AFM,并在某种程度上支持Type 1和...
  • FontTools简介 这篇文章更全面地介绍了FontTools和现代字体开发。 它是从初学者/中级字体设计师和开发人员(即Stephen Nixon / )的角度编写的,旨在为希望更好地了解字体开发的某些常见过程和工具的设计师或开发...
  • node-fonttools 与机绑定以反编译和编译字体。 这个怎么运作 (也称为TTX)是一个使用Python编写的用于处理字体的库。 它支持TrueType,OpenType,AFM以及某种程度上的Type 1,以及某些Mac特定的格式。 使用 ,...
  • 包装fonttools(pyftsubset)周围的字体以提取字体字符 自动提取字体子集的Python脚本。 对CSS3属性unicode-range有用 docker run -it --rm \ -e FONT_NAME=myfont.woff \ -e FONT_FILES_KEYS=key1 key2 \ -e ...
  • ufo2ft(“ UFO到FontTools”)是的分支,其目标是从不具有FDK依赖关系的UFO生成OpenType字体二进制文件。 该库提供了两个函数compileOTF和compileTTF ,它们的工作方式完全相同: from defcon import Font from...
  • 离线安装包,测试可用。使用 pip install [完整包名] 进行安装
  • 离线安装包,测试可用
  • fontTools is a library for manipulating fonts, written in Python. Theproject includes the TTX tool, that can convert TrueType and OpenTypefonts to and from an XML text format, which i...

    What is this?

    fontTools is a library for manipulating fonts, written in Python. The

    project includes the TTX tool, that can convert TrueType and OpenType

    fonts to and from an XML text format, which is also called TTX. It

    supports TrueType, OpenType, AFM and to an extent Type 1 and some

    Mac-specific formats. The project has an MIT open-source

    licence.

    Among other things this means you can use it free of charge.

    Installation

    FontTools 4.x requires Python 3.6

    or later. FontTools 3.x requires Python 2.7 or later.

    NOTE From August 2019, until no later than January 1 2020, the support

    for Python 2.7 will be limited to only critical bug fixes, and no new features

    will be added to the py27 branch. You can read more here

    and here for the

    reasons behind this decision.

    The package is listed in the Python Package Index (PyPI), so you can

    install it with pip:

    pip install fonttools

    If you would like to contribute to its development, you can clone the

    repository from GitHub, install the package in 'editable' mode and

    modify the source code in place. We recommend creating a virtual

    environment, using virtualenv or

    Python 3 venv module.

    # download the source code to 'fonttools' folder

    git clone https://github.com/fonttools/fonttools.git

    cd fonttools

    # create new virtual environment called e.g. 'fonttools-venv', or anything you like

    python -m virtualenv fonttools-venv

    # source the `activate` shell script to enter the environment (Un*x); to exit, just type `deactivate`

    . fonttools-venv/bin/activate

    # to activate the virtual environment in Windows `cmd.exe`, do

    fonttools-venv\Scripts\activate.bat

    # install in 'editable' mode

    pip install -e .

    TTX – From OpenType and TrueType to XML and Back

    Once installed you can use the ttx command to convert binary font

    files (.otf, .ttf, etc) to the TTX XML format, edit them, and

    convert them back to binary format. TTX files have a .ttx file

    extension.

    ttx /path/to/font.otf

    ttx /path/to/font.ttx

    The TTX application can be used in two ways, depending on what

    platform you run it on:

    As a command line tool (Windows/DOS, Unix, macOS)

    By dropping files onto the application (Windows, macOS)

    TTX detects what kind of files it is fed: it will output a .ttx file

    when it sees a .ttf or .otf, and it will compile a .ttf or

    .otf when the input file is a .ttx file. By default, the output

    file is created in the same folder as the input file, and will have the

    same name as the input file but with a different extension. TTX will

    never overwrite existing files, but if necessary will append a unique

    number to the output filename (before the extension) such as

    Arial#1.ttf

    When using TTX from the command line there are a bunch of extra options.

    These are explained in the help text, as displayed when typing

    ttx -h at the command prompt. These additional options include:

    specifying the folder where the output files are created

    specifying which tables to dump or which tables to exclude

    merging partial .ttx files with existing .ttf or .otf

    files

    listing brief table info instead of dumping to .ttx

    splitting tables to separate .ttx files

    disabling TrueType instruction disassembly

    The TTX file format

    The following tables are currently supported:

    BASE, CBDT, CBLC, CFF, CFF2, COLR, CPAL, DSIG, EBDT, EBLC, FFTM,

    Feat, GDEF, GMAP, GPKG, GPOS, GSUB, Glat, Gloc, HVAR, JSTF, LTSH,

    MATH, META, MVAR, OS/2, SING, STAT, SVG, Silf, Sill, TSI0, TSI1,

    TSI2, TSI3, TSI5, TSIB, TSID, TSIJ, TSIP, TSIS, TSIV, TTFA, VDMX,

    VORG, VVAR, ankr, avar, bsln, cidg, cmap, cvar, cvt, feat, fpgm,

    fvar, gasp, gcid, glyf, gvar, hdmx, head, hhea, hmtx, kern, lcar,

    loca, ltag, maxp, meta, mort, morx, name, opbd, post, prep, prop,

    sbix, trak, vhea and vmtx

    Other tables are dumped as hexadecimal data.

    TrueType fonts use glyph indices (GlyphIDs) to refer to glyphs in most

    places. While this is fine in binary form, it is really hard to work

    with for humans. Therefore we use names instead.

    The glyph names are either extracted from the CFF table or the

    post table, or are derived from a Unicode cmap table. In the

    latter case the Adobe Glyph List is used to calculate names based on

    Unicode values. If all of these methods fail, names are invented based

    on GlyphID (eg glyph00142)

    It is possible that different glyphs use the same name. If this happens,

    we force the names to be unique by appending #n to the name (n

    being an integer number.) The original names are being kept, so this has

    no influence on a "round tripped" font.

    Because the order in which glyphs are stored inside the binary font is

    important, we maintain an ordered list of glyph names in the font.

    Other Tools

    Commands for merging and subsetting fonts are also available:

    pyftmerge

    pyftsubset

    fontTools Python Module

    The fontTools Python module provides a convenient way to

    programmatically edit font files.

    >>> from fontTools.ttLib import TTFont

    >>> font = TTFont('/path/to/font.ttf')

    >>> font

    >>>

    A selection of sample Python programs is in the

    Snippets

    directory.

    Optional Requirements

    The fontTools package currently has no (required) external dependencies

    besides the modules included in the Python Standard Library.

    However, a few extra dependencies are required by some of its modules, which

    are needed to unlock optional features.

    The fonttools PyPI distribution also supports so-called "extras", i.e. a

    set of keywords that describe a group of additional dependencies, which can be

    used when installing via pip, or when specifying a requirement.

    For example:

    pip install fonttools[ufo,lxml,woff,unicode]

    This command will install fonttools, as well as the optional dependencies that

    are required to unlock the extra features named "ufo", etc.

    Lib/fontTools/misc/etree.py

    The module exports a ElementTree-like API for reading/writing XML files, and

    allows to use as the backend either the built-in xml.etree module or

    lxml. The latter is preferred whenever present,

    as it is generally faster and more secure.

    Extra: lxml

    Lib/fontTools/ufoLib

    Package for reading and writing UFO source files; it requires:

    fs: (aka pyfilesystem2) filesystem

    abstraction layer.

    enum34: backport for the built-in enum

    module (only required on Python < 3.4).

    Extra: ufo

    Lib/fontTools/ttLib/woff2.py

    Module to compress/decompress WOFF 2.0 web fonts; it requires:

    brotli: Python bindings of

    the Brotli compression library.

    Extra: woff

    Lib/fontTools/ttLib/sfnt.py

    To better compress WOFF 1.0 web fonts, the following module can be used

    instead of the built-in zlib library:

    zopfli: Python bindings of

    the Zopfli compression library.

    Extra: woff

    Lib/fontTools/unicode.py

    To display the Unicode character names when dumping the cmap table

    with ttx we use the unicodedata module in the Standard Library.

    The version included in there varies between different Python versions.

    To use the latest available data, you can install:

    unicodedata2:

    unicodedata backport for Python 2.7 and 3.x updated to the latest

    Unicode version 12.0. Note this is not necessary if you use Python 3.8

    as the latter already comes with an up-to-date unicodedata.

    Extra: unicode

    Lib/fontTools/varLib/interpolatable.py

    Module for finding wrong contour/component order between different masters.

    It requires one of the following packages in order to solve the so-called

    "minimum weight perfect matching problem in bipartite graphs", or

    the Assignment problem:

    scipy: the Scientific Library

    for Python, which internally uses NumPy

    arrays and hence is very fast;

    munkres: a pure-Python

    module that implements the Hungarian or Kuhn-Munkres algorithm.

    Extra: interpolatable

    Lib/fontTools/varLib/plot.py

    Module for visualizing DesignSpaceDocument and resulting VariationModel.

    matplotlib: 2D plotting library.

    Extra: plot

    Lib/fontTools/misc/symfont.py

    Advanced module for symbolic font statistics analysis; it requires:

    sympy: the Python library for

    symbolic mathematics.

    Extra: symfont

    Lib/fontTools/t1Lib.py

    To get the file creator and type of Macintosh PostScript Type 1 fonts

    on Python 3 you need to install the following module, as the old MacOS

    module is no longer included in Mac Python:

    xattr: Python wrapper for

    extended filesystem attributes (macOS platform only).

    Extra: type1

    Lib/fontTools/pens/cocoaPen.py

    Pen for drawing glyphs with Cocoa NSBezierPath, requires:

    PyObjC: the bridge between

    Python and the Objective-C runtime (macOS platform only).

    Lib/fontTools/pens/qtPen.py

    Pen for drawing glyphs with Qt's QPainterPath, requires:

    PyQt5: Python bindings for

    the Qt cross platform UI and application toolkit.

    Lib/fontTools/pens/reportLabPen.py

    Pen to drawing glyphs as PNG images, requires:

    reportlab: Python toolkit

    for generating PDFs and graphics.

    Testing

    To run the test suite, you need to install pytest.

    When you run the pytest command, the tests will run against the

    installed fontTools package, or the first one found in the

    PYTHONPATH.

    You can also use tox to

    automatically run tests on different Python versions in isolated virtual

    environments.

    pip install tox

    tox

    Note that when you run tox without arguments, the tests are executed

    for all the environments listed in tox.ini's envlist. In our case,

    this includes Python 3.6 and 3.7, so for this to work the python3.6

    and python3.7 executables must be available in your PATH.

    You can specify an alternative environment list via the -e option,

    or the TOXENV environment variable:

    tox -e py36

    TOXENV="py36-cov,htmlcov" tox

    Development Community

    TTX/FontTools development is ongoing in an active community of

    developers, that includes professional developers employed at major

    software corporations and type foundries as well as hobbyists.

    Feature requests and bug reports are always welcome at

    https://github.com/fonttools/fonttools/issues/

    The best place for discussions about TTX from an end-user perspective as

    well as TTX/FontTools development is the

    https://groups.google.com/d/forum/fonttools mailing list. There is also

    a development https://groups.google.com/d/forum/fonttools-dev mailing

    list for continuous integration notifications. You can also email Behdad

    privately at behdad@behdad.org

    History

    The fontTools project was started by Just van Rossum in 1999, and was

    maintained as an open source project at

    http://sourceforge.net/projects/fonttools/. In 2008, Paul Wise (pabs3)

    began helping Just with stability maintenance. In 2013 Behdad Esfahbod

    began a friendly fork, thoroughly reviewing the codebase and making

    changes at https://github.com/behdad/fonttools to add new features and

    support for new font formats.

    Acknowledgements

    In alphabetical order:

    Olivier Berten, Samyak Bhuta, Erik van Blokland, Petr van Blokland,

    Jelle Bosma, Sascha Brawer, Tom Byrer, Frédéric Coiffier, Vincent

    Connare, Dave Crossland, Simon Daniels, Peter Dekkers, Behdad Esfahbod,

    Behnam Esfahbod, Hannes Famira, Sam Fishman, Matt Fontaine, Yannis

    Haralambous, Greg Hitchcock, Jeremie Hornus, Khaled Hosny, John Hudson,

    Denis Moyogo Jacquerye, Jack Jansen, Tom Kacvinsky, Jens Kutilek,

    Antoine Leca, Werner Lemberg, Tal Leming, Peter Lofting, Cosimo Lupo,

    Masaya Nakamura, Dave Opstad, Laurence Penney, Roozbeh Pournader, Garret

    Rieger, Read Roberts, Guido van Rossum, Just van Rossum, Andreas Seidel,

    Georg Seifert, Miguel Sousa, Adam Twardoch, Adrien Tétar, Vitaly Volkov,

    Paul Wise.

    Copyrights

    Copyright (c) 1999-2004 Just van Rossum, LettError

    (just@letterror.com)

    See LICENSE for the full license.

    Copyright (c) 2000 BeOpen.com. All Rights Reserved.

    Copyright (c) 1995-2001 Corporation for National Research Initiatives.

    All Rights Reserved.

    Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All

    Rights Reserved.

    Have fun!

    展开全文
  • Python_FontTools

    2020-12-05 09:03:12
    Font_Tools的使用1、fontTools使用总结from fontTools.ttLib import TTFont# 加载字体文件:font = TTFont('local_fonts.woff')# 保存为xml文件:font.saveXML('local_fonts.xml')# 获取各节点名称,返回为列表print...

    Font_Tools的使用

    1、fontTools使用总结

    from fontTools.ttLib import TTFont

    # 加载字体文件:

    font = TTFont('local_fonts.woff')

    # 保存为xml文件:

    font.saveXML('local_fonts.xml')

    # 获取各节点名称,返回为列表

    print(font.keys()) # ['GlyphOrder', 'head', 'hhea', 'maxp', 'OS/2', 'hmtx', 'cmap', 'loca', 'glyf', 'name', 'post', 'GSUB']

    # 获取getGlyphOrder节点的name值,返回为列表

    print(font.getGlyphOrder()) # ['glyph00000', 'x', 'uniF013', 'uniF4D4', 'uniEE40', 'uniF7E1', 'uniF34B', 'uniE1A0', 'uniF1BE', 'uniE91E', 'uniF16F', 'uniF724']

    print(font.getGlyphNames()) # ['glyph00000', 'uniE1A0', 'uniE91E', 'uniEE40', 'uniF013', 'uniF16F', 'uniF1BE', 'uniF34B', 'uniF4D4', 'uniF724', 'uniF7E1', 'x']

    # 获取cmap节点code与name值映射, 返回为字典

    print(font.getBestCmap()) # {120: 'x', 57760: 'uniE1A0', 59678: 'uniE91E', 60992: 'uniEE40', 61459: 'uniF013', 61807: 'uniF16F', 61886: 'uniF1BE', 62283: 'uniF34B', 62676: 'uniF4D4', 63268: 'uniF724', 63457: 'uniF7E1'}

    # 获取glyf节点TTGlyph字体xy坐标信息

    print(font['glyf']['uniE1A0'].coordinates) # GlyphCoordinates([(50, 335),(50, 468),(76, 544),(95, 638),(148, 676),(202, 710),(282, 710),(402, 710),(459, 617),(487, 574),(504, 501),(520, 437),(519, 335),(520, 271),(508, 166),(494, 126),(466, 46),(362, -39),(282, -49),(176, -35),(115, 37),(43, 121),(43, 335),(135, 335),(135, 154),(177, 95),(229, 35),(282, 35),(343, 35),(385, 107),(428, 155),(428, 339),(428, 515),(385, 576),(344, 635),(286, 635),(218, 635),(179, 583),(135, 506)])

    # 获取glyf节点TTGlyph字体xMin,yMin,xMax,yMax坐标信息

    print(font['glyf']['uniE1A0'].xMin, font['glyf']['uniE1A0'].yMin,

    font['glyf']['uniE1A0'].xMax, font['glyf']['uniE1A0'].yMax) # 0 -49 521 711

    # 获取glyf节点TTGlyph字体on信息

    print(font['glyf']['uniE1A0'].flags) # array('B', [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0])

    # 获取GlyphOrder节点GlyphID的id信息, 返回int型

    print(font.getGlyphID('uniE1A0')) # 7

    2、加载字体文件

    font = TTFont('local_fonts.woff')

    3、保存为xml文件:

    font.saveXML('local_fonts.xml')

    4、获取各节点名称,返回为列表:

    font.keys()

    # ['GlyphOrder', 'head', 'hhea', 'maxp', 'OS/2', 'hmtx', 'cmap', 'loca', 'glyf', 'name', 'post', 'GSUB']

    5、获取getGlyphOrder节点的name值,返回为列表:

    font.getGlyphOrder()

    # ['glyph00000', 'x', 'uniF013', 'uniF4D4', 'uniEE40', 'uniF7E1', 'uniF34B', 'uniE1A0', 'uniF1BE', 'uniE91E', 'uniF16F', 'uniF724']

    font.getGlyphNames()

    # ['glyph00000', 'uniE1A0', 'uniE91E', 'uniEE40', 'uniF013', 'uniF16F', 'uniF1BE', 'uniF34B', 'uniF4D4', 'uniF724', 'uniF7E1', 'x']

    6、获取cmap节点code与name值映射, 返回为字典:

    font.getBestCmap()

    # {120: 'x', 57760: 'uniE1A0', 59678: 'uniE91E', 60992: 'uniEE40', 61459: 'uniF013', 61807: 'uniF16F', 61886: 'uniF1BE', 62283: 'uniF34B', 62676: 'uniF4D4', 63268: 'uniF724', 63457: 'uniF7E1'}

    7、获取glyf节点TTGlyph字体xy坐标信息:

    font['glyf']['uniE1A0'].coordinates

    # GlyphCoordinates([(50, 335),(50, 468),(76, 544),(95, 638),(148, 676),(202, 710),(282, 710),(402, 710),(459, 617),(487, 574),(504, 501),(520, 437),(519, 335),(520, 271),(508, 166),(494, 126),(466, 46),(362, -39),(282, -49),(176, -35),(115, 37),(43, 121),(43, 335),(135, 335),(135, 154),(177, 95),(229, 35),(282, 35),(343, 35),(385, 107),(428, 155),(428, 339),(428, 515),(385, 576),(344, 635),(286, 635),(218, 635),(179, 583),(135, 506)])

    8、获取glyf节点TTGlyph字体xMin,yMin,xMax,yMax坐标信息:

    font['glyf']['uniE1A0'].xMin

    font['glyf']['uniE1A0'].yMin

    font['glyf']['uniE1A0'].xMax

    font['glyf']['uniE1A0'].yMax

    # 0 -49 521 711

    9、获取glyf节点TTGlyph字体on信息(0表示弧形/1表示矩形):

    font['glyf']['uniE1A0'].coordinates.flags

    # array('B', [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0])

    10、 获取GlyphOrder节点GlyphID的id信息, 返回int型:

    font.getGlyphID('uniE1A0')

    # 7

    展开全文
  • 本文主要介绍如果使用Python第三方库fontTools对OpenType字体文件(包括TrueType轮廓和Postscript轮廓)的解析操作。 fontTools简介 fontTools是由一组操作字体的库和组件组成的Python第三方库,要求Python3.6以

    Python微信订餐小程序课程视频

    https://edu.csdn.net/course/detail/36074

    Python实战量化交易理财系统

    https://edu.csdn.net/course/detail/35475

    前言

    本文主要介绍如果使用Python第三方库fontTools对OpenType字体文件(包括TrueType轮廓和Postscript轮廓)的解析操作。

    fontTools简介

    fontTools是由一组操作字体的库和组件组成的Python第三方库,要求Python3.6以及更高的版本。其中包括merge(字体合并)、subset(取字体子集)以及ttx(将OpenType转化为XML)等。

    安装

    pip install fontTools
    
    

    本文中使用的版本为4.28.5

    字体文件的解析

    读取

    如前文所述,OpenType字体文件标准是有sfnt结构封装的,基于sfnt的表结构,OpenType格式的字体文件可以分为多个表结构。

    创建TTFont实例,通过keys()可以查看字体文件的所有表名:

    from fontTools.ttLib.ttFont import TTFont
    font = TTFont("Resources/simsun.ttf")
    print(font.keys()) 
    
    

    运行结果如下:

    ['GlyphOrder', 'head', 'hhea', 'maxp', 'OS/2', 'hmtx', 'cmap', 'fpgm', 'prep', 'cvt ', 'loca', 'glyf', 'name', 'post', 'gasp', 'EBDT', 'EBLC', 'GDEF', 'GPOS', 'GSUB', 'MERG', 'meta', 'vhea', 'vmtx']
    
    

    表名中包含‘glyf‘,所以simsun.ttf是一个使用TrueType轮廓的字体文件。

    换成使用Postscript轮廓的字体文件:

    font = TTFont("Resources/AdobeSongStd-Light.otf")
    print(font.keys())
    
    

    运行结果如下:

    ['GlyphOrder', 'head', 'hhea', 'maxp', 'OS/2', 'name', 'cmap', 'post', 'CFF ', 'BASE', 'GPOS', 'GSUB', 'VORG', 'hmtx', 'vhea', 'vmtx', 'DSIG']
    
    

    表名中没有’glyf’而存在’CFF ',后者是存储Postscript信息的表格。

    对于TrueType Collection文件则可以使用如下方法读取,返回一个TTFont实例的列表

    from fontTools.ttLib.ttCollection import TTCollection
    collection = TTCollection("Resources/simsun.ttc")
    print(list(collection))
    
    

    运行结果如下:

    [object at 0x000001F8BA66A700>, object at 0x000001F8BE072AF0>]
    
    

    直接从这些表格提取到具体信息是复杂的,但TTFont提供了一些方法以方便地获取信息:

    font.getGlyphOrder() # 返回一个字形名称列表,以其在文件中的顺序排序
    font.getGlyphNames() # 返回一个字形名称列表,以字母顺序排序
    font.getBestCmap() # 返回一个字形ID为键、字形名称为值的字典
    font.getReverseGlyphMap() # 返回一个字形名称为键、字形ID为值的字典
    font.getGlyphName(10000) # 输入字形ID返回字形名称
    font.getGlyphID("uni70E0") # 输入字形名称返回字形ID
    font.getGlyphSet() # 返回一个\_TTGlyphSet对象,包含字形轮廓数据
    
    

    上述方法中,最后一项与轮廓数据有关的方法是最重要的。可惜的是,官方文档似乎并没有对这个对象做进一步解释,故下文是我读源码及其中注释后的分析,如有错漏,敬请指教。

    Pen与_TTGlyphset

    我认为,作者设计这一部分时的难点在于OpenType字体文件标准存在两种不同轮廓描述方式。Pen和_TTGlyphset的存在使得两种不同的轮廓描述方式可以用同一套方法解析和显示。

    The Pen Protocol

    基于TrueType轮廓的字体文件和基于Postscript轮廓是两种截然不同的数据格式。Pen是一个用于标准化的”画"出轮廓的对象,或者是数据和实际轮廓间的媒介。

    具体来说,Pen对象的子类包含将上述两种轮廓数据转化为画线、移动等模拟实际轮廓的方法。在fontTools的pen库中包含将轮廓数据转化为qt、reportLab等第三方库中实例的Pen子类。

    _TTGlpyhset

    _TTGlyphset是一个类似字典的,以字形名称为键、_TTGlyph为值的对象。_TTGlyph中包含字形数据轮廓数据并可以通过draw方法“画”出。_TTGlyph的两个子类_TTGlyphGlyf和_TTGlyphCFF分别对应TrueType轮廓和Postscript轮廓。具体使用方法如下:

    font = TTFont("Resources/simsun.ttf")
    glyph = font.getGlyphSet()["uni70E0"]
    glyph.draw(pen) # pen为实例化后的Pen子类
    
    

    freetypePen

    以freetype-py库为例,使用freetypePen首先需要安装freetype-py:

    pip install freetype-py
    
    

    以下代码修改自自fontTools的官方文档提供的范例程序:

    from fontTools.ttLib import TTFont
    from fontTools.pens.freetypePen import FreeTypePen
    from fontTools.misc.transform import Offset
    
    pen = FreeTypePen(None) # 实例化Pen子类
    font = TTFont("Resources/simsun.ttf") # 实例化TTFont
    glyph = font.getGlyphSet()["uni70E0"] # 通过字形名称选择某一字形对象
    glyph.draw(pen) # “画”出字形轮廓
    width, ascender, descender = glyph.width, font['OS/2'].usWinAscent, -font['OS/2'].usWinDescent # 获取字形的宽度和上沿以及下沿
    height = ascender - descender # 利用上沿和下沿计算字形高度
    pen.show(width=width, height=height, transform=Offset(0, -descender)) # 显示以及矫正
    
    

    运行结果如下:

    注意,可能由于fontTools==4.28.5版本问题,通过pip安装后freetypePen.py并没有包含在pens文件夹下,需要使用的可以从fontTools的GitHub仓库中下载,放到site-packages\fontTools\pens文件夹下,下载路径为https://github.com/fonttools/fonttools/blob/main/Lib/fontTools/pens/freetypePen.py。该问题可能在后续版本中修复。

    ttx

    最后是我个人认为fontTools中最为实用的一个组件ttx,其功能为将TTFont实例转化为XML格式以及将XML文件转化为TTFont。基于这一组件,我们可以较为方便的实现对字体文件内容的修改。

    from fontTools.ttLib import TTFont
    
    font = TTFont("Resources/simsun.ttf") # 实例化TTFont
    font.saveXML("simsun.xml") # TTFont实例转化为XML文件,参数为XML文件路径
    font.importXML("simsun1.xml") # XML文件转化为TTFont实例,参数为XML文件路径
    
    

    除了将整个字体文件转化为XML文件,ttx还可以将文件中的单个表转化为XML文件,避免多余的存储和时间消耗:

    from fontTools.ttLib.ttFont import TTFont
    
    font = TTFont("Resources/simsun.ttf")
    font.saveXML("temp2.xml",tables=["glyf"]) # tables为需要转化的表名组成列表
    
    

    单个表的XML文件直接导入TTFont时,只会影响XML文件存在的表,而其他表不变:

    font.importXML("temp2.xml")
    
    

    参考

    https://fonttools.readthedocs.io/en/latest/index.html

    https://github.com/fonttools/fonttools

    展开全文
  • Python_FontTools使用

    千次阅读 2019-12-11 00:23:45
    pip install fontTools FontCreator工具:https://www.high-logic.com/font-editor/fontcreator 在线FontEditor工具:http://fontstore.baidu.com/static/editor/index.html 1、fontTools使用 woff字体文件点击...
  • 使用fontTools

    2021-05-30 00:14:17
    # 使用fontTools库 from fontTools.ttLib import TTFont import numpy as np from PIL import Image font = TTFont("D:/zitigen/字体/字体管家方萌简.ttf") # 输出的uniMap是一个字典,key代表的unicode的int值,...
  • 在使用字体渲染某些字符时,有可能渲染出空白或者“口”字形,原因在于该字体文件中不包含该字符的字形。 可能的原因包括: 字符不在字体的cmap表中(cmap表是字体文件声明的支持字符表); 字符不在字体的glyf表...
  • 资源分类:Python库 所属语言:Python 资源全名:fonttools-3.2.2.zip 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
  • python反爬虫之fontTools库的使用
  • Myanmar Font Tools-crx插件

    2021-04-05 03:21:22
    语言:English 缅甸语写作和阅读工具 使chrome能够自动显示缅甸官方Unicode和ZawGyi官方字体,而无需安装缅甸字体或更改字体设置。 还有一个缅甸键盘模拟器,用户可以使用它通过上下文菜单(测试版)输入缅甸Unicode...
  • 在 我为fontforge写的剧本:import fontforge import fontTools.feaLib.builder as feaLibBuilder from fontTools.ttLib import TTFont font_name = 'maeh.ttf' font = fontforge.font() glyph = font.createChar(33...
  • 资源分类:Python库 所属语言:Python 资源全名:fonttools-3.27.0.zip 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
  • fonttools 使用教程

    千次阅读 2019-02-26 16:06:00
    from fontTools.ttLib.ttFont import TTFont from fontTools import subset # ttf to xml font = TTFont('arial.ttf') font.saveXML('arial.xml') #xml to ttf font = TTFont() font.importXML('arial.xml') font....
  • 资源来自pypi官网。 资源全名:fonttools-3.26.0.zip
  • fonttools-4.26.1-cp310-cp310-win32
  • 00004__fonttools__字体裁剪

    千次阅读 2018-11-02 22:10:19
    fonttools__字体裁剪 一、复制字体文件 例如可以从C:\Windows\Fonts目录下的SIMSUNB.TTF字体 二、软件安装 需要提前安装好python,然后使用fonttools中的pyftsubset工具提取,命令如下: pip install fonttools ...
  • fonttools-4.26.1-cp39-cp39-win32
  • fonttools-4.26.1-cp38-cp38-win32
  • OpenType字体,由提供支持 这是解决OpenType字体之间的命名,样式和映射问题的快速工具。 在没有给出任何选项的情况下,它会扫描并维护字体表之间的metedata一致性; 使用简单但功能强大的配置文件,可以自动生成...
  • 字体工具 这是尝试编写Rust库以读取,操作和写入TTF / OTF文件的尝试。 它处于实验开发的极早期阶段。 欢迎捐款。
  • fonttools-4.26.1-cp37-cp37m-win32

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 40,606
精华内容 16,242
关键字:

fonttools

友情链接: NG-Swich.rar