The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Theme.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/Theme.vala.
Store order for theme colors in setting tab
1 /* 2 Copyright (C) 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 Bird; 16 using Cairo; 17 18 namespace BirdFont { 19 20 public class Theme : GLib.Object { 21 22 static Gee.HashMap<string, Color> colors; 23 public static Gee.ArrayList<string> color_list; 24 25 public static void text_color (Text text, string name) { 26 Color c; 27 28 if (unlikely (!colors.has_key (name))) { 29 warning (@"Theme does not have a color for $name"); 30 return; 31 } 32 33 c = colors.get (name); 34 text.set_source_rgba (c.r, c.g, c.b, c.a); 35 } 36 37 public static void color (Context cr, string name) { 38 Color c; 39 40 if (unlikely (!colors.has_key (name))) { 41 warning (@"Theme does not have a color for $name"); 42 return; 43 } 44 45 c = colors.get (name); 46 cr.set_source_rgba (c.r, c.g, c.b, c.a); 47 } 48 49 public static void color_opacity (Context cr, string name, double opacity) { 50 Color c; 51 52 if (unlikely (!colors.has_key (name))) { 53 warning (@"Theme does not have a color for $name"); 54 return; 55 } 56 57 c = colors.get (name); 58 cr.set_source_rgba (c.r, c.g, c.b, opacity); 59 } 60 61 public static void text_color_opacity (Text text, string name, double opacity) { 62 Color c; 63 64 if (unlikely (!colors.has_key (name))) { 65 warning (@"Theme does not have a color for $name"); 66 return; 67 } 68 69 c = colors.get (name); 70 text.set_source_rgba (c.r, c.g, c.b, opacity); 71 } 72 public static Color get_color (string name) { 73 Color c; 74 75 if (unlikely (!colors.has_key (name))) { 76 warning (@"Theme does not have a color for $name"); 77 return new Color (0, 0, 0, 1); 78 } 79 80 return colors.get (name); 81 } 82 83 public static void save_color (string name, double r, double g, double b, double a) { 84 colors.set (name, new Color (r, g, b, a)); 85 write_theme (); 86 } 87 88 public static void load_theme () { 89 File default_theme = SearchPaths.find_file (null, "theme.xml"); 90 File user_theme = get_child (BirdFont.get_settings_directory (), "theme.xml"); 91 92 colors = new Gee.HashMap<string, Color> (); 93 color_list = new Gee.ArrayList<string> (); 94 95 if (default_theme.query_exists ()) { 96 parse_theme (default_theme); 97 } 98 99 if (user_theme.query_exists ()) { 100 parse_theme (user_theme); 101 } 102 103 color_list.sort (); 104 } 105 106 public static void write_theme () { 107 DataOutputStream os; 108 File file; 109 110 file = get_child (BirdFont.get_settings_directory (), "theme.xml"); 111 112 try { 113 if (file.query_exists ()) { 114 file.delete (); 115 } 116 } catch (GLib.Error e) { 117 warning (e.message); 118 } 119 120 try { 121 os = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION)); 122 os.put_string ("""<?xml version="1.0" encoding="utf-8" standalone="yes"?>"""); 123 os.put_string ("\n"); 124 125 os.put_string ("<theme>\n"); 126 foreach (string name in colors.keys) { 127 Color color = colors.get (name); 128 129 os.put_string ("\t<color "); 130 131 os.put_string (@"name=\"$(Markup.escape_text (name))\" "); 132 os.put_string (@"red=\"$(color.r)\" "); 133 os.put_string (@"green=\"$(color.g)\" "); 134 os.put_string (@"blue=\"$(color.b)\" "); 135 os.put_string (@"alpha=\"$(color.a)\""); 136 137 os.put_string ("/>\n"); 138 } 139 os.put_string ("</theme>\n"); 140 141 os.close (); 142 } catch (GLib.Error e) { 143 warning (e.message); 144 } 145 } 146 147 static void parse_theme (File f) { 148 string xml_data; 149 XmlParser parser; 150 151 try { 152 FileUtils.get_contents((!) f.get_path (), out xml_data); 153 parser = new XmlParser (xml_data); 154 parse_colors (parser.get_root_tag ()); 155 } catch (GLib.Error e) { 156 warning (e.message); 157 } 158 } 159 160 static void parse_colors (Tag tag) { 161 foreach (Tag t in tag) { 162 if (t.get_name () == "color") { 163 parse_color (t.get_attributes ()); 164 } 165 } 166 } 167 168 static void parse_color (Attributes attributes) { 169 string name = ""; 170 double r = 0; 171 double g = 0; 172 double b = 0; 173 double a = 1; 174 175 foreach (Attribute attr in attributes) { 176 if (attr.get_name () == "name") { 177 name = attr.get_content (); 178 } 179 180 if (attr.get_name () == "red") { 181 r = double.parse (attr.get_content ()); 182 } 183 184 if (attr.get_name () == "green") { 185 g = double.parse (attr.get_content ()); 186 } 187 188 if (attr.get_name () == "blue") { 189 b = double.parse (attr.get_content ()); 190 } 191 192 if (attr.get_name () == "alpha") { 193 a = double.parse (attr.get_content ()); 194 } 195 } 196 197 color_list.add (name); 198 colors.set (name, new Color (r, g, b, a)); 199 } 200 } 201 202 } 203