The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

DefaultCharactersSet.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
Circle boundaries heads/master
1 /* 2 Copyright (C) 2013, 2014 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 namespace BirdFont { 16 17 /** Default character sets for several languages. */ 18 public class DefaultCharacterSet { 19 20 public static DefaultLanguages languages; 21 22 public static void create_default_character_sets () { 23 languages = new DefaultLanguages (); 24 25 add_language (t_("Default Language"), "", ""); 26 add_language (t_("Private Use Area"), "PRIVATE_USE", ""); 27 28 add_language (t_("Chinese"), "zh", ""); 29 add_language (t_("English"), "en", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z"); 30 add_language (t_("Greek"), "el", "Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ ς τ υ φ χ ψ ω"); 31 add_language (t_("Japanese"), "ja", ""); 32 add_language (t_("Javanese"), "jv", "ꦀ ꦁ ꦂ ꦃ ꦄ ꦅ ꦆ ꦇ ꦈ ꦉ ꦊ ꦋ ꦌ ꦍ ꦎ ꦏ ꦐ ꦑ ꦒ ꦓ ꦔ ꦕ ꦖ ꦗ ꦘ ꦙ ꦚ ꦛ ꦜ ꦝ ꦞ ꦟ ꦠ ꦡ ꦢ ꦣ ꦤ ꦥ ꦦ ꦧ ꦨ ꦩ ꦪ ꦫ ꦬ ꦭ ꦮ ꦯ ꦰ ꦱ ꦲ ꦳ ꦴ ꦵ ꦶ ꦷ ꦸ ꦹ ꦺ ꦻ ꦼ ꦽ ꦾ ꦿ ꧀ ꧁ ꧂ ꧃ ꧄ ꧅ ꧆ ꧇ ꧈ ꧉ ꧊ ꧋ ꧌ ꧍ ꧏ ꧐ ꧑ ꧒ ꧓ ꧔ ꧕ ꧖ ꧗ ꧘ ꧙ ꧞ ꧟ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z"); 33 add_language (t_("Latin"), "la", ""); 34 add_language (t_("Russian"), "ro", "А Б В Г Д Е Ё Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ъ Ы Ь Э Ю Я а б в г д е ё ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ы ь э ю я"); 35 add_language (t_("Swedish"), "sv", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Å Ä Ö a b c d e f g h i j k l m n o p q r s t u v w x y z å ä ö"); 36 add_language (t_("Thai"), "th", "ก-ฺ ฿-๛"); 37 } 38 39 /** Add a new language to the menu for default character set. 40 * @param language A localized string for the name of the language. 41 * @param language_code ISO code 42 * @param characters all characters (including characters with diacritical marks). 43 * For languages with too many glyphs should this string be left empty. 44 * See the functions for chinese and japanese. 45 */ 46 public static void add_language (string language, string language_code, string characters) { 47 DefaultLanguages.names.add (language); 48 DefaultLanguages.codes.add (language_code); 49 DefaultLanguages.characters.add (characters); 50 } 51 52 /** Add all glyphs for the current locale settings to this glyph range. */ 53 public static void use_default_range (GlyphRange gr) { 54 string language = get_prefered_language_code (); 55 56 if (language == "PRIVATE_USE") { 57 use_private_area (gr); 58 } else if (language.has_prefix ("ja")) { 59 use_default_range_japanese (gr); 60 } else if (language.has_prefix ("la")) { 61 use_default_range_latin (gr); 62 } else if (language.has_prefix ("zh")) { // TODO: not just simplified chinese 63 use_default_range_chinese (gr); 64 } else { 65 use_default_range_alphabetic (gr); 66 } 67 68 gr.name = "Default"; 69 } 70 71 private static string get_prefered_language_code () { 72 string prefered_language; 73 string[] languages = Intl.get_language_names (); 74 75 prefered_language = Preferences.get ("language"); 76 77 if (prefered_language != "") { 78 return prefered_language; 79 } 80 81 if (languages.length == 0) { 82 return ""; 83 } 84 85 return languages[0]; 86 } 87 88 public static string get_characters_for_prefered_language () { 89 string lang = get_prefered_language_code (); 90 int i = 0; 91 string characters = ""; 92 93 foreach (string code in DefaultLanguages.codes) { 94 if (lang.has_prefix (code)) { 95 characters = DefaultLanguages.characters.get (i); 96 } 97 i++; 98 } 99 100 return characters; 101 } 102 103 private static void use_private_area (GlyphRange gr) { 104 gr.add_range (0xe000, 0xf8ff); 105 } 106 107 private static void use_default_range_alphabetic (GlyphRange gr) { 108 string lower_case, upper_case; 109 string all_characters; 110 111 all_characters = get_characters_for_prefered_language (); 112 113 try { 114 if (all_characters != "") { 115 gr.parse_ranges (all_characters); 116 } else { 117 /// All lower case letters in alphabetic order separated by space. Letters with diacritical marks should be included. 118 lower_case = t_("a b c d e f g h i j k l m n o p q r s t u v w x y z"); 119 120 /// All upper case letters in alphabetic order separated by space. Letters with diacritical marks should be included. 121 upper_case = t_("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"); 122 123 gr.parse_ranges (lower_case); 124 gr.parse_ranges (upper_case); 125 } 126 } catch (GLib.Error e) { 127 warning (e.message); 128 } 129 130 gr.add_range ('0', '9'); 131 132 gr.add_single (' '); // TODO: add all spaces here. 133 134 gr.add_single ('.'); 135 gr.add_single ('?'); 136 137 gr.add_single (','); 138 139 gr.add_single ('’'); 140 141 gr.add_range ('“', '”'); 142 143 gr.add_single ('&'); 144 145 gr.add_range (':', ';'); 146 147 gr.add_single ('/'); 148 149 gr.add_range ('!', '/'); 150 151 gr.add_single ('-'); 152 gr.add_range ('‐', '—'); 153 gr.add_range ('<', '@'); 154 gr.add_range ('(', ')'); 155 } 156 157 public static void use_default_range_japanese (GlyphRange gr) { 158 // hiragana 159 gr.add_range ('ぁ', 'ゖ'); 160 gr.add_range ('゙', 'ゟ'); 161 162 // halfwidth and fullwidth forms 163 gr.add_range ('!', 'ᄒ'); 164 gr.add_range ('ᅡ', 'ᅦ'); 165 gr.add_range ('ᅧ', 'ᅬ'); 166 gr.add_range ('ᅭ', 'ᅲ'); 167 gr.add_range ('ᅳ', 'ᅵ'); 168 gr.add_range ('¢', '₩'); 169 gr.add_range ('│', '○'); 170 171 // katakana phonetic extensions 172 gr.add_range ('ㇰ', 'ㇿ'); 173 174 // kana supplement 175 gr.add_single ('𛀀'); 176 gr.add_single ('𛀁'); 177 178 // kanbun 179 gr.add_range ('㆐', '㆟'); 180 } 181 182 public static void use_default_range_chinese (GlyphRange gr) { 183 string pinyin_tones; 184 185 // pinyin 186 pinyin_tones = "ˇ ˉ ˊ ˋ ˙ ā á ǎ à ō ó ǒ ò ē é ě è ī í ǐ ì ū ú ǔ ù ǖ ǘ ǚ ǜ ü Ā Á Ǎ À Ō Ó Ǒ Ò Ē É Ě È"; 187 gr.add_range ('a', 'z'); 188 189 foreach (string c in pinyin_tones.split (" ")) { 190 gr.add_single (c.get_char ()); 191 } 192 193 // CJK punctuations and symbols 194 gr.add_range (' ', '々'); 195 gr.add_range ('〇', '】'); 196 gr.add_range ('〓', '〟'); 197 gr.add_range ('︐', '︙'); 198 199 // CJK numbers and months 200 gr.add_range ('0', '9'); 201 gr.add_range ('㈠', '㈩'); 202 gr.add_range ('㋀', '㋋'); 203 gr.add_range ('㉑', '㉟'); 204 gr.add_range ('㊱', '㊿'); 205 gr.add_range ('㊀', '㊉'); 206 207 // CJK fullwidth letters and symbols 208 gr.add_range ('!', '・'); 209 gr.add_range ('¢', '₩'); 210 gr.add_single ('│'); 211 212 // CJK special characters 213 gr.add_range ('㍘', '㏿'); 214 gr.add_range ('㋌', '㋏'); 215 216 // CJK strokes 217 gr.add_range ('㇀', '㇢'); 218 219 // CJK supplements 220 gr.add_range ('⺀', '⺙'); 221 gr.add_range ('⺛', '⻳'); 222 223 // GB2312 (punctuations) 224 gr.add_single ('―'); 225 gr.add_single ('¤'); 226 gr.add_single ('§'); 227 gr.add_single ('¨'); 228 gr.add_single ('°'); 229 gr.add_single ('±'); 230 gr.add_single ('×'); 231 gr.add_single ('÷'); 232 233 // GB2312 (greek letters) 234 gr.add_range ('Α', 'Ω'); 235 gr.add_range ('α', 'ω'); 236 237 // GB2312 (cyrillic letters) 238 gr.add_range ('А', 'я'); 239 gr.add_single ('ё'); 240 gr.add_single ('Ё'); 241 242 // GB2312 (U+4e00 to U+fa20) 243 gr.add_range ('一', '龥'); 244 gr.add_single ('郎'); 245 gr.add_single ('凉'); 246 gr.add_single ('秊'); 247 gr.add_single ('裏'); 248 gr.add_single ('隣'); 249 250 gr.add_range ('兀', '﨏'); 251 gr.add_single ('﨑'); 252 gr.add_single ('﨓'); 253 gr.add_single ('﨔'); 254 gr.add_single ('礼'); 255 gr.add_single ('﨟'); 256 gr.add_single ('蘒'); 257 gr.add_single ('﨡'); 258 gr.add_single ('﨣'); 259 gr.add_single ('﨤'); 260 gr.add_single ('﨧'); 261 gr.add_single ('﨨'); 262 gr.add_single ('﨩'); 263 } 264 265 public static void use_default_range_latin (GlyphRange gr) { 266 gr.add_range ('!', '~'); 267 gr.add_range ('¡', 'ʯ'); 268 } 269 270 public static void use_full_unicode_range (GlyphRange gr) { 271 CharDatabase.get_full_unicode (gr); 272 273 if (gr.get_length () == 0) { 274 try { 275 gr.parse_ranges ("null-ͷ ͺ-; ΄-Ί Ό Ύ-Ρ Σ-ԧ Ա-Ֆ ՙ-՟ ա-և ։-֊ ֏ ֑-ׇ א-ת װ-״ ؀-؄ ؆-؛ ؞-܍ ܏-݊ ݍ-ޱ ߀-ߺ ࠀ-࠭ ࠰-࠾ ࡀ-࡛ ࡞ ࢠ ࢢ-ࢬ ࣤ-ࣾ ऀ-ॷ ॹ-ॿ ঁ-ঃ অ-ঌ এ-ঐ ও-ন প-র ল শ-হ ়-ৄ ে-ৈ ো-ৎ ৗ ড়-ঢ় য়-৻ ਁ-ਃ ਅ-ਊ ਏ-ਐ ਓ-ਨ ਪ-ਰ ਲ-ਲ਼ ਵ-ਸ਼ ਸ-ਹ ਼ ਾ-ੂ ੇ-ੈ ੋ-੍ ੑ ਖ਼-ੜ ਫ਼ ੤-ੵ ઁ-ઃ અ-ઍ એ-ઑ ઓ-ન પ-ર લ-ળ વ-હ ઼-ૅ ે-ૉ ો-્ ૐ ૠ-૱ ଁ-ଃ ଅ-ଌ ଏ-ଐ ଓ-ନ ପ-ର ଲ-ଳ ଵ-ହ ଼-ୄ େ-ୈ ୋ-୍ ୖ-ୗ ଡ଼-ଢ଼ ୟ-୷ ஂ-ஃ அ-ஊ எ-ஐ ஒ-க ங-ச ஜ ஞ-ட ண-த ந-ப ம-ஹ ா-ூ ெ-ை ொ-் ௐ ௗ ௤-௺ ఁ-ః అ-ఌ ఎ-ఐ ఒ-న ప-ళ వ-హ ఽ-ౄ ె-ై ొ-్ ౕ-ౖ ౘ-ౙ ౠ-౯ ౸-౿ ಂ-ಃ ಅ-ಌ ಎ-ಐ ಒ-ನ ಪ-ಳ ವ-ಹ ಼-ೄ ೆ-ೈ ೊ-್ ೕ-ೖ ೞ ೠ-೯ ೱ-ೲ ം-ഃ അ-ഌ എ-ഐ ഒ-ഺ ഽ-ൄ െ-ൈ ൊ-ൎ ൗ ൠ-൵ ൹-ൿ ං-ඃ අ-ඖ ක-න ඳ-ර ල ව-ෆ ් ා-ු ූ ෘ-ෟ ෲ-෴ ก-ฺ ฿-๛ ກ-ຂ ຄ ງ-ຈ ຊ ຍ ດ-ທ ນ-ຟ ມ-ຣ ລ ວ ສ-ຫ ອ-ູ ົ-ຽ ເ-ໄ ໆ ່-ໍ ໐-໙ ໜ-ໟ ༀ-ཇ ཉ-ཬ ཱ-ྗ ྙ-ྼ ྾-࿌ ࿎-࿚ က-Ⴥ Ⴧ Ⴭ ა-ቈ ቊ-ቍ ቐ-ቖ ቘ ቚ-ቝ በ-ኈ ኊ-ኍ ነ-ኰ ኲ-ኵ ኸ-ኾ ዀ ዂ-ዅ ወ-ዖ ዘ-ጐ ጒ-ጕ ጘ-ፚ ፝-፼ ᎀ-᎙ Ꭰ-Ᏼ ᐀-᚜ ᚠ-ᛰ ᜀ-ᜌ ᜎ-᜔ ᜠ-᜶ ᝀ-ᝓ ᝠ-ᝬ ᝮ-ᝰ ᝲ-ᝳ ក-៝ ០-៩ ៰-៹ ᠀-᠎ ᠐-᠙ ᠠ-ᡷ ᢀ-ᢪ ᢰ-ᣵ ᤀ-ᤜ ᤠ-ᤫ ᤰ-᤻ ᥀ ᥄-ᥭ ᥰ-ᥴ ᦀ-ᦫ ᦰ-ᧉ ᧐-᧚ ᧞-ᨛ ᨞-ᩞ ᩠-᩼ ᩿-᪉ ᪐-᪙ ᪠-᪭ ᬀ-ᭋ ᭐-᭼ ᮀ-᯳ ᯼-᰷ ᰻-᱉ ᱍ-᱿ ᳀-᳇ ᳐-ᳶ ᴀ-ᷦ ᷼-ἕ Ἐ-Ἕ ἠ-ὅ Ὀ-Ὅ ὐ-ὗ Ὑ Ὓ Ὕ Ὗ-ώ ᾀ-ᾴ ᾶ-ῄ ῆ-ΐ ῖ-Ί ῝-` ῲ-ῴ ῶ-῾  -⁤ -₎ ₐ-ₜ ₠-₹ ⃐-⃰ ℀-↉ ←-⏳ ␀-␦ ⑀-⑊ ①-⛿ ✁-⭌ ⭐-⭙ Ⰰ-Ⱞ ⰰ-ⱞ Ⱡ-ⳳ ⳹-ⴥ ⴧ ⴭ ⴰ-ⵧ ⵯ-⵰ ⵿-ⶖ ⶠ-ⶦ ⶨ-ⶮ ⶰ-ⶶ ⶸ-ⶾ ⷀ-ⷆ ⷈ-ⷎ ⷐ-ⷖ ⷘ-ⷞ ⷠ-⸻ ⺀-⺙ ⺛-⻳ ⼀-⿕ ⿰-⿻  -〿 ぁ-ゖ ゙-ヿ ㄅ-ㄭ ㄱ-ㆎ ㆐-ㆺ ㇀-㇣ ㇰ-㈞ ㈠-㋾ ㌀-㏿ ䷀-䷿ ꀀ-ꒌ ꒐-꓆ ꓐ-ꘫ Ꙁ-ꚗ ꚟ-꛷ ꜀-ꞎ Ꞑ-ꞓ Ꞡ-Ɦ ꟸ-꠫ ꠰-꠹ ꡀ-꡷ ꢀ-꣄ ꣎-꣙ ꣠-ꣻ ꤀-꥓ ꥟-ꥼ ꦀ-꧍ ꧏ-꧙ ꧞-꧟ ꨀ-ꨶ ꩀ-ꩍ ꩐-꩙ ꩜-ꩻ ꪀ-ꫂ ꫛ-꫶ ꬁ-ꬆ ꬉ-ꬎ ꬑ-ꬖ ꬠ-ꬦ ꬨ-ꬮ ꯀ-꯭ ꯰-꯹ ힰ-ퟆ ퟋ-ퟻ 豈-舘 並-龎 ff-st ﬓ-ﬗ יִ-זּ טּ-לּ מּ נּ-סּ ףּ-פּ צּ-﯁ ﯓ-﴿ ﵐ-ﶏ ﶒ-ﷇ ﷰ-﷽ ︀-︙ ︠-︦ ︰-﹒ ﹔-﹦ ﹨-﹫ ﹰ-ﹴ ﹶ-ﻼ !-ᄒ ᅡ-ᅦ ᅧ-ᅬ ᅭ-ᅲ ᅳ-ᅵ ¢-₩ │-○ -� 𐀀-𐀋 𐀍-𐀦 𐀨-𐀺 𐀼-𐀽 𐀿-𐁍 𐁐-𐁝 𐂀-𐃺 𐄀-𐄂 𐄇-𐄳 𐄷-𐆊 𐆐-𐆛 𐇐-𐇽 𐊀-𐊜 𐊠-𐋐 𐌀-𐌞 𐌠-𐌣 𐌰-𐍊 𐎀-𐎝 𐎟-𐏃 𐏈-𐏕 𐐀-𐒝 𐒠-𐒩 𐠀-𐠅 𐠈 𐠊-𐠵 𐠷-𐠸 𐠼 𐠿-𐡕 𐡗-𐡟 𐤀-𐤛 𐤟-𐤹 𐤿 𐦀-𐦷 𐦾-𐦿 𐨀-𐨃 𐨅-𐨆 𐨌-𐨓 𐨕-𐨗 𐨙-𐨳 𐨸-𐨺 𐨿-𐩇 𐩐-𐩘 𐩠-𐩿 𐬀-𐬵 𐬹-𐭕 𐭘-𐭲 𐭸-𐭿 𐰀-𐱈 𐹠-𐹾 𑀀-𑁍 𑁒-𑁯 𑂀-𑃁 𑃐-𑃨 𑃰-𑃹 𑄀-𑄴 𑄶-𑅃 𑆀-𑇈 𑇐-𑇙 𑚀-𑚷 𑛀-𑛉 𒀀-𒍮 𒐀-𒑢 𒑰-𒑳 𓀀-𓐮 𖠀-𖨸 𖼀-𖽄 𖽐-𖽾 𖾏-𖾟 𛀀-𛀁 𝀀-𝃵 𝄀-𝄦 𝄩-𝇝 𝈀-𝉅 𝌀-𝍖 𝍠-𝍱 𝐀-𝚥 𝚨-𝟋 𝟎-𝟿 𞸀-𞸃 𞸅-𞸟 𞸡-𞸢 𞸤 𞸧 𞸩-𞸲 𞸴-𞸷 𞸹 𞸻 𞹂 𞹇 𞹉 𞹋 𞹍-𞹏 𞹑-𞹒 𞹔 𞹗 𞹙 𞹛 𞹝 𞹟 𞹡-𞹢 𞹤 𞹧-𞹪 𞹬-𞹲 𞹴-𞹷 𞹹-𞹼 𞹾 𞺀-𞺉 𞺋-𞺛 𞺡-𞺣 𞺥-𞺩 𞺫-𞺻 𞻰-𞻱 🀀-🀫 🀰-🂓 🂠-🂮 🂱-🂾 🃁-🃏 🃑-🃟 🄀-🄊 🄐-🄮 🄰-🅫 🅰-🆚 🇦-🈂 🈐-🈺 🉀-🉈 🉐-🉑 🌀-🌠 🌰-🌵 🌷-🍼 🎀-🎓 🎠-🏄 🏆-🏊 🏠-🏰 🐀-🐾 👀 👂-📷 📹-📼 🔀-🔽 🕀-🕃 🕐-🕧 🗻-🙀 🙅-🙏 🚀-🛅 🜀-🝳 丽-𪘀 󠀁 󠀠-󠁿 󠄀-󠇯"); 276 } catch (MarkupError e) { 277 warning (e.message); 278 gr.add_range ('\0', (unichar) 0xFFF8); 279 } 280 } 281 282 gr.name = "Unicode"; 283 } 284 285 public static DefaultLanguages get_default_languages () { 286 return languages; 287 } 288 } 289 290 public class DefaultLanguages { 291 public static Gee.ArrayList<string> names; 292 public static Gee.ArrayList<string> codes; 293 public static Gee.ArrayList<string> characters; 294 295 public DefaultLanguages () { 296 names = new Gee.ArrayList<string> (); 297 codes = new Gee.ArrayList<string> (); 298 characters = new Gee.ArrayList<string> (); 299 } 300 301 public string? get_name (int index) { 302 if (0 <= index < names.size) { 303 return names.get (index); 304 } 305 306 return null; 307 } 308 309 public string? get_code (int index) { 310 if (0 <= index < codes.size) { 311 return codes.get (index); 312 } 313 314 return null; 315 } 316 } 317 318 } 319