美文网首页程序员
用fonttools获取字体的cmap

用fonttools获取字体的cmap

作者: 千羽之城88 | 来源:发表于2019-10-10 20:45 被阅读0次

cmap 是字符与字体字形对应的表(The 'cmap' table maps character codes to glyph indices),我们可以通过的 fonttools 中的 ttx 命令得到列表,fonttools 是免费的,我们先到这里下载:

https://github.com/fonttools/fonttools

mac 系统安装如下:

brew install fonttools

我们先看看一个字体包含什么表:

ttx -l "/Library/Fonts/MinionPro-Regular.otf"

Listing table info for "/Library/Fonts/MinionPro-Regular.otf":
    tag     checksum    length    offset
    ----  ----------  --------  --------
    BASE  0x65195DBD        70    210728
    CFF   0x03E00685    145687      6216
    DSIG  0x31030AF4      7840    210800
    GPOS  0xA233A174     41074    169652
    GSUB  0x86E4C647     11002    158648
    OS/2  0x6045D4FB        96       320
    cmap  0x5F4539C8      4106      2076
    head  0xFDC157B5        54       220
    hhea  0x09140BCC        36       276
    hmtx  0xE934AE13      6744    151904
    maxp  0x06965000         6       312
    name  0x114F1B92      1657       416
    post  0xFFB80032        32      6184

接着,我们 dump 出这个字体 cmap 来:

ttx -t cmap  "/Library/Fonts/MinionPro-Regular.otf"

得到一个 xml 文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<ttFont sfntVersion="OTTO" ttLibVersion="3.38">

  <cmap>
    <tableVersion version="0"/>
    <cmap_format_4 platformID="0" platEncID="3" language="0">
      <map code="0x20" name="space"/><!-- SPACE -->
      <map code="0x21" name="exclam"/><!-- EXCLAMATION MARK -->
      <map code="0x22" name="quotedbl"/><!-- QUOTATION MARK -->
      <map code="0x23" name="numbersign"/><!-- NUMBER SIGN -->
      <map code="0x24" name="dollar"/><!-- DOLLAR SIGN -->
      <map code="0x25" name="percent"/><!-- PERCENT SIGN -->
      <map code="0x26" name="ampersand"/><!-- AMPERSAND -->
      <map code="0x27" name="quotesingle"/><!-- APOSTROPHE -->
      <map code="0x28" name="parenleft"/><!-- LEFT PARENTHESIS -->
      <map code="0x29" name="parenright"/><!-- RIGHT PARENTHESIS -->
      <map code="0x2a" name="asterisk"/><!-- ASTERISK -->
      <map code="0x2b" name="plus"/><!-- PLUS SIGN -->
      <map code="0x2c" name="comma"/><!-- COMMA -->
      <map code="0x2d" name="hyphen"/><!-- HYPHEN-MINUS -->
      ...
    </cmap_format_4>
  </cmap>

</ttFont>

得到cmap后就可以对它进行操作了。

参考

  1. https://simoncozens.github.io/fonts-and-layout/opentype.html 【需要重点阅读】
  2. https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html
  3. https://github.com/fonttools/fonttools

相关文章

网友评论

    本文标题:用fonttools获取字体的cmap

    本文链接:https://www.haomeiwen.com/subject/toaapctx.html