Commit e62e53b
Font specific settings file
Changed files (8)
modifiedlibbirdfont/BirdFontFile.valamodifiedlibbirdfont/Font.valaaddedlibbirdfont/FontSettings.valaaddedlibbirdfont/KerningStrings.valaaddedlibbirdfont/KerningStringsTab.valamodifiedlibbirdfont/KerningTools.valamodifiedlibbirdfont/Preferences.valamodifiedlibbirdxml/Xml.vala
Diff
@@ -662,15 +662,15 @@ class BirdFontFile : GLib.Object { } if (t.get_name () == "postscript_name") {- font.postscript_name = XmlParser.parse_escaped_text (t.get_content ());+ font.postscript_name = XmlParser.decode (t.get_content ()); } if (t.get_name () == "name") {- font.name = XmlParser.parse_escaped_text (t.get_content ());+ font.name = XmlParser.decode (t.get_content ()); } if (t.get_name () == "subfamily") {- font.subfamily = XmlParser.parse_escaped_text (t.get_content ());+ font.subfamily = XmlParser.decode (t.get_content ()); } if (t.get_name () == "bold") {@@ -682,23 +682,23 @@ class BirdFontFile : GLib.Object { } if (t.get_name () == "full_name") {- font.full_name = XmlParser.parse_escaped_text (t.get_content ());+ font.full_name = XmlParser.decode (t.get_content ()); } if (t.get_name () == "unique_identifier") {- font.unique_identifier = XmlParser.parse_escaped_text (t.get_content ());+ font.unique_identifier = XmlParser.decode (t.get_content ()); } if (t.get_name () == "version") {- font.version = XmlParser.parse_escaped_text (t.get_content ());+ font.version = XmlParser.decode (t.get_content ()); } if (t.get_name () == "description") {- font.description = XmlParser.parse_escaped_text (t.get_content ());+ font.description = XmlParser.decode (t.get_content ()); } if (t.get_name () == "copyright") {- font.copyright = XmlParser.parse_escaped_text (t.get_content ());+ font.copyright = XmlParser.decode (t.get_content ()); } if (t.get_name () == "kerning") {@@ -101,6 +101,9 @@ public class Font : GLib.Object { public static string? default_license = null; + public FontSettings settings;+ public KerningStrings kerning_strings;+ public Font () { KerningClasses kerning_classes; @@ -135,6 +138,10 @@ public class Font : GLib.Object { ligatures_substitution = new Ligatures (this); background_images = new Gee.ArrayList<BackgroundImage> ();+ + settings = new FontSettings ();+ kerning_strings = new KerningStrings ();+ kerning_strings.load (this); } public static void set_default_license (string license) {@@ -626,6 +633,8 @@ public class Font : GLib.Object { } else { save_bf (); }+ + settings.save (get_file_name ()); } public bool save_bfp () {@@ -763,6 +772,8 @@ public class Font : GLib.Object { font_file = null; } + settings.load (get_file_name ());+ return loaded; } new file mode 100644@@ -0,0 +1,125 @@+/*+ Copyright (C) 2015 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++using Bird;++namespace BirdFont {++/** Font specific settings file. */+public class FontSettings : GLib.Object {++ string font_name;+ Gee.HashMap<string, string> settings;++ public FontSettings () {+ settings = new Gee.HashMap<string, string> ();+ font_name = "";+ }+ + public string get_setting (string key) {+ if (settings.has_key (key)) {+ return settings.get (key);+ }+ + return "";+ }++ public void set_setting (string key, string v) {+ settings.set (key, v);+ save (font_name);+ }+ + File get_settings_file () {+ File config_directory = BirdFont.get_settings_directory ();+ File settings = get_child (config_directory, "settings");+ return get_child (config_directory, font_name.replace (".bf", ".config"));+ }+ + public void save (string font_file_name) {+ File f;+ StringBuilder sb;+ + font_name = font_file_name;+ + try {+ f = get_settings_file ();+ + if (f.query_exists ()) {+ f.delete ();+ }+ + sb = new StringBuilder ();+ + sb.append ("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");+ sb.append ("<settings>");+ + foreach (var k in settings.keys) {+ sb.append ("\t<setting key=\"");+ sb.append (k);+ sb.append (" \" ");+ sb.append ("value=\"");+ sb.append (XmlParser.encode (settings.get (k)));+ sb.append ("\" />");+ }+ + sb.append ("</settings>");+ + FileUtils.set_contents ((!) f.get_path (), sb.str);+ + } catch (Error e) {+ stderr.printf ("Can not save settings. (%s)", e.message); + }+ }+ + public void load (string font_file_name) {+ File f;+ string xml_data;+ XmlParser parser;+ + settings.clear ();+ font_name = font_file_name;+ f = get_settings_file ();+ + if (f.query_exists ()) {+ FileUtils.get_contents((!) f.get_path (), out xml_data);+ parser = new XmlParser (xml_data);+ parse_settings (parser.get_root_tag ()); + }+ }+ + void parse_settings (Tag tag) {+ foreach (Tag t in tag) {+ if (t.get_name () == "setting") {+ parse_setting (t);+ }+ }+ }++ void parse_setting (Tag tag) {+ string key = "";+ string v = "";+ foreach (Attribute a in tag.get_attributes ()) {+ if (a.get_name () == "key") {+ key = a.get_content ();+ }++ if (a.get_name () == "value") {+ v = XmlParser.decode (a.get_content ());+ }+ }+ }++}++}new file mode 100644@@ -0,0 +1,81 @@+/*+ Copyright (C) 2015 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++namespace BirdFont {++public class KerningStrings : GLib.Object {++ Gee.ArrayList<string> kerning_strings = new Gee.ArrayList<string> ();+ int current_position = 0; ++ public KerningStrings () {+ }+ + public string next () {+ string w = "";+ + if (0 <= current_position < kerning_strings.size) {+ w = kerning_strings.get (current_position);+ }+ + current_position++;+ + return w;+ }++ public string previous () {+ string w = "";++ current_position--;+ + if (0 <= current_position < kerning_strings.size) {+ w = kerning_strings.get (current_position);+ }++ return w;+ + }+ + public void load (Font font) {+ string path;+ + path = font.settings.get_setting ("kerning_string_file");+ load_new_string (font, path);+ }+ + public void load_new_string (Font font, string kerning_strings_file) {+ File f;+ string data;+ string[] strings;+ + try {+ kerning_strings.clear ();++ FileUtils.get_contents(kerning_strings_file, out data);+ strings = data.split ("\n");+ + foreach (string s in strings) {+ kerning_strings.add (s.replace ("\r", ""));+ }+ + current_position = int.parse (font.settings.get_setting ("kerning_string_position")); + + font.settings.set_setting ("kerning_string_file", kerning_strings_file);+ } catch (GLib.Error e) {+ warning (e.message);+ }+ }+}++}new file mode 100644@@ -0,0 +1,197 @@+/*+ Copyright (C) 2015 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++using Cairo;+using Math;++namespace BirdFont {++public class KerningStringsTab : FontDisplay {+ + double scroll = 0;+ double content_height = 1;+ WidgetAllocation allocation;+ + TextArea kerning_text;+ Button load_text;+ + Gee.ArrayList<Widget> widgets;+ + public KerningStringsTab () {+ double margin = 12 * MainWindow.units;+ double label_size = 20 * MainWindow.units;+ double label_margin = 4 * MainWindow.units;+ Headline headline;+ Font font = BirdFont.get_current_font ();+ + allocation = new WidgetAllocation ();+ widgets = new Gee.ArrayList<Widget> ();++ headline = new Headline (t_("Kerning Strings"));+ headline.margin_bottom = 20 * MainWindow.units;+ widgets.add (headline);++ kerning_text = new TextArea (label_size);+ kerning_text.margin_bottom = margin;+ kerning_text.set_text (""); // FIXME:+ widgets.add (kerning_text);+ }+ + public override void draw (WidgetAllocation allocation, Context cr) {+ this.allocation = allocation;+ + layout ();+ + // background+ cr.save ();+ cr.rectangle (0, 0, allocation.width, allocation.height);+ cr.set_line_width (0);+ + Theme.color (cr, "Background 4");++ cr.fill ();+ cr.stroke ();+ cr.restore ();++ foreach (Widget w in widgets) {+ if (w.is_on_screen ()) { + if (w is Text) {+ cr.save ();+ Theme.color (cr, "Foreground 1");+ w.draw (cr);+ cr.restore ();+ } else {+ w.draw (cr);+ }+ }+ }+ } ++ void layout () {+ double y = -scroll;+ + foreach (Widget w in widgets) {+ w.widget_x = 17 * MainWindow.units;+ w.widget_y = y;+ w.allocation = allocation;+ + if (w is TextArea) {+ ((TextArea) w).layout ();+ }+ + y += w.get_height () + w.margin_bottom;+ }+ + content_height = y + scroll;+ update_scrollbar ();+ }++ public void scroll_event (double p) {+ scroll += p;+ layout ();+ GlyphCanvas.redraw ();+ }++ public override void key_press (uint keyval) {+ kerning_text.key_press (keyval);+ }+ + public override void button_press (uint button, double x, double y) {+ TextArea t;+ TextArea old;+ + foreach (Widget w in widgets) {+ if (w.is_over (x, y)) {+ if (w is TextArea) {+ t = (TextArea) w;+ if (kerning_text != t) {+ old = (!) kerning_text;+ old.draw_carret = false;+ }+ + t.draw_carret = true;+ t.button_press (button, x, y);+ kerning_text = t;+ }+ }+ }++ GlyphCanvas.redraw ();+ }+ + public override void button_release (int button, double x, double y) {+ TextArea t;+ + kerning_text.button_release (button, x, y);+ + GlyphCanvas.redraw ();+ }++ public override void motion_notify (double x, double y) {+ kerning_text.motion (x, y);+ }++ public override string get_label () {+ return t_("Name and Description");+ }++ public override string get_name () {+ return "Description";+ }++ public override bool has_scrollbar () {+ return true;+ }+ + public override void scroll_wheel_down (double x, double y) {+ scroll += 25 * MainWindow.units;++ if (scroll + allocation.height >= content_height) {+ scroll = content_height - allocation.height;+ }+ + update_scrollbar ();+ GlyphCanvas.redraw ();+ }+ + public override void scroll_wheel_up (double x, double y) {+ scroll -= 25 * MainWindow.units;+ + if (scroll < 0) {+ scroll = 0;+ }+ + update_scrollbar ();+ GlyphCanvas.redraw ();+ }++ public override void selected_canvas () {+ update_scrollbar ();+ GlyphCanvas.redraw ();+ }+ + public override void update_scrollbar () {+ double h = content_height - allocation.height;+ MainWindow.set_scrollbar_size (allocation.height / content_height);+ MainWindow.set_scrollbar_position (scroll / h);+ }++ public override void scroll_to (double percent) {+ double h = content_height - allocation.height;+ scroll = percent * h;+ GlyphCanvas.redraw ();+ }+}++}@@ -1,5 +1,5 @@ /*- Copyright (C) 2013 2014 Johan Mattsson+ Copyright (C) 2013 2014 2015 Johan Mattsson This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -89,6 +89,22 @@ public class KerningTools : ToolCollection { d.insert_unichar (); }); kerning_tools.add_tool (insert_unicode);++ Tool previous_kerning_string = new Tool ("previous_kerning_string", t_("Previous kerning string"));+ previous_kerning_string.select_action.connect ((self) => {+ KerningDisplay d = MainWindow.get_kerning_display ();+ Font f = BirdFont.get_current_font ();+ d.add_text (f.kerning_strings.next ());+ });+ kerning_tools.add_tool (previous_kerning_string);++ Tool next_kerning_string = new Tool ("next_kerning_string", t_("Next kerning string"));+ next_kerning_string.select_action.connect ((self) => {+ KerningDisplay d = MainWindow.get_kerning_display ();+ Font f = BirdFont.get_current_font ();+ d.add_text (f.kerning_strings.previous ()); + });+ kerning_tools.add_tool (next_kerning_string); kerning_tools.set_persistent (false); kerning_tools.set_unique (false);@@ -89,37 +89,21 @@ public class Preferences { } public static void load () {- File app_dir;+ File config_dir; File settings; FileStream? settings_file; unowned FileStream b; string? l; - printd ("get app");- app_dir = BirdFont.get_settings_directory ();- - if (is_null (app_dir)) {- warning ("No app directory.");- return;- }+ config_dir = BirdFont.get_settings_directory ();+ settings = get_child (config_dir, "settings"); - printd ("get settings file");- settings = get_child (app_dir, "settings");-- if (is_null (settings)) {- warning ("No setting directory.");- return;- }-- printd ("create map"); data = new HashMap<string, string> (); - printd ("look at settings"); if (!settings.query_exists ()) { return; }- - printd ("open settings file");+ settings_file = FileStream.open ((!) settings.get_path (), "r"); if (settings_file == null) {@@ -127,7 +111,6 @@ public class Preferences { return; } - printd ("parse settings file"); b = (!) settings_file; l = b.read_line (); while ((l = b.read_line ())!= null) {@@ -157,10 +140,10 @@ public class Preferences { public static void save () { try {- File app_dir = BirdFont.get_settings_directory ();- File settings = get_child (app_dir, "settings");+ File config_dir = BirdFont.get_settings_directory ();+ File settings = get_child (config_dir, "settings"); - return_if_fail (app_dir.query_exists ());+ return_if_fail (config_dir.query_exists ()); if (settings.query_exists ()) { settings.delete ();@@ -189,7 +189,7 @@ public class XmlParser : GLib.Object { /** Replace escaped character with plain text characters. * & will be replaced with & etc. */- public static string parse_escaped_text (string s) {+ public static string decode (string s) { string t; t = s.replace (""", "\""); t = t.replace ("';", "'");@@ -198,6 +198,17 @@ public class XmlParser : GLib.Object { t = t.replace ("&", "&"); return t; }++ public static string encode (string s) {+ string t;+ t = s.replace ("\"", """);+ t = t.replace ("'", "';");+ t = t.replace ("<", "<");+ t = t.replace (">", ">");+ t = t.replace ("&", "&");+ return t;+ }+ } }