如何将字体文件拆分为多个部分?

发布于 2025-01-17 06:38:50 字数 438 浏览 1 评论 0原文

如何自动将字体文件分割成多个部分?

为什么我需要这个?因为在 Chrome 99 之前,它限制字体的最大未压缩文件大小为 30 MB。看 https://chromium.googlesource.com/external/ ots/+/v6.1.1/src/ots.cc

此外,每个文件的中文字体通常超过 30 MB。 例如 CNS11643 字体: https://data.gov.tw/dataset/5961

因此,我需要一个自动化工具分割字体文件。

How to split a font file to multiple parts automatically?

Why do I need this? Because as up to Chrome 99, it limits maximum uncompressed file size of a font to 30 MB. See
https://chromium.googlesource.com/external/ots/+/v6.1.1/src/ots.cc

Additionally, Chinese fonts often excceed 30 MB per file.
For example, CNS11643 fonts:
https://data.gov.tw/dataset/5961

Thus, I need an automation tool to split a font file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2025-01-24 06:38:50

FontForge应用程序支持Python脚本,可用于实现自动字体分割。

这是一个示例脚本 2

import glob
import os
import fontforge

# Up to Chrome 99, the max uncompressed size (e.g. ttf) of a font file is 30 MB.
# Please tune this value to split a font file to multiple files with size <= 30 MB.
maxGlyphsPerFile = 15000
# The glob expression of all font files.
fontFilesPath = "../fonts/*.ttf"

def splitFontFile(originalFile):
    filename = os.path.basename(file)
    filename = filename.split('.')[0]
    font = fontforge.open(originalFile)
    font.selection.all()
    glyphs = font.selection.byGlyphs
    totalGlyphs = len(list(glyphs))
    totalFiles = int(totalGlyphs / maxGlyphsPerFile + 0.5)
    print(f'{file} totalFiles: {totalFiles}')

    for f in range(totalFiles):
        g = 0
        gMin = f * maxGlyphsPerFile
        gMax = (f + 1) * maxGlyphsPerFile
        for glyph in glyphs:
            # Clear glyph out of range.
            if not(gMin <= g and g < gMax):
                glyph.clear()
            g = g + 1
        # Use this to check uncompressed font file sizes.
        #font.generate(f'{filename}-{f+1}.ttf')
        # Use this for compressed outputs.
        font.generate(f'{filename}-{f+1}.woff2')
        font.close()
        font = fontforge.open(originalFile)
        font.selection.all()
        glyphs = font.selection.byGlyphs
    font.close()
    
for file in glob.glob(fontFilesPath):
    splitFontFile(file)
    print('Done!')

FontForge app supports Python scripting, which can be used to achieve automatic font splitting.

This is an example script 2.

import glob
import os
import fontforge

# Up to Chrome 99, the max uncompressed size (e.g. ttf) of a font file is 30 MB.
# Please tune this value to split a font file to multiple files with size <= 30 MB.
maxGlyphsPerFile = 15000
# The glob expression of all font files.
fontFilesPath = "../fonts/*.ttf"

def splitFontFile(originalFile):
    filename = os.path.basename(file)
    filename = filename.split('.')[0]
    font = fontforge.open(originalFile)
    font.selection.all()
    glyphs = font.selection.byGlyphs
    totalGlyphs = len(list(glyphs))
    totalFiles = int(totalGlyphs / maxGlyphsPerFile + 0.5)
    print(f'{file} totalFiles: {totalFiles}')

    for f in range(totalFiles):
        g = 0
        gMin = f * maxGlyphsPerFile
        gMax = (f + 1) * maxGlyphsPerFile
        for glyph in glyphs:
            # Clear glyph out of range.
            if not(gMin <= g and g < gMax):
                glyph.clear()
            g = g + 1
        # Use this to check uncompressed font file sizes.
        #font.generate(f'{filename}-{f+1}.ttf')
        # Use this for compressed outputs.
        font.generate(f'{filename}-{f+1}.woff2')
        font.close()
        font = fontforge.open(originalFile)
        font.selection.all()
        glyphs = font.selection.byGlyphs
    font.close()
    
for file in glob.glob(fontFilesPath):
    splitFontFile(file)
    print('Done!')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文