The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Add abstract table class

These changes was commited to the Birdfont repository Fri, 03 Oct 2014 18:34:56 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Fri, 03 Oct 2014 18:34:56 +0000 (20:34 +0200)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Fri, 03 Oct 2014 18:34:56 +0000 (20:34 +0200)
commit 7e367f830822a94d609a44bf2d43e0b8caa5d75d
tree 35853de5b0e1d117eedcdde4420dd77d4bfcaa58
parent 5266176197d77d85a4f80af0816ab0fe92e621d2
Add abstract table class

32 files changed:
libbirdfont/Glyph.vala
libbirdfont/GlyphSequence.vala
libbirdfont/GridTool.vala
libbirdfont/KerningList.vala
libbirdfont/LigatureList.vala
libbirdfont/Ligatures.vala
libbirdfont/OpenFontFormat/CffTable.vala
libbirdfont/OpenFontFormat/Cmap.vala
libbirdfont/OpenFontFormat/CvtTable.vala
libbirdfont/OpenFontFormat/DirectoryTable.vala
libbirdfont/OpenFontFormat/FontData.vala
libbirdfont/OpenFontFormat/GaspTable.vala
libbirdfont/OpenFontFormat/GdefTable.vala
libbirdfont/OpenFontFormat/GlyfTable.vala
libbirdfont/OpenFontFormat/GposTable.vala
libbirdfont/OpenFontFormat/GsubTable.vala
libbirdfont/OpenFontFormat/HeadTable.vala
libbirdfont/OpenFontFormat/HheaTable.vala
libbirdfont/OpenFontFormat/HmtxTable.vala
libbirdfont/OpenFontFormat/KernTable.vala
libbirdfont/OpenFontFormat/LigatureSet.vala
libbirdfont/OpenFontFormat/LocaTable.vala
libbirdfont/OpenFontFormat/MaxpTable.vala
libbirdfont/OpenFontFormat/NameTable.vala
libbirdfont/OpenFontFormat/OffsetTable.vala
libbirdfont/OpenFontFormat/OpenFontFormatWriter.vala
libbirdfont/OpenFontFormat/Os2Table.vala
libbirdfont/OpenFontFormat/OtfTable.vala [new ]
libbirdfont/OpenFontFormat/PostTable.vala
libbirdfont/OpenFontFormat/Table.vala [deleted ]
libbirdfont/SpacingClassTab.vala
--- a/libbirdfont/Glyph.vala +++ b/libbirdfont/Glyph.vala @@ -969,7 +969,6 @@ double xt, yt; Path np; EditPoint inserted; - PathList pl; if (path_list.size == 0) { np = new Path ();
--- a/libbirdfont/GlyphSequence.vala +++ b/libbirdfont/GlyphSequence.vala @@ -58,7 +58,7 @@ }); ligature_sequence.ranges.clear (); - foreach (Glyph? g in ligature_sequence.glyph) { + for (int i = 0; i < ligature_sequence.glyph.size; i++) { ligature_sequence.ranges.add (null); }
--- a/libbirdfont/GridTool.vala +++ b/libbirdfont/GridTool.vala @@ -89,17 +89,6 @@ size_y = w; update_lines (); - } - - public static void ttf_grid (ref int pixel_x, ref int pixel_y) { - double coordinate_x = Glyph.path_coordinate_x (pixel_x); - double coordinate_y = Glyph.path_coordinate_y (pixel_y); - - coordinate_x = GlyfData.tie_to_ttf_grid_x (MainWindow.get_current_glyph (), coordinate_x); - coordinate_y = GlyfData.tie_to_ttf_grid_y (BirdFont.get_current_font (), coordinate_y); - - pixel_x = Glyph.reverse_path_coordinate_x (coordinate_x); - pixel_y = Glyph.reverse_path_coordinate_y (coordinate_y); } public static void ttf_grid_coordinate (ref double x, ref double y) {
--- a/libbirdfont/KerningList.vala +++ b/libbirdfont/KerningList.vala @@ -177,27 +177,28 @@ glyph_range_first = new GlyphRange (); glyph_range_next = new GlyphRange (); - glyph_range_first.parse_ranges (left); - glyph_range_next.parse_ranges (right); - try { - if (left != "" && right != "") { - if (glyph_range_first.is_class () || glyph_range_next.is_class ()) { - - kerning = classes.get_kerning_for_range (glyph_range_first, glyph_range_next); - class_index = classes.get_kerning_item_index (glyph_range_first, glyph_range_next); - - classes.delete_kerning_for_class (left, right); - } else { - kerning = classes.get_kerning (left, right); - classes.delete_kerning_for_pair (left, right); - } + glyph_range_first.parse_ranges (left); + glyph_range_next.parse_ranges (right); + } catch (GLib.MarkupError e) { + warning (e.message); + return; + } + + if (left != "" && right != "") { + if (glyph_range_first.is_class () || glyph_range_next.is_class ()) { - undo_items.add (new UndoItem (left, right, kerning, class_index)); - font.touch (); + kerning = classes.get_kerning_for_range (glyph_range_first, glyph_range_next); + class_index = classes.get_kerning_item_index (glyph_range_first, glyph_range_next); + + classes.delete_kerning_for_class (left, right); + } else { + kerning = classes.get_kerning (left, right); + classes.delete_kerning_for_pair (left, right); } - } catch (MarkupError e) { - warning (e.message); + + undo_items.add (new UndoItem (left, right, kerning, class_index)); + font.touch (); } }
--- a/libbirdfont/LigatureList.vala +++ b/libbirdfont/LigatureList.vala @@ -17,135 +17,78 @@ namespace BirdFont { - public class LigatureList : FontDisplay { - - int scroll = 0; - int visible_rows = 0; - WidgetAllocation allocation; + public class LigatureList : Table { + Gee.ArrayList<Row> rows = new Gee.ArrayList<Row> (); + + public const int NEW_LIGATURE = -1; public LigatureList () { - allocation = new WidgetAllocation (); } - public override void draw (WidgetAllocation allocation, Context cr) { - Ligatures ligatures = BirdFont.get_current_font ().get_ligatures (); - int y = 20; - int s = 0; - bool color = (scroll % 2) == 0; - - this.allocation = allocation; - - visible_rows = (int) (allocation.height / 18.0); - - cr.save (); - cr.set_source_rgba (1, 1, 1, 1); - cr.rectangle (0, 0, allocation.width, allocation.height); - cr.fill (); - cr.restore (); - - cr.save (); - cr.set_source_rgba (0.3, 0.3, 0.3, 1); - cr.set_font_size (12); - - ligatures.get_ligatures ((substitution, ligature) => { - if (s++ >= scroll) { - draw_row (allocation, cr, substitution, ligature, y, color); - y += 18; - color = !color; - } - }); - - cr.restore (); - } - - private static void draw_row (WidgetAllocation allocation, Context cr, - string substitution, string ligature, int y, bool color) { - - if (color) { - cr.save (); - cr.set_source_rgba (224/255.0, 224/255.0, 224/255.0, 1); - cr.rectangle (0, y - 14, allocation.width, 18); - cr.fill (); - cr.restore (); - } - - cr.move_to (30, y); - cr.show_text (substitution); - cr.move_to (230, y); - cr.show_text (ligature); + public override Gee.ArrayList<Row> get_rows () { + return rows; } - public override string get_label () { - return t_("Ligatures"); + void add_ligature (string subst, string liga) { + Font font = BirdFont.get_current_font (); + Ligatures ligatures = font.get_ligatures (); + ligatures.add_ligature (subst, liga); } - public override string get_name () { - return "Ligatures"; - } + public override void selected_row (Row row, int column, bool delete_button) { + Font font = BirdFont.get_current_font (); + Ligatures ligatures = font.get_ligatures (); + + if (row.get_index () == NEW_LIGATURE) { + add_ligature (t_("substitute"), t_("ligature")); + MainWindow.native_window.hide_text_input (); + } else if (ligatures.count () != 0) { + if (delete_button) { + return_if_fail (0 <= row.get_index () < ligatures.count ()); + ligatures.remove_at (row.get_index ()); + MainWindow.native_window.hide_text_input (); + } else if (column == 0) { + if (!(0 <= row.get_index () < ligatures.count ())) { + warning (@"Index: $(row.get_index ()) ligatures.count (): $(ligatures.count ())"); + return; + } + ligatures.set_ligature (row.get_index ()); + } else if (column == 2) { + return_if_fail (0 <= row.get_index () < ligatures.count ()); + ligatures.set_substitution (row.get_index ()); + } + } - public override bool has_scrollbar () { - return true; + update_rows (); + update_scrollbar (); + font.touch (); } - - public override void scroll_wheel_down (double x, double y) { - Ligatures ligatures = BirdFont.get_current_font ().get_ligatures (); - uint liga = ligatures.count (); - scroll += 3; - if (scroll > liga - visible_rows) { - scroll = (int) (liga - visible_rows); - } + public override void update_rows () { + int i; + Font font = BirdFont.get_current_font (); + Ligatures ligatures = font.get_ligatures (); - if (visible_rows > liga) { - scroll = 0; - } + rows.clear (); + rows.add (new Row (t_("New Ligature"), NEW_LIGATURE, false)); - update_scrollbar (); - redraw_area (0, 0, allocation.width, allocation.height); - } - - public override void scroll_wheel_up (double x, double y) { - scroll -= 3; + i = 0; + ligatures.get_ligatures ((subst, liga) => { + rows.add (new Row.columns_3 (@"$subst", "->", @"$liga", i)); + i++; + }); - if (scroll < 0) { - scroll = 0; - } - - update_scrollbar (); - redraw_area (0, 0, allocation.width, allocation.height); + GlyphCanvas.redraw (); } - public override void selected_canvas () { - update_scrollbar (); - redraw_area (0, 0, allocation.width, allocation.height); + public override string get_label () { + return t_("Ligatures"); } - - public void update_scrollbar () { - Ligatures ligatures = BirdFont.get_current_font ().get_ligatures (); - uint rows = ligatures.count (); - if (rows == 0 || visible_rows == 0) { - MainWindow.set_scrollbar_size (0); - MainWindow.set_scrollbar_position (0); - } else { - MainWindow.set_scrollbar_size ((double) visible_rows / rows); - MainWindow.set_scrollbar_position ((double) scroll / rows); - } - } - - public override void scroll_to (double percent) { - Ligatures ligatures = BirdFont.get_current_font ().get_ligatures (); - uint liga = ligatures.count (); - scroll = (int) (percent * liga); - - if (scroll > liga - visible_rows) { - scroll = (int) (liga - visible_rows); - } - - redraw_area (0, 0, allocation.width, allocation.height); + public override string get_name () { + return "Ligatures"; } } } -
--- a/libbirdfont/Ligatures.vala +++ b/libbirdfont/Ligatures.vala @@ -18,6 +18,9 @@ namespace BirdFont { public class Ligatures : GLib.Object { + + public Gee.ArrayList<string> ligature = new Gee.ArrayList<string> (); + public Gee.ArrayList<string> substitution = new Gee.ArrayList<string> (); public delegate void LigatureIterator (string substitution, string ligature); public delegate void SingleLigatureIterator (GlyphSequence substitution, GlyphCollection ligature); @@ -64,11 +67,27 @@ } public int count () { - return 1; + return ligature.size; } + public void remove_at (int i) { + return_if_fail (0 <= i < ligature.size); + return_if_fail (0 <= i < substitution.size); + ligature.remove_at (i); + substitution.remove_at (i); + } + public void set_ligature (int index) { + } + + public void set_substitution (int index) { + } + + public void add_ligature (string subst, string liga) { + substitution.insert (0, subst); + ligature.insert (0, liga); + } } }
--- a/libbirdfont/OpenFontFormat/CffTable.vala +++ b/libbirdfont/OpenFontFormat/CffTable.vala @@ -15,7 +15,7 @@ namespace BirdFont { /** Type2, PostScript outlines */ - public class CffTable : Table { + public class CffTable : OtfTable { static const uint8 HSTEM = 1; static const uint8 VSTEM = 3;
--- a/libbirdfont/OpenFontFormat/Cmap.vala +++ b/libbirdfont/OpenFontFormat/Cmap.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class CmapTable : Table { + public class CmapTable : OtfTable { GlyfTable glyf_table; CmapSubtableFormat4 cmap_format4 = new CmapSubtableFormat4 ();
--- a/libbirdfont/OpenFontFormat/CvtTable.vala +++ b/libbirdfont/OpenFontFormat/CvtTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class CvtTable : Table { + public class CvtTable : OtfTable { public CvtTable () { id = "cvt ";
--- a/libbirdfont/OpenFontFormat/DirectoryTable.vala +++ b/libbirdfont/OpenFontFormat/DirectoryTable.vala @@ -15,7 +15,7 @@ namespace BirdFont { /** Table with list of tables sorted by table tag. */ - public class DirectoryTable : Table { + public class DirectoryTable : OtfTable { public CmapTable cmap_table; public CvtTable cvt_table; @@ -36,7 +36,7 @@ public OffsetTable offset_table; - Gee.ArrayList<Table> tables; + Gee.ArrayList<OtfTable> tables; public DirectoryTable () { offset_table = new OffsetTable (this); @@ -60,7 +60,7 @@ id = "Directory table"; - tables = new Gee.ArrayList<Table> (); + tables = new Gee.ArrayList<OtfTable> (); } public void process () throws GLib.Error { @@ -86,7 +86,7 @@ process_directory (); // this table } - public Gee.ArrayList<Table> get_tables () { + public Gee.ArrayList<OtfTable> get_tables () { if (tables.size == 0) { tables.add (offset_table); tables.add (this); @@ -393,7 +393,7 @@ public long get_font_file_size () { long length = 0; - foreach (Table t in tables) { + foreach (OtfTable t in tables) { length += t.get_font_data ().length_with_padding (); } @@ -408,7 +408,7 @@ // Check sum adjustment for the entire font public uint32 get_font_file_checksum () { uint32 check_sum = 0; - foreach (Table t in tables) { + foreach (OtfTable t in tables) { t.get_font_data ().continous_check_sum (ref check_sum); } return check_sum; @@ -436,7 +436,7 @@ head_table.process (); // write the directory - foreach (Table t in tables) { + foreach (OtfTable t in tables) { if (t is DirectoryTable || t is OffsetTable) { continue;
--- a/libbirdfont/OpenFontFormat/FontData.vala +++ b/libbirdfont/OpenFontFormat/FontData.vala @@ -102,7 +102,7 @@ uint l; if (length_with_padding () % 4 != 0) { - warning ("Table is not padded to correct size."); + warning ("OtfTable is not padded to correct size."); } seek (0);
--- a/libbirdfont/OpenFontFormat/GaspTable.vala +++ b/libbirdfont/OpenFontFormat/GaspTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class GaspTable : Table { + public class GaspTable : OtfTable { public const uint16 NONE = 0; public const uint16 GASP_GRIDFIT = 1;
--- a/libbirdfont/OpenFontFormat/GdefTable.vala +++ b/libbirdfont/OpenFontFormat/GdefTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class GdefTable : Table { + public class GdefTable : OtfTable { public GdefTable () { id = "GDEF";
--- a/libbirdfont/OpenFontFormat/GlyfTable.vala +++ b/libbirdfont/OpenFontFormat/GlyfTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class GlyfTable : Table { + public class GlyfTable : OtfTable { // Flags for composite glyph static const uint16 BOTH_ARE_WORDS = 1 << 0; static const uint16 BOTH_ARE_XY_VALUES = 1 << 1;
--- a/libbirdfont/OpenFontFormat/GposTable.vala +++ b/libbirdfont/OpenFontFormat/GposTable.vala @@ -15,7 +15,7 @@ namespace BirdFont { - public class GposTable : Table { + public class GposTable : OtfTable { GlyfTable glyf_table; KernList pairs;
--- a/libbirdfont/OpenFontFormat/GsubTable.vala +++ b/libbirdfont/OpenFontFormat/GsubTable.vala @@ -15,7 +15,7 @@ namespace BirdFont { - public class GsubTable : Table { + public class GsubTable : OtfTable { GlyfTable glyf_table; @@ -155,7 +155,7 @@ warning (@"Bad identifier expecting 1 found $identifier"); } - uint16 coverage_offset = fd.read_ushort (); + uint16 coverage_offset = fd.read_ushort (); // TODO: read coverage uint16 num_sets = fd.read_ushort (); Gee.ArrayList<int> liga_set_offsets = new Gee.ArrayList<int> ();
--- a/libbirdfont/OpenFontFormat/HeadTable.vala +++ b/libbirdfont/OpenFontFormat/HeadTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class HeadTable : Table { + public class HeadTable : OtfTable { public int16 xmin = int16.MIN; public int16 ymin = int16.MIN;
--- a/libbirdfont/OpenFontFormat/HheaTable.vala +++ b/libbirdfont/OpenFontFormat/HheaTable.vala @@ -16,7 +16,7 @@ namespace BirdFont { - public class HheaTable : Table { + public class HheaTable : OtfTable { Fixed version; int16 ascender;
--- a/libbirdfont/OpenFontFormat/HmtxTable.vala +++ b/libbirdfont/OpenFontFormat/HmtxTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class HmtxTable : Table { + public class HmtxTable : OtfTable { uint32 nmetrics; uint32 nmonospaced;
--- a/libbirdfont/OpenFontFormat/KernTable.vala +++ b/libbirdfont/OpenFontFormat/KernTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class KernTable : Table { + public class KernTable : OtfTable { public static const uint16 HORIZONTAL = 1; public static const uint16 MINIMUM = 1 << 1;
--- a/libbirdfont/OpenFontFormat/LigatureSet.vala +++ b/libbirdfont/OpenFontFormat/LigatureSet.vala @@ -43,7 +43,7 @@ return (!) ligatures.get (0).substitution.get (0).to_string (); } - public FontData get_set_data () { + public FontData get_set_data () throws GLib.Error { FontData fd, ligature_data; uint16 o, pos;
--- a/libbirdfont/OpenFontFormat/LocaTable.vala +++ b/libbirdfont/OpenFontFormat/LocaTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class LocaTable : Table { + public class LocaTable : OtfTable { uint32* glyph_offsets = null; public uint32 size = 0;
--- a/libbirdfont/OpenFontFormat/MaxpTable.vala +++ b/libbirdfont/OpenFontFormat/MaxpTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class MaxpTable : Table { + public class MaxpTable : OtfTable { GlyfTable glyf_table;
--- a/libbirdfont/OpenFontFormat/NameTable.vala +++ b/libbirdfont/OpenFontFormat/NameTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class NameTable : Table { + public class NameTable : OtfTable { public static const uint16 COPYRIGHT_NOTICE = 0; public static const uint16 FONT_NAME = 1;
--- a/libbirdfont/OpenFontFormat/OffsetTable.vala +++ b/libbirdfont/OpenFontFormat/OffsetTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class OffsetTable : Table { + public class OffsetTable : OtfTable { DirectoryTable directory_table; public uint16 num_tables = 0;
--- a/libbirdfont/OpenFontFormat/OpenFontFormatWriter.vala +++ b/libbirdfont/OpenFontFormat/OpenFontFormatWriter.vala @@ -43,7 +43,7 @@ public void write_ttf_font (Font nfont) throws Error { long dl; uint8* data; - Gee.ArrayList<Table> tables; + Gee.ArrayList<OtfTable> tables; FontData fd; uint l; @@ -59,7 +59,7 @@ return; } - foreach (Table t in tables) { + foreach (OtfTable t in tables) { fd = t.get_font_data (); data = fd.table_data; l = fd.length_with_padding ();
--- a/libbirdfont/OpenFontFormat/Os2Table.vala +++ b/libbirdfont/OpenFontFormat/Os2Table.vala @@ -16,7 +16,7 @@ namespace BirdFont { - public class Os2Table : Table { + public class Os2Table : OtfTable { public static const uint16 ITALIC = 1; public static const uint16 UNDERSCORE = 1 << 1;
--- /dev/null +++ b/libbirdfont/OpenFontFormat/OtfTable.vala @@ -1,1 +1,114 @@ + + /* + Copyright (C) 2012, 2013 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 + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + */ + + namespace BirdFont { + + public class OtfTable : Object { + + public string id = "NO_ID"; + + public uint32 checksum = 0; + public uint32 offset = 0; + public uint32 length = 0; + + protected FontData? font_data = null; + + public virtual string get_id () { + return id; + } + + public FontData get_font_data () { + if (font_data == null) { + warning (@"No font data for $(id)."); + font_data = new FontData (); + } + + return (!) font_data; + } + + public bool has_data () { + return length > 0; + } + + public virtual void parse (FontData dis) throws GLib.Error { + warning (@"Parse is not implemented for $(id)."); + } + + /** Validate table checksum. */ + public bool validate (FontData dis) { + bool valid; + + if (length == 0) { + stderr.printf (@"OtfTable $id is of zero length.\n"); + valid = false; + } else { + valid = OtfTable.validate_table (dis, checksum, offset, length, id); + } + + if (!valid) { + stderr.printf (@"OtfTable $id is invalid.\n"); + } + + return valid; + } + + public static bool validate_table (FontData dis, uint32 checksum, uint32 offset, uint32 length, string name) { + uint32 ch = calculate_checksum (dis, offset, length, name); + bool c; + + c = (ch == checksum); + + if (!c) { + stderr.printf(@"Checksum does not match data for $(name).\n"); + stderr.printf(@"name: $name, checksum: $checksum, offset: $offset, length: $length\n"); + stderr.printf(@"calculated checksum $(ch)\n"); + } + + return c; + } + + public static uint32 calculate_checksum (FontData dis, uint32 offset, uint32 length, string name) { + uint32 checksum = 0; + uint32 l; + + dis.seek (offset); + + l = (length % 4 > 0) ? length / 4 + 1 : length / 4; + + for (uint32 i = 0; i < l; i++) { + checksum += dis.read_ulong (); + } + + return checksum; + } + + public static uint16 max_pow_2_less_than_i (uint16 ind) { + uint16 last = 0; + uint16 i = 1; + + while ((i <<= 1) < ind) { + last = i; + } + + return last; + } + + public static uint16 max_log_2_less_than_i (uint16 ind) { + return (uint16) (Math.log (ind) / Math.log (2)); + } + } + + }
--- a/libbirdfont/OpenFontFormat/PostTable.vala +++ b/libbirdfont/OpenFontFormat/PostTable.vala @@ -14,7 +14,7 @@ namespace BirdFont { - public class PostTable : Table { + public class PostTable : OtfTable { GlyfTable glyf_table;
--- a/libbirdfont/OpenFontFormat/Table.vala +++ /dev/null @@ -1,114 +1,1 @@ - - /* - Copyright (C) 2012, 2013 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 - published by the Free Software Foundation; either version 3 of the - License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - */ - - namespace BirdFont { - - public class Table : Object { - - public string id = "NO_ID"; - - public uint32 checksum = 0; - public uint32 offset = 0; - public uint32 length = 0; - - protected FontData? font_data = null; - - public virtual string get_id () { - return id; - } - - public FontData get_font_data () { - if (font_data == null) { - warning (@"No font data for $(id)."); - font_data = new FontData (); - } - - return (!) font_data; - } - - public bool has_data () { - return length > 0; - } - - public virtual void parse (FontData dis) throws GLib.Error { - warning (@"Parse is not implemented for $(id)."); - } - - /** Validate table checksum. */ - public bool validate (FontData dis) { - bool valid; - - if (length == 0) { - stderr.printf (@"Table $id is of zero length.\n"); - valid = false; - } else { - valid = Table.validate_table (dis, checksum, offset, length, id); - } - - if (!valid) { - stderr.printf (@"Table $id is invalid.\n"); - } - - return valid; - } - - public static bool validate_table (FontData dis, uint32 checksum, uint32 offset, uint32 length, string name) { - uint32 ch = calculate_checksum (dis, offset, length, name); - bool c; - - c = (ch == checksum); - - if (!c) { - stderr.printf(@"Checksum does not match data for $(name).\n"); - stderr.printf(@"name: $name, checksum: $checksum, offset: $offset, length: $length\n"); - stderr.printf(@"calculated checksum $(ch)\n"); - } - - return c; - } - - public static uint32 calculate_checksum (FontData dis, uint32 offset, uint32 length, string name) { - uint32 checksum = 0; - uint32 l; - - dis.seek (offset); - - l = (length % 4 > 0) ? length / 4 + 1 : length / 4; - - for (uint32 i = 0; i < l; i++) { - checksum += dis.read_ulong (); - } - - return checksum; - } - - public static uint16 max_pow_2_less_than_i (uint16 ind) { - uint16 last = 0; - uint16 i = 1; - - while ((i <<= 1) < ind) { - last = i; - } - - return last; - } - - public static uint16 max_log_2_less_than_i (uint16 ind) { - return (uint16) (Math.log (ind) / Math.log (2)); - } - } - - }
--- a/libbirdfont/SpacingClassTab.vala +++ b/libbirdfont/SpacingClassTab.vala @@ -16,20 +16,18 @@ namespace BirdFont { - public class SpacingClassTab : FontDisplay { - - int scroll = 0; - int visible_rows = 0; - WidgetAllocation allocation = new WidgetAllocation (); - Gee.ArrayList<Row> rows = new Gee.ArrayList<Row> (); + public class SpacingClassTab : Table { public Gee.ArrayList<SpacingClass> classes = new Gee.ArrayList<SpacingClass> (); - public static int NEW_CLASS = -1; - Gee.ArrayList<string> connections = new Gee.ArrayList<string> (); + Gee.ArrayList<Row> rows = new Gee.ArrayList<Row> (); public SpacingClassTab () { + } + + public override Gee.ArrayList<Row> get_rows () { + return rows; } public static void remove_all_spacing_classes () { @@ -109,7 +107,7 @@ KerningTools.update_spacing_classes (); } - public void selected_row (Row row, int column, bool delete_button) { + public override void selected_row (Row row, int column, bool delete_button) { Font font = BirdFont.get_current_font (); if (row.get_index () == -1) { @@ -139,18 +137,13 @@ font.touch (); } } - } - - public override void selected_canvas () { - update_rows (); - update_scrollbar (); } void update_all_rows (SpacingClass s) { update_rows (); } - void update_rows () { + public override void update_rows () { int i = 0; rows.clear (); @@ -162,109 +155,6 @@ } GlyphCanvas.redraw (); - } - - public override void draw (WidgetAllocation allocation, Context cr) { - double y = 20 * MainWindow.units; - int s = 0; - bool color = (scroll + 1 % 2) == 0; - - if (allocation.width != this.allocation.width - || allocation.height != this.allocation.height) { - this.allocation = allocation; - update_rows (); - update_scrollbar (); - } - - visible_rows = (int) (allocation.height / 18.0); - - cr.save (); - cr.set_source_rgba (1, 1, 1, 1); - cr.rectangle (0, 0, allocation.width, allocation.height); - cr.fill (); - cr.restore (); - - cr.save (); - cr.set_source_rgba (0.3, 0.3, 0.3, 1); - cr.set_font_size (12); - - foreach (Row r in rows) { - if (s++ >= scroll) { - draw_row (allocation, cr, r, y, color, true); - y += 18 * MainWindow.units; - color = !color; - } - } - - cr.restore (); - } - - private static void draw_row (WidgetAllocation allocation, Context cr, - Row row, double y, bool color, bool dark) { - - double margin; - - if (color) { - cr.save (); - cr.set_source_rgba (224/255.0, 224/255.0, 224/255.0, 1); - cr.rectangle (0, y - 14 * MainWindow.units, allocation.width, 18 * MainWindow.units); - cr.fill (); - cr.restore (); - } - - if (row.has_delete_button ()) { - cr.save (); - cr.set_line_width (1); - cr.move_to (10 * MainWindow.units, y - 8 * MainWindow.units); - cr.line_to (15 * MainWindow.units, y - 3 * MainWindow.units); - cr.move_to (10 * MainWindow.units, y - 3 * MainWindow.units); - cr.line_to (15 * MainWindow.units, y - 8 * MainWindow.units); - cr.stroke (); - cr.restore (); - } - - for (int i = 0; i < row.columns; i++) { - cr.save (); - margin = (row.has_delete_button ()) ? 120 * MainWindow.units : 3* MainWindow.units; - cr.move_to (margin + i * 120 * MainWindow.units, y); - cr.set_font_size (12 * MainWindow.units); - cr.show_text (row.get_column (i)); - cr.restore (); - } - } - - public override void button_release (int button, double ex, double ey) { - int s = 0; - double y = 0; - int colum = -1; - Row? selected = null; - bool over_delete = false; - - foreach (Row r in rows) { - if (s++ >= scroll) { - y += 18 * MainWindow.units; - - if (y - 10 * MainWindow.units <= ey * MainWindow.units <= y + 5 * MainWindow.units) { - - if (r.has_delete_button ()) { - colum = (int) ((ex - 120) / 120 * MainWindow.units); - } else { - colum = (int) ((ex) / 120 * MainWindow.units); - } - - over_delete = (ex < 18 && r.has_delete_button ()); - selected = r; - break; - } - } - } - - if (selected != null) { - selected_row ((!) selected, colum, over_delete); - } - - update_scrollbar (); - redraw_area (0, 0, allocation.width, allocation.height); } public override string get_label () { @@ -273,62 +163,8 @@ public override string get_name () { return "SpacingClasses"; - } - - public override bool has_scrollbar () { - return true; - } - - public override void scroll_wheel_down (double x, double y) { - int nrows = rows.size; - scroll += 3; - - if (scroll > nrows - visible_rows) { - scroll = (int) (nrows - visible_rows); - } - - if (visible_rows > nrows) { - scroll = 0; - } - - update_scrollbar (); - redraw_area (0, 0, allocation.width, allocation.height); - } - - public override void scroll_wheel_up (double x, double y) { - scroll -= 3; - - if (scroll < 0) { - scroll = 0; - } - - update_scrollbar (); - redraw_area (0, 0, allocation.width, allocation.height); - } - - public void update_scrollbar () { - uint rows = rows.size; - - if (rows == 0 || visible_rows == 0) { - MainWindow.set_scrollbar_size (0); - MainWindow.set_scrollbar_position (0); - } else { - MainWindow.set_scrollbar_size ((double) visible_rows / rows); - MainWindow.set_scrollbar_position ((double) scroll / rows); - } - } - - public override void scroll_to (double percent) { - uint rows = rows.size; - scroll = (int) (percent * rows); - - if (scroll > rows - visible_rows) { - scroll = (int) (rows - visible_rows); - } - - redraw_area (0, 0, allocation.width, allocation.height); } } }