[cairo-commit] src/cairo-pdf-interchange.c

Adrian Johnson ajohnson at kemper.freedesktop.org
Mon Aug 21 11:54:46 UTC 2017


 src/cairo-pdf-interchange.c |   92 ++++++++++++++++++++++++--------------------
 1 file changed, 52 insertions(+), 40 deletions(-)

New commits:
commit 4c12e2aec38eb03d17787ece5a5566af902d16b7
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Mon Aug 21 21:23:45 2017 +0930

    pdf: Don't emit /PageLabel dict when no labels defined

diff --git a/src/cairo-pdf-interchange.c b/src/cairo-pdf-interchange.c
index 3d81c771..b7ce1dca 100644
--- a/src/cairo-pdf-interchange.c
+++ b/src/cairo-pdf-interchange.c
@@ -654,57 +654,69 @@ cairo_pdf_interchange_write_page_labels (cairo_pdf_surface_t *surface)
     char *prev_prefix;
     int num, prev_num;
     cairo_int_status_t status;
+    cairo_bool_t has_labels;
 
+    /* Check if any labels defined */
     num_elems = _cairo_array_num_elements (&surface->page_labels);
-    if (num_elems > 0) {
-	surface->page_labels_res = _cairo_pdf_surface_new_object (surface);
-	_cairo_output_stream_printf (surface->output,
-				     "%d 0 obj\n"
-				     "<< /Nums [\n",
-				     surface->page_labels_res.id);
-	prefix = NULL;
-	prev_prefix = NULL;
-	num = 0;
-	prev_num = 0;
-	for (i = 0; i < num_elems; i++) {
-	    _cairo_array_copy_element (&surface->page_labels, i, &label);
-	    if (label) {
-		prefix = split_label (label, &num);
-	    } else {
-		prefix = NULL;
-		num = i + 1;
-	    }
+    has_labels = FALSE;
+    for (i = 0; i < num_elems; i++) {
+	_cairo_array_copy_element (&surface->page_labels, i, &label);
+	if (label) {
+	    has_labels = TRUE;
+	    break;
+	}
+    }
 
-	    if (!strcmp_null (prefix, prev_prefix) || num != prev_num + 1) {
-		_cairo_output_stream_printf (surface->output,  "   %d << ", i);
+    if (!has_labels)
+	return CAIRO_STATUS_SUCCESS;
 
-		if (num)
-		    _cairo_output_stream_printf (surface->output,  "/S /D /St %d ", num);
+    surface->page_labels_res = _cairo_pdf_surface_new_object (surface);
+    _cairo_output_stream_printf (surface->output,
+				 "%d 0 obj\n"
+				 "<< /Nums [\n",
+				 surface->page_labels_res.id);
+    prefix = NULL;
+    prev_prefix = NULL;
+    num = 0;
+    prev_num = 0;
+    for (i = 0; i < num_elems; i++) {
+	_cairo_array_copy_element (&surface->page_labels, i, &label);
+	if (label) {
+	    prefix = split_label (label, &num);
+	} else {
+	    prefix = NULL;
+	    num = i + 1;
+	}
 
-		if (prefix) {
-		    char *s;
-		    status = _cairo_utf8_to_pdf_string (prefix, &s);
-		    if (unlikely (status))
-			return status;
+	if (!strcmp_null (prefix, prev_prefix) || num != prev_num + 1) {
+	    _cairo_output_stream_printf (surface->output,  "   %d << ", i);
 
-		    _cairo_output_stream_printf (surface->output,  "/P %s ", s);
-		    free (s);
-		}
+	    if (num)
+		_cairo_output_stream_printf (surface->output,  "/S /D /St %d ", num);
+
+	    if (prefix) {
+		char *s;
+		status = _cairo_utf8_to_pdf_string (prefix, &s);
+		if (unlikely (status))
+		    return status;
 
-		_cairo_output_stream_printf (surface->output,  ">>\n");
+		_cairo_output_stream_printf (surface->output,  "/P %s ", s);
+		free (s);
 	    }
-	    free (prev_prefix);
-	    prev_prefix = prefix;
-	    prefix = NULL;
-	    prev_num = num;
+
+	    _cairo_output_stream_printf (surface->output,  ">>\n");
 	}
-	free (prefix);
 	free (prev_prefix);
-	_cairo_output_stream_printf (surface->output,
-				     "  ]\n"
-				     ">>\n"
-				     "endobj\n");
+	prev_prefix = prefix;
+	prefix = NULL;
+	prev_num = num;
     }
+    free (prefix);
+    free (prev_prefix);
+    _cairo_output_stream_printf (surface->output,
+				 "  ]\n"
+				 ">>\n"
+				 "endobj\n");
 
     return CAIRO_STATUS_SUCCESS;
 }


More information about the cairo-commit mailing list