[gimp] libgimpbase: add gimp_unit_format_string()
- From: Michael Natterer <mitch src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gimp] libgimpbase: add gimp_unit_format_string()
- Date: Sun, 31 Oct 2010 21:11:21 +0000 (UTC)
commit cab852fb7369cfd0a934d0126985c91f040a43a3
Author: Michael Natterer <mitch gimp org>
Date: Sun Oct 31 22:10:31 2010 +0100
libgimpbase: add gimp_unit_format_string()
which is the string formatting function from GimpUnitMenu as proper
public API.
libgimpbase/gimpbase.def | 1 +
libgimpbase/gimpunit.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++
libgimpbase/gimpunit.h | 61 +++++++++++----------
3 files changed, 171 insertions(+), 29 deletions(-)
---
diff --git a/libgimpbase/gimpbase.def b/libgimpbase/gimpbase.def
index 76bbccc..2a6359a 100644
--- a/libgimpbase/gimpbase.def
+++ b/libgimpbase/gimpbase.def
@@ -105,6 +105,7 @@ EXPORTS
gimp_type_get_translation_domain
gimp_type_set_translation_context
gimp_type_set_translation_domain
+ gimp_unit_format_string
gimp_unit_get_abbreviation
gimp_unit_get_deletion_flag
gimp_unit_get_digits
diff --git a/libgimpbase/gimpunit.c b/libgimpbase/gimpunit.c
index def23ba..470b245 100644
--- a/libgimpbase/gimpunit.c
+++ b/libgimpbase/gimpunit.c
@@ -345,6 +345,144 @@ gimp_unit_get_plural (GimpUnit unit)
return _gimp_unit_vtable.unit_get_plural (unit);
}
+static gint
+print (gchar *buf,
+ gint len,
+ gint start,
+ const gchar *fmt,
+ ...)
+{
+ va_list args;
+ gint printed;
+
+ va_start (args, fmt);
+
+ printed = g_vsnprintf (buf + start, len - start, fmt, args);
+ if (printed < 0)
+ printed = len - start;
+
+ va_end (args);
+
+ return printed;
+}
+
+/**
+ * gimp_unit_format_string:
+ * @format: A printf-like format string which is used to create the unit
+ * string.
+ * @unit: A unit.
+ *
+ * The @format string supports the following percent expansions:
+ *
+ * <informaltable pgwide="1" frame="none" role="enum">
+ * <tgroup cols="2"><colspec colwidth="1*"/><colspec colwidth="8*"/>
+ * <tbody>
+ * <row>
+ * <entry>% f</entry>
+ * <entry>Factor (how many units make up an inch)</entry>
+ * </row>
+ * <row>
+ * <entry>% y</entry>
+ * <entry>Symbol (e.g. "''" for GIMP_UNIT_INCH)</entry>
+ * </row>
+ * <row>
+ * <entry>% a</entry>
+ * <entry>Abbreviation</entry>
+ * </row>
+ * <row>
+ * <entry>% s</entry>
+ * <entry>Singular</entry>
+ * </row>
+ * <row>
+ * <entry>% p</entry>
+ * <entry>Plural</entry>
+ * </row>
+ * <row>
+ * <entry>%%</entry>
+ * <entry>Literal percent</entry>
+ * </row>
+ * </tbody>
+ * </tgroup>
+ * </informaltable>
+ *
+ * Returns: A newly allocated string with above percent expressions
+ * replaced with the resp. strings for @unit.
+ *
+ * Since: GIMP 2.8
+ **/
+gchar *
+gimp_unit_format_string (const gchar *format,
+ GimpUnit unit)
+{
+ gchar buffer[1024];
+ gint i = 0;
+
+ g_return_val_if_fail (format != NULL, NULL);
+ g_return_val_if_fail (unit == GIMP_UNIT_PERCENT ||
+ (unit >= GIMP_UNIT_PIXEL &&
+ unit < gimp_unit_get_number_of_units ()), NULL);
+
+ while (i < (sizeof (buffer) - 1) && *format)
+ {
+ switch (*format)
+ {
+ case '%':
+ format++;
+ switch (*format)
+ {
+ case 0:
+ g_warning ("%s: unit-menu-format string ended within %%-sequence",
+ G_STRFUNC);
+ break;
+
+ case '%':
+ buffer[i++] = '%';
+ break;
+
+ case 'f': /* factor (how many units make up an inch) */
+ i += print (buffer, sizeof (buffer), i, "%f",
+ gimp_unit_get_factor (unit));
+ break;
+
+ case 'y': /* symbol ("''" for inch) */
+ i += print (buffer, sizeof (buffer), i, "%s",
+ gimp_unit_get_symbol (unit));
+ break;
+
+ case 'a': /* abbreviation */
+ i += print (buffer, sizeof (buffer), i, "%s",
+ gimp_unit_get_abbreviation (unit));
+ break;
+
+ case 's': /* singular */
+ i += print (buffer, sizeof (buffer), i, "%s",
+ gimp_unit_get_singular (unit));
+ break;
+
+ case 'p': /* plural */
+ i += print (buffer, sizeof (buffer), i, "%s",
+ gimp_unit_get_plural (unit));
+ break;
+
+ default:
+ g_warning ("%s: unit-menu-format contains unknown format "
+ "sequence '%%%c'", G_STRFUNC, *format);
+ break;
+ }
+ break;
+
+ default:
+ buffer[i++] = *format;
+ break;
+ }
+
+ format++;
+ }
+
+ buffer[MIN (i, sizeof (buffer) - 1)] = 0;
+
+ return g_strdup (buffer);
+}
/*
* GIMP_TYPE_PARAM_UNIT
diff --git a/libgimpbase/gimpunit.h b/libgimpbase/gimpunit.h
index 448d041..49f1c4e 100644
--- a/libgimpbase/gimpunit.h
+++ b/libgimpbase/gimpunit.h
@@ -60,35 +60,38 @@ GParamSpec * gimp_param_spec_unit (const gchar *name,
gint gimp_unit_get_number_of_units (void);
gint gimp_unit_get_number_of_built_in_units (void) G_GNUC_CONST;
-GimpUnit gimp_unit_new (gchar *identifier,
- gdouble factor,
- gint digits,
- gchar *symbol,
- gchar *abbreviation,
- gchar *singular,
- gchar *plural);
-
-gboolean gimp_unit_get_deletion_flag (GimpUnit unit);
-void gimp_unit_set_deletion_flag (GimpUnit unit,
- gboolean deletion_flag);
-
-gdouble gimp_unit_get_factor (GimpUnit unit);
-
-gint gimp_unit_get_digits (GimpUnit unit);
-
-const gchar * gimp_unit_get_identifier (GimpUnit unit);
-
-const gchar * gimp_unit_get_symbol (GimpUnit unit);
-const gchar * gimp_unit_get_abbreviation (GimpUnit unit);
-const gchar * gimp_unit_get_singular (GimpUnit unit);
-const gchar * gimp_unit_get_plural (GimpUnit unit);
-
-gdouble gimp_pixels_to_units (gdouble pixels,
- GimpUnit unit,
- gdouble resolution);
-gdouble gimp_units_to_pixels (gdouble value,
- GimpUnit unit,
- gdouble resolution);
+GimpUnit gimp_unit_new (gchar *identifier,
+ gdouble factor,
+ gint digits,
+ gchar *symbol,
+ gchar *abbreviation,
+ gchar *singular,
+ gchar *plural);
+
+gboolean gimp_unit_get_deletion_flag (GimpUnit unit);
+void gimp_unit_set_deletion_flag (GimpUnit unit,
+ gboolean deletion_flag);
+
+gdouble gimp_unit_get_factor (GimpUnit unit);
+
+gint gimp_unit_get_digits (GimpUnit unit);
+
+const gchar * gimp_unit_get_identifier (GimpUnit unit);
+
+const gchar * gimp_unit_get_symbol (GimpUnit unit);
+const gchar * gimp_unit_get_abbreviation (GimpUnit unit);
+const gchar * gimp_unit_get_singular (GimpUnit unit);
+const gchar * gimp_unit_get_plural (GimpUnit unit);
+
+gchar * gimp_unit_format_string (const gchar *format,
+ GimpUnit unit);
+
+gdouble gimp_pixels_to_units (gdouble pixels,
+ GimpUnit unit,
+ gdouble resolution);
+gdouble gimp_units_to_pixels (gdouble value,
+ GimpUnit unit,
+ gdouble resolution);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]