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