The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

CharDatabase.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/CharDatabase.vala.
Fix translation
1 /* 2 Copyright (C) 2012 2015 Johan Mattsson 3 4 All rights reserved. 5 */ 6 7 using Gee; 8 using Sqlite; 9 10 namespace BirdFont { 11 12 public class CharDatabase { 13 public static GlyphRange full_unicode_range; 14 15 static unowned Database db; 16 static Database? database = null; 17 18 public CharDatabase () { 19 File f; 20 21 full_unicode_range = new GlyphRange (); 22 f = get_database_file (); 23 open_database () ; 24 } 25 26 public static void open_database () { 27 File f = get_database_file (); 28 printd (@"Open database: " + (!) f.get_path () + "\n"); 29 30 int rc = Database.open_v2 ((!) f.get_path (), out database, OPEN_READONLY); 31 32 db = (!) database; 33 34 if (rc != Sqlite.OK) { 35 stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ()); 36 } 37 } 38 39 public static File get_database_file () { 40 return SearchPaths.find_file (null, "ucd.sqlite"); 41 } 42 43 public static GlyphRange search (string s) { 44 GlyphRange result = new GlyphRange (); 45 GlyphRange ucd_result = new GlyphRange (); 46 int rc, cols; 47 Statement statement; 48 string select; 49 unichar c; 50 string query = s.strip (); 51 52 if (query.has_prefix ("U+") || query.has_prefix ("u+")) { 53 c = Font.to_unichar (query.down ()); 54 55 if (c != '\0') { 56 result.add_single (c); 57 } 58 } 59 60 if (query.char_count () == 1) { 61 result.add_single (s.get_char (0)); 62 } 63 64 string[] terms = query.split (" "); 65 66 bool first = true; 67 select = ""; 68 69 foreach (string term in terms) { 70 if (first) { 71 select = "SELECT unicode FROM Words " 72 + "WHERE word GLOB '" + term.replace ("'", "''") + "' "; 73 } else { 74 select += "OR word GLOB '" + term.replace ("'", "''") + "' "; 75 } 76 77 first = false; 78 } 79 select += ";"; 80 81 rc = db.prepare_v2 (select, select.length, out statement, null); 82 83 if (rc == Sqlite.OK) { 84 cols = statement.column_count(); 85 86 if (cols != 1) { 87 warning ("Expecting one column."); 88 return result; 89 } 90 91 while (true) { 92 rc = statement.step (); 93 94 if (rc == Sqlite.DONE) { 95 break; 96 } else if (rc == Sqlite.ROW) { 97 c = (unichar) statement.column_int64 (0); 98 99 if (has_all_terms (c, query)) { 100 ucd_result.add_single (c); 101 } 102 } else { 103 warning ("Error: %d, %s\n", rc, db.errmsg ()); 104 break; 105 } 106 } 107 108 try { 109 if (ucd_result.get_length () > 0) { 110 ucd_result.sort (); 111 result.parse_ranges (ucd_result.get_all_ranges ()); 112 } 113 } catch (MarkupError e) { 114 warning (e.message); 115 } 116 117 } else { 118 warning ("SQL error: %d, %s\n", rc, db.errmsg ()); 119 } 120 121 return result; 122 } 123 124 public static bool has_ascender (unichar c) { 125 if (!c.islower()) return true; 126 127 switch (c) { 128 case 'b': return true; 129 case 'd': return true; 130 case 'f': return true; 131 case 'h': return true; 132 case 'k': return true; 133 case 'l': return true; 134 } 135 136 return false; 137 } 138 139 public static bool has_descender (unichar c) { 140 switch (c) { 141 case 'g': return true; 142 case 'j': return true; 143 case 'p': return true; 144 case 'q': return true; 145 case 'y': return true; 146 } 147 148 return false; 149 } 150 151 static bool has_term (unichar c, string term) { 152 Statement statement; 153 string select = "SELECT unicode FROM Words " 154 + "WHERE word GLOB '" + term.replace ("'", "''") + "' " 155 + @"AND unicode = $((int64) c);"; 156 157 int rc = db.prepare_v2 (select, select.length, out statement, null); 158 159 if (rc == Sqlite.OK) { 160 rc = statement.step (); 161 162 if (rc == Sqlite.DONE) { 163 return false; 164 } else if (rc == Sqlite.ROW) { 165 c = (unichar) statement.column_int64 (0); 166 return true; 167 } 168 } else { 169 warning ("Error: %d, %s\n", rc, db.errmsg ()); 170 return false; 171 } 172 173 return false; 174 } 175 176 static bool has_all_terms (unichar c, string query) { 177 string[] terms = query.split (" "); 178 179 foreach (string term in terms) { 180 if (!has_term (c, term)) { 181 return false; 182 } 183 } 184 185 return true; 186 } 187 188 public static string get_unicode_database_entry (unichar c) { 189 string description = ""; 190 int rc, cols; 191 Statement statement; 192 string select = "SELECT description FROM Description " 193 + @"WHERE unicode = $((int64) c)"; 194 195 rc = db.prepare_v2 (select, select.length, out statement, null); 196 197 if (rc == Sqlite.OK) { 198 cols = statement.column_count(); 199 200 if (cols != 1) { 201 warning ("Expecting one column."); 202 return description; 203 } 204 205 while (true) { 206 rc = statement.step (); 207 208 if (rc == Sqlite.DONE) { 209 break; 210 } else if (rc == Sqlite.ROW) { 211 description = statement.column_text (0); 212 } else { 213 printerr ("Error: %d, %s\n", rc, db.errmsg ()); 214 break; 215 } 216 } 217 218 } else { 219 printerr ("SQL error: %d, %s\n", rc, db.errmsg ()); 220 } 221 222 if (description == "") { 223 description = Font.to_hex (c).replace ("U+", "") + "\tUNICODE CHARACTER"; 224 } 225 226 return description; 227 } 228 229 public static void get_full_unicode (GlyphRange glyph_range) { 230 try { 231 if (!is_null (full_unicode_range)) { 232 glyph_range.parse_ranges (full_unicode_range.get_all_ranges ()); 233 } 234 } catch (MarkupError e) { 235 warning (e.message); 236 } 237 } 238 } 239 240 } 241