Clone in a terminal:
Commit b127e5e
Johan Mattsson - 2014-09-09T22:18:12+00:00
b127e5ed67f77dd7fed8f9ec4b5b568a307c6386johan.mattsson.m@gmail.com

Command line utility for autotracing images

Changed files (3)

Diff

diff --git a/birdfont-autotrace.sh b/birdfont-autotrace.shnew file mode 100755index 0000000..be9cd1b--- /dev/null+++ b/birdfont-autotrace.sh@@ -0,0 +1,6 @@+#!/bin/sh+export LD_LIBRARY_PATH="./build/:$LD_LIBRARY_PATH"+PKG_PATH=$(dirname "$(readlink -f "$0")")+cd "${PKG_PATH}"++gdb ./build/bin/birdfont-autotrace "$@"diff --git a/birdfont-autotrace/AutoTrace.vala b/birdfont-autotrace/AutoTrace.valanew file mode 100644index 0000000..f3894fa--- /dev/null+++ b/birdfont-autotrace/AutoTrace.vala@@ -0,0 +1,180 @@+/*+    Copyright (C) 2014 Johan Mattsson++    This program is free software: you can redistribute it and/or modify+    it under the terms of the GNU General Public License as published by+    the Free Software Foundation, either version 3 of the License, or+    (at your option) any later version.++    This program 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 General Public License for more details.++    You should have received a copy of the GNU General Public License+    along with this program.  If not, see <http://www.gnu.org/licenses/>.+*/++using Cairo;+using Gee;++namespace BirdFont {++class AutoTrace {+	+	ArrayList<string> files;++	bool help;+	bool quadratic_control_points;+	double cutoff;+	double details;+	double simplification;+	+	public AutoTrace (string[] arg) {+		files = new ArrayList<string> ();+		help = false;+		quadratic_control_points = false;+		cutoff = 1;+		details = 1;+		simplification = 0.5;+		+		for (int i = 1; i < arg.length; i++) {+			if (arg[i] == "-q" || arg[i] == "--quadratic") {+				quadratic_control_points = true;+			} else if (arg[i] == "-c" || arg[i] == "--cutoff" && i + 1 < arg.length) {+				cutoff = double.parse (arg[i + 1]);+				i++;+			} else if (arg[i] == "-d" || arg[i] == "--details" && i + 1 < arg.length) {+				details = double.parse (arg[i + 1]);+				i++;+			} else if (arg[i] == "-s" || arg[i] == "--simplification" && i + 1 < arg.length) {+				simplification = double.parse (arg[i + 1]);+				i++;+			} else if (arg[i] == "-h" || arg[i] == "--help") 	{+				help = true;+			} else {+				files.add (arg[i]);+			}+		}		+	}+	+	public bool has_help_flag () {+		return help;+	}+	+	public static void print_help (string[] arg) {+		stdout.printf (t_("Usage:"));+		stdout.printf (arg[0]);+		stdout.printf (" [" + t_("OPTION") + " ...] " + t_("FILE") + " ..."+"\n");+		stdout.printf ("-c, --cutoff                    " + t_("brighness cutoff, from 0.001 to 2, the default value is 1\n"));+		stdout.printf ("-d, --details                   " + t_("details, from 0.001 to 9.999, the default value is 1\n"));+		stdout.printf ("-h, --help                      " + t_("print this message\n"));+		stdout.printf ("-q, --quadratic                 " + t_("use quadratic control points\n"));+		stdout.printf ("-s, --simplification            " + t_("simplification, from 0.001 to 1, the default value is 0.5\n"));+		stdout.printf ("\n");+	}++	public int trace () {+		BackgroundImage bg;+		File file;+		PathList pl;+		Glyph g;+		double w, h;+		Font font;+		string svg;+		string file_name;+		DataOutputStream data_stream;+		+		foreach (string f in files) {+			file = File.new_for_path (f);+			if (!file.query_exists ()) {+				stdout.printf (@"$f\n");+				stdout.printf (t_("File does not exist.") + "\n");+				return 1;+			}+			+			if (((!) file.get_basename ()).index_of (".") == -1) {+				stdout.printf (@"$f\n");+				stdout.printf (t_("Unknown file format.") + "\n");+				return 2;+			}+		}+		+		if (quadratic_control_points) {+			DrawingTools.point_type = PointType.QUADRATIC;+		} else {+			DrawingTools.point_type = PointType.CUBIC;+		}+		+		foreach (string f in files) {+			file_name = f;+			file_name = file_name.substring (0, file_name.last_index_of ("."));+			file_name = @"$file_name.svg";+			+			stdout.printf (t_("Writing") + " " + file_name + "\n");+		+			font = BirdFont.new_font ();+			bg = new BackgroundImage (f);+			+			bg.set_trace_resolution (details);+			bg.set_threshold (cutoff);+			bg.set_trace_simplification (simplification);+			bg.set_high_contrast (true);++			g = new Glyph.no_lines ("");+			+			GlyphCanvas.current_display = g;+			BirdFont.current_glyph = g;++			h = bg.get_img ().get_height ();+			+			font.top_limit = h / 2;+			font.bottom_limit = -h / 2;+			font.top_position = h / 2;+			font.bottom_position = -h / 2;+						+			w = bg.get_img ().get_width ();+			g.left_limit = -w / 2.0;+			g.right_limit = w / 2.0;+									+			pl = bg.autotrace ();+			+			foreach (Path p in pl.paths) {+				g.add_path (p);+			}+			+			svg = ExportTool.export_to_string (g, false);+			+			file = File.new_for_path (file_name);+			+			if (file.query_exists ()) {+				file.delete ();+			}+				+			data_stream = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));+			data_stream.put_string (svg);+		}+		+		return 0;+	}+}++public static int main (string[] arg) {+	AutoTrace autotrace;+	Preferences p;+	+	p = new Preferences ();+	+	BirdFont.init_gettext ();+	+	autotrace = new AutoTrace (arg);+	+	if (autotrace.has_help_flag () || arg.length <= 1) {+		AutoTrace.print_help (arg);+		return 0;+	}+	+	return autotrace.trace ();	+}++}diff --git a/libbirdfont/BackgroundImage.vala b/libbirdfont/BackgroundImage.valaindex 2888b51..9ff7a37 100644--- a/libbirdfont/BackgroundImage.vala+++ b/libbirdfont/BackgroundImage.vala@@ -1159,9 +1159,12 @@ public class BackgroundImage { 		double image_scale_y; 		TracedPoint average_point; 		int pi;+		ImageSurface img;++		img = (contrast_image == null) ? get_contrast_image () : (!) contrast_image; -		image_scale_x = ((double) size_margin / get_img ().get_width ());-		image_scale_y = ((double) size_margin / get_img ().get_height ());+		image_scale_x = ((double) size_margin / img.get_width ());+		image_scale_y = ((double) size_margin / img.get_height ()); 				 		foreach (TracedPoint p in points) { 			start_points.add (p);

Contact

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