summaryrefslogtreecommitdiff
path: root/cd/src/cairo/cdcairops.c
blob: b08aee408b5ae45c94593e1742666d3869bbeddd (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/** \file
 * \brief Cairo PS Driver
 *
 * See Copyright Notice in cd.h
 */

#include <stdlib.h>
#include <stdio.h>

#include "cd.h"
#include "cdps.h"
#include "cdcairo.h"
#include "cdcairoctx.h"

#include <cairo-ps.h>


static void set_comment_attrib(cdCtxCanvas *ctxcanvas, char* data)
{
  if (data)
    cairo_ps_surface_dsc_comment(cairo_get_target(ctxcanvas->cr), data);
}

static cdAttribute comment_attrib =
{
  "DSCCOMMENT",
  set_comment_attrib,
  NULL
}; 

static void cdkillcanvas (cdCtxCanvas *ctxcanvas)
{
  cdcairoKillCanvas(ctxcanvas);
}

static void cdflush(cdCtxCanvas *ctxcanvas)
{
  if (!ctxcanvas->eps)
    cairo_show_page(ctxcanvas->cr);
}

static void cdcreatecanvas(cdCanvas* canvas, void* data)
{
  cdCtxCanvas *ctxcanvas;
  char* strdata = (char*)data;
  char filename[10240] = "";
  cairo_surface_t *surface;
  int res = 300;
  double w_pt;         /* Largura do papel (points) */
  double h_pt;         /* Altura do papel (points) */
  double scale;          /* Fator de conversao de coordenadas (pixel2points) */
  int eps = 0;               /* Postscrip encapsulado? */
  int level = 0;
  int landscape = 0;         /* page orientation */

  /* Starting parameters */
  if (strdata == NULL) 
    return;

  strdata += cdGetFileName(strdata, filename);
  if (filename[0] == 0)
    return;

  cdSetPaperSize(CD_A4, &w_pt, &h_pt);

  while (*strdata != '\0')
  {
    while (*strdata != '\0' && *strdata != '-') 
      strdata++;

    if (*strdata != '\0')
    {
      float num;
      strdata++;
      switch (*strdata++)
      {
      case 'p':
        {
          int paper;
          sscanf(strdata, "%d", &paper);
          cdSetPaperSize(paper, &w_pt, &h_pt);
          break;
        }
      case 'w':
        sscanf(strdata, "%g", &num);
        w_pt = CD_MM2PT*num;
        break;
      case 'h':
        sscanf(strdata, "%g", &num);
        h_pt = CD_MM2PT*num;
        break;
      case 'e':
        eps = 1;
        break;
      case 'o':
        landscape = 1;
        break;
      case '2':
        level = 2;
        break;
      case '3':
        level = 3;
        break;
      case 's':
        sscanf(strdata, "%d", &res);
        break;
      }
    }

    while (*strdata != '\0' && *strdata != ' ') 
      strdata++;
  }

  if (landscape)
    _cdSwapDouble(w_pt, h_pt);

  scale = 72.0/res;

  canvas->w = (int)(w_pt/scale + 0.5);   /* Converte p/ unidades do usuario */
  canvas->h = (int)(h_pt/scale + 0.5); /* Converte p/ unidades do usuario */
  canvas->w_mm = w_pt/CD_MM2PT;   /* Converte p/ milimetros */
  canvas->h_mm = h_pt/CD_MM2PT; /* Converte p/ milimetros */
  canvas->bpp = 24;
  canvas->xres = canvas->w / canvas->w_mm;
  canvas->yres = canvas->h / canvas->h_mm;

  surface = cairo_ps_surface_create(filename, w_pt, h_pt);

#if (CAIRO_VERSION_MAJOR>1 || (CAIRO_VERSION_MAJOR==1 && CAIRO_VERSION_MINOR>=6))
  if (level == 2)
    cairo_ps_surface_restrict_to_level(surface, CAIRO_PS_LEVEL_2);
  else if (level == 3)
    cairo_ps_surface_restrict_to_level(surface, CAIRO_PS_LEVEL_3);
#endif

  if (eps)
    cairo_ps_surface_set_eps(surface, 1);

  cairo_ps_surface_dsc_comment(surface, "%%Title: CanvasDraw");
  cairo_ps_surface_dsc_begin_setup (surface);
  cairo_ps_surface_dsc_begin_page_setup (surface);

  ctxcanvas = cdcairoCreateCanvas(canvas, cairo_create(surface));
  ctxcanvas->eps = eps;

  cairo_surface_destroy(surface);

  cdRegisterAttribute(canvas, &comment_attrib);
}

static void cdinittable(cdCanvas* canvas)
{
  cdcairoInitTable(canvas);
  canvas->cxKillCanvas = cdkillcanvas;
  canvas->cxFlush = cdflush;
}

static cdContext cdCairoPSContext =
{
  CD_CAP_ALL & ~(CD_CAP_PLAY | CD_CAP_YAXIS | CD_CAP_REGION | CD_CAP_WRITEMODE | CD_CAP_PALETTE),
  0,
  cdcreatecanvas,  
  cdinittable,
  NULL,                 
  NULL
};

cdContext* cdContextCairoPS(void)
{
  return &cdCairoPSContext;
}