[pango/pango2: 8/301] matrix: Some improvements




commit a597e3379e503b956c2495068f1c7317f6574501
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jun 10 17:05:37 2022 -0400

    matrix: Some improvements
    
    Add a function to the rotation of a matrix,
    and improve our calculation of slant ratio.

 pango/pango-matrix.c | 51 +++++++++++++++++++++++++++++++++++++-----------
 pango/pango-matrix.h |  2 ++
 tests/testmatrix.c   | 55 ++++++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 91 insertions(+), 17 deletions(-)
---
diff --git a/pango/pango-matrix.c b/pango/pango-matrix.c
index 90d1df3a2..b98b139dc 100644
--- a/pango/pango-matrix.c
+++ b/pango/pango-matrix.c
@@ -262,6 +262,32 @@ pango_matrix_get_font_scale_factors (const PangoMatrix *matrix,
     *yscale = minor;
 }
 
+#define RAD_TO_DEG(x) ((x)/G_PI * 180)
+
+/**
+ * pango_matrix_get_rotation:
+ * @matrix: a `PangoMatrix`
+ *
+ * Returns the angle (in degrees) that this
+ * matrix rotates the X axis by.
+ *
+ * For font matrices, this is typically zero.
+ *
+ * Returns: the rotation of @matrix
+ */
+double
+pango_matrix_get_rotation (const PangoMatrix *matrix)
+{
+  double x, y;
+
+  x = 1;
+  y = 0;
+
+  pango_matrix_transform_distance (matrix, &x, &y);
+
+  return RAD_TO_DEG (acos (x /  sqrtf (x*x + y*y)));
+}
+
 /**
  * pango_matrix_get_slant_ratio:
  * @matrix: a `PangoMatrix`
@@ -282,18 +308,21 @@ pango_matrix_get_font_scale_factors (const PangoMatrix *matrix,
 double
 pango_matrix_get_slant_ratio (const PangoMatrix *matrix)
 {
-  double x0, y0;
-  double x1, y1;
-
-  x0 = 0;
-  y0 = 1;
-  pango_matrix_transform_distance (matrix, &x0, &y0);
-
-  x1 = 1;
-  y1 = 0;
-  pango_matrix_transform_distance (matrix, &x1, &y1);
+  if (matrix)
+    {
+      double a = matrix->xx;
+      double b = matrix->xy;
+      double c = matrix->yx;
+      double d = matrix->yy;
+
+      if (c != 0 || d != 0)
+        {
+          double s = sqrtf (c * c + d * d);
+          return (a*c + b*d) / (s*s);
+        }
+    }
 
-  return (x0 * x1 + y0 * y1) / (x0 * x0 + y0 * y0);
+  return 0;
 }
 
 /**
diff --git a/pango/pango-matrix.h b/pango/pango-matrix.h
index e47afd1cd..cce6569cb 100644
--- a/pango/pango-matrix.h
+++ b/pango/pango-matrix.h
@@ -125,6 +125,8 @@ double pango_matrix_get_font_scale_factor (const PangoMatrix *matrix) G_GNUC_PUR
 PANGO_AVAILABLE_IN_1_38
 void pango_matrix_get_font_scale_factors (const PangoMatrix *matrix,
                                          double *xscale, double *yscale);
+PANGO_AVAILABLE_IN_ALL
+double pango_matrix_get_rotation (const PangoMatrix *matrix) G_GNUC_PURE;
 PANGO_AVAILABLE_IN_1_50
 double pango_matrix_get_slant_ratio (const PangoMatrix *matrix) G_GNUC_PURE;
 
diff --git a/tests/testmatrix.c b/tests/testmatrix.c
index 1ba79839a..aa3091936 100644
--- a/tests/testmatrix.c
+++ b/tests/testmatrix.c
@@ -186,28 +186,71 @@ test_matrix_transform_pixel_rect (void)
   g_assert_cmpfloat_with_epsilon (rect.height, 2, 0.1);
 }
 
+static void
+pango_matrix_postrotate (PangoMatrix *m,
+                         double       angle)
+{
+  PangoMatrix rot = (PangoMatrix) PANGO_MATRIX_INIT;
+
+  pango_matrix_rotate (&rot, angle);
+  pango_matrix_concat (&rot, m);
+  *m = rot;
+}
+
 static void
 test_matrix_slant_ratio (void)
 {
-  PangoMatrix m = (PangoMatrix) { 1, 0, 0.2, 1, 0, 0 };
+  PangoMatrix m = (PangoMatrix) { 1, 0.2, 0, 1, 0, 0 };
   PangoMatrix m2 = (PangoMatrix) { 1, 0.4, 0, 1, 0, 0 };
+  PangoMatrix m3 = (PangoMatrix) { 1, 0.3, 0, 2, 0, 0 };
+  double a;
+  double sx, sy;
   double r;
 
+  a = pango_matrix_get_rotation (&m);
+  g_assert_cmpfloat_with_epsilon (a, 0, 0.001);
+
+  pango_matrix_get_font_scale_factors (&m, &sx, &sy);
+  g_assert_cmpfloat_with_epsilon (sx, 1, 0.001);
+  g_assert_cmpfloat_with_epsilon (sy, 1, 0.001);
+
   r = pango_matrix_get_slant_ratio (&m);
-  g_assert_cmphex (r, ==, 0.2);
+  g_assert_cmpfloat_with_epsilon (r, 0.2, 0.001);
 
-  pango_matrix_rotate (&m, 45);
+  pango_matrix_postrotate (&m, 45);
+
+  a = pango_matrix_get_rotation (&m);
+  g_assert_cmpfloat_with_epsilon (a, 45, 0.001);
+
+  pango_matrix_postrotate (&m, -a);
+
+  pango_matrix_get_font_scale_factors (&m, &sx, &sy);
+  g_assert_cmpfloat_with_epsilon (sx, 1, 0.001);
+  g_assert_cmpfloat_with_epsilon (sy, 1, 0.001);
 
   r = pango_matrix_get_slant_ratio (&m);
-  g_assert_cmphex (r, ==, 0.2);
+  g_assert_cmpfloat_with_epsilon (r, 0.2, 0.001);
 
   pango_matrix_scale (&m, 2, 3);
 
+  a = pango_matrix_get_rotation (&m);
+  g_assert_cmpfloat_with_epsilon (a, 0, 0.001);
+
+  pango_matrix_get_font_scale_factors (&m, &sx, &sy);
+  g_assert_cmpfloat_with_epsilon (sx, 2, 0.001);
+  g_assert_cmpfloat_with_epsilon (sy, 3, 0.001);
+
+  pango_matrix_scale (&m, 1/sx, 1/sy);
+
   r = pango_matrix_get_slant_ratio (&m);
-  g_assert_cmphex (r, ==, 0.2);
+  g_assert_cmpfloat_with_epsilon (r, 0.2, 0.001);
 
   r = pango_matrix_get_slant_ratio (&m2);
-  g_assert_cmphex (r, ==, 0.4);
+  g_assert_cmpfloat_with_epsilon (r, 0.4, 0.001);
+
+  r = pango_matrix_get_slant_ratio (&m3);
+
+  g_assert_cmpfloat_with_epsilon (r, 0.15, 0.001);
 }
 
 int


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