[cairo-commit] 2 commits - src/cairo-pdf-surface.c test/Makefile.sources test/meson.build test/pdf-operators-text.c test/reference

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Jul 27 20:13:25 UTC 2021


 src/cairo-pdf-surface.c                            |   24 +--
 test/Makefile.sources                              |    1 
 test/meson.build                                   |    1 
 test/pdf-operators-text.c                          |  157 +++++++++++++++++++++
 test/reference/pdf-operators-text.image16.ref.png  |binary
 test/reference/pdf-operators-text.pdf.ref.png      |binary
 test/reference/pdf-operators-text.ps.ref.png       |binary
 test/reference/pdf-operators-text.quartz.xfail.png |binary
 test/reference/pdf-operators-text.ref.png          |binary
 test/reference/pdf-operators-text.script.xfail.png |binary
 test/reference/pdf-operators-text.svg.ref.png      |binary
 test/reference/pdf-operators-text.xcb.ref.png      |binary
 test/reference/pdf-operators-text.xlib.ref.png     |binary
 13 files changed, 171 insertions(+), 12 deletions(-)

New commits:
commit d60e3f3501f39b0487179601fa9dc4b703a49c93
Merge: 220222c5f 6fbfc9d92
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jul 27 20:13:23 2021 +0000

    Merge branch 'issue-426' into 'master'
    
    pdf: use floating point widths when emitting fonts
    
    Closes #426
    
    See merge request cairo/cairo!213

commit 6fbfc9d927fb9f64527f568050aa9e91485290e0
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jul 27 20:13:23 2021 +0000

    pdf: use floating point widths when emitting fonts

diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index a9caed8ff..7a13ecf57 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -5557,8 +5557,8 @@ _cairo_pdf_surface_emit_cff_font (cairo_pdf_surface_t		*surface,
 	    int glyph = font_subset->latin_to_subset_glyph_index[i];
 	    if (glyph > 0) {
 		_cairo_output_stream_printf (surface->output,
-					     " %ld",
-					     (long)(subset->widths[glyph]*PDF_UNITS_PER_EM));
+					     " %f",
+					     (subset->widths[glyph]*PDF_UNITS_PER_EM));
 	    } else {
 		_cairo_output_stream_printf (surface->output, " 0");
 	    }
