~jaromil/freej/master

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*  FreeJ
 *  (c) Copyright 2009 Denis Roio <jaromil@dyne.org>
 *
 * This source code  is free software; you can  redistribute it and/or
 * modify it under the terms of the GNU Public License as published by
 * the Free Software  Foundation; either version 3 of  the License, or
 * (at your option) any later version.
 *
 * This source code 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.  Please refer
 * to the GNU Public License for more details.
 *
 * You should  have received  a copy of  the GNU Public  License along
 * with this source code; if  not, write to: Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <config.h>

#ifdef WITH_CAIRO
#include <jutils.h>
#include <context.h>


#include <cairo_layer.h>

#include <jsparser_data.h>
#include <factory.h>

// our objects are allowed to be created trough the factory engine
FACTORY_REGISTER_INSTANTIATOR(Layer, CairoLayer, VectorLayer, cairo);

CairoColor::CairoColor(cairo_t *cai) :Color() {
  r = 0.;
  g = 0.;
  b = 0.;
  a = 255.;
  cairo = cai;
}
CairoColor::~CairoColor() { }
void CairoColor::set() {
  double sr = r / 255.;
  double sg = g / 255.;
  double sb = b / 255.;
  double sa = a / 255.;
  func("Color::set r[%.2f] g[%.2f] b[%.2f] a[%.2f]",sr,sg,sb,sa);
  cairo_set_source_rgba(cairo, sr, sg, sb, sa);
};

CairoLayer::CairoLayer()
  :Layer() {


  surf = NULL;
  cairo = NULL;
  pixels = NULL;
  color = NULL;
  saved_color = NULL;

  set_name("VEC");
  set_filename("/vector layer");
  jsclass = &vector_layer_class;
}

CairoLayer::~CairoLayer() {
  
  if(cairo)  cairo_destroy(cairo);
  if(surf)   cairo_surface_destroy(surf);
  if(pixels) free(pixels);
  if(color) free(color);
}

bool CairoLayer::_init() {
  // create the surface
  stride = cairo_format_stride_for_width
    (CAIRO_FORMAT_ARGB32, geo.w);
  pixels = malloc (stride * geo.h);
  surf = cairo_image_surface_create_for_data
    ((unsigned char*)pixels, CAIRO_FORMAT_ARGB32, geo.w, geo.h, stride);
  // create the drawing context
  cairo = cairo_create(surf);
  // This  function references  target,  so you  can immediately  call
  // cairo_surface_destroy()  on it if  you don't  need to  maintain a
  // separate reference to it.

  color = new CairoColor( cairo );

  opened = true;
  return(true);

}

void *CairoLayer::feed() {
  return(pixels);
}

bool CairoLayer::open(const char *file) {
  /* we don't need this */
  return true;
}

void CairoLayer::close() {
  /* neither this */
  return;
}

///////////////////////////////////////////////
// public methods exported to language bindings

// Cairo API
void CairoLayer::save() { cairo_save(cairo); }
void CairoLayer::restore() { cairo_restore(cairo); }
void CairoLayer::new_path() { cairo_new_path(cairo); }
void CairoLayer::close_path() { cairo_close_path(cairo); }
void CairoLayer::scale(double xx, double yy) {
  func("%s :: x[%.2f] y[%.2f]", __FUNCTION__, xx, yy);
  cairo_scale(cairo, xx, yy); }
void CairoLayer::rotate(double angle) { cairo_rotate(cairo, angle); }
void CairoLayer::translate(int xx, int yy) {
  func("%s :: x[%.2f] y[%.2f]", __FUNCTION__, xx, yy);
  cairo_translate(cairo, xx, yy);
}
void CairoLayer::move_to(double xx, double yy) { 
  func("%s :: x[%.2f] y[%.2f]", __FUNCTION__, xx, yy);
  cairo_move_to(cairo, xx, yy);
}
void CairoLayer::line_to(double xx, double yy) {
  func("%s :: x[%.2f] y[%.2f]", __FUNCTION__, xx, yy);
  cairo_line_to(cairo, xx, yy);
}
void CairoLayer::curve_to(int x1, int y1, int x2, int y2, int x3, int y3) {
    cairo_curve_to(cairo, x1, y1, x2, y2, x3, y3); }
void CairoLayer::arc(double xc, double yc, double radius, double angle1, double angle2) {
  cairo_arc(cairo, xc, yc, radius, angle1, angle2); }
void CairoLayer::rect(double x1, double y1, double x2, double y2) {
  cairo_rectangle(cairo, x1, y1, x2, y2);
}
void CairoLayer::fill() { cairo_fill(cairo); }
void CairoLayer::stroke() { cairo_stroke(cairo); }
void CairoLayer::set_line_width(double wid) { cairo_set_line_width(cairo, wid); }
int CairoLayer::get_line_width() { return cairo_get_line_width(cairo); }

// Mozilla's GFX compatibility API
void CairoLayer::quad_curve_to(double x1, double y1, double x2, double y2) {
  double xc, yc;
  cairo_get_current_point(cairo, &xc, &yc);
  cairo_curve_to(cairo,
		 (xc + x1 * 2.0) / 3.0,
		 (yc + y1 * 2.0) / 3.0,
		 (x1 * 2.0 + x2) / 3.0,
		 (y1 * 2.0 + y2) / 3.0,
		 x2, y2);
}

void CairoLayer::fill_rect(double x1, double y1, double x2, double y2) {
  cairo_save(cairo);
  cairo_rectangle(cairo, x1, y1, x2, y2);
  cairo_fill(cairo);
  cairo_restore(cairo);
}

// Color facilities

void CairoLayer::push_color() {
  if(saved_color) {
    warning("previously saved color lost");
    delete saved_color;
  }
  saved_color = color;
  color = new CairoColor(cairo);
}
void CairoLayer::pop_color() {
  if(!saved_color) {
    warning("no previourly saved color found");
    return;
  } 
  delete color;
  color = saved_color;
  saved_color = NULL;
  func("popped back color: r[%.2f] g[%.2f] b[%.2f] a[%.2f]",
       color->r, color->g, color->b, color->a);
}

#endif