Commit 3e34f1d
Display error message when a glyph has too many points
Changed files (7)
modifiedlibbirdfont/ExportTool.valamodifiedlibbirdfont/MainWindow.valamodifiedlibbirdfont/MenuTab.valamodifiedlibbirdfont/OpenFontFormat/GlyfData.valamodifiedlibbirdfont/OpenFontFormat/GlyfTable.valamodifiedlibbirdfont/OpenFontFormat/Os2Table.valamodifiedlibbirdfont/PreviewTools.vala
Diff
@@ -17,6 +17,8 @@ using B; namespace BirdFont { public class ExportTool : GLib.Object {++ public static string? error_message = null; public ExportTool (string n) { }@@ -404,8 +406,7 @@ os.put_string ( <p class="big"> <span class="capstosmallcaps">OTF</span> features, <span class="swashes">like swashes </span>- <span class="alternates">alternates & </span>- <span class="smallcaps">small caps</span>, can be added + <span class="alternates">alternates & </span> small caps, can be added to the font.</span> </p> </div>@@ -565,16 +566,19 @@ os.put_string ( OpenFontFormatWriter fo = new OpenFontFormatWriter (f.units_per_em); File file = (!) File.new_for_path (ttf);- File file_mac = (!) File.new_for_path (ttf_mac);+ File file_mac = (!) File.new_for_path (ttf_mac); ++ error_message = null; try { fo.open (file, file_mac); fo.write_ttf_font (f); fo.close (); } catch (Error e) {- warning (@"Can't write TTF font to $ttf");+ warning (@"Can't create TTF font to $ttf"); critical (@"$(e.message)");- }+ error_message = e.message; + } } static void write_eot (string ttf, string eot) {@@ -112,7 +112,17 @@ public class MainWindow : GLib.Object { } } - public static void show_message (string text) { + public static void show_message (string text) {+ Tab t = MainWindow.get_tab_bar ().get_selected_tab (); + string tab_name = t.get_display ().get_name ();+ + if (tab_name == "Preview") {+ print ("select over"); + MenuTab.select_overview (); + }+ + print ("show mess");+ MessageDialog md = new MessageDialog (text); show_dialog (md); } @@ -210,7 +210,12 @@ public class MenuTab : FontDisplay { public static void signal_file_exported () { IdleSource idle = new IdleSource (); idle.set_callback (() => {- export_callback.file_exported ();+ export_callback.file_exported (); + + if (ExportTool.error_message != null) {+ MainWindow.show_message (t_("Can't create TTF font.") + "\n" + (!) ExportTool.error_message);+ } + return false; }); idle.attach (null);@@ -58,53 +58,48 @@ public class GlyfData : GLib.Object { public GlyfData (Glyph g) { bool process;- PathList qp = g.get_quadratic_paths (); + PathList all_quadratic = g.get_quadratic_paths (); + PathList qp = new PathList (); glyph = g; - foreach (Path p in qp.paths) {+ int i = 0;+ foreach (Path p in all_quadratic.paths) { if (p.points.size > 0) {- if (!is_empty (p)) {+ if (likely (!is_empty (p))) { // Add points at extrema p.add_extrema ();+ qp.add (p);+ } else {+ warning (@"Path number $i is empty in $(glyph.get_name ())"); }+ i++; } } - process = true;- - while (process) {- points.clear ();- paths.clear ();- foreach (Path p in qp.paths) {- if (!is_empty (p)) {- paths.add (p);- foreach (EditPoint ep in p.points) {- points.add (ep);- }- }- }- - if (paths.size == 0) {- break;+ points.clear ();+ paths.clear ();+ foreach (Path p in qp.paths) {+ paths.add (p);++ foreach (EditPoint ep in p.points) {+ points.add (ep); }+ } + if (paths.size > 0) { process_end_points (); process_flags (); process_x ();- - // error checking is done here- process = !process_y (); - + process_y (); process_bounding_box ();- }+ } } bool is_empty (Path p) { EditPoint? last = null; if (unlikely (p.points.size < 2)) {- warning (@"A path in $(glyph.get_name ()) has less than three points, it will not be exported."); return true; } @@ -114,8 +109,7 @@ public class GlyfData : GLib.Object { } last = ep; }- - warning (@"A path in $(glyph.get_name ()) ($(glyph.get_hex ())) has no area but $(p.points.size) points at $(p.get_first_point ().x),$(p.get_first_point ().y).");+ return true; } @@ -130,6 +124,23 @@ public class GlyfData : GLib.Object { public uint16 get_nflags () { return nflags; }++ /** Count off curve points and on curve points.+ * @return the number of points or uint16.MAX if more than uint16.MAX points where found. + */ + public int get_num_points () {+ int points = 0;+ + foreach (Path quadratic in paths) {+ points += 2 * quadratic.points.size;+ + if (points >= uint16.MAX) {+ return uint16.MAX; + }+ }+ + return points;+ } void process_end_points () { uint16 last_end_point = 0;@@ -138,6 +149,7 @@ public class GlyfData : GLib.Object { end_points.clear (); end_point = 0; + int path_number = 0; foreach (Path quadratic in paths) { if (unlikely (quadratic.points.size == 0)) { warning (@"No points in path (before conversion $(quadratic.points.size))");@@ -145,12 +157,12 @@ public class GlyfData : GLib.Object { } if (unlikely (quadratic.points.size < 2)) {- warning ("A path contains less than three points, it will not be exported.");+ warning ("A path contains less than three points, it will not be exported. Path number: $path_number"); continue; } foreach (EditPoint e in quadratic.points) {- if (unlikely (nflags == uint16.MAX - 1) {+ if (unlikely (nflags == uint16.MAX - 1)) { warning (@"Too many end points in $(glyph.get_name ())"); break; }@@ -159,7 +171,7 @@ public class GlyfData : GLib.Object { type = e.get_right_handle ().type; // off curve- if (unlikely (nflags == uint16.MAX - 1) {+ if (unlikely (nflags == uint16.MAX - 1)) { warning (@"Too many end points in $(glyph.get_name ())"); break; }@@ -196,7 +208,7 @@ public class GlyfData : GLib.Object { // off curve flags.add (CoordinateFlags.NONE); - if (unlikely (nflags == uint16.MAX) {+ if (unlikely (nflags == uint16.MAX)) { warning (@"Too many flags in $(glyph.get_name ())"); return; } @@ -242,7 +254,7 @@ public class GlyfData : GLib.Object { } } - bool process_y () {+ void process_y () { double prev = 0; double y; Font font = OpenFontFormatWriter.get_current_font ();@@ -250,6 +262,8 @@ public class GlyfData : GLib.Object { int epi = 0; coordinate_y.clear ();++ int path_number = 0; foreach (Path p in paths) { foreach (EditPoint e in p.points) {@@ -257,17 +271,8 @@ public class GlyfData : GLib.Object { coordinate_y.add ((int16) y); if ((int16) y == 0 && (int16) coordinate_x.get (coordinate_y.size - 1) == 0) {- warning (@"Point on point in TTF. Index $(coordinate_y.size - 1)");- - // FIXME: distorted shape- /*- if (BirdFont.has_argument ("--test")) {- print (glyph.get_name () + "\n");- print (points.get (epi).to_string ());- PenTool.remove_point_simplify (new PointSelection (points.get (epi), p));- return false;- }- */+ warning (@"Point on point in TTF. Index $(coordinate_y.size - 1) "+ + @"Path: $path_number in $(glyph.get_name ())"); } prev = rint (e.y * UNITS - font.base_line * UNITS);@@ -277,25 +282,13 @@ public class GlyfData : GLib.Object { // off curve y = rint (e.get_right_handle ().y * UNITS - prev - font.base_line * UNITS); coordinate_y.add ((int16) y);-- if ((int16) y == 0 && (int16) coordinate_x.get (coordinate_y.size - 1) == 0) {- warning (@"Point on point in TTF (off curve) Index: $(coordinate_y.size - 1) ");- if (BirdFont.has_argument ("--test")) {- print (glyph.get_name () + "\n");- print (points.get (epi).to_string ());- - // FIXME: distorted shape- PenTool.remove_point_simplify (new PointSelection (points.get (epi), p));- return false;- }- } prev = rint (e.get_right_handle ().y * UNITS - font.base_line * UNITS); epi++; }+ + path_number++; }- - return true; } void process_bounding_box () {@@ -223,6 +223,12 @@ public class GlyfTable : OtfTable { g.remove_empty_paths (); glyf_data = g.get_ttf_data ();+ + int points = glyf_data.get_num_points ();+ if (unlikely (points >= uint16.MAX)) {+ warning (@"Too many points in glyph $(g.get_name ()) ($points)");+ throw new FileError.FAILED (t_("Too many control points") + " " + t_("in") + @" $(g.get_name ())");+ } this.glyf_data.add (glyf_data); @@ -104,7 +104,6 @@ public class Os2Table : OtfTable { fd.add_u32 (0); // ulUnicodeRange3 Bits 64-95 fd.add_u32 (0); // ulUnicodeRange4 Bits 96-127 } else {- warning(@"unicodeRange1: $unicodeRange1 unicodeRange2: $unicodeRange2 unicodeRange3 $unicodeRange3 unicodeRange4: $unicodeRange4"); fd.add_u32 (unicodeRange1); // ulUnicodeRange1 Bits 0-31 fd.add_u32 (unicodeRange2); // ulUnicodeRange2 Bits 32-63 fd.add_u32 (unicodeRange3); // ulUnicodeRange3 Bits 64-95@@ -62,7 +62,10 @@ public class PreviewTools : ToolCollection { Preview.generate_html_document (); } - MainWindow.tabs.select_tab_name ("Preview");+ if (ExportTool.error_message == null) {+ MainWindow.tabs.select_tab_name ("Preview");+ }+ return false; });