[evince] libdocument: Use a cmp function to compare size of area in ev_mapping list_get



commit 4416eb17ad90a12243d1b876c501d2d5e8bf4aa7
Author: José Aliste <jose aliste gmail com>
Date:   Fri Mar 30 22:40:08 2018 -0300

    libdocument: Use a cmp function to compare size of area in ev_mapping list_get
    
    This is a fixup commit to previous commit by Fabian Franzen
    based on work of Bastien Nocera. We try to avoid multiplications
    by comparing width and lengths of the areas first.

 libdocument/ev-mapping-list.c |   31 +++++++++++++++++++++++--------
 1 files changed, 23 insertions(+), 8 deletions(-)
---
diff --git a/libdocument/ev-mapping-list.c b/libdocument/ev-mapping-list.c
index 6ca1b34..3efd42e 100644
--- a/libdocument/ev-mapping-list.c
+++ b/libdocument/ev-mapping-list.c
@@ -99,11 +99,28 @@ ev_mapping_list_nth (EvMappingList *mapping_list,
         return (EvMapping *)g_list_nth_data (mapping_list->list, n);
 }
 
-static gdouble
-get_mapping_area_size (EvMapping *mapping)
+static int
+cmp_mapping_area_size (EvMapping *a,
+                      EvMapping *b)
 {
-       return (mapping->area.x2 - mapping->area.x1) *
-              (mapping->area.y2 - mapping->area.y1);
+       gdouble wa, ha, wb, hb;
+
+       wa = a->area.x2 - a->area.x1;
+       ha = a->area.y2 - a->area.y1;
+       wb = b->area.x2 - b->area.x1;
+       hb = b->area.y2 - b->area.y1;
+
+       if (wa == wb) {
+               if (ha == hb)
+                       return 0;
+               return (ha < hb) ? -1 : 1;
+       }
+
+       if (ha == hb) {
+               return (wa < wb) ? -1 : 1;
+       }
+
+       return (wa * ha < wb * hb) ? -1 : 1;
 }
 
 /**
@@ -136,10 +153,8 @@ ev_mapping_list_get (EvMappingList *mapping_list,
 
                        /* In case of only one match choose that. Otherwise
                         * compare the area of the bounding boxes and return the
-                        * smallest element.
-                        * In this way we allow most of the elements to be selectable
-                        * by the user */
-                       if(!found || (get_mapping_area_size(mapping) < get_mapping_area_size(found)))
+                        * smallest element */
+                       if(found == NULL || cmp_mapping_area_size (mapping, found) < 0)
                                found = mapping;
                }
        }


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