| 1 |
from fontTools.ttLib import newTable, TTFont |
| 2 |
from gftools.fix import * |
| 3 |
from glob import glob |
| 4 |
import pytest |
| 5 |
import os |
| 6 |
from copy import deepcopy |
| 7 |
|
| 8 |
|
| 9 |
TEST_DATA = os.path.join("data", "test") |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture |
| 13 |
def static_font(): |
| 14 |
return TTFont(os.path.join(TEST_DATA, "Lora-Regular.ttf")) |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture |
| 18 |
def var_font(): |
| 19 |
return TTFont(os.path.join(TEST_DATA, "Inconsolata[wdth,wght].ttf")) |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def var_fonts(): |
| 24 |
paths = [ |
| 25 |
os.path.join(TEST_DATA, "Raleway[wght].ttf"), |
| 26 |
os.path.join(TEST_DATA, "Raleway-Italic[wght].ttf"), |
| 27 |
] |
| 28 |
return [TTFont(p) for p in paths] |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def static_fonts(): |
| 33 |
return [TTFont(f) for f in glob(os.path.join("data", "test", "mavenpro", "*.ttf"))] |
| 34 |
|
| 35 |
|
| 36 |
def test_remove_tables(static_font): |
| 37 |
|
| 38 |
tsi1_tbl = newTable("TSI1") |
| 39 |
static_font["TSI1"] = tsi1_tbl |
| 40 |
assert "TSI1" in static_font |
| 41 |
|
| 42 |
tsi2_tbl = newTable("TSI2") |
| 43 |
static_font["TSI2"] = tsi2_tbl |
| 44 |
remove_tables(static_font, ["TSI1", "TSI2"]) |
| 45 |
assert "TSI1" not in static_font |
| 46 |
assert "TSI2" not in static_font |
| 47 |
|
| 48 |
|
| 49 |
remove_tables(static_font, ["glyf"]) |
| 50 |
assert "glyf" in static_font |
| 51 |
|
| 52 |
|
| 53 |
def test_add_dummy_dsig(static_font): |
| 54 |
assert "DSIG" not in static_font |
| 55 |
add_dummy_dsig(static_font) |
| 56 |
assert "DSIG" in static_font |
| 57 |
|
| 58 |
|
| 59 |
def test_fix_hinted_font(static_font): |
| 60 |
static_font["head"].flags &= ~(1 << 3) |
| 61 |
assert static_font["head"].flags & (1 << 3) != (1 << 3) |
| 62 |
static_font["fpgm"] = newTable("fpgm") |
| 63 |
fix_hinted_font(static_font) |
| 64 |
assert static_font["head"].flags & (1 << 3) == (1 << 3) |
| 65 |
|
| 66 |
|
| 67 |
def test_fix_unhinted_font(static_font): |
| 68 |
for tbl in ("prep", "gasp"): |
| 69 |
if tbl in static_font: |
| 70 |
del static_font[tbl] |
| 71 |
|
| 72 |
fix_unhinted_font(static_font) |
| 73 |
assert static_font["gasp"].gaspRange == {65535: 15} |
| 74 |
assert "prep" in static_font |
| 75 |
|
| 76 |
|
| 77 |
def test_fix_fs_type(static_font): |
| 78 |
static_font["OS/2"].fsType = 1 |
| 79 |
assert static_font["OS/2"].fsType == 1 |
| 80 |
fix_fs_type(static_font) |
| 81 |
assert static_font["OS/2"].fsType == 0 |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
STYLE_HEADERS = "style, weight_class, fs_selection, mac_style" |
| 86 |
STYLE_TABLE = [ |
| 87 |
("Hairline", 1, (1 << 6), (0 << 0)), |
| 88 |
("Thin", 100, (1 << 6), (0 << 0)), |
| 89 |
("ExtraLight", 200, (1 << 6), (0 << 0)), |
| 90 |
("Light", 300, (1 << 6), (0 << 0)), |
| 91 |
("Regular", 400, (1 << 6), (0 << 0)), |
| 92 |
("Medium", 500, (1 << 6), (0 << 0)), |
| 93 |
("SemiBold", 600, (1 << 6), (0 << 0)), |
| 94 |
("Bold", 700, (1 << 5), (1 << 0)), |
| 95 |
("ExtraBold", 800, (1 << 6), (0 << 0)), |
| 96 |
("Black", 900, (1 << 6), (0 << 0)), |
| 97 |
("ExtraBlack", 1000, (1 << 6), (0 << 0)), |
| 98 |
("Hairline Italic", 1, (1 << 0), (1 << 1)), |
| 99 |
("Thin Italic", 100, (1 << 0), (1 << 1)), |
| 100 |
("ExtraLight Italic", 200, (1 << 0), (1 << 1)), |
| 101 |
("Light Italic", 300, (1 << 0), (1 << 1)), |
| 102 |
("Italic", 400, (1 << 0), (1 << 1)), |
| 103 |
("Medium Italic", 500, (1 << 0), (1 << 1)), |
| 104 |
("SemiBold Italic", 600, (1 << 0), (1 << 1)), |
| 105 |
("Bold Italic", 700, (1 << 0) | (1 << 5), (1 << 0) | (1 << 1)), |
| 106 |
("ExtraBold Italic", 800, (1 << 0), (1 << 1)), |
| 107 |
("Black Italic", 900, (1 << 0), (1 << 1)), |
| 108 |
("ExtraBlack Italic", 1000, (1 << 0), (1 << 1)), |
| 109 |
|
| 110 |
("SemiCondensed Bold Italic", 700, (1 << 0) | (1 << 5), (1 << 0) | (1 << 1)), |
| 111 |
("12pt Italic", 400, (1 << 0), (1 << 1)), |
| 112 |
] |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize(STYLE_HEADERS, STYLE_TABLE) |
| 116 |
def test_fix_weight_class(static_font, style, weight_class, fs_selection, mac_style): |
| 117 |
name = static_font["name"] |
| 118 |
name.setName(style, 2, 3, 1, 0x409) |
| 119 |
name.setName(style, 17, 3, 1, 0x409) |
| 120 |
fix_weight_class(static_font) |
| 121 |
assert static_font["OS/2"].usWeightClass == weight_class |
| 122 |
|
| 123 |
|
| 124 |
def test_unknown_weight_class(static_font): |
| 125 |
name = static_font["name"] |
| 126 |
name.setName("Foobar", 2, 3, 1, 0x409) |
| 127 |
name.setName("Foobar", 17, 3, 1, 0x409) |
| 128 |
from gftools.fix import WEIGHT_NAMES |
| 129 |
|
| 130 |
with pytest.raises(ValueError, match="Cannot determine usWeightClass"): |
| 131 |
fix_weight_class(static_font) |
| 132 |
|
| 133 |
|
| 134 |
@pytest.mark.parametrize(STYLE_HEADERS, STYLE_TABLE) |
| 135 |
def test_fs_selection(static_font, style, weight_class, fs_selection, mac_style): |
| 136 |
|
| 137 |
for i in range(7, 12): |
| 138 |
static_font["OS/2"].fsSelection &= ~(1 << i) |
| 139 |
name = static_font["name"] |
| 140 |
name.setName(style, 2, 3, 1, 0x409) |
| 141 |
name.setName(style, 17, 3, 1, 0x409) |
| 142 |
fix_fs_selection(static_font) |
| 143 |
assert static_font["OS/2"].fsSelection == fs_selection |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.parametrize(STYLE_HEADERS, STYLE_TABLE) |
| 147 |
def test_fix_mac_style(static_font, style, weight_class, fs_selection, mac_style): |
| 148 |
name = static_font["name"] |
| 149 |
name.setName(style, 2, 3, 1, 0x409) |
| 150 |
name.setName(style, 17, 3, 1, 0x409) |
| 151 |
fix_mac_style(static_font) |
| 152 |
assert static_font["head"].macStyle == mac_style |
| 153 |
|
| 154 |
|
| 155 |
def _check_vertical_metrics(fonts): |
| 156 |
ref_font = fonts[0] |
| 157 |
y_min = min(f["head"].yMin for f in fonts) |
| 158 |
y_max = max(f["head"].yMax for f in fonts) |
| 159 |
for font in fonts: |
| 160 |
|
| 161 |
assert font["OS/2"].fsSelection & (1 << 7) > 0 |
| 162 |
|
| 163 |
|
| 164 |
assert font["OS/2"].usWinAscent == ref_font["OS/2"].usWinAscent |
| 165 |
assert font["OS/2"].usWinDescent == ref_font["OS/2"].usWinDescent |
| 166 |
assert font["OS/2"].sTypoAscender == ref_font["OS/2"].sTypoAscender |
| 167 |
assert font["OS/2"].sTypoDescender == ref_font["OS/2"].sTypoDescender |
| 168 |
assert font["OS/2"].sTypoLineGap == ref_font["OS/2"].sTypoLineGap |
| 169 |
assert font["hhea"].ascent == ref_font["hhea"].ascent |
| 170 |
assert font["hhea"].descent == ref_font["hhea"].descent |
| 171 |
assert font["hhea"].lineGap == ref_font["hhea"].lineGap |
| 172 |
|
| 173 |
|
| 174 |
assert font["OS/2"].sTypoAscender == font["hhea"].ascent |
| 175 |
assert font["OS/2"].sTypoDescender == ref_font["hhea"].descent |
| 176 |
assert font["OS/2"].sTypoLineGap == ref_font["hhea"].lineGap |
| 177 |
|
| 178 |
|
| 179 |
assert font["OS/2"].usWinAscent == y_max |
| 180 |
assert font["OS/2"].usWinDescent == abs(y_min) |
| 181 |
|
| 182 |
|
| 183 |
def test_fix_vertical_metrics_family_consistency(static_fonts): |
| 184 |
_check_vertical_metrics(static_fonts) |
| 185 |
static_fonts[0]["OS/2"].sTypoLineGap = 1000 |
| 186 |
static_fonts[0]["OS/2"].usWinAscent = 4000 |
| 187 |
|
| 188 |
fix_vertical_metrics(static_fonts) |
| 189 |
_check_vertical_metrics(static_fonts) |
| 190 |
|
| 191 |
|
| 192 |
def test_fix_vertical_metrics_win_values(static_fonts): |
| 193 |
_check_vertical_metrics(static_fonts) |
| 194 |
for font in static_fonts: |
| 195 |
font["OS/2"].usWinAscent = font["OS/2"].usWinDescent = 0 |
| 196 |
assert font["OS/2"].usWinAscent == 0 and font["OS/2"].usWinDescent == 0 |
| 197 |
|
| 198 |
fix_vertical_metrics(static_fonts) |
| 199 |
_check_vertical_metrics(static_fonts) |
| 200 |
|
| 201 |
|
| 202 |
def test_fix_vertical_metrics_typo_and_hhea_match(static_fonts): |
| 203 |
_check_vertical_metrics(static_fonts) |
| 204 |
for font in static_fonts: |
| 205 |
font["hhea"].ascent = 5000 |
| 206 |
font["OS/2"].sTypoAscender == 1000 |
| 207 |
assert font["hhea"].ascent != font["OS/2"].sTypoAscender |
| 208 |
|
| 209 |
fix_vertical_metrics(static_fonts) |
| 210 |
_check_vertical_metrics(static_fonts) |
| 211 |
|
| 212 |
|
| 213 |
def test_fix_vertical_metrics_typo_metrics_enabled(static_fonts): |
| 214 |
_check_vertical_metrics(static_fonts) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
for font in static_fonts: |
| 219 |
font["OS/2"].fsSelection &= ~(1 << 7) |
| 220 |
font["OS/2"].usWinAscent = 500 |
| 221 |
font["OS/2"].usWinDescent = 300 |
| 222 |
|
| 223 |
fix_vertical_metrics(static_fonts) |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
for font in static_fonts: |
| 228 |
assert font["OS/2"].sTypoAscender == 500 |
| 229 |
assert font["OS/2"].sTypoDescender == -300 |
| 230 |
assert font["OS/2"].sTypoLineGap == 0 |
| 231 |
_check_vertical_metrics(static_fonts) |
| 232 |
|
| 233 |
|
| 234 |
@pytest.mark.parametrize( |
| 235 |
"font_path", |
| 236 |
[ |
| 237 |
(os.path.join(TEST_DATA, "CairoPlay[slnt,wght]-no-empty-glyphs.ttf")), |
| 238 |
(os.path.join(TEST_DATA, "CairoPlay[slnt,wght]-gid1-not-empty.ttf")), |
| 239 |
], |
| 240 |
) |
| 241 |
def test_fix_colr_v0_font(font_path): |
| 242 |
|
| 243 |
|
| 244 |
from gftools.fix import fix_colr_font |
| 245 |
|
| 246 |
font = TTFont(font_path) |
| 247 |
|
| 248 |
gid1 = font.getGlyphOrder()[1] |
| 249 |
assert font["glyf"][gid1].numberOfContours != 0 |
| 250 |
|
| 251 |
fix_colr_font(font) |
| 252 |
gid1 = font.getGlyphOrder()[1] |
| 253 |
assert font["glyf"][gid1].numberOfContours == 0 |
| 254 |
assert "SVG " not in font |
| 255 |
|
| 256 |
|
| 257 |
@pytest.fixture |
| 258 |
def colr_v1_font(): |
| 259 |
return TTFont(os.path.join(TEST_DATA, "Nabla[EDPT,EHLT].subset.ttf")) |
| 260 |
|
| 261 |
|
| 262 |
def test_fix_colr_v1_font(colr_v1_font): |
| 263 |
|
| 264 |
from gftools.fix import fix_colr_font |
| 265 |
|
| 266 |
assert "SVG " not in colr_v1_font |
| 267 |
colr_v1_font["COLR"].version = 1 |
| 268 |
fix_colr_font(colr_v1_font) |
| 269 |
assert "SVG " in colr_v1_font |
| 270 |
|
| 271 |
|
| 272 |
def test_ofl_license_strings(static_font): |
| 273 |
from gftools.fix import fix_license_strings |
| 274 |
from gftools.constants import OFL_LICENSE_INFO, OFL_LICENSE_URL |
| 275 |
|
| 276 |
for id in (13, 14): |
| 277 |
assert ( |
| 278 |
"http://scripts.sil.org/OFL" |
| 279 |
in static_font["name"].getName(id, 3, 1, 0x409).toUnicode() |
| 280 |
) |
| 281 |
fix_license_strings(static_font) |
| 282 |
for id, expected in ((13, OFL_LICENSE_INFO), (14, OFL_LICENSE_URL)): |
| 283 |
assert expected == static_font["name"].getName(id, 3, 1, 0x409).toUnicode() |