[pango/synthetic-slant: 1/2] Add pango_matrix_get_slant_ratio




commit d3283a7248e4893f85ea9593a34148b803670417
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Sep 11 12:30:46 2021 -0400

    Add pango_matrix_get_slant_ratio
    
    Add API to extract the slant ratio from a PangoMatrix.
    Tests included.

 pango/pango-matrix.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 pango/pango-matrix.h |  2 ++
 tests/testmatrix.c   | 22 ++++++++++++++++++++++
 3 files changed, 66 insertions(+)
---
diff --git a/pango/pango-matrix.c b/pango/pango-matrix.c
index a066eb57..42bacbeb 100644
--- a/pango/pango-matrix.c
+++ b/pango/pango-matrix.c
@@ -262,6 +262,48 @@ pango_matrix_get_font_scale_factors (const PangoMatrix *matrix,
     *yscale = minor;
 }
 
+/**
+ * pango_matrix_get_slant_ratio:
+ * @matrix: a `PangoMatrix`
+ *
+ * Gets the slant ratio of a matrix.
+ *
+ * For a simple shear matrix in the form:
+ *
+ *     1 λ
+ *     0 1
+ *
+ * this is simply λ.
+ *
+ * Returns: the slant ratio of @matrix
+ *
+ * Since: 1.50
+ */
+double
+pango_matrix_get_slant_ratio (const PangoMatrix *matrix)
+{
+  double x0, y0;
+  double x1, y1;
+  double l0, l1;
+  double d;
+  double angle;
+
+  x0 = 0;
+  y0 = 1;
+  pango_matrix_transform_distance (matrix, &x0, &y0);
+
+  x1 = 1;
+  y1 = 0;
+  pango_matrix_transform_distance (matrix, &x1, &y1);
+
+  l0 = sqrt (x0*x0 + y0*y0);
+  l1 = sqrt (x1*x1 + y1*y1);
+  d = x0 * x1 + y0 * y1;
+  angle = acos (d / (l0 * l1));
+
+  return 1. / tan (angle);
+}
+
 /**
  * pango_matrix_transform_distance:
  * @matrix: (nullable): a `PangoMatrix`
diff --git a/pango/pango-matrix.h b/pango/pango-matrix.h
index d4277401..cb53a422 100644
--- a/pango/pango-matrix.h
+++ b/pango/pango-matrix.h
@@ -121,6 +121,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_1_50
+double pango_matrix_get_slant_ratio (const PangoMatrix *matrix) G_GNUC_PURE;
 
 
 G_END_DECLS
diff --git a/tests/testmatrix.c b/tests/testmatrix.c
index 607f2b5f..aed25d16 100644
--- a/tests/testmatrix.c
+++ b/tests/testmatrix.c
@@ -21,6 +21,7 @@
 
 #include <glib.h>
 #include <pango/pango.h>
+#include <math.h>
 
 #define matrix_equal(m1, m2) \
   (G_APPROX_VALUE ((m1)->xx, (m2)->xx, 0.0001) && \
@@ -185,6 +186,26 @@ test_matrix_transform_pixel_rect (void)
   g_assert_cmpfloat_with_epsilon (rect.height, 2, 0.1);
 }
 
+static void
+test_matrix_slant_ratio (void)
+{
+  PangoMatrix m = (PangoMatrix) { 1, 0, 0.2, 1, 0, 0 };
+  double r;
+
+  r = pango_matrix_get_slant_ratio (&m);
+  g_assert_cmphex (r, ==, 0.2);
+
+  pango_matrix_rotate (&m, 45);
+
+  r = pango_matrix_get_slant_ratio (&m);
+  g_assert_cmphex (r, ==, 0.2);
+
+  pango_matrix_scale (&m, 2, 3);
+
+  r = pango_matrix_get_slant_ratio (&m);
+  g_assert_cmphex (r, ==, 0.2);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -199,6 +220,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/matrix/transform-distance", test_matrix_transform_distance);
   g_test_add_func ("/matrix/transform-rect", test_matrix_transform_rect);
   g_test_add_func ("/matrix/transform-pixel-rect", test_matrix_transform_pixel_rect);
+  g_test_add_func ("/matrix/slant-ratio", test_matrix_slant_ratio);
 
   return g_test_run ();
 }


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