Clone in a terminal:
Commit b5f1a47
Johan Mattsson - 2015-09-18T18:11:10+00:00
b5f1a47e0d8176f345ba7d82167256d92369f58ejohan.mattsson.m@gmail.com

Create fontconfig cache in a background thread

Changed files (6)

Diff

diff --git a/birdfont/GtkWindow.vala b/birdfont/GtkWindow.valaindex 2e4f048..10b93ee 100644--- a/birdfont/GtkWindow.vala+++ b/birdfont/GtkWindow.vala@@ -483,6 +483,16 @@ public class GtkWindow : Gtk.Window, NativeWindow { 		} 	} +	public void run_non_blocking_background_thread (Task t) {+		unowned Thread<void*> bg;+		+		try {+			bg = Thread.create<void> (t.perform_task, true);+		} catch (GLib.Error e) {+			warning (e.message);+		}+	}+	 	public void* background_thread () {	 		background_task.run (); 		MenuTab.stop_background_thread ();diff --git a/libbirdfont/NativeWindow.vala b/libbirdfont/NativeWindow.valaindex a8b2671..c690814 100644--- a/libbirdfont/NativeWindow.vala+++ b/libbirdfont/NativeWindow.vala@@ -51,9 +51,12 @@ public interface NativeWindow : GLib.Object { 	/** Load images in a background thread. */ 	public abstract void load_background_image (); -	/** Load images in a background thread. */+	/** Run a background thread and block the gui. */ 	public abstract void run_background_thread (Task t); 	+	/** Run a background thread without blocking the gui. */+	public abstract void run_non_blocking_background_thread (Task t);+	 	/** Copy text to clipboard. */ 	public abstract void set_clipboard_text (string text); 	diff --git a/libbirdfont/Renderer/FallbackFont.vala b/libbirdfont/Renderer/FallbackFont.valaindex ec2bd08..6192e3e 100644--- a/libbirdfont/Renderer/FallbackFont.vala+++ b/libbirdfont/Renderer/FallbackFont.vala@@ -36,6 +36,7 @@ public class FallbackFont : GLib.Object { 	 	FcConfig* font_config = null; 	FontFace* default_font = null;+	static bool font_config_stated = false; 	 	string default_font_file_name = "Roboto-Regular.ttf"; 	string default_font_family_name = "Roboto";@@ -49,8 +50,20 @@ public class FallbackFont : GLib.Object { 		string home = Environment.get_home_dir (); 		 		font_directories = new Gee.ArrayList<File> ();-		font_config = FcInitLoadConfigAndFonts (); +		if (!font_config_stated) {+			font_config_stated = true;+			+			IdleSource idle = new IdleSource ();+			idle.set_callback (() => {+				Task t = new Task ();+				t.task.connect (init_font_config);+				MainWindow.native_window.run_non_blocking_background_thread (t);+				return false;+			});+			idle.attach (null);+		}+		 		add_font_folder ("/usr/share/fonts/"); 		add_font_folder ("/usr/local/share/fonts/"); 		add_font_folder (home + "/.local/share/fonts");@@ -74,6 +87,17 @@ public class FallbackFont : GLib.Object { 		} 	} +	public void init_font_config () {+		FcConfig* fc = FcInitLoadConfigAndFonts ();+		IdleSource idle = new IdleSource ();+		+		idle.set_callback (() => {+			font_config = fc;+			return false;+		});+		idle.attach (null);+	}+ 	public Font get_single_glyph_font (unichar c) { 		Font f; 		unichar last; diff --git a/libbirdfont/Renderer/fontconfig.c b/libbirdfont/Renderer/fontconfig.cindex 7281410..7cb6304 100644--- a/libbirdfont/Renderer/fontconfig.c+++ b/libbirdfont/Renderer/fontconfig.c@@ -30,6 +30,11 @@ gchar* find_font (FcConfig* fontconfig, const gchar* characters) { 	gchar* remaining_characters; 	gunichar character; 	+	if (fontconfig == NULL) {+		g_warning("Font config not loaded.");+		return NULL;+	}+	 	result = NULL; 	pattern = FcPatternCreate (); 	@@ -88,6 +93,11 @@ gchar* find_font_file (FcConfig* font_config, const gchar* font_name) { 	FcObjectSet* font_properties; 	FcFontSet* fonts; 	int i;++	if (font_config == NULL) {+		g_warning("Font config not loaded.");+		return NULL;+	} 	 	path = NULL; 	name = font_name;diff --git a/libbirdfont/Task.vala b/libbirdfont/Task.valaindex 0465bab..03b80cf 100644--- a/libbirdfont/Task.vala+++ b/libbirdfont/Task.vala@@ -24,6 +24,11 @@ public class Task : GLib.Object { 	public void run () { 		task (); 	}++	public void* perform_task() {+		run ();+		return null;+	} }  }diff --git a/scripts/builder.py b/scripts/builder.pyindex 8ee31ac..a02913a 100644--- a/scripts/builder.py+++ b/scripts/builder.py@@ -66,12 +66,12 @@ class Builder(object):             'basename': 'mkdir ' + build_directory,             'actions': ['mkdir -p ' + path.join('build', 'bin'),                         'mkdir -p ' + build_directory, -                        'touch ' + build_file],+                        '[ -e "' + build_file + '" ] || touch "' + build_file + '"'],             'targets': [build_file],         }                  copied_csources = []-        copied_cheader = []+        copied_cheader = []          for csource in copied_cheader_paths + copied_csource_paths:             dest = path.join(build_directory, path.basename(csource))             @@ -82,7 +82,7 @@ class Builder(object):                              yield {                 'basename': 'copy ' + csource,-                'file_dep': [build_file] + bindep,+                'file_dep': [build_file] + [csource],                 'actions': ['cp ' + csource + ' ' + build_directory],                 'targets': [dest]             }@@ -155,7 +155,7 @@ def is_up_to_date(task):     if len(dependency_times) == 0 or len(target_times) == 0:         return False -    return dependency_times[-1] > target_times[0]+    return dependency_times[-1] <= target_times[0]  def execute_task(task):     if is_up_to_date(task):

Contact

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