.
1 /*
2 Copyright (C) 2014 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14 using Cairo;
15 using Math;
16
17 namespace BirdFont {
18
19 public class SpacingData : GLib.Object {
20
21 public KerningClasses kerning_classes;
22
23 public Gee.ArrayList<SpacingClass> classes = new Gee.ArrayList<SpacingClass> ();
24 Gee.ArrayList<string> connections = new Gee.ArrayList<string> ();
25
26 public SpacingData (KerningClasses kerning) {
27 kerning_classes = kerning;
28 }
29
30 public KerningClasses get_kerning_classes () {
31 return kerning_classes;
32 }
33
34 public Gee.ArrayList<string> get_all_connections (string glyph) {
35 string t;
36 Gee.ArrayList<string> c = new Gee.ArrayList<string> ();
37
38 connections.clear ();
39
40 add_connections (glyph);
41
42 for (int i = 0; i < connections.size; i++) {
43 return_val_if_fail (0 <= i < connections.size, c);
44 t = connections.get (i);
45 c.add (t.dup ());
46 }
47
48 connections.clear ();
49
50 return c;
51 }
52
53 bool has_connection (string s) {
54 foreach (string t in connections) {
55 if (t == s) {
56 return true;
57 }
58 }
59
60 return false;
61 }
62
63 public void add_connections (string glyph) {
64 connections.add (glyph);
65
66 foreach (SpacingClass s in classes) {
67 if (s.first == glyph) {
68 if (!has_connection (s.next)) {
69 add_connections (s.next);
70 }
71 }
72
73 if (s.next == glyph) {
74 if (!has_connection (s.first)) {
75 add_connections (s.first);
76 }
77 }
78 }
79
80 connections.sort ((a, b) => {
81 return strcmp ((string) a, (string) b);
82 });
83 }
84
85 public void add_class (string first, string next) {
86 SpacingClass s = new SpacingClass (first, next);
87 s.updated.connect (update_all_rows);
88 s.updated.connect (update_kerning);
89 classes.add (s);
90 update_kerning (s);
91 }
92
93 void update_all_rows (SpacingClass s) {
94 MainWindow.get_spacing_class_tab ().update_rows ();
95 }
96
97 public void update_kerning (SpacingClass s) {
98 Font font = kerning_classes.font;
99 GlyphCollection? g;
100 GlyphCollection gc;
101
102 if (s.next != "?") {
103 kerning_classes.update_space_class (s.next);
104 g = font.get_glyph_collection (s.next);
105 if (g != null) {
106 gc = (!) g;
107 gc.get_current ().update_spacing_class ();
108 }
109 }
110
111 if (s.first != "?") {
112 kerning_classes.update_space_class (s.first);
113 g = font.get_glyph_collection (s.first);
114 if (g != null) {
115 gc = (!) g;
116 gc.get_current ().update_spacing_class ();
117 }
118 }
119
120 KerningTools.update_spacing_classes ();
121 }
122 }
123
124 }
125