.
1 /*
2 Copyright (C) 2012 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 CharDatabase {
20
21 public static HashMap<string, string> entries;
22 public static HashMultiMap<string, string> index;
23
24 public static GlyphRange full_unicode_range;
25 public static bool database_is_loaded = false;
26
27 public CharDatabase () {
28 entries = new HashMap<string, string> ();
29 index = new HashMultiMap<string, string> ();
30
31 full_unicode_range = new GlyphRange ();
32 }
33
34 public static GlyphRange search (string s) {
35 GlyphRange result = new GlyphRange ();
36 GlyphRange ucd_result = new GlyphRange ();
37 unichar c;
38
39 return_val_if_fail (!is_null (index), result);
40 return_val_if_fail (result.get_length () == 0, result);
41
42 if (s.has_prefix ("U+") || s.has_prefix ("u+")) {
43 c = Font.to_unichar (s.down ());
44
45 if (c != '\0') {
46 result.add_single (c);
47 }
48 }
49
50 if (s.char_count () == 1) {
51 result.add_single (s.get_char ());
52 }
53
54 foreach (string i in index.get (s)) {
55 c = Font.to_unichar ("U+" + i.down ());
56 ucd_result.add_single (c);
57 }
58
59 try {
60 if (ucd_result.get_length () > 0) {
61 ucd_result.sort ();
62 result.parse_ranges (ucd_result.get_all_ranges ());
63 }
64 } catch (MarkupError e) {
65 warning (e.message);
66 }
67
68 return result;
69 }
70
71 public static bool has_ascender (unichar c) {
72 if (!c.islower()) return true;
73
74 switch (c) {
75 case 'b': return true;
76 case 'd': return true;
77 case 'f': return true;
78 case 'h': return true;
79 case 'k': return true;
80 case 'l': return true;
81 }
82
83 return false;
84 }
85
86 public static bool has_descender (unichar c) {
87 switch (c) {
88 case 'g': return true;
89 case 'j': return true;
90 case 'p': return true;
91 case 'q': return true;
92 case 'y': return true;
93 }
94
95 return false;
96 }
97
98 /** Convert from the U+xx form to the unicode database hex value. */
99 static string to_database_hex (unichar c) {
100 string hex_char = Font.to_hex (c).replace ("U+", "");
101
102 if (hex_char.char_count () == 2) {
103 hex_char = "00" + hex_char;
104 }
105
106 if (hex_char.char_count () == 6 && hex_char.has_prefix ("0")) {
107 hex_char = hex_char.substring (1);
108 }
109
110 hex_char = hex_char.up ();
111 return hex_char;
112 }
113
114 public static string get_unicode_database_entry (unichar c) {
115 string description;
116 string? d;
117
118 d = entries.get (to_database_hex (c));
119
120 if (d == null) {
121 description = Font.to_hex (c).replace ("U+", "") + "\tUNICODE CHARACTER";
122 } else {
123 description = (!) d;
124 }
125
126 return description;
127 }
128
129 public static void get_full_unicode (GlyphRange glyph_range) {
130 try {
131 if (!is_null (full_unicode_range)) {
132 glyph_range.parse_ranges (full_unicode_range.get_all_ranges ());
133 }
134 } catch (MarkupError e) {
135 warning (e.message);
136 }
137 }
138 }
139
140 }
141