@@ -75,12 +75,49 @@ function write($file, $encoding = null) {
7575
7676 if (isset ($ hhea ["ascent " ])) {
7777 $ this ->addPair ("FontHeightOffset " , $ font ->normalizeFUnit ($ hhea ["lineGap " ]));
78- $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ hhea ["ascent " ]));
79- $ this ->addPair ("Descender " , $ font ->normalizeFUnit ($ hhea ["descent " ]));
8078 }
8179 else {
8280 $ this ->addPair ("FontHeightOffset " , $ font ->normalizeFUnit ($ os2 ["typoLineGap " ]));
81+ }
82+
83+ $ glyf = $ font ->getData ("glyf " );
84+ $ glyphIndexArray = $ font ->getUnicodeCharMap ();
85+
86+ // capHeight is based on capital H
87+ if (\array_key_exists (72 , $ glyphIndexArray )) {
88+ $ upperH = $ glyf [$ glyphIndexArray [72 ]];
89+ $ upperH ->parseData ();
90+ $ this ->addPair ("CapHeight " , $ font ->normalizeFUnit ($ upperH ->yMax ));
91+ }
92+
93+ // xHeight is based on lowercase x
94+ if (\array_key_exists (120 , $ glyphIndexArray )) {
95+ $ lowerX = $ glyf [$ glyphIndexArray [120 ]];
96+ $ lowerX ->parseData ();
97+ $ this ->addPair ("XHeight " , $ font ->normalizeFUnit ($ lowerX ->yMax ));
98+ }
99+
100+ // ascender is based on lowercase d
101+ if (\array_key_exists (100 , $ glyphIndexArray )) {
102+ $ lowerD = $ glyf [$ glyphIndexArray [100 ]];
103+ $ lowerD ->parseData ();
104+ $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ lowerD ->yMax ));
105+ } elseif (isset ($ hhea ["ascent " ])) {
106+ $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ hhea ["ascent " ]));
107+ }
108+ else {
83109 $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ os2 ["typoAscender " ]));
110+ }
111+
112+ // descender is based on lowercase p
113+ if (\array_key_exists (112 , $ glyphIndexArray )) {
114+ $ lowerP = $ glyf [$ glyphIndexArray [112 ]];
115+ $ lowerP ->parseData ();
116+ $ this ->addPair ("Descender " , $ font ->normalizeFUnit ($ lowerP ->yMin ));
117+ } elseif (isset ($ hhea ["ascent " ])) {
118+ $ this ->addPair ("Descender " , $ font ->normalizeFUnit ($ hhea ["descent " ]));
119+ }
120+ else {
84121 $ this ->addPair ("Descender " , -abs ($ font ->normalizeFUnit ($ os2 ["typoDescender " ])));
85122 }
86123
@@ -92,8 +129,6 @@ function write($file, $encoding = null) {
92129 $ font ->normalizeFUnit ($ head ["yMax " ]),
93130 ));
94131
95- $ glyphIndexArray = $ font ->getUnicodeCharMap ();
96-
97132 if ($ glyphIndexArray ) {
98133 $ hmtx = $ font ->getData ("hmtx " );
99134 $ names = $ font ->getData ("post " , "names " );
0 commit comments