From 0c07ae3bbfce06f382f2b8ea3b2d53cf870f3a15 Mon Sep 17 00:00:00 2001 From: Jehan Date: Fri, 20 Jan 2017 01:10:06 +0100 Subject: [PATCH] libgimpbase: new gimp_unit_get_accurate_digits() API function. --- libgimpbase/gimpbase.def | 1 + libgimpbase/gimpunit.c | 31 +++++++++++++++++++++++++++++++ libgimpbase/gimpunit.h | 2 ++ 3 files changed, 34 insertions(+) diff --git a/libgimpbase/gimpbase.def b/libgimpbase/gimpbase.def index c2abcffc2c..526359efe3 100644 --- a/libgimpbase/gimpbase.def +++ b/libgimpbase/gimpbase.def @@ -174,6 +174,7 @@ EXPORTS gimp_unit_get_number_of_built_in_units gimp_unit_get_number_of_units gimp_unit_get_plural + gimp_unit_get_scaled_digits gimp_unit_get_singular gimp_unit_get_symbol gimp_unit_get_type diff --git a/libgimpbase/gimpunit.c b/libgimpbase/gimpunit.c index 8daa4dc9c6..2b6ad57e46 100644 --- a/libgimpbase/gimpunit.c +++ b/libgimpbase/gimpunit.c @@ -21,6 +21,7 @@ #include "config.h" +#include #include #include @@ -255,6 +256,36 @@ gimp_unit_get_digits (GimpUnit unit) return _gimp_unit_vtable.unit_get_digits (unit); } +/** + * gimp_unit_get_scaled_digits: + * @unit: The unit you want to know the digits. + * @resolution: the resolution in PPI. + * + * Returns the number of digits a @unit field should provide to get + * enough accuracy so that every pixel position shows a different + * value from neighboring pixels. + * + * Note: when needing digit accuracy to display a diagonal distance, + * the @resolution may not correspond to the image's horizontal or + * vertical resolution, but instead to the result of: + * `distance_in_pixel / distance_in_inch`. + * + * Returns: The suggested number of digits. + **/ +gint +gimp_unit_get_scaled_digits (GimpUnit unit, + gdouble resolution) +{ + gint digits; + + g_return_val_if_fail (_gimp_unit_vtable.unit_get_digits != NULL, 2); + + digits = ceil (log10 (1.0 / + gimp_pixels_to_units (1.0, unit, resolution))); + + return MAX (digits, gimp_unit_get_digits (unit)); +} + /** * gimp_unit_get_identifier: * @unit: The unit you want to know the identifier of. diff --git a/libgimpbase/gimpunit.h b/libgimpbase/gimpunit.h index 2311a5441a..5f87b92214 100644 --- a/libgimpbase/gimpunit.h +++ b/libgimpbase/gimpunit.h @@ -79,6 +79,8 @@ void gimp_unit_set_deletion_flag (GimpUnit unit, gdouble gimp_unit_get_factor (GimpUnit unit); gint gimp_unit_get_digits (GimpUnit unit); +gint gimp_unit_get_scaled_digits (GimpUnit unit, + gdouble resolution); const gchar * gimp_unit_get_identifier (GimpUnit unit);