[cairo] PDF text doesn't render

Behdad Esfahbod behdad at behdad.org
Thu Sep 20 12:23:46 PDT 2007


On Thu, 2007-09-20 at 15:14 -0400, Lichtl, Adam wrote:
> Hello,
> 
> I have been able to reproduce most of the examples in the (extremely
> helpful) tutorial at http://www.cairographics.org/tutorial using both a
> PostScript surface and a PDF surface.  Unfortunately, I am unable to get
> the text to show up on the PDF surface.  I have been poking around the
> web and also this mailing list's archives, but am unable to get it to
> work.  I would appreciate any assistance.  Below is the complete source
> code for the program (it is pretty simple) and attached is the output
> pdf.  Please help!

Your attached PDF renders fine here, both in acroread and Evince.


> #include <stdio.h>
> #include <string.h>
> #include <cairo/cairo.h>
> #include <cairo/cairo-pdf.h>
> 
> int main() {
>    FILE *file = fopen("out.pdf", "w");

You don't need this line.  In fact it may corrupt your output.
cairo_pdf_surface_create() opens the file itself.

>    cairo_surface_t *surface = cairo_pdf_surface_create("out.pdf", 2600,
> 100);
>    cairo_t *cr = cairo_create(surface);
>    cairo_surface_destroy(surface);
>    cairo_scale(cr, 100, 100);
> 
>    int i;
>    char letter[2];
>    cairo_font_extents_t fe;
>    cairo_text_extents_t te;
>    char alphabet[] = "AbCdEfGhIjKlMnOpQrStUvWxYz";
> 
>    cairo_set_source_rgb(cr, 0, 0, 0);
>    cairo_select_font_face (cr, "Times-Roman", CAIRO_FONT_SLANT_NORMAL,
>                                               CAIRO_FONT_WEIGHT_BOLD);
>    cairo_set_font_size(cr, 1.0);
>    cairo_font_extents(cr, &fe);
>    for (i = 0; i < strlen(alphabet); i++) {
>      *letter = '\0';
>      strncat(letter, alphabet + i, 1);
> 
>      cairo_text_extents(cr, letter, &te);
>      cairo_move_to(cr, i + 0.5 - te.x_bearing - te.width / 2,
>                            0.5 - fe.descent + fe.height / 2);
>      cairo_show_text (cr, letter);
>    }
> 
>    cairo_show_page(cr);
>    cairo_destroy(cr);
>    fclose(file);

Ditto for the fclose().

>    return 0;
> }
> 
> This program works just fine if "pdf" is replaced with "ps" everywhere.
>   Also, all other drawing functions in the tutorial work fine on the pdf
> surface - it is just that the text isn't showing up.  I have tried
> playing around with the font name in the cairo_select_font_face command,
> and have also tried omitting it completely.
> 
> Thank you,
> -Adam
> 
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo
-- 
behdad
http://behdad.org/

"Those who would give up Essential Liberty to purchase a little
 Temporary Safety, deserve neither Liberty nor Safety."
        -- Benjamin Franklin, 1759





More information about the cairo mailing list