File: cairo-example.c

package info (click to toggle)
pstoedit 4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: cpp: 30,257; perl: 1,693; ansic: 1,218; java: 860; makefile: 295; sh: 82; awk: 15
file content (147 lines) | stat: -rw-r--r-- 4,111 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* 
   This file is part of pstoedit
   
   Copyright (C) 2009 Dan McMahill dan_AT_mcmahill_DOT_net
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

/*
 * This is a toy example of rendering an image with cairo.
 *
 * To use the example, run
 *
 *   pstoedit -f somefile.ps sample.c
 *
 * to generate the C code file "sample.c"
 *
 * Now compile the program with:
 *
 *   gcc -g -Wall -O2 `pkg-config --cflags cairo cairo-pdf cairo-xlib` -c sample.c
 *   gcc -g -Wall -O2 `pkg-config --cflags cairo cairo-pdf cairo-xlib` -c test.c
 *   gcc -g -Wall -O2 -o testprog test.o sample.o `pkg-config --libs cairo cairo-pdf cairo-xlib`
 */


#include<cairo.h>
#include<cairo-pdf.h>
#include<cairo-xlib.h>
#include<X11/Xlib.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>



/*
 * here "myfig" is the default for the cairo driver
 * "myfig" could have been changed to something else by using
 *
 *   pstoedit -f "cairo: -funcname othername" somefile.ps sample.c
 *
 * when running pstoedit
 */
#include "sample.h"

void do_xlib(void)
{
  Display *dpy;
  Window rootwin;
  Window win;
  XEvent e;
  int scr;
  cairo_surface_t *cs;
  int i;
  char tmps[128];

  if(!(dpy = XOpenDisplay(NULL))) {
    fprintf(stderr, "ERROR: Could not open display\n");
    exit(1);
  }
  
  scr = DefaultScreen(dpy);
  rootwin = RootWindow(dpy, scr);
  
  win = XCreateSimpleWindow(dpy, rootwin, 1, 1, myfig_width[0], myfig_height[0], 0, 
			    WhitePixel(dpy, scr), WhitePixel(dpy, scr));
  
  XStoreName(dpy, win, "pstoedit -- cairo driver");
  XSelectInput(dpy, win, ExposureMask|ButtonPressMask);
  XMapWindow(dpy, win);
  
  
  cs = cairo_xlib_surface_create(dpy, win, DefaultVisual(dpy, 0), myfig_width[0], myfig_height[0]);
  

  for (i = 0 ; i < myfig_total_pages ; i++) {
    printf ("Xlib rendering page #%d of %d (%d x %d)\n", i+1, myfig_total_pages,
	    myfig_width[i], myfig_height[i]);
    XResizeWindow(dpy, win, myfig_width[i], myfig_height[i]);
    sprintf(tmps, "pstoedit/cairo page %d of %d", i+1, myfig_total_pages);
    XStoreName(dpy, win, tmps);

    cairo_xlib_surface_set_size (cs, myfig_width[i], myfig_height[i]);
    while(1) {
      XNextEvent(dpy, &e);
      if(e.type==Expose && e.xexpose.count<1) {
	myfig_render[i](cs, NULL);
      } else if(e.type==ButtonPress) break;
    }
  }
  cairo_surface_destroy(cs);
  XCloseDisplay(dpy);
}

void do_pdf(void)
{
  cairo_surface_t *cs;
  cairo_t *cr;
  int i;

  /* We will resize the pages later */
  printf ("Creating initial cairo PDF surface with dimensions %d x %d\n",
	  myfig_width[0], myfig_height[0]);

  cs = cairo_pdf_surface_create("test.pdf", myfig_width[0], myfig_height[0]);

  for (i = 0 ; i < myfig_total_pages ; i++) {
    printf ("PDF rendering page #%d of %d (%d x %d)\n", i+1, myfig_total_pages,
	    myfig_width[i], myfig_height[i]);

    /* 
     * Note:  If you have older versions of cairo (1.4.10 and older I
     * believe) then this set_size screws up the y size some.
     */
    cairo_pdf_surface_set_size (cs, myfig_width[i], myfig_height[i]);

    cr = myfig_render[i](cs, NULL);
    cairo_show_page(cr);
    cairo_destroy(cr);
    cairo_surface_flush(cs);
  }

  cairo_surface_flush(cs);
  cairo_surface_destroy(cs);
}

int main(int argc, char *argv[])
{

  myfig_init();
  do_pdf();
  do_xlib();
  return 0;
}