.
1 /*
2 Copyright (C) 2013 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
15 using Gee;
16
17 namespace BirdFont {
18
19 public class CharDatabaseParser : GLib.Object {
20
21 public signal void sync ();
22
23 GlyphRange utf8 = new GlyphRange ();
24
25 public CharDatabaseParser () {
26 }
27
28 private void add_entry (string data) {
29 string[] e;
30 string[] r;
31 string[] d;
32 string index_values;
33 unichar ch;
34 string unicode_hex;
35
36 if (data.has_prefix ("@")) { // ignore comments
37 return;
38 }
39
40 if (data.has_prefix (";")) {
41 return;
42 }
43
44 index_values = data.down ();
45 index_values = index_values.replace ("\n\tx", "");
46 index_values = index_values.replace ("\n\t*", "");
47 index_values = index_values.replace ("\n\t=", "");
48 index_values = index_values.replace ("\n\t#", "");
49 index_values = index_values.replace (" - ", " ");
50 index_values = index_values.replace ("(", "");
51 index_values = index_values.replace (")", "");
52 index_values = index_values.replace ("<font>", "");
53 index_values = index_values.replace (" a ", " ");
54 index_values = index_values.replace (" is ", " ");
55 index_values = index_values.replace (" the ", " ");
56
57 e = index_values.split ("\t");
58
59 return_if_fail (e.length > 0);
60
61 unicode_hex = e[0].up ();
62
63 ch = Font.to_unichar ("U+" + unicode_hex.down ());
64
65 Idle.add (() => {
66 CharDatabase.entries.set (unicode_hex, data);
67 return false;
68 });
69 sync ();
70
71 utf8.add_single (ch);
72
73 foreach (string s in e) {
74 r = s.split ("\n");
75 foreach (string t in r) {
76 d = t.split (" ");
77 foreach (string token in d) {
78 if (token != "") {
79 Idle.add (() => {
80 CharDatabase.index.set (token, unicode_hex);
81 return false;
82 });
83 sync ();
84 }
85 }
86 }
87 }
88 }
89
90 private void parse_all_entries () {
91 FileInputStream fin;
92 DataInputStream din;
93 string? line;
94 string data;
95 string description = "";
96 File file;
97
98 if (BirdFont.has_argument ("--no-ucd")) {
99 warning ("Not loading UCD.");
100 return;
101 }
102
103 file = get_unicode_database ();
104
105 try {
106 fin = file.read ();
107 din = new DataInputStream (fin);
108
109 line = din.read_line (null);
110 while (true) {
111 data = (!) line;
112 description = data;
113
114 while ((line = din.read_line (null)) != null) {
115 data = (!) line;
116 if (data.has_prefix ("\t")) {
117 description += "\n";
118 description += data;
119 } else {
120 if (description.index_of ("<not a character>") == -1) {
121 add_entry (description);
122 }
123 break;
124 }
125 }
126
127 if (line == null) {
128 break;
129 }
130 }
131
132 if (description == "") {
133 warning ("no description found");
134 }
135
136 fin.close ();
137 din.close ();
138 } catch (GLib.Error e) {
139 warning (e.message);
140 warning ("In %s", (!) get_unicode_database ().get_path ());
141 }
142 }
143
144 public CharDatabaseParser load () {
145 parse_all_entries ();
146
147 IdleSource idle = new IdleSource ();
148 idle.set_callback (() => {
149 CharDatabase.full_unicode_range = utf8;
150 CharDatabase.database_is_loaded = true;
151 return false;
152 });
153 idle.attach (null);
154
155 return this;
156 }
157
158 static File get_unicode_database () {
159 return SearchPaths.get_char_database ();
160 }
161 }
162
163 }
164