|
4 | 4 | import os.path
|
5 | 5 |
|
6 | 6 | from fontTools import ttLib
|
7 |
| -from fontline.utilities import get_sha1 |
8 |
| - |
9 | 7 | from standardstreams import stderr
|
10 | 8 |
|
11 | 9 |
|
| 10 | +from fontline.metrics import MetricsObject |
| 11 | + |
| 12 | + |
12 | 13 | def get_font_report(fontpath):
|
13 | 14 | tt = ttLib.TTFont(fontpath)
|
| 15 | + metrics = MetricsObject(tt, fontpath) |
14 | 16 |
|
15 |
| - # Vertical metrics values as integers |
16 |
| - os2_typo_ascender = tt["OS/2"].sTypoAscender |
17 |
| - os2_typo_descender = tt["OS/2"].sTypoDescender |
18 |
| - os2_win_ascent = tt["OS/2"].usWinAscent |
19 |
| - os2_win_descent = tt["OS/2"].usWinDescent |
20 |
| - os2_typo_linegap = tt["OS/2"].sTypoLineGap |
21 |
| - try: |
22 |
| - os2_x_height = tt["OS/2"].sxHeight |
23 |
| - except AttributeError: |
24 |
| - os2_x_height = "---- (OS/2 table does not contain sxHeight record)" |
25 |
| - try: |
26 |
| - os2_cap_height = tt["OS/2"].sCapHeight |
27 |
| - except AttributeError: |
28 |
| - os2_cap_height = "---- (OS/2 table does not contain sCapHeight record)" |
29 |
| - hhea_ascent = tt["hhea"].ascent |
30 |
| - hhea_descent = tt["hhea"].descent |
31 |
| - hhea_linegap = tt["hhea"].lineGap |
32 |
| - ymax = tt["head"].yMax |
33 |
| - ymin = tt["head"].yMin |
34 |
| - units_per_em = tt["head"].unitsPerEm |
35 |
| - |
36 |
| - # Bit flag checks |
37 |
| - fsselection_bit7_mask = 1 << 7 |
38 |
| - fsselection_bit7_set = (tt["OS/2"].fsSelection & fsselection_bit7_mask) != 0 |
39 |
| - |
40 |
| - # Calculated values |
41 |
| - os2_typo_total_height = os2_typo_ascender + -(os2_typo_descender) |
42 |
| - os2_win_total_height = os2_win_ascent + os2_win_descent |
43 |
| - hhea_total_height = hhea_ascent + -(hhea_descent) |
44 |
| - |
45 |
| - hhea_btb_distance = hhea_total_height + hhea_linegap |
46 |
| - typo_btb_distance = os2_typo_total_height + os2_typo_linegap |
47 |
| - win_external_leading = hhea_linegap - ( |
48 |
| - (os2_win_ascent + os2_win_descent) - (hhea_ascent - hhea_descent) |
49 |
| - ) |
50 |
| - if win_external_leading < 0: |
51 |
| - win_external_leading = 0 |
52 |
| - win_btb_distance = os2_win_ascent + os2_win_descent + win_external_leading |
| 17 | + # The file path header |
| 18 | + report = ["=== " + fontpath + " ==="] |
53 | 19 |
|
54 |
| - typo_to_upm = 1.0 * typo_btb_distance / units_per_em |
55 |
| - winascdesc_to_upm = 1.0 * win_btb_distance / units_per_em |
56 |
| - hheaascdesc_to_upm = 1.0 * hhea_btb_distance / units_per_em |
| 20 | + report.append(metrics.version) |
57 | 21 |
|
58 |
| - # The file path header |
59 |
| - report = [" "] |
60 |
| - report.append("=== " + fontpath + " ===") |
61 |
| - namerecord_list = tt["name"].names |
62 |
| - # The version string |
63 |
| - for needle in namerecord_list: |
64 |
| - if needle.nameID == 5: |
65 |
| - report.append(needle.toStr()) |
66 |
| - break |
67 | 22 | # The SHA1 string
|
68 |
| - report.append("SHA1: " + get_sha1(fontpath)) |
| 23 | + report.append("SHA1: " + metrics.sha1) |
69 | 24 | report.append("")
|
70 | 25 | # The vertical metrics strings
|
71 | 26 | report.append("--- Metrics ---")
|
72 |
| - report.append("[head] Units per Em: {}".format(units_per_em)) |
73 |
| - report.append("[head] yMax: {}".format(ymax)) |
74 |
| - report.append("[head] yMin: {}".format(ymin)) |
75 |
| - report.append("[OS/2] CapHeight: {}".format(os2_cap_height)) |
76 |
| - report.append("[OS/2] xHeight: {}".format(os2_x_height)) |
77 |
| - report.append("[OS/2] TypoAscender: {}".format(os2_typo_ascender)) |
78 |
| - report.append("[OS/2] TypoDescender: {}".format(os2_typo_descender)) |
79 |
| - report.append("[OS/2] WinAscent: {}".format(os2_win_ascent)) |
80 |
| - report.append("[OS/2] WinDescent: {}".format(os2_win_descent)) |
81 |
| - report.append("[hhea] Ascent: {}".format(hhea_ascent)) |
82 |
| - report.append("[hhea] Descent: {}".format(hhea_descent)) |
| 27 | + report.append("[head] Units per Em: {}".format(metrics.units_per_em)) |
| 28 | + report.append("[head] yMax: {}".format(metrics.ymax)) |
| 29 | + report.append("[head] yMin: {}".format(metrics.ymin)) |
| 30 | + report.append("[OS/2] CapHeight: {}".format(metrics.os2_cap_height)) |
| 31 | + report.append("[OS/2] xHeight: {}".format(metrics.os2_x_height)) |
| 32 | + report.append("[OS/2] TypoAscender: {}".format(metrics.os2_typo_ascender)) |
| 33 | + report.append("[OS/2] TypoDescender: {}".format(metrics.os2_typo_descender)) |
| 34 | + report.append("[OS/2] WinAscent: {}".format(metrics.os2_win_ascent)) |
| 35 | + report.append("[OS/2] WinDescent: {}".format(metrics.os2_win_descent)) |
| 36 | + report.append("[hhea] Ascent: {}".format(metrics.hhea_ascent)) |
| 37 | + report.append("[hhea] Descent: {}".format(metrics.hhea_descent)) |
83 | 38 | report.append("")
|
84 |
| - report.append("[hhea] LineGap: {}".format(hhea_linegap)) |
85 |
| - report.append("[OS/2] TypoLineGap: {}".format(os2_typo_linegap)) |
| 39 | + report.append("[hhea] LineGap: {}".format(metrics.hhea_linegap)) |
| 40 | + report.append("[OS/2] TypoLineGap: {}".format(metrics.os2_typo_linegap)) |
86 | 41 | report.append("")
|
87 | 42 |
|
88 | 43 | report.append("--- Ascent to Descent Calculations ---")
|
89 |
| - report.append("[hhea] Ascent to Descent: {}".format(hhea_total_height)) |
| 44 | + report.append("[hhea] Ascent to Descent: {}".format(metrics.hhea_total_height)) |
90 | 45 | report.append(
|
91 |
| - "[OS/2] TypoAscender to TypoDescender: {}".format(os2_typo_total_height) |
| 46 | + "[OS/2] TypoAscender to TypoDescender: {}".format(metrics.os2_typo_total_height) |
92 | 47 | )
|
93 | 48 | report.append(
|
94 |
| - "[OS/2] WinAscent to WinDescent: {}".format(os2_win_total_height) |
| 49 | + "[OS/2] WinAscent to WinDescent: {}".format(metrics.os2_win_total_height) |
95 | 50 | )
|
96 | 51 | report.append("")
|
97 | 52 |
|
98 | 53 | report.append("--- Delta Values ---")
|
99 | 54 | report.append(
|
100 | 55 | "[hhea] Ascent to [OS/2] TypoAscender: {}".format(
|
101 |
| - hhea_ascent - os2_typo_ascender |
| 56 | + metrics.hhea_ascent - metrics.os2_typo_ascender |
102 | 57 | )
|
103 | 58 | )
|
104 | 59 | report.append(
|
105 | 60 | "[hhea] Descent to [OS/2] TypoDescender: {}".format(
|
106 |
| - os2_typo_descender - hhea_descent |
| 61 | + metrics.os2_typo_descender - metrics.hhea_descent |
107 | 62 | )
|
108 | 63 | )
|
109 | 64 | report.append(
|
110 | 65 | "[OS/2] WinAscent to [OS/2] TypoAscender: {}".format(
|
111 |
| - os2_win_ascent - os2_typo_ascender |
| 66 | + metrics.os2_win_ascent - metrics.os2_typo_ascender |
112 | 67 | )
|
113 | 68 | )
|
114 | 69 | report.append(
|
115 | 70 | "[OS/2] WinDescent to [OS/2] TypoDescender: {}".format(
|
116 |
| - os2_win_descent - -(os2_typo_descender) |
| 71 | + metrics.os2_win_descent + metrics.os2_typo_descender |
117 | 72 | )
|
118 | 73 | )
|
119 | 74 | report.append("")
|
120 | 75 | report.append("--- Baseline to Baseline Distances ---")
|
121 |
| - report.append("hhea metrics: {}".format(hhea_btb_distance)) |
122 |
| - report.append("typo metrics: {}".format(typo_btb_distance)) |
123 |
| - report.append("win metrics: {}".format(win_btb_distance)) |
| 76 | + report.append("hhea metrics: {}".format(metrics.hhea_btb_distance)) |
| 77 | + report.append("typo metrics: {}".format(metrics.typo_btb_distance)) |
| 78 | + report.append("win metrics: {}".format(metrics.win_btb_distance)) |
124 | 79 | report.append("")
|
125 | 80 | report.append(
|
126 |
| - "[OS/2] fsSelection USE_TYPO_METRICS bit set: {}".format(fsselection_bit7_set) |
| 81 | + "[OS/2] fsSelection USE_TYPO_METRICS bit set: {}".format(metrics.fsselection_bit7_set) |
127 | 82 | )
|
128 | 83 | report.append("")
|
129 | 84 | report.append("--- Ratios ---")
|
130 |
| - report.append("hhea metrics / UPM: {0:.3g}".format(hheaascdesc_to_upm)) |
131 |
| - report.append("typo metrics / UPM: {0:.3g}".format(typo_to_upm)) |
132 |
| - report.append("win metrics / UPM: {0:.3g}".format(winascdesc_to_upm)) |
| 85 | + report.append("hhea metrics / UPM: {0:.3g}".format(metrics.hheaascdesc_to_upm)) |
| 86 | + report.append("typo metrics / UPM: {0:.3g}".format(metrics.typo_to_upm)) |
| 87 | + report.append("win metrics / UPM: {0:.3g}".format(metrics.winascdesc_to_upm)) |
133 | 88 |
|
134 | 89 | return "\n".join(report)
|
135 | 90 |
|
|
0 commit comments