[cairo] PDF Issue in OS X Preview

Ken Treis ken at miriamtech.com
Wed Oct 25 09:55:25 PDT 2006


I'm generating PDFs using the Ruby bindings and cairo-1.2.4.  
Everything works fine for me until I try to set scaling. The  
resulting PDF files look fine in Acrobat Reader but not in OS X Preview.

At this point, I'm just trying to draw a couple of lines. Here's the  
snippet of test code that I'm using:

> require 'cairo'
>
> def drawOn(cr)
> 	cr.set_source_rgb(1.0, 1.0, 1.0)
> 	cr.paint
>
> 	cr.set_source_rgba(0.0, 0.0, 0.0, 1.0)
> 	cr.set_line_join(Cairo::LINE_JOIN_MITER)
> 	cr.set_line_width(1)
>
> 	cr.move_to(100, 100)
> 	cr.line_to(100, 200)
> 	cr.stroke
>
> 	cr.move_to(200, 100)
> 	cr.line_to(200, 200)
> 	cr.stroke
>
> 	cr.show_page
> 	cr.target.finish
> end
>
> surface = Cairo::PDFSurface.new("twoLines-1x1.pdf", 11*72, 8.5*72)
> context = Cairo::Context.new(surface)
> drawOn(context)
>
> surface = Cairo::PDFSurface.new('twoLines-2x2.pdf', 11*72, 8.5*72)
> context = Cairo::Context.new(surface)
> context.scale(2, 2)
> drawOn(context)

Here are the files that it generated:

	http://www.miriamtech.com/twoLines-1x1.pdf
	http://www.miriamtech.com/twoLines-2x2.pdf

In OS X Preview, the 2x2 PDF shows the lines in the same place as the  
1x1. The lines themselves have double the line width, but their  
coordinates were not scaled.

	http://www.miriamtech.com/screenshot-1x1.png
	http://www.miriamtech.com/screenshot-2x2.png

But yet Acrobat Reader shows everything fine -- it seems to scale the  
coordinates as well as the line widths.

I don't know much about PDF internals, but I've done some PostScript  
work (some time ago). For grins, I generated PostScript versions of  
the same. In the resulting PostScript, a line is drawn like this (1x1):

> 100 100 moveto 100 200 lineto [1 0 0 1 0 0] concat
> 1 setlinewidth
> 0 setlinecap
> 0 setlinejoin
> 10 setmiterlimit
> stroke

or like this (2x2):

> 200 200 moveto 200 400 lineto [2 0 0 2 0 0] concat
> 1 setlinewidth
> 0 setlinecap
> 0 setlinejoin
> 10 setmiterlimit
> stroke

So the cairo PostScript backend scales the coordinates before putting  
them in the file. Contrast this with the PDF backend, which draws  
lines like this (1x1):

> 10 M 100 100 m 100 200 l q 1 0 0 1 0 0 cm
> S Q

or like this (2x2):

> 10 M 100 100 m 100 200 l q 2 0 0 2 0 0 cm
> S Q

In other words, the coordinates aren't being scaled by cairo before  
it drops them in the file. I'll confess again that I don't know much  
about how PDF internals work. And even if this were the problem, it's  
strange that Acrobat displays them properly.

Any help/ideas would be greatly appreciated. Thanks in advance.

--
Ken Treis
Miriam Technologies, Inc.



More information about the cairo mailing list