diff options
author | William Hua <william.hua@canonical.com> | 2013-06-05 20:45:45 -0400 |
---|---|---|
committer | William Hua <william.hua@canonical.com> | 2013-06-05 20:45:45 -0400 |
commit | 91ddc799bd01ce206e2822ae4b6c58b087efc6b6 (patch) | |
tree | 825bb36978c56d40980be45c7fb09bacef60fc59 /data/main.vala | |
parent | 97e9428331438a66fb78696b736d7046e96889cb (diff) | |
download | ayatana-indicator-keyboard-91ddc799bd01ce206e2822ae4b6c58b087efc6b6.tar.gz ayatana-indicator-keyboard-91ddc799bd01ce206e2822ae4b6c58b087efc6b6.tar.bz2 ayatana-indicator-keyboard-91ddc799bd01ce206e2822ae4b6c58b087efc6b6.zip |
Align text within icons properly.
Diffstat (limited to 'data/main.vala')
-rw-r--r-- | data/main.vala | 113 |
1 files changed, 95 insertions, 18 deletions
diff --git a/data/main.vala b/data/main.vala index 2c28ace7..f942a591 100644 --- a/data/main.vala +++ b/data/main.vala @@ -1,17 +1,31 @@ int main (string[] args) { var force = false; + var width = 220.0; + var height = 220.0; + var radius = 40.0; var colour = "grey"; + var font = "Ubuntu"; + var weight = 500; + var layout_size = 120; + var subscript_size = 80; string output_path = null; string no_subscript_path = null; string with_subscript_path = null; - OptionEntry[] options = new OptionEntry[6]; + OptionEntry[] options = new OptionEntry[13]; options[0] = { "force", 'f', 0, OptionArg.NONE, ref force, "Overwrite existing files" }; - options[1] = { "colour", 'c', 0, OptionArg.STRING, ref colour, "Icon colour", "COLOUR" }; - options[2] = { "output", 'o', 0, OptionArg.FILENAME, ref output_path, "Output directory", "PATH" }; - options[3] = { "no-subscript", 's', 0, OptionArg.FILENAME, ref no_subscript_path, "Icon template", "PATH" }; - options[4] = { "with-subscript", 'S', 0, OptionArg.FILENAME, ref with_subscript_path, "Subscript icon template", "PATH" }; - options[5] = { null }; + options[1] = { "width", 'w', 0, OptionArg.DOUBLE, ref width, "Icon width", "DOUBLE" }; + options[2] = { "height", 'h', 0, OptionArg.DOUBLE, ref height, "Icon height", "DOUBLE" }; + options[3] = { "radius", 'r', 0, OptionArg.DOUBLE, ref radius, "Icon radius", "DOUBLE" }; + options[4] = { "colour", 'c', 0, OptionArg.STRING, ref colour, "Icon colour", "COLOUR" }; + options[5] = { "font", 'f', 0, OptionArg.STRING, ref font, "Font family", "NAME" }; + options[6] = { "weight", 'W', 0, OptionArg.INT, ref weight, "Font weight (100 to 1000)", "INT" }; + options[7] = { "layout-size", 's', 0, OptionArg.INT, ref layout_size, "Layout font size", "INT" }; + options[8] = { "subscript-size", 'S', 0, OptionArg.INT, ref subscript_size, "Subscript font size", "INT" }; + options[9] = { "output", 'o', 0, OptionArg.FILENAME, ref output_path, "Output directory", "PATH" }; + options[10] = { "no-subscript", 'i', 0, OptionArg.FILENAME, ref no_subscript_path, "Icon template", "PATH" }; + options[11] = { "with-subscript", 'I', 0, OptionArg.FILENAME, ref with_subscript_path, "Subscript icon template", "PATH" }; + options[12] = { null }; try { var context = new OptionContext ("- generate keyboard layout icons"); @@ -78,27 +92,70 @@ int main (string[] args) { try { uint8[] contents; + var layout_font = @"font-family:$font;font-weight:$weight;font-size:$layout_size"; + var subscript_font = @"font-family:$font;font-weight:$weight;font-size:$subscript_size"; + File.new_for_path (no_subscript_path).load_contents (null, out contents, null); - no_subscript_data = ((string) contents).replace ("@COLOUR@", colour); + no_subscript_data = (string) contents; + no_subscript_data = no_subscript_data.replace ("@WIDTH@", @"$width"); + no_subscript_data = no_subscript_data.replace ("@HEIGHT@", @"$height"); + no_subscript_data = no_subscript_data.replace ("@RADIUS@", @"$radius"); + no_subscript_data = no_subscript_data.replace ("@COLOUR@", colour); + no_subscript_data = no_subscript_data.replace ("@LAYOUT_FONT@", layout_font); + no_subscript_data = no_subscript_data.replace ("@SUBSCRIPT_FONT@", subscript_font); File.new_for_path (with_subscript_path).load_contents (null, out contents, null); - with_subscript_data = ((string) contents).replace ("@COLOUR@", colour); + with_subscript_data = (string) contents; + with_subscript_data = with_subscript_data.replace ("@WIDTH@", @"$width"); + with_subscript_data = with_subscript_data.replace ("@HEIGHT@", @"$height"); + with_subscript_data = with_subscript_data.replace ("@RADIUS@", @"$radius"); + with_subscript_data = with_subscript_data.replace ("@COLOUR@", colour); + with_subscript_data = with_subscript_data.replace ("@LAYOUT_FONT@", layout_font); + with_subscript_data = with_subscript_data.replace ("@SUBSCRIPT_FONT@", subscript_font); } catch (Error error) { stderr.printf ("error: %s\n", error.message); return 4; } + var font_map = new PangoFT2.FontMap (); + var layout_layout = new Pango.Layout (font_map.create_context ()); + var subscript_layout = new Pango.Layout (font_map.create_context ()); + + var font_description = new Pango.FontDescription (); + font_description.set_family (font); + font_description.set_weight ((Pango.Weight) weight); + font_description.set_size (layout_size * Pango.SCALE); + layout_layout.set_font_description (font_description); + + font_description = new Pango.FontDescription (); + font_description.set_family (font); + font_description.set_weight ((Pango.Weight) weight); + font_description.set_size (subscript_size * Pango.SCALE); + subscript_layout.set_font_description (font_description); + foreach (var entry in occurrences.entries) { var layout = entry.key; var count = entry.value; var file = File.new_for_path (@"$output_path/$layout.svg"); if (force || !file.query_exists (null)) { - var output_data = no_subscript_data; + int layout_width; + int layout_height; + + layout_layout.set_text (layout, -1); + layout_layout.get_size (out layout_width, out layout_height); + var layout_baseline = layout_layout.get_baseline (); + var layout_x = 0.5 * (width - 1.0 * layout_width / Pango.SCALE); + var layout_y = 0.5 * (height - 1.0 * layout_height / Pango.SCALE) + 1.0 * layout_baseline / Pango.SCALE; + + var output_data = no_subscript_data; output_data = output_data.replace ("@LAYOUT@", layout); - output_data = output_data.replace ("@LAYOUT_X@", "3.5"); - output_data = output_data.replace ("@LAYOUT_Y@", "15.5"); + output_data = output_data.replace ("@LAYOUT_X@", @"$layout_x"); + output_data = output_data.replace ("@LAYOUT_Y@", @"$layout_y"); + output_data = output_data.replace ("@SUBSCRIPT@", ""); + output_data = output_data.replace ("@SUBSCRIPT_X@", "0"); + output_data = output_data.replace ("@SUBSCRIPT_Y@", "0"); try { file.replace_contents (output_data.data, null, false, FileCreateFlags.REPLACE_DESTINATION, null, null); @@ -108,21 +165,41 @@ int main (string[] args) { } if (count > 1) { - var partial_data = with_subscript_data; + int layout_width; + int layout_height; + + layout_layout.set_text (layout, -1); + layout_layout.get_size (out layout_width, out layout_height); + var layout_baseline = layout_layout.get_baseline (); + var layout_y = 0.5 * (height - 1.0 * layout_height / Pango.SCALE) + 1.0 * layout_baseline / Pango.SCALE; + + var partial_data = with_subscript_data; partial_data = partial_data.replace ("@LAYOUT@", layout); + partial_data = partial_data.replace ("@LAYOUT_Y@", @"$layout_y"); for (var i = 1; i <= count; i++) { file = File.new_for_path (@"$output_path/$layout-$i.svg"); if (force || !file.query_exists (null)) { - var output_data = partial_data; + var subscript = @"$i"; + int subscript_width; + int subscript_height; + + subscript_layout.set_text (subscript, -1); + subscript_layout.get_size (out subscript_width, out subscript_height); + var subscript_baseline = subscript_layout.get_baseline (); - output_data = output_data.replace ("@LAYOUT_X@", "3.5"); - output_data = output_data.replace ("@LAYOUT_Y@", "15.5"); - output_data = output_data.replace ("@SUBSCRIPT@", @"$i"); - output_data = output_data.replace ("@SUBSCRIPT_X@", "15"); - output_data = output_data.replace ("@SUBSCRIPT_Y@", "10"); + var layout_x = 0.5 * (width - 1.0 * (layout_width + subscript_width) / Pango.SCALE); + var subscript_x = layout_x + 1.0 * layout_width / Pango.SCALE; + var subscript_y = layout_y - 0.5 * subscript_height / Pango.SCALE + 1.0 * subscript_baseline / Pango.SCALE; + + var output_data = partial_data; + output_data = output_data.replace ("@LAYOUT_X@", @"$layout_x"); + output_data = output_data.replace ("@LAYOUT_Y@", @"$layout_y"); + output_data = output_data.replace ("@SUBSCRIPT@", subscript); + output_data = output_data.replace ("@SUBSCRIPT_X@", @"$subscript_x"); + output_data = output_data.replace ("@SUBSCRIPT_Y@", @"$subscript_y"); try { file.replace_contents (output_data.data, null, false, FileCreateFlags.REPLACE_DESTINATION, null, null); |