diff options
-rw-r--r-- | data/icon-1.svg.in | 2 | ||||
-rw-r--r-- | data/icon.svg.in | 2 | ||||
-rw-r--r-- | data/main.vala | 44 | ||||
-rw-r--r-- | lib/main.vala | 127 |
4 files changed, 126 insertions, 49 deletions
diff --git a/data/icon-1.svg.in b/data/icon-1.svg.in index 1ff8f4a2..4f6fbce7 100644 --- a/data/icon-1.svg.in +++ b/data/icon-1.svg.in @@ -1 +1 @@ -<?xml version="1.0" encoding="UTF-8"?><svg version="1.1" width="@WIDTH@" height="@HEIGHT@"><defs><mask id="m"><rect x="0" y="0" width="@WIDTH@" height="@HEIGHT@" style="fill:white"/><text x="@LAYOUT_X@" y="@LAYOUT_Y@" style="@LAYOUT_FONT@;fill:black">@LAYOUT@</text><text x="@SUBSCRIPT_X@" y="@SUBSCRIPT_Y@" style="@SUBSCRIPT_FONT@;fill:black">@SUBSCRIPT@</text></mask></defs><rect x="0" y="0" width="@WIDTH@" height="@HEIGHT@" rx="@RADIUS@" mask="url(#m)" style="fill:@COLOUR@"/></svg> +<?xml version="1.0" encoding="UTF-8"?><svg version="1.1" width="@WIDTH@" height="@HEIGHT@"><defs><mask id="m"><rect x="0" y="0" width="@WIDTH@" height="@HEIGHT@" style="fill:white"/><text x="@LAYOUT_X@" y="@LAYOUT_Y@" style="@LAYOUT_FONT@;fill:black">@LAYOUT@</text><text x="@SUBSCRIPT_X@" y="@SUBSCRIPT_Y@" style="@SUBSCRIPT_FONT@;fill:black">@SUBSCRIPT@</text></mask></defs><rect x="@ICON_X@" y="@ICON_Y@" width="@ICON_WIDTH@" height="@ICON_HEIGHT@" rx="@RADIUS@" mask="url(#m)" style="fill:@COLOUR@"/></svg> diff --git a/data/icon.svg.in b/data/icon.svg.in index 1f80e510..5e0dcbc8 100644 --- a/data/icon.svg.in +++ b/data/icon.svg.in @@ -1 +1 @@ -<?xml version="1.0" encoding="UTF-8"?><svg version="1.1" width="@WIDTH@" height="@HEIGHT@"><defs><mask id="m"><rect x="0" y="0" width="@WIDTH@" height="@HEIGHT@" style="fill:white"/><text x="@LAYOUT_X@" y="@LAYOUT_Y@" style="@LAYOUT_FONT@;fill:black">@LAYOUT@</text></mask></defs><rect x="0" y="0" width="@WIDTH@" height="@HEIGHT@" rx="@RADIUS@" mask="url(#m)" style="fill:@COLOUR@"/></svg> +<?xml version="1.0" encoding="UTF-8"?><svg version="1.1" width="@WIDTH@" height="@HEIGHT@"><defs><mask id="m"><rect x="0" y="0" width="@WIDTH@" height="@HEIGHT@" style="fill:white"/><text x="@LAYOUT_X@" y="@LAYOUT_Y@" style="@LAYOUT_FONT@;fill:black">@LAYOUT@</text></mask></defs><rect x="@ICON_X@" y="@ICON_Y@" width="@ICON_WIDTH@" height="@ICON_HEIGHT@" rx="@RADIUS@" mask="url(#m)" style="fill:@COLOUR@"/></svg> diff --git a/data/main.vala b/data/main.vala index b34dac01..3dd940b7 100644 --- a/data/main.vala +++ b/data/main.vala @@ -2,8 +2,10 @@ int main (string[] args) { var force = false; var width = 22.0; var height = 22.0; - var radius = 4.0; - var colour = "white"; + var icon_width = 20.0; + var icon_height = 20.0; + var radius = 2.0; + var colour = "black"; var font = "Ubuntu"; var weight = 500; var layout_size = 12; @@ -12,20 +14,22 @@ int main (string[] args) { string no_subscript_path = null; string with_subscript_path = null; - OptionEntry[] options = new OptionEntry[13]; + OptionEntry[] options = new OptionEntry[15]; options[0] = { "force", 'f', 0, OptionArg.NONE, ref force, "Overwrite existing files" }; - 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 }; + options[1] = { "width", 'w', 0, OptionArg.DOUBLE, ref width, "Template width", "DOUBLE" }; + options[2] = { "height", 'h', 0, OptionArg.DOUBLE, ref height, "Template height", "DOUBLE" }; + options[3] = { "icon-width", 'W', 0, OptionArg.DOUBLE, ref icon_width, "Icon width", "DOUBLE" }; + options[4] = { "icon-height", 'H', 0, OptionArg.DOUBLE, ref icon_height, "Icon height", "DOUBLE" }; + options[5] = { "radius", 'r', 0, OptionArg.DOUBLE, ref radius, "Icon radius", "DOUBLE" }; + options[6] = { "colour", 'c', 0, OptionArg.STRING, ref colour, "Icon colour", "COLOUR" }; + options[7] = { "font", 'F', 0, OptionArg.STRING, ref font, "Font family", "NAME" }; + options[8] = { "weight", 'G', 0, OptionArg.INT, ref weight, "Font weight (100 to 1000)", "INT" }; + options[9] = { "layout-size", 's', 0, OptionArg.INT, ref layout_size, "Layout font size", "INT" }; + options[10] = { "subscript-size", 'S', 0, OptionArg.INT, ref subscript_size, "Subscript font size", "INT" }; + options[11] = { "output", 'o', 0, OptionArg.FILENAME, ref output_path, "Output directory", "PATH" }; + options[12] = { "no-subscript", 'i', 0, OptionArg.FILENAME, ref no_subscript_path, "Icon template", "PATH" }; + options[13] = { "with-subscript", 'I', 0, OptionArg.FILENAME, ref with_subscript_path, "Subscript icon template", "PATH" }; + options[14] = { null }; try { var context = new OptionContext ("- generate keyboard layout icons"); @@ -92,6 +96,8 @@ int main (string[] args) { try { uint8[] contents; + var icon_x = 0.5 * (width - icon_width); + var icon_y = 0.5 * (height - icon_height); 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"; @@ -99,6 +105,10 @@ int main (string[] args) { 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 ("@ICON_X@", @"$icon_x"); + no_subscript_data = no_subscript_data.replace ("@ICON_Y@", @"$icon_y"); + no_subscript_data = no_subscript_data.replace ("@ICON_WIDTH@", @"$icon_width"); + no_subscript_data = no_subscript_data.replace ("@ICON_HEIGHT@", @"$icon_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); @@ -108,6 +118,10 @@ int main (string[] args) { 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 ("@ICON_X@", @"$icon_x"); + with_subscript_data = with_subscript_data.replace ("@ICON_Y@", @"$icon_y"); + with_subscript_data = with_subscript_data.replace ("@ICON_WIDTH@", @"$icon_width"); + with_subscript_data = with_subscript_data.replace ("@ICON_HEIGHT@", @"$icon_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); diff --git a/lib/main.vala b/lib/main.vala index d85a8c1b..e0bb9207 100644 --- a/lib/main.vala +++ b/lib/main.vala @@ -14,6 +14,7 @@ public class Indicator.Keyboard.Service : Object { private Icon[] icons; private string[] icon_strings; + private int[] icon_string_uniques; private uint[] icon_string_subscripts; [DBus (visible = false)] @@ -57,7 +58,7 @@ public class Indicator.Keyboard.Service : Object { const int H = 20; const double R = 2.0; const double TEXT_SIZE = 12.0; - const double SUBSCRIPT_SIZE = 10.0; + const double SUBSCRIPT_SIZE = 8.0; Pango.FontDescription description; var style = get_style_context (); @@ -88,7 +89,7 @@ public class Indicator.Keyboard.Service : Object { int text_height; text_layout.get_pixel_size (out text_width, out text_height); - if (subscript > 1) { + if (subscript > 0) { var subscript_layout = Pango.cairo_create_layout (context); subscript_layout.set_alignment (Pango.Alignment.CENTER); description.set_absolute_size (Pango.units_from_double (SUBSCRIPT_SIZE)); @@ -126,35 +127,6 @@ public class Indicator.Keyboard.Service : Object { } [DBus (visible = false)] - private uint get_icon_string_subscript (uint index) { - uint icon_string_subscript = 0; - Variant array = null; - - if (this.icon_string_subscripts == null) { - this.source_settings.get ("sources", "@a(ss)", out array); - this.icon_string_subscripts = new uint[array.n_children ()]; - } - - if (index < this.icon_string_subscripts.length) { - icon_string_subscript = this.icon_string_subscripts[index]; - - if (icon_string_subscript == 0) { - this.icon_string_subscripts[index] = 1; - - for (var i = (int) index - 1; i >= 0 && this.icon_string_subscripts[index] == 1; i--) { - if (get_icon_string (i) == get_icon_string (index)) { - this.icon_string_subscripts[index] = get_icon_string_subscript (i) + 1; - } - } - - icon_string_subscript = this.icon_string_subscripts[index]; - } - } - - return icon_string_subscript; - } - - [DBus (visible = false)] private string get_icon_string (uint index) { string icon_string = null; Variant array = null; @@ -197,6 +169,68 @@ public class Indicator.Keyboard.Service : Object { } [DBus (visible = false)] + private bool is_icon_string_unique (uint index) { + bool icon_string_unique = true; + Variant array = null; + + if (this.icon_string_uniques == null) { + this.source_settings.get ("sources", "@a(ss)", out array); + this.icon_string_uniques = new int[array.n_children ()]; + + for (var i = 0; i < this.icon_string_uniques.length; i++) { + this.icon_string_uniques[i] = -1; + } + } + + if (index < this.icon_string_uniques.length) { + if (this.icon_string_uniques[index] == -1) { + this.icon_string_uniques[index] = 1; + + var icon_string = get_icon_string (index); + + for (var i = 0; i < this.icon_string_uniques.length && this.icon_string_uniques[index] == 1; i++) { + if (i != index && get_icon_string (i) == icon_string) { + this.icon_string_uniques[index] = 0; + } + } + } + + icon_string_unique = this.icon_string_uniques[index] != 0; + } + + return icon_string_unique; + } + + [DBus (visible = false)] + private uint get_icon_string_subscript (uint index) { + uint icon_string_subscript = 0; + Variant array = null; + + if (this.icon_string_subscripts == null) { + this.source_settings.get ("sources", "@a(ss)", out array); + this.icon_string_subscripts = new uint[array.n_children ()]; + } + + if (index < this.icon_string_subscripts.length) { + icon_string_subscript = this.icon_string_subscripts[index]; + + if (icon_string_subscript == 0) { + this.icon_string_subscripts[index] = 1; + + for (var i = (int) index - 1; i >= 0 && this.icon_string_subscripts[index] == 1; i--) { + if (get_icon_string (i) == get_icon_string (index)) { + this.icon_string_subscripts[index] = get_icon_string_subscript (i) + 1; + } + } + + icon_string_subscript = this.icon_string_subscripts[index]; + } + } + + return icon_string_subscript; + } + + [DBus (visible = false)] private Icon get_icon (uint index) { Icon icon = null; Variant array = null; @@ -220,7 +254,35 @@ public class Indicator.Keyboard.Service : Object { array.get_child (index, "(ss)", out type, out name); if (type == "xkb") { - this.icons[index] = create_icon (get_icon_string (index), get_icon_string_subscript (index)); + var icon_string = get_icon_string (index); + var icon_unique = is_icon_string_unique (index); + var icon_subscript = get_icon_string_subscript (index); + string icon_name; + + if (icon_unique) { + icon_name = @"$icon_string"; + } else { + icon_name = @"$icon_string-$icon_subscript"; + } + + var icon_theme = Gtk.IconTheme.get_default (); + var icon_info = icon_theme.lookup_icon (icon_name, 22, 0); + + if (icon_info != null) { + try { + this.icons[index] = Icon.new_for_string (icon_info.get_filename ()); + } catch (Error error) { + this.icons[index] = null; + } + } + + if (this.icons[index] == null) { + if (icon_unique) { + this.icons[index] = create_icon (get_icon_string (index), 0); + } else { + this.icons[index] = create_icon (get_icon_string (index), get_icon_string_subscript (index)); + } + } } else if (type == "ibus") { var ibus = get_ibus (); string[] names = { name, null }; @@ -422,6 +484,7 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] private void handle_changed_sources (string key) { this.icon_string_subscripts = null; + this.icon_string_uniques = null; this.icon_strings = null; this.icons = null; |