Clone in a terminal:
Commit 72fa284
Johan Mattsson - 2014-11-17T07:29:32+00:00
72fa28418161f1d204cba89c5b32a4ad450257e4johan.mattsson.m@gmail.com

Set font name and description

Changed files (6)

Diff

diff --git a/libbirdfont/DescriptionDisplay.vala b/libbirdfont/DescriptionDisplay.valaindex bd1f17c..17f202e 100644--- a/libbirdfont/DescriptionDisplay.vala+++ b/libbirdfont/DescriptionDisplay.vala@@ -23,6 +23,14 @@ public class DescriptionDisplay : FontDisplay { 	double content_height = 1; 	WidgetAllocation allocation; 	+	TextArea? keyboard_focus = null;+	+	TextArea postscript_name;+	TextArea name;+	TextArea style;+	TextArea weight;+	TextArea full_name;+	TextArea unique_id; 	TextArea version; 	TextArea description; 	TextArea copyright;@@ -30,28 +38,100 @@ public class DescriptionDisplay : FontDisplay { 	Gee.ArrayList<Widget> widgets; 	 	public DescriptionDisplay () {-		double margin = 20 * MainWindow.units;-		double label_size = 22 * MainWindow.units;+		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> (); 		-		copyright = new TextArea (label_size);+		postscript_name = new LineTextArea (label_size);+		name = new LineTextArea (label_size);+		style = new LineTextArea (label_size);+		weight = new LineTextArea (label_size);+		full_name = new LineTextArea (label_size);+		unique_id = new LineTextArea (label_size);+		version = new LineTextArea (label_size); 		description = new TextArea (label_size);-		version = new TextArea (label_size);+		copyright = new TextArea (label_size); -		widgets.add (new Text (t_("Version"),label_size, label_margin));+		headline = new Headline (t_("Name and Description"));+		headline.margin_bottom = 20 * MainWindow.units;+		widgets.add (headline);++		widgets.add (new Text (t_("PostScript Name"), label_size, label_margin));+		postscript_name.margin_bottom = margin;+		postscript_name.set_text (font.postscript_name);+		postscript_name.text_changed.connect ((t) => {+			font.postscript_name = t;+		});+		widgets.add (postscript_name);+		+		widgets.add (new Text (t_("Name"), label_size, label_margin));+		name.margin_bottom = margin;+		name.set_text (font.name);+		name.text_changed.connect ((t) => {+			font.name = t;+		});+		widgets.add (name);+				+		widgets.add (new Text (t_("Style"), label_size, label_margin));+		style.margin_bottom = margin;+		style.set_text (font.subfamily);+		style.text_changed.connect ((t) => {+			font.subfamily = t;+		});+		widgets.add (style);+		+		widgets.add (new Text (t_("Weight"), label_size, label_margin));+		weight.margin_bottom = margin;+		weight.set_text (font.get_weight ());+		weight.text_changed.connect ((t) => {+			font.set_weight (t);+		});+		widgets.add (weight);+		+		widgets.add (new Text (t_("Full Name (Name and Style)"), label_size, label_margin));+		full_name.margin_bottom = margin;+		full_name.set_text (font.full_name);+		full_name.text_changed.connect ((t) => {+			font.full_name = t;+		});+		widgets.add (full_name);+		+		widgets.add (new Text (t_("Unique Identifier"), label_size, label_margin));+		unique_id.margin_bottom = margin;+		unique_id.set_text (font.unique_identifier);+		unique_id.text_changed.connect ((t) => {+			font.unique_identifier = t;+		});+		widgets.add (unique_id);+		+		widgets.add (new Text (t_("Version"), label_size, label_margin)); 		version.margin_bottom = margin;+		version.set_text (font.version);+		version.text_changed.connect ((t) => {+			font.version = t;+		}); 		widgets.add (version);  		widgets.add (new Text (t_("Description"), label_size, label_margin)); 		description.margin_bottom = margin;+		description.set_text (font.description);+		description.text_changed.connect ((t) => {+			font.description = t;+		}); 		widgets.add (description); 		 		widgets.add (new Text (t_("Copyright"), label_size, label_margin)); 		copyright.margin_bottom = margin;+		copyright.set_text (font.copyright);+		copyright.text_changed.connect ((t) => {+			font.copyright = t;+		}); 		widgets.add (copyright); 	} @@ -72,7 +152,7 @@ public class DescriptionDisplay : FontDisplay { 		foreach (Widget w in widgets) { 			if (w is Text) { 				cr.save ();-				cr.set_source_rgba (101 / 255.0, 108 / 255.0, 116 / 255.0, 1);+				cr.set_source_rgba (0, 0, 0, 1); 				w.draw (cr); 				cr.restore (); 			} else {@@ -82,34 +162,49 @@ public class DescriptionDisplay : FontDisplay { 	}	  	void layout () {-		double y = 10 * MainWindow.units;+		double y = -scroll; 		double margin; 		 		foreach (Widget w in widgets) { 			w.widget_x = 17 * MainWindow.units; 			w.widget_y = y;+			w.allocation = allocation; 			y += w.get_height () + w.margin_bottom; 		}+		+		content_height = y + scroll;+		update_scrollbar (); 	}  	public override void key_press (uint keyval) { 		unichar c = (unichar) keyval; 		string s;+		TextArea focus; 		+		if (keyboard_focus == null) {+			return;+		}+		+		focus = (!) keyboard_focus; 		if (!KeyBindings.has_alt () && !KeyBindings.has_ctrl ()) { 			switch (c) {+				case Key.RIGHT:+					focus.move_carret_next ();+					break;+				case Key.LEFT:+					focus.move_carret_previous ();+					break; 				case Key.BACK_SPACE:-					copyright.remove_last_character ();+					focus.remove_last_character (); 					break; 				case Key.ENTER:-					copyright.insert_text ("\n");+					focus.insert_text ("\n"); 					break;				 				default: 					if (!is_modifier_key (keyval)) { 						s = (!) c.to_string ();-						if (s.validate ()) {-							copyright.insert_text (s);-							print (@"keyval: $keyval\n");+						if (s.validate () && keyboard_focus != null) {+							focus.insert_text (s); 						} 					} 					break;@@ -120,6 +215,26 @@ public class DescriptionDisplay : FontDisplay { 	}  	public override void button_press (uint button, double x, double y) {+		TextArea t;+		+		if (keyboard_focus != null) {+			t = (!) keyboard_focus;+			t.draw_carret = false;+			keyboard_focus = null;+		}+		+		foreach (Widget w in widgets) {+			if (w.widget_x <= x <= w.widget_x + w.get_width ()+				&&  w.widget_y <= y <= w.widget_y + w.get_height ()) {+				+				if (w is TextArea) {+					t = (TextArea) w;+					t.draw_carret = true;+					keyboard_focus = t;+				}+			}+		}+ 		GlyphCanvas.redraw (); 	} 	diff --git a/libbirdfont/Headline.vala b/libbirdfont/Headline.valanew file mode 100644index 0000000..7537868--- /dev/null+++ b/libbirdfont/Headline.vala@@ -0,0 +1,52 @@+/*+    Copyright (C) 2014 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 {++class Headline : Widget {	++	Text label;+	+	public Headline (string text) {+		label = new Text ();+		label.set_text (text);+	}+	+	public override void draw (Context cr) {+		cr.save ();+		cr.set_source_rgba (101 / 255.0, 108 / 255.0, 116 / 255.0, 1);+		cr.rectangle (0, widget_y, allocation.width, 40 * MainWindow.units);+		cr.fill ();+		cr.restore ();+			+		cr.save ();+		cr.set_source_rgba (1, 1, 1, 1);+		label.set_font_size (20 * MainWindow.units);+		label.draw_at_baseline (cr, 21 * MainWindow.units, widget_y + 25 * MainWindow.units);+		cr.restore ();+	}+	+	public override double get_height () {+		return 40 * MainWindow.units;+	}+	+	public override double get_width () {+		return allocation.width;+	}	+}++}diff --git a/libbirdfont/Renderer/LineTextArea.vala b/libbirdfont/Renderer/LineTextArea.valanew file mode 100644index 0000000..15c7221--- /dev/null+++ b/libbirdfont/Renderer/LineTextArea.vala@@ -0,0 +1,31 @@+/*+    Copyright (C) 2014 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 LineTextArea : TextArea {++	public LineTextArea (double size) {+		base (size);+		single_line = true;+		min_height = size;+		height = min_height;+		layout ();+	}+}++}diff --git a/libbirdfont/Renderer/TextArea.vala b/libbirdfont/Renderer/TextArea.valaindex a12be9c..549b8f8 100644--- a/libbirdfont/Renderer/TextArea.vala+++ b/libbirdfont/Renderer/TextArea.vala@@ -22,13 +22,14 @@ public class TextArea : Widget { 	public double min_width = 500; 	public double min_height = 100; 	public double font_size;-	public double margin = 10;+	public double padding = 3.3;+	public bool single_line = false; 	-	public bool draw_carret = true;+	public bool draw_carret = false; 	public bool draw_border = true; -	private double width;-	private double height;+	public double width;+	public double height;  	int carret = 0; 	string text = "";@@ -36,6 +37,8 @@ public class TextArea : Widget { 		 	int iter_pos; 	+	public signal void text_changed (string text);+	 	public TextArea (double font_size = 14) { 		this.font_size = font_size; 		width = min_width;@@ -55,10 +58,19 @@ public class TextArea : Widget { 	} 	 	public void set_text (string t) {-		int tl = t.length;-		text = t;+		int tl;+		+		if (single_line) {+			text = t.replace ("\n", "").replace ("\r", "");+		} else {+			text = t;+		}+		+		tl = t.length; 		carret = tl; 		text_length += tl;+		+		text_changed (text); 	} 	 	public void remove_last_character () {@@ -73,33 +85,66 @@ public class TextArea : Widget { 		set_text (text.substring (0, last_index) + text.substring (carret)); 	} 	+	public void move_carret_next () {+		int index = 0;+		unichar c;+		char* s = (char*) text + carret;+		string n = (string) s;+		+		n.get_next_char (ref index, out c);+		carret += index;+	}++	public void move_carret_previous () {+		int last_index = 0;+		int index = 0;+		unichar c;+		+		while (text.get_next_char (ref index, out c) && index < carret) {+			last_index = index;+		}+		+		carret = last_index;+	}+		 	public void insert_text (string t) {+		string s;+		+		if (single_line) {+			s = t.replace ("\n", "").replace ("\r", "");+		} else {+			s = t;+		}+ 		string nt = text.substring (0, carret);-		int tl = t.length;-		nt += t;+		int tl = s.length;+		nt += s; 		nt += text.substring (carret); 		carret += tl; 		text = nt; 		text_length += tl;+		+		text_changed (text); 	} 	-	void layout () {+	public void layout () { 		Text word; 		double p; 		double tx, ty; 		string w; 		double xmax = 0;-		double width = this.width - 2 * margin;-		double height = this.height - 2 * margin;+		double width = this.width - 2 * padding;+		double height = this.height - 2 * padding;+		bool carret_visibility; 		 		iter_pos = 0; 		word = new Text (); 		word.set_font_size (font_size); 		 		tx = 0;-		ty = 0;+		ty = font_size; 		while (iter_pos < text_length) {-			w = get_next_word (out draw_carret);+			w = get_next_word (out carret_visibility); 			 			if (w == "") { 				break;@@ -109,9 +154,11 @@ public class TextArea : Widget { 			 			p = word.get_sidebearing_extent (); 			-			if (tx + p > width || w == "\n") {-				tx = 0;-				ty += font_size;+			if (!single_line) {+				if (tx + p > width || w == "\n") {+					tx = 0;+					ty += font_size;+				} 			} 			 			if (w != "\n") {@@ -126,10 +173,10 @@ public class TextArea : Widget { 		this.width = min_width; 		 		if (xmax > width) {-			this.width = xmax + 2 * margin;+			this.width = xmax + 2 * padding; 		} 		-		this.height = fmax (min_height, ty + 2 * margin);+		this.height = fmax (min_height, ty + 2 * padding); 	} 	 	public override void draw (Context cr) {@@ -137,8 +184,7 @@ public class TextArea : Widget { 		double p; 		double tx, ty; 		string w;-		bool draw_carret;-		double carret_y;+		bool carret_at_end_of_word; 		double scale; 		double width; 		double x = widget_x;@@ -150,7 +196,7 @@ public class TextArea : Widget { 			// background 			cr.save (); 			cr.set_line_width (1);-			cr.set_source_rgba (1, 1, 1, 1);+			cr.set_source_rgba (101 / 255.0, 108 / 255.0, 116 / 255.0, 1); 			create_border (cr, x, y); 			cr.fill (); 			cr.restore ();@@ -164,22 +210,24 @@ public class TextArea : Widget { 		} 		 		cr.save ();-		cr.set_source_rgba (38 / 255.0, 39 / 255.0, 43 / 255.0, 1); 		 		iter_pos = 0; 		word = new Text (); 		-		width = this.width - margin;-		x += margin;+		width = this.width - padding;+		x += padding; 		word.set_font_size (font_size); 		scale = word.get_scale ();-		//y += font_size - scale * (word.font.bottom_limit - word.font.base_line); 		y += font_size; 		+		if (draw_carret && iter_pos == 0 && text_length == 0) {+			draw_carret_at (cr, x + padding, y + padding);+		}+		 		tx = 0; 		ty = 0; 		while (iter_pos < text_length) {-			w = get_next_word (out draw_carret);+			w = get_next_word (out carret_at_end_of_word); 			 			if (w == "") { 				break;@@ -198,37 +246,40 @@ public class TextArea : Widget { 				tx += p; 			} 			-			if (draw_carret && this.draw_carret) {-				cr.save ();-				cr.set_line_width (1);-				carret_y = y + ty;-				carret_y += scale * -word.font.bottom_limit;-				cr.move_to (x + tx, carret_y);-				cr.line_to (x + tx, carret_y - font_size);-				cr.stroke ();-				cr.restore ();+			if (carret_at_end_of_word && draw_carret) {+				draw_carret_at (cr, x + tx, y + ty + scale * - word.font.bottom_limit); 			} 		} 		cr.restore (); 	} 	-	string get_next_word (out bool draw_carret) {+	void draw_carret_at (Context cr, double x, double y) {+		cr.save ();+		cr.set_source_rgba (0, 0, 0, 0.5);+		cr.set_line_width (1);+		cr.move_to (x, y);+		cr.line_to (x, y - font_size);+		cr.stroke ();+		cr.restore ();		+	}+	+	string get_next_word (out bool carret_at_end_of_word) { 		int i; 		int ni; 		int pi; 		string n; 		int nl; 		-		draw_carret = false;+		carret_at_end_of_word = false; 		 		if (iter_pos >= text_length) {-			draw_carret = true;+			carret_at_end_of_word = true; 			return "".dup (); 		} 		 		if (text.get_char (iter_pos) == '\n') { 			iter_pos += "\n".length;-			draw_carret = (iter_pos == carret);+			carret_at_end_of_word = (iter_pos == carret); 			return "\n".dup (); 		} 		@@ -257,20 +308,20 @@ public class TextArea : Widget { 		if (iter_pos < carret < iter_pos + nl) { 			n = text.substring (iter_pos, carret - iter_pos); 			nl = n.length;-			draw_carret = true;+			carret_at_end_of_word = true; 		} 		 		iter_pos += nl; 		 		if (iter_pos == carret) {-			draw_carret = true;+			carret_at_end_of_word = true; 		} 		 		return n; 	}  	void create_border (Context cr, double x, double y) {-		double radius = margin;		+		double radius = padding;		 		double w = width; 		double h = height; 		diff --git a/libbirdfont/SettingsDisplay.vala b/libbirdfont/SettingsDisplay.valaindex e9a8fcb..4dbd091 100644--- a/libbirdfont/SettingsDisplay.vala+++ b/libbirdfont/SettingsDisplay.vala@@ -223,9 +223,7 @@ public class SettingsDisplay : FontDisplay { 		} 	} -	public override void draw (WidgetAllocation allocation, Context cr) {-		Text headline;-		+	public override void draw (WidgetAllocation allocation, Context cr) {		 		this.allocation = allocation; 		 		layout ();diff --git a/libbirdfont/Widget.vala b/libbirdfont/Widget.valaindex 9e8b316..bfddddb 100644--- a/libbirdfont/Widget.vala+++ b/libbirdfont/Widget.vala@@ -26,6 +26,8 @@ public abstract class Widget : GLib.Object { 	public abstract double get_height (); 	public abstract double get_width (); 	public abstract void draw (Context cr);+	+	public WidgetAllocation allocation = new WidgetAllocation (); }  }

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112