Commit d579ec0
Use webfont values for decender and ascender in TTF fonts
Changed files (5)
modifiedlibbirdfont/OpenFontFormat/DirectoryTable.valamodifiedlibbirdfont/OpenFontFormat/HeadTable.valamodifiedlibbirdfont/OpenFontFormat/HheaTable.valamodifiedlibbirdfont/OpenFontFormat/OpenFontFormatReader.valamodifiedlibbirdfont/OpenFontFormat/Os2Table.vala
Diff
@@ -55,7 +55,7 @@ public class DirectoryTable : OtfTable { gpos_table = new GposTable (); maxp_table = new MaxpTable (glyf_table); name_table = new NameTable ();- os_2_table = new Os2Table (); + os_2_table = new Os2Table (glyf_table, hmtx_table, hhea_table); post_table = new PostTable (glyf_table); id = "Directory table";@@ -75,7 +75,7 @@ public class DirectoryTable : OtfTable { hhea_table.process (); maxp_table.process (); name_table.process ();- os_2_table.process (glyf_table, hmtx_table);+ os_2_table.process (); head_table.process (); loca_table.process (glyf_table, head_table); post_table.process ();@@ -87,7 +87,7 @@ public class DirectoryTable : OtfTable { } public void process_mac () throws GLib.Error {- os_2_table.process_mac (glyf_table, hmtx_table);+ os_2_table.process_mac (); offset_table.process (); process_directory (); // this table }@@ -205,7 +205,7 @@ public class HeadTable : OtfTable { font_data.add_short (ymax); font_data.add_u16 (0); // mac style- font_data.add_u16 (8); // smallest recommended size in pixels, ppem+ font_data.add_u16 (7); // smallest recommended size in pixels, ppem font_data.add_16 (2); // deprecated direction hint font_data.add_16 (loca_offset_size); // long offset font_data.add_16 (0); // Use current glyph data format@@ -1,5 +1,5 @@ /*- Copyright (C) 2012, 2013 Johan Mattsson+ Copyright (C) 2012 2013 2015 Johan Mattsson This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -18,10 +18,12 @@ namespace BirdFont { public class HheaTable : OtfTable { + public int16 ascender;+ public int16 descender;+ public int16 line_gap;+ Fixed version;- int16 ascender;- int16 descender;- int16 linegap;+ uint16 max_advance; int16 min_lsb; int16 min_rsb;@@ -37,19 +39,42 @@ public class HheaTable : OtfTable { HeadTable head_table; HmtxTable hmtx_table; + int16 winAscent = 0;+ int16 winDescent = 0;+ public HheaTable (GlyfTable g, HeadTable h, HmtxTable hm) { glyf_table = g; head_table = h; hmtx_table = hm; id = "hhea"; }- - public double get_ascender () {- return ascender * 1000 / head_table.get_units_per_em ();++ public int16 get_winascent () {+ if (winAscent != 0) {+ return winAscent;+ }+ + foreach (GlyfData glyph in glyf_table.glyf_data) {+ if (glyph.bounding_box_ymax > winAscent) {+ winAscent = glyph.bounding_box_ymax;+ }+ }+ + return winAscent; }+ + public int16 get_windescent () {+ if (winDescent != 0) {+ return winDescent;+ } - public double get_descender () {- return descender * 1000 / head_table.get_units_per_em ();+ foreach (GlyfData glyph in glyf_table.glyf_data) {+ if (glyph.bounding_box_ymin < winDescent) {+ winDescent = glyph.bounding_box_ymin;+ }+ }+ + return winDescent; } public override void parse (FontData dis) throws GLib.Error {@@ -63,7 +88,7 @@ public class HheaTable : OtfTable { ascender = dis.read_short (); descender = dis.read_short ();- linegap = dis.read_short ();+ line_gap = dis.read_short (); max_advance = dis.read_ushort (); min_lsb = dis.read_short (); min_rsb = dis.read_short ();@@ -83,10 +108,10 @@ public class HheaTable : OtfTable { } public void process () throws GLib.Error {- int16 ascender, descender; FontData fd = new FontData (); Fixed version = 1 << 16; Font font = OpenFontFormatWriter.get_current_font ();+ int upm, total_height; fd.add_fixed (version); // table version @@ -95,10 +120,16 @@ public class HheaTable : OtfTable { descender = (int16) rint (font.bottom_limit * HeadTable.UNITS); descender -= (int16) rint (font.base_line * HeadTable.UNITS);- ++ upm = HeadTable.units_per_em;+ total_height = get_winascent () - get_windescent ();+ ascender = (int16) rint (upm * get_winascent () / (double) total_height);+ descender = (int16) (ascender - upm);+ line_gap = (int16) rint (total_height - upm);+ fd.add_16 (ascender); // Ascender fd.add_16 (descender); // Descender- fd.add_16 (0); // LineGap+ fd.add_16 (line_gap); // LineGap fd.add_u16 (hmtx_table.max_advance); // maximum advance width value in 'hmtx' table. @@ -118,7 +149,8 @@ public class HheaTable : OtfTable { fd.add_16 (0); // metricDataFormat 0 for current format. - fd.add_u16 ((uint16) glyf_table.glyphs.size); // numberOfHMetrics Number of hMetric entries in 'hmtx' table+ // numberOfHMetrics Number of hMetric entries in 'hmtx' table+ fd.add_u16 ((uint16) glyf_table.glyphs.size); // padding fd.pad ();@@ -132,19 +132,7 @@ public class OpenFontFormatReader : Object { f = BirdFont.get_current_font (); } - f.top_position = get_ascender ();- f.top_limit = f.top_position + 5;- f.xheight_position = f.top_position + 5;- f.bottom_position = get_descender ();- f.bottom_limit = f.bottom_position - 5; - }-- public double get_ascender () {- return directory_table.hhea_table.get_ascender ();- }- - public double get_descender () {- return directory_table.hhea_table.get_descender ();+ // FIXME: set guides to ascent & descent } public uint32 get_head_checksum () {@@ -1,5 +1,5 @@ /*- Copyright (C) 2012, 2013, 2014 Johan Mattsson+ Copyright (C) 2012 2013 2014 2015 Johan Mattsson This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -29,31 +29,37 @@ public class Os2Table : OtfTable { public static const uint16 WWS = 1 << 8; public static const uint16 OBLIQUE = 1 << 9; - public Os2Table () {+ GlyfTable glyf_table;+ HmtxTable hmtx_table;+ HheaTable hhea_table;+ + public Os2Table (GlyfTable gt, HmtxTable hmtx, HheaTable hhea) { id = "OS/2";+ glyf_table = gt;+ hmtx_table = hmtx;+ hhea_table = hhea; }- + public override void parse (FontData dis) throws Error { } - public void process (GlyfTable glyf_table, HmtxTable hmtx_table) {- process_table (glyf_table, hmtx_table, false);+ public void process () {+ process_table (false); } - public void process_mac (GlyfTable glyf_table, HmtxTable hmtx_table) {- process_table (glyf_table, hmtx_table, true);+ public void process_mac () {+ process_table (true); } - public void process_table (GlyfTable glyf_table, HmtxTable hmtx_table, bool mac) {+ public void process_table (bool mac) { FontData fd = new FontData (); Font font = OpenFontFormatWriter.get_current_font ();- int16 ascender;- int16 descender; uint16 style = 0; UnicodeRangeBits ranges = new UnicodeRangeBits (); CodePageBits pages = new CodePageBits (); uint32 unicodeRange1, unicodeRange2, unicodeRange3, unicodeRange4; uint32 codepage1, codepage2;+ int16 win_descent; fd.add_u16 (0x0004); // version @@ -125,20 +131,20 @@ public class Os2Table : OtfTable { fd.add_u16 (glyf_table.get_first_char ()); // usFirstCharIndex fd.add_u16 (glyf_table.get_last_char ()); // usLastCharIndex - ascender = (int16) rint (font.top_limit * HeadTable.UNITS);- descender = (int16) rint (font.bottom_limit * HeadTable.UNITS);- - fd.add_16 (ascender); // sTypoAscender- fd.add_16 (descender); // sTypoDescender- fd.add_16 (100); // sTypoLineGap+ fd.add_16 (hhea_table.ascender); // sTypoAscender+ fd.add_16 (hhea_table.descender); // sTypoDescender+ fd.add_16 (hhea_table.line_gap); // sTypoLineGap - fd.add_u16 (ascender); // usWinAscent+ // usWinAscent+ fd.add_u16 (hhea_table.get_winascent ()); - if (descender > 0) {- warning (@"usWinDescent is unsigned, can not write $(-descender) to the field.");+ // usWinDescent (not like sTypoDescender)+ win_descent = hhea_table.get_windescent ();+ if (win_descent < 0) {+ warning (@"usWinDescent is unsigned, can not write $(-win_descent) to the field."); fd.add_u16 (0); } else {- fd.add_u16 (-descender); // usWinDescent (not like sTypoDescender)+ fd.add_u16 (-win_descent); } pages.get_pages (font, out codepage1, out codepage2);@@ -150,14 +156,19 @@ public class Os2Table : OtfTable { fd.add_u32 (codepage1); // ulCodePageRange1 Bits 0-31 (this value is used by Word on Windows) fd.add_u32 (codepage2); // ulCodePageRange2 Bits 32-63 }++ int16 xheight = (int16) rint ((font.xheight_position - font.base_line) * HeadTable.UNITS);+ int16 cap_height = (int16) rint ((font.top_limit - font.base_line) * HeadTable.UNITS); - fd.add_16 (ascender); // sHeight- fd.add_16 (ascender); // sCapHeight+ fd.add_16 (xheight); // sxHeight+ fd.add_16 (cap_height); // sCapHeight fd.add_16 (0); // usDefaultChar fd.add_16 (0x0020); // usBreakChar, also known as space - fd.add_16 (2); // usMaxContext (two, becase the font has kerning but not ligatures).+ // usMaxContext+ int16 num_glyphs = 2; // FIXME: add one for each parts of ligatures+ fd.add_16 (num_glyphs); // padding fd.pad ();