[cairo-commit] 4 commits - src/cairo-output-stream.c src/cairo-pdf-surface.c

Adrian Johnson ajohnson at kemper.freedesktop.org
Sun Feb 11 05:05:08 PST 2007


 src/cairo-output-stream.c |    4 +
 src/cairo-pdf-surface.c   |  176 ++++++++++++++--------------------------------
 2 files changed, 61 insertions(+), 119 deletions(-)

New commits:
diff-tree ac01dcb5ad077d8ea08a93348fc23d4a891abaae (from d6f15b26009ff8e1d52a037265bcaa80c5ce6735)
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sun Feb 11 23:14:40 2007 +1030

    PDF: Remove dead code
    
    The function _cairo_pdf_surface_write_fonts is the
    original PDF TrueType font embedding function that was
    disabled in commit f500cef19f049a4a0ed296172618db2f26794932
    shortly before Type3 font support was added.
    
    TrueType font embedding was later reintroduced as new code
    making this function obsolete.

diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index a985fc6..1e5e521 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -2429,114 +2429,6 @@ _cairo_pdf_surface_emit_font_subsets (ca
     return CAIRO_STATUS_SUCCESS;
 }
 
-#if 0
-static cairo_status_t
-_cairo_pdf_surface_write_fonts (cairo_pdf_surface_t *surface)
-{
-    cairo_font_subset_t *font;
-    cairo_pdf_resource_t font_resource;
-    int num_fonts, i, j;
-    const char *data;
-    char *compressed;
-    unsigned long data_size, compressed_size;
-    cairo_pdf_resource_t stream, descriptor;
-    cairo_status_t status = CAIRO_STATUS_SUCCESS;
-
-    num_fonts = _cairo_array_num_elements (&surface->fonts);
-    for (i = 0; i < num_fonts; i++) {
-	_cairo_array_copy_element (&surface->fonts, i, &font);
-
-	status = _cairo_font_subset_generate (font, &data, &data_size);
-	if (status)
-	    goto fail;
-
-	compressed = compress_dup (data, data_size, &compressed_size);
-	if (compressed == NULL) {
-	    status = CAIRO_STATUS_NO_MEMORY;
-	    goto fail;
-	}
-
-	stream = _cairo_pdf_surface_new_object (surface);
-	_cairo_output_stream_printf (surface->output,
-				     "%d 0 obj\r\n"
-				     "<< /Filter /FlateDecode\r\n"
-				     "   /Length %lu\r\n"
-				     "   /Length1 %lu\r\n"
-				     ">>\r\n"
-				     "stream\r\n",
-				     stream.id,
-				     compressed_size,
-				     data_size);
-	_cairo_output_stream_write (surface->output, compressed, compressed_size);
-	_cairo_output_stream_printf (surface->output,
-				     "\r\n"
-				     "endstream\r\n"
-				     "endobj\r\n");
-	free (compressed);
-
-	descriptor = _cairo_pdf_surface_new_object (surface);
-	_cairo_output_stream_printf (surface->output,
-				     "%d 0 obj\r\n"
-				     "<< /Type /FontDescriptor\r\n"
-				     "   /FontName /7%s\r\n"
-				     "   /Flags 4\r\n"
-				     "   /FontBBox [ %ld %ld %ld %ld ]\r\n"
-				     "   /ItalicAngle 0\r\n"
-				     "   /Ascent %ld\r\n"
-				     "   /Descent %ld\r\n"
-				     "   /CapHeight 500\r\n"
-				     "   /StemV 80\r\n"
-				     "   /StemH 80\r\n"
-				     "   /FontFile2 %u 0 R\r\n"
-				     ">>\r\n"
-				     "endobj\r\n",
-				     descriptor.id,
-				     font->base_font,
-				     font->x_min,
-				     font->y_min,
-				     font->x_max,
-				     font->y_max,
-				     font->ascent,
-				     font->descent,
-				     stream.id);
-
-	font_resource.id = font->font_id;
-	_cairo_pdf_surface_update_object (surface, font_resource);
-	_cairo_output_stream_printf (surface->output,
-				     "%d 0 obj\r\n"
-				     "<< /Type /Font\r\n"
-				     "   /Subtype /TrueType\r\n"
-				     "   /BaseFont /%s\r\n"
-				     "   /FirstChar 0\r\n"
-				     "   /LastChar %d\r\n"
-				     "   /FontDescriptor %d 0 R\r\n"
-				     "   /Widths ",
-				     font->font_id,
-				     font->base_font,
-				     font->num_glyphs,
-				     descriptor.id);
-
-	_cairo_output_stream_printf (surface->output,
-				     "[");
-
-	for (j = 0; j < font->num_glyphs; j++)
-	    _cairo_output_stream_printf (surface->output,
-					 " %d",
-					 font->widths[j]);
-
-	_cairo_output_stream_printf (surface->output,
-				     " ]\r\n"
-				     ">>\r\n"
-				     "endobj\r\n");
-
-    fail:
-	_cairo_font_subset_destroy (font);
-    }
-
-    return status;
-}
-#endif
-
 static cairo_pdf_resource_t
 _cairo_pdf_surface_write_catalog (cairo_pdf_surface_t *surface)
 {
diff-tree d6f15b26009ff8e1d52a037265bcaa80c5ce6735 (from a2fefcc9e962e81a00fb16ae3d1bc29d9e61eaa0)
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sun Feb 11 22:20:58 2007 +1030

    Output-stream: Omit the minus sign from negative zero

diff --git a/src/cairo-output-stream.c b/src/cairo-output-stream.c
index f9e527f..9dd39a4 100644
--- a/src/cairo-output-stream.c
+++ b/src/cairo-output-stream.c
@@ -224,6 +224,10 @@ _cairo_dtostr (char *buffer, size_t size
   char *p;
   int decimal_len;
 
+  /* Omit the minus sign from negative zero. */
+  if (d == 0)
+      d = 0;
+
   snprintf (buffer, size, "%f", d);
 
   locale_data = localeconv ();
diff-tree a2fefcc9e962e81a00fb16ae3d1bc29d9e61eaa0 (from 9f47879ae31f003be3b32f9ecb3efb3486d72cc7)
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sun Feb 11 21:57:43 2007 +1030

    PDF: Compress Type3 fonts

diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index 61bfe15..a985fc6 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -2181,7 +2181,7 @@ _cairo_pdf_surface_emit_bitmap_glyph (ca
 	    return cairo_surface_status (&image->base);
     }
 
-    *glyph_ret = _cairo_pdf_surface_open_stream (surface, FALSE, NULL);
+    *glyph_ret = _cairo_pdf_surface_open_stream (surface, TRUE, NULL);
 
     _cairo_output_stream_printf (surface->output,
 				 "%f 0 %f %f %f %f d1\r\n",
diff-tree 9f47879ae31f003be3b32f9ecb3efb3486d72cc7 (from 9dce321a113871ade7de244d3abf9ed08f43dd85)
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sun Feb 11 21:50:46 2007 +1030

    PDF: Fix Type3 font metrics to make text selection work

diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index 490f9f7..61bfe15 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -2149,13 +2149,16 @@ static cairo_int_status_t
 _cairo_pdf_surface_emit_bitmap_glyph (cairo_pdf_surface_t	*surface,
 				      cairo_scaled_font_t	*scaled_font,
 				      unsigned long		 glyph_index,
-				      cairo_pdf_resource_t	*glyph_ret)
+				      cairo_pdf_resource_t	*glyph_ret,
+                                      cairo_box_t               *bbox,
+                                      double                    *width)
 {
     cairo_scaled_glyph_t *scaled_glyph;
     cairo_status_t status;
     cairo_image_surface_t *image;
     unsigned char *row, *byte;
     int rows, cols;
+    double x_advance, y_advance;
 
     status = _cairo_scaled_glyph_lookup (scaled_font,
 					 glyph_index,
@@ -2165,6 +2168,12 @@ _cairo_pdf_surface_emit_bitmap_glyph (ca
     if (status)
 	return status;
 
+    x_advance = scaled_glyph->metrics.x_advance;
+    y_advance = scaled_glyph->metrics.y_advance;
+    cairo_matrix_transform_distance (&scaled_font->ctm, &x_advance, &y_advance);
+    *bbox = scaled_glyph->bbox;
+    *width = x_advance;
+
     image = scaled_glyph->surface;
     if (image->format != CAIRO_FORMAT_A1) {
 	image = _cairo_image_surface_clone (image, CAIRO_FORMAT_A1);
@@ -2175,14 +2184,15 @@ _cairo_pdf_surface_emit_bitmap_glyph (ca
     *glyph_ret = _cairo_pdf_surface_open_stream (surface, FALSE, NULL);
 
     _cairo_output_stream_printf (surface->output,
-				 "0 0 %f %f %f %f d1\r\n",
+				 "%f 0 %f %f %f %f d1\r\n",
+                                 x_advance,
 				 _cairo_fixed_to_double (scaled_glyph->bbox.p1.x),
-				 - _cairo_fixed_to_double (scaled_glyph->bbox.p2.y),
+				 _cairo_fixed_to_double (scaled_glyph->bbox.p2.y),
 				 _cairo_fixed_to_double (scaled_glyph->bbox.p2.x),
-				 - _cairo_fixed_to_double (scaled_glyph->bbox.p1.y));
+				 _cairo_fixed_to_double (scaled_glyph->bbox.p1.y));
 
     _cairo_output_stream_printf (surface->output,
-				 "%f 0.0 0.0 %f %f %f cm\r\n",
+				 "%f 0 0 %f %f %f cm\r\n",
 				 _cairo_fixed_to_double (scaled_glyph->bbox.p2.x) - _cairo_fixed_to_double (scaled_glyph->bbox.p1.x),
 				 _cairo_fixed_to_double (scaled_glyph->bbox.p1.y) - _cairo_fixed_to_double (scaled_glyph->bbox.p2.y),
 				 _cairo_fixed_to_double (scaled_glyph->bbox.p1.x),
@@ -2221,7 +2231,9 @@ static void
 _cairo_pdf_surface_emit_glyph (cairo_pdf_surface_t	*surface,
 			       cairo_scaled_font_t	*scaled_font,
 			       unsigned long		 glyph_index,
-			       cairo_pdf_resource_t	*glyph_ret)
+			       cairo_pdf_resource_t	*glyph_ret,
+                               cairo_box_t              *bbox,
+                               double                   *width)
 {
     cairo_status_t status;
 
@@ -2233,7 +2245,9 @@ _cairo_pdf_surface_emit_glyph (cairo_pdf
 	status = _cairo_pdf_surface_emit_bitmap_glyph (surface,
 						       scaled_font,
 						       glyph_index,
-						       glyph_ret);
+						       glyph_ret,
+                                                       bbox,
+                                                       width);
 
     if (status)
 	_cairo_surface_set_error (&surface->base, status);
@@ -2246,7 +2260,10 @@ _cairo_pdf_surface_emit_type3_font_subse
     cairo_pdf_resource_t *glyphs, encoding, char_procs, subset_resource, to_unicode_stream;
     cairo_pdf_font_t font;
     cairo_matrix_t matrix;
+    double *widths;
     unsigned int i;
+    cairo_box_t font_bbox = {{0,0},{0,0}};
+    cairo_box_t bbox = {{0,0},{0,0}};
 
     glyphs = malloc (font_subset->num_glyphs * sizeof (cairo_pdf_resource_t));
     if (glyphs == NULL) {
@@ -2254,11 +2271,35 @@ _cairo_pdf_surface_emit_type3_font_subse
 	return CAIRO_STATUS_NO_MEMORY;
     }
 
+    widths = malloc (font_subset->num_glyphs * sizeof (double));
+    if (widths == NULL) {
+        free (glyphs);
+	_cairo_surface_set_error (&surface->base, CAIRO_STATUS_NO_MEMORY);
+	return CAIRO_STATUS_NO_MEMORY;
+    }
+
     for (i = 0; i < font_subset->num_glyphs; i++) {
 	_cairo_pdf_surface_emit_glyph (surface,
 				       font_subset->scaled_font,
 				       font_subset->glyphs[i],
-				       &glyphs[i]);
+				       &glyphs[i],
+                                       &bbox,
+                                       &widths[i]);
+        if (i == 0) {
+            font_bbox.p1.x = bbox.p1.x;
+            font_bbox.p1.y = bbox.p1.y;
+            font_bbox.p2.x = bbox.p2.x;
+            font_bbox.p2.y = bbox.p2.y;
+        } else {
+            if (bbox.p1.x < font_bbox.p1.x)
+                font_bbox.p1.x = bbox.p1.x;
+            if (bbox.p1.y < font_bbox.p1.y)
+                font_bbox.p1.y = bbox.p1.y;
+            if (bbox.p2.x > font_bbox.p2.x)
+                font_bbox.p2.x = bbox.p2.x;
+            if (bbox.p2.y > font_bbox.p2.y)
+                font_bbox.p2.y = bbox.p2.y;
+        }
     }
 
     encoding = _cairo_pdf_surface_new_object (surface);
@@ -2297,13 +2338,17 @@ _cairo_pdf_surface_emit_type3_font_subse
 				 "%d 0 obj\r\n"
 				 "<< /Type /Font\r\n"
 				 "   /Subtype /Type3\r\n"
-				 "   /FontBBox [0 0 0 0]\r\n"
+				 "   /FontBBox [%f %f %f %f]\r\n"
 				 "   /FontMatrix [ %f %f %f %f 0 0 ]\r\n"
 				 "   /Encoding %d 0 R\r\n"
 				 "   /CharProcs %d 0 R\r\n"
 				 "   /FirstChar 0\r\n"
 				 "   /LastChar %d\r\n",
 				 subset_resource.id,
+				 _cairo_fixed_to_double (font_bbox.p1.x),
+				 _cairo_fixed_to_double (font_bbox.p1.y),
+				 _cairo_fixed_to_double (font_bbox.p2.x),
+				 _cairo_fixed_to_double (font_bbox.p2.y),
 				 matrix.xx,
 				 matrix.yx,
 				 -matrix.xy,
@@ -2315,9 +2360,10 @@ _cairo_pdf_surface_emit_type3_font_subse
     _cairo_output_stream_printf (surface->output,
 				 "   /Widths [");
     for (i = 0; i < font_subset->num_glyphs; i++)
-	_cairo_output_stream_printf (surface->output, " 0");
+	_cairo_output_stream_printf (surface->output, " %f", widths[i]);
     _cairo_output_stream_printf (surface->output,
 				 "]\r\n");
+    free (widths);
 
     if (to_unicode_stream.id != 0)
         _cairo_output_stream_printf (surface->output,


More information about the cairo-commit mailing list