[pango: 1/4] Introduce and use FLOOR and CEIL macros in Pango units




commit 1c7c84a263af1e0688e2fedb3a98e2f10b55f0eb
Author: Sebastian Keller <skeller gnome org>
Date:   Mon Nov 22 01:41:33 2021 +0100

    Introduce and use FLOOR and CEIL macros in Pango units
    
    This replaces the code that was trying to implement those via the ROUND
    macro. This avoids a potential issue with values that already were on
    whole units being rounded up to the next whole unit in the code that was
    implementing CEIL this way.

 pango/pango-types.h     | 26 ++++++++++++++++++++++++++
 pango/pangocairo-font.c |  8 ++++----
 2 files changed, 30 insertions(+), 4 deletions(-)
---
diff --git a/pango/pango-types.h b/pango/pango-types.h
index f18225ba..ed86f699 100644
--- a/pango/pango-types.h
+++ b/pango/pango-types.h
@@ -106,6 +106,32 @@ typedef guint32 PangoGlyph;
  * PANGO_PIXELS also behaves differently for +512 and -512.
  */
 
+/**
+ * PANGO_UNITS_FLOOR:
+ * @d: a dimension in Pango units.
+ *
+ * Rounds a dimension down to whole device units, but does not
+ * convert it to device units.
+ *
+ * Return value: rounded down dimension in Pango units.
+ * Since: 1.50
+ */
+#define PANGO_UNITS_FLOOR(d)                \
+  ((d) & ~(PANGO_SCALE - 1))
+
+/**
+ * PANGO_UNITS_CEIL:
+ * @d: a dimension in Pango units.
+ *
+ * Rounds a dimension up to whole device units, but does not
+ * convert it to device units.
+ *
+ * Return value: rounded up dimension in Pango units.
+ * Since: 1.50
+ */
+#define PANGO_UNITS_CEIL(d)                 \
+  (((d) + (PANGO_SCALE - 1)) & ~(PANGO_SCALE - 1))
+
 /**
  * PANGO_UNITS_ROUND:
  * @d: a dimension in Pango units.
diff --git a/pango/pangocairo-font.c b/pango/pangocairo-font.c
index 597a320d..fb36d294 100644
--- a/pango/pangocairo-font.c
+++ b/pango/pangocairo-font.c
@@ -817,13 +817,13 @@ _pango_cairo_font_private_glyph_extents_cache_init (PangoCairoFontPrivate *cf_pr
   if (cf_priv->is_hinted)
     {
       if (cf_priv->font_extents.y < 0)
-        cf_priv->font_extents.y = PANGO_UNITS_ROUND (cf_priv->font_extents.y - PANGO_SCALE/2);
+        cf_priv->font_extents.y = PANGO_UNITS_FLOOR (cf_priv->font_extents.y);
       else
-        cf_priv->font_extents.y = PANGO_UNITS_ROUND (cf_priv->font_extents.y + PANGO_SCALE/2);
+        cf_priv->font_extents.y = PANGO_UNITS_CEIL (cf_priv->font_extents.y);
       if (cf_priv->font_extents.height < 0)
-        cf_priv->font_extents.height = PANGO_UNITS_ROUND (cf_priv->font_extents.height - PANGO_SCALE/2);
+        cf_priv->font_extents.height = PANGO_UNITS_FLOOR (cf_priv->font_extents.height);
       else
-        cf_priv->font_extents.height = PANGO_UNITS_ROUND (cf_priv->font_extents.height + PANGO_SCALE/2);
+        cf_priv->font_extents.height = PANGO_UNITS_CEIL (cf_priv->font_extents.height);
     }
 
   if (PANGO_GRAVITY_IS_IMPROPER (cf_priv->gravity))


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