@@ -5599,8 +5599,8 @@ _cairo_pdf_surface_emit_cff_font (cairo_pdf_surface_t		*surface,
 
 	for (i = 0; i < font_subset->num_glyphs; i++)
 	    _cairo_output_stream_printf (surface->output,
-					 " %ld",
-					 (long)(subset->widths[i]*PDF_UNITS_PER_EM));
+					 " %f",
+					 (subset->widths[i]*PDF_UNITS_PER_EM));
 
 	_cairo_output_stream_printf (surface->output,
 				     " ]]\n"
@@ -5796,8 +5796,8 @@ _cairo_pdf_surface_emit_type1_font (cairo_pdf_surface_t		*surface,
 	    int glyph = font_subset->latin_to_subset_glyph_index[i];
 	    if (glyph > 0) {
 		_cairo_output_stream_printf (surface->output,
-					     " %ld",
-					     (long)(subset->widths[glyph]*PDF_UNITS_PER_EM));
+					     " %f",
+					     (subset->widths[glyph]*PDF_UNITS_PER_EM));
 	    } else {
 		_cairo_output_stream_printf (surface->output, " 0");
 	    }
@@ -5805,8 +5805,8 @@ _cairo_pdf_surface_emit_type1_font (cairo_pdf_surface_t		*surface,
     } else {
 	for (i = 0; i < font_subset->num_glyphs; i++)
 	    _cairo_output_stream_printf (surface->output,
-					 " %ld",
-					 (long)(subset->widths[i]*PDF_UNITS_PER_EM));
+					 " %f",
+					 (subset->widths[i]*PDF_UNITS_PER_EM));
     }
 
     _cairo_output_stream_printf (surface->output,
@@ -6004,8 +6004,8 @@ _cairo_pdf_surface_emit_truetype_font_subset (cairo_pdf_surface_t		*surface,
 	    int glyph = font_subset->latin_to_subset_glyph_index[i];
 	    if (glyph > 0) {
 		_cairo_output_stream_printf (surface->output,
-					     " %ld",
-					     (long)(subset.widths[glyph]*PDF_UNITS_PER_EM));
+					     " %f",
+					     (subset.widths[glyph]*PDF_UNITS_PER_EM));
 	    } else {
 		_cairo_output_stream_printf (surface->output, " 0");
 	    }
@@ -6048,8 +6048,8 @@ _cairo_pdf_surface_emit_truetype_font_subset (cairo_pdf_surface_t		*surface,
 
 	for (i = 0; i < font_subset->num_glyphs; i++)
 	    _cairo_output_stream_printf (surface->output,
-					 " %ld",
-					 (long)(subset.widths[i]*PDF_UNITS_PER_EM));
+					 " %f",
+					 (subset.widths[i]*PDF_UNITS_PER_EM));
 
 	_cairo_output_stream_printf (surface->output,
 				     " ]]\n"
diff --git a/test/Makefile.sources b/test/Makefile.sources
index 493141203..0c8d4923d 100644
--- a/test/Makefile.sources
+++ b/test/Makefile.sources
@@ -432,6 +432,7 @@ quartz_surface_test_sources = quartz-surface-source.c
 pdf_surface_test_sources = \
 	pdf-features.c \
 	pdf-mime-data.c \
+	pdf-operators-text.c \
 	pdf-surface-source.c \
 	pdf-tagged-text.c
 
diff --git a/test/meson.build b/test/meson.build
index 0dd555b5e..6b5eba754 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -438,6 +438,7 @@ test_quartz_sources = [
 test_pdf_sources = [
   'pdf-features.c',
   'pdf-mime-data.c',
+  'pdf-operators-text.c',
   'pdf-surface-source.c',
   'pdf-tagged-text.c',
 ]
diff --git a/test/pdf-operators-text.c b/test/pdf-operators-text.c
new file mode 100644
index 000000000..556b9d7d8
--- /dev/null
+++ b/test/pdf-operators-text.c
@@ -0,0 +1,157 @@
+/*
+ * Copyright © 2021 Adrian Johnson
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Red Hat, Inc. not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Red Hat, Inc. makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Adrian Johnson <ajohnson at redneon.com>
+ */
+
+/* Test pdf-operators text positioning. This test is designed to expose rounding
+ * errors in the PDF operations for relative positioning in very long strings.
+ *
+ * The text width is expected to match the width of the rectangle
+ * enclosing the text.
+ */
+
+#include "cairo-test.h"
+
+#define WIDTH  10000
+#define HEIGHT 60
+
+/* Using a non integer size helps expose rounding errors */
+#define FONT_SIZE 10.12345678912345
+
+#define WORD "Text"
+#define NUM_WORDS 450
+
+#define BORDER 10
+
+static cairo_user_data_key_t font_face_key;
+
+static cairo_status_t
+user_font_init (cairo_scaled_font_t  *scaled_font,
+                cairo_t              *cr,
+                cairo_font_extents_t *metrics)
+{
+    cairo_font_face_t *font_face = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
+                                                                  &font_face_key);
+    cairo_set_font_face (cr, font_face);
+    cairo_font_extents (cr, metrics);
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static cairo_status_t
+user_font_render_glyph (cairo_scaled_font_t  *scaled_font,
+                        unsigned long         index,
+                        cairo_t              *cr,
+                        cairo_text_extents_t *metrics)
+{
+    char text[2];
+    cairo_font_face_t *font_face = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
+                                                                  &font_face_key);
+
+    text[0] = index; /* Only using ASCII for this test */
+    text[1] = 0;
+    cairo_set_font_face (cr, font_face);
+    cairo_text_extents (cr, text, metrics);
+    cairo_text_path (cr, text);
+    cairo_fill (cr);
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static cairo_font_face_t *
+create_user_font_face (cairo_font_face_t *orig_font)
+{
+    cairo_font_face_t *user_font_face;
+
+    user_font_face = cairo_user_font_face_create ();
+    cairo_user_font_face_set_init_func (user_font_face, user_font_init);
+    cairo_user_font_face_set_render_glyph_func (user_font_face, user_font_render_glyph);
+    cairo_font_face_set_user_data (user_font_face, &font_face_key, (void*) orig_font, NULL);
+    return user_font_face;
+}
+
+static void
+draw_text (cairo_t *cr, const char *text)
+{
+    cairo_text_extents_t extents;
+
+    cairo_move_to (cr, BORDER, BORDER);
+    cairo_set_source_rgb (cr, 0, 0, 0);
+
+    cairo_show_text (cr, text);
+    cairo_text_extents (cr, text,&extents);
+
+    cairo_rectangle (cr,
+                     BORDER + extents.x_bearing,
+                     BORDER + extents.y_bearing,
+                     extents.width,
+                     extents.height);
+    cairo_set_line_width (cr, 1);
+    cairo_stroke (cr);
+}
+
+
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+    int i;
+    char *text;
+    cairo_font_face_t *font_face;
+
+    cairo_set_source_rgb (cr, 1, 1, 1);
+    cairo_paint (cr);
+
+    cairo_select_font_face (cr, "Dejavu Sans",
+			    CAIRO_FONT_SLANT_NORMAL,
+			    CAIRO_FONT_WEIGHT_NORMAL);
+
+    cairo_set_font_size (cr, FONT_SIZE);
+
+    text = malloc (strlen(WORD) * NUM_WORDS + 1);
+    for (i = 0; i < NUM_WORDS; i++)
+	strcat (text, WORD);
+
+    cairo_save (cr);
+    cairo_translate (cr, BORDER, BORDER);
+    draw_text (cr, text);
+    cairo_restore (cr);
+
+    font_face = create_user_font_face (cairo_get_font_face (cr));
+    cairo_set_font_face (cr, font_face);
+    cairo_font_face_destroy (font_face);
+    cairo_set_font_size (cr, FONT_SIZE);
+
+    cairo_save (cr);
+    cairo_translate (cr, BORDER, BORDER*3);
+    draw_text (cr, text);
+    cairo_restore (cr);
+
+    free (text);
+    return CAIRO_TEST_SUCCESS;
+}
+
+CAIRO_TEST (pdf_operators_text,
+	    "Test pdf-operators.c glyph positioning",
+	    "pdf", /* keywords */
+	    NULL, /* requirements */
+	    WIDTH, HEIGHT,
+	    NULL, draw)
diff --git a/test/reference/pdf-operators-text.image16.ref.png b/test/reference/pdf-operators-text.image16.ref.png
new file mode 100644
index 000000000..e85ee006c
Binary files /dev/null and b/test/reference/pdf-operators-text.image16.ref.png differ
diff --git a/test/reference/pdf-operators-text.pdf.ref.png b/test/reference/pdf-operators-text.pdf.ref.png
new file mode 100644
index 000000000..b52b833f0
Binary files /dev/null and b/test/reference/pdf-operators-text.pdf.ref.png differ
diff --git a/test/reference/pdf-operators-text.ps.ref.png b/test/reference/pdf-operators-text.ps.ref.png
new file mode 100644
index 000000000..e6c1b05a1
Binary files /dev/null and b/test/reference/pdf-operators-text.ps.ref.png differ
diff --git a/test/reference/pdf-operators-text.quartz.xfail.png b/test/reference/pdf-operators-text.quartz.xfail.png
new file mode 100644
index 000000000..62e006aa2
Binary files /dev/null and b/test/reference/pdf-operators-text.quartz.xfail.png differ
diff --git a/test/reference/pdf-operators-text.ref.png b/test/reference/pdf-operators-text.ref.png
new file mode 100644
index 000000000..970f3fd99
Binary files /dev/null and b/test/reference/pdf-operators-text.ref.png differ
diff --git a/test/reference/pdf-operators-text.script.xfail.png b/test/reference/pdf-operators-text.script.xfail.png
new file mode 100644
index 000000000..ae4df9932
Binary files /dev/null and b/test/reference/pdf-operators-text.script.xfail.png differ
diff --git a/test/reference/pdf-operators-text.svg.ref.png b/test/reference/pdf-operators-text.svg.ref.png
new file mode 100644
index 000000000..c8b6b73a2
Binary files /dev/null and b/test/reference/pdf-operators-text.svg.ref.png differ
diff --git a/test/reference/pdf-operators-text.xcb.ref.png b/test/reference/pdf-operators-text.xcb.ref.png
new file mode 100644
index 000000000..bbb8a446d
Binary files /dev/null and b/test/reference/pdf-operators-text.xcb.ref.png differ
diff --git a/test/reference/pdf-operators-text.xlib.ref.png b/test/reference/pdf-operators-text.xlib.ref.png
new file mode 100644
index 000000000..bbb8a446d
Binary files /dev/null and b/test/reference/pdf-operators-text.xlib.ref.png differ


More information about the cairo-commit mailing list