[gimp] app: improve Measure Tool digit precision.



commit d1c3c3deb08026ab9a280efab054a5f20b360c5e
Author: Jehan <jehan girinstud io>
Date:   Thu Jan 19 15:42:00 2017 +0100

    app: improve Measure Tool digit precision.
    
    When working with high resolution, you may have cases where measured
    length won't be displayed with enough digits; i.e. several pixels length
    would show up as 0. For instance at 4000 PPI, up to 7 pixels show up as
    0 mm, then at 0.1 mm from the 8th pixel (actually reaching over 0.05 mm,
    approximating as 0.1), then 0.2 at 24 pixels (actually: 0.152), and so
    on. At such a resolution, 3 digits are needed for 'mm' instead of the
    1 digit returned by gimp_unit_get_digits() so that we display reliable
    lengths.
    Therefore we need to compute ideal digit precision. Configured digits
    for a given unit will now be used as a minimum value, but actually used
    digits may be higher.

 app/tools/gimpmeasuretool.c |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)
---
diff --git a/app/tools/gimpmeasuretool.c b/app/tools/gimpmeasuretool.c
index a90262e..4678488 100644
--- a/app/tools/gimpmeasuretool.c
+++ b/app/tools/gimpmeasuretool.c
@@ -887,6 +887,9 @@ gimp_measure_tool_dialog_update (GimpMeasureTool *measure,
   gdouble           xres;
   gdouble           yres;
   gchar             format[128];
+  gint              unit_distance_digits;
+  gint              unit_width_digits;
+  gint              unit_height_digits;
 
   /*  calculate distance and angle  */
   ax = measure->x[1] - measure->x[0];
@@ -936,6 +939,20 @@ gimp_measure_tool_dialog_update (GimpMeasureTool *measure,
   if (unit_angle > 180.0)
     unit_angle = fabs (360.0 - unit_angle);
 
+  /* Compute minimum digits to display accurate values, so that
+     every pixel shows a different value in unit. */
+  unit_distance_digits = ceil (log10 (pixel_distance / unit_distance));
+  unit_distance_digits = MAX (gimp_unit_get_digits (shell->unit),
+                              unit_distance_digits);
+
+  unit_width_digits    = ceil (log10 (pixel_width / unit_width));
+  unit_width_digits    = MAX (gimp_unit_get_digits (shell->unit),
+                              unit_width_digits);
+
+  unit_height_digits   = ceil (log10 (pixel_height / unit_height));
+  unit_height_digits   = MAX (gimp_unit_get_digits (shell->unit),
+                              unit_height_digits);
+
   if (shell->unit == GIMP_UNIT_PIXEL)
     {
       gimp_tool_replace_status (GIMP_TOOL (measure), display,
@@ -947,10 +964,10 @@ gimp_measure_tool_dialog_update (GimpMeasureTool *measure,
     {
       g_snprintf (format, sizeof (format),
                   "%%.%df %s, %%.2f\302\260 (%%.%df × %%.%df)",
-                  gimp_unit_get_digits (shell->unit),
+                  unit_distance_digits,
                   gimp_unit_get_plural (shell->unit),
-                  gimp_unit_get_digits (shell->unit),
-                  gimp_unit_get_digits (shell->unit));
+                  unit_width_digits,
+                  unit_height_digits);
 
       gimp_tool_replace_status (GIMP_TOOL (measure), display, format,
                                 unit_distance, unit_angle,
@@ -962,15 +979,14 @@ gimp_measure_tool_dialog_update (GimpMeasureTool *measure,
     {
       gchar buf[128];
 
-      g_snprintf (format, sizeof (format), "%%.%df",
-                  gimp_unit_get_digits (shell->unit));
-
       /* Distance */
       g_snprintf (buf, sizeof (buf), "%.1f", pixel_distance);
       gtk_label_set_text (GTK_LABEL (measure->distance_label[0]), buf);
 
       if (shell->unit != GIMP_UNIT_PIXEL)
         {
+          g_snprintf (format, sizeof (format), "%%.%df",
+                      unit_distance_digits);
           g_snprintf (buf, sizeof (buf), format, unit_distance);
           gtk_label_set_text (GTK_LABEL (measure->distance_label[1]), buf);
 
@@ -1006,6 +1022,8 @@ gimp_measure_tool_dialog_update (GimpMeasureTool *measure,
 
       if (shell->unit != GIMP_UNIT_PIXEL)
         {
+          g_snprintf (format, sizeof (format), "%%.%df",
+                      unit_width_digits);
           g_snprintf (buf, sizeof (buf), format, unit_width);
           gtk_label_set_text (GTK_LABEL (measure->width_label[1]), buf);
 
@@ -1024,6 +1042,8 @@ gimp_measure_tool_dialog_update (GimpMeasureTool *measure,
       /* Height */
       if (shell->unit != GIMP_UNIT_PIXEL)
         {
+          g_snprintf (format, sizeof (format), "%%.%df",
+                      unit_height_digits);
           g_snprintf (buf, sizeof (buf), format, unit_height);
           gtk_label_set_text (GTK_LABEL (measure->height_label[1]), buf);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]