.
1 /*
2 Copyright (C) 2012 2015 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 using Sqlite;
17
18 namespace BirdFont {
19
20 public class CharDatabase {
21 public static GlyphRange full_unicode_range;
22
23 static unowned Database db;
24 static Database? database = null;
25
26 public CharDatabase () {
27 File f;
28
29 full_unicode_range = new GlyphRange ();
30 f = get_database_file ();
31 open_database () ;
32 }
33
34 public static void open_database () {
35 File f = get_database_file ();
36 int rc = Database.open ((!) f.get_path (), out database);
37
38 db = (!) database;
39
40 if (rc != Sqlite.OK) {
41 stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
42 }
43 }
44
45 public static File get_database_file () {
46 return SearchPaths.find_file (null, "ucd.sqlite");
47 }
48
49 public static GlyphRange search (string s) {
50 GlyphRange result = new GlyphRange ();
51 GlyphRange ucd_result = new GlyphRange ();
52 int rc, cols;
53 Statement statement;
54 string select;
55 unichar c;
56
57 if (s.has_prefix ("U+") || s.has_prefix ("u+")) {
58 c = Font.to_unichar (s.down ());
59
60 if (c != '\0') {
61 result.add_single (c);
62 }
63 }
64
65 if (s.char_count () == 1) {
66 result.add_single (s.get_char (0));
67 }
68
69 select = "SELECT unicode FROM Words "
70 + "WHERE word = '" + s.replace ("'", "''") + "';";
71
72 rc = db.prepare_v2 (select, select.length, out statement, null);
73
74 if (rc == Sqlite.OK) {
75 cols = statement.column_count();
76
77 if (cols != 1) {
78 warning ("Expecting one column.");
79 return result;
80 }
81
82 while (true) {
83 rc = statement.step ();
84
85 if (rc == Sqlite.DONE) {
86 break;
87 } else if (rc == Sqlite.ROW) {
88 c = (unichar) statement.column_int64 (0);
89 ucd_result.add_single (c);
90 } else {
91 warning ("Error: %d, %s\n", rc, db.errmsg ());
92 break;
93 }
94 }
95
96 try {
97 if (ucd_result.get_length () > 0) {
98 ucd_result.sort ();
99 result.parse_ranges (ucd_result.get_all_ranges ());
100 }
101 } catch (MarkupError e) {
102 warning (e.message);
103 }
104
105 } else {
106 warning ("SQL error: %d, %s\n", rc, db.errmsg ());
107 }
108
109 return result;
110 }
111
112 public static bool has_ascender (unichar c) {
113 if (!c.islower()) return true;
114
115 switch (c) {
116 case 'b': return true;
117 case 'd': return true;
118 case 'f': return true;
119 case 'h': return true;
120 case 'k': return true;
121 case 'l': return true;
122 }
123
124 return false;
125 }
126
127 public static bool has_descender (unichar c) {
128 switch (c) {
129 case 'g': return true;
130 case 'j': return true;
131 case 'p': return true;
132 case 'q': return true;
133 case 'y': return true;
134 }
135
136 return false;
137 }
138
139 public static string get_unicode_database_entry (unichar c) {
140 string description = "";
141 int rc, cols;
142 Statement statement;
143 string select = "SELECT description FROM Description "
144 + @"WHERE unicode = $((int64) c)";
145
146 rc = db.prepare_v2 (select, select.length, out statement, null);
147
148 if (rc == Sqlite.OK) {
149 cols = statement.column_count();
150
151 if (cols != 1) {
152 warning ("Expecting one column.");
153 return description;
154 }
155
156 while (true) {
157 rc = statement.step ();
158
159 if (rc == Sqlite.DONE) {
160 break;
161 } else if (rc == Sqlite.ROW) {
162 description = statement.column_text (0);
163 } else {
164 printerr ("Error: %d, %s\n", rc, db.errmsg ());
165 break;
166 }
167 }
168
169 } else {
170 printerr ("SQL error: %d, %s\n", rc, db.errmsg ());
171 }
172
173 if (description == "") {
174 description = Font.to_hex (c).replace ("U+", "") + "\tUNICODE CHARACTER";
175 }
176
177 return description;
178 }
179
180 public static void get_full_unicode (GlyphRange glyph_range) {
181 try {
182 if (!is_null (full_unicode_range)) {
183 glyph_range.parse_ranges (full_unicode_range.get_all_ranges ());
184 }
185 } catch (MarkupError e) {
186 warning (e.message);
187 }
188 }
189 }
190
191 }
192