/usr/include/mapnik/cairo_context.hpp is in libmapnik-dev 2.2.0+ds1-6build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | /*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_CAIRO_CONTEXT_HPP
#define MAPNIK_CAIRO_CONTEXT_HPP
// mapnik
#include <mapnik/debug.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/color.hpp>
#include <mapnik/stroke.hpp>
#include <mapnik/image_data.hpp>
#include <mapnik/image_compositing.hpp>
#include <mapnik/font_engine_freetype.hpp>
#include <mapnik/gradient.hpp>
#include <mapnik/vertex.hpp>
#include <mapnik/noncopyable.hpp>
// boost
#include <boost/shared_ptr.hpp>
// cairo
#include <cairo.h>
// stl
#include <map>
#include <vector>
#include <stdexcept>
// agg
#include "agg_basics.h"
namespace mapnik {
class text_path;
typedef cairo_status_t ErrorStatus;
/// Throws the appropriate exception, if exceptions are enabled.
inline void throw_exception(ErrorStatus status)
{
throw std::runtime_error("cairo: fixme");
}
//We inline this because it is called so often.
inline void check_status_and_throw_exception(ErrorStatus status)
{
if(status != CAIRO_STATUS_SUCCESS)
throw_exception(status);
}
template<class T>
void check_object_status_and_throw_exception(const T& object)
{
check_status_and_throw_exception(object.get_status());
}
class cairo_face : private mapnik::noncopyable
{
public:
cairo_face(boost::shared_ptr<freetype_engine> const& engine, face_ptr const& face);
~cairo_face();
cairo_font_face_t * face() const;
private:
class handle
{
public:
handle(boost::shared_ptr<freetype_engine> const& engine, face_ptr const& face)
: engine_(engine), face_(face) {}
private:
boost::shared_ptr<freetype_engine> engine_;
face_ptr face_;
};
static void destroy(void *data)
{
handle *h = static_cast<handle *>(data);
delete h;
}
private:
face_ptr face_;
cairo_font_face_t *c_face_;
};
typedef boost::shared_ptr<cairo_face> cairo_face_ptr;
class cairo_face_manager : private mapnik::noncopyable
{
public:
cairo_face_manager(boost::shared_ptr<freetype_engine> engine);
cairo_face_ptr get_face(face_ptr face);
private:
typedef std::map<face_ptr,cairo_face_ptr> cairo_face_cache;
boost::shared_ptr<freetype_engine> font_engine_;
cairo_face_cache cache_;
};
class cairo_pattern : private mapnik::noncopyable
{
public:
cairo_pattern(image_data_32 const& data)
{
int pixels = data.width() * data.height();
const unsigned int *in_ptr = data.getData();
const unsigned int *in_end = in_ptr + pixels;
unsigned int *out_ptr;
surface_ = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, data.width(), data.height());
out_ptr = reinterpret_cast<unsigned int *>(cairo_image_surface_get_data(surface_));
while (in_ptr < in_end)
{
unsigned int in = *in_ptr++;
unsigned int r = (in >> 0) & 0xff;
unsigned int g = (in >> 8) & 0xff;
unsigned int b = (in >> 16) & 0xff;
unsigned int a = (in >> 24) & 0xff;
//r = r * a / 255;
//g = g * a / 255;
//b = b * a / 255;
*out_ptr++ = (a << 24) | (r << 16) | (g << 8) | b;
}
// mark the surface as dirty as we've modified it behind cairo's back
cairo_surface_mark_dirty(surface_);
pattern_ = cairo_pattern_create_for_surface(surface_);
}
~cairo_pattern()
{
if (surface_) cairo_surface_destroy(surface_);
if (pattern_) cairo_pattern_destroy(pattern_);
}
void set_matrix(cairo_matrix_t const& matrix)
{
cairo_pattern_set_matrix(pattern_, &matrix);
}
void set_origin(double x, double y)
{
cairo_matrix_t matrix;
cairo_pattern_get_matrix(pattern_,&matrix);
matrix.x0 = -x;
matrix.y0 = -y;
cairo_pattern_set_matrix(pattern_,&matrix);
}
void set_extend(cairo_extend_t extend)
{
cairo_pattern_set_extend(pattern_, extend);
}
void set_filter(cairo_filter_t filter)
{
cairo_pattern_set_filter(pattern_, filter);
}
cairo_pattern_t * pattern() const
{
return pattern_;
}
private:
cairo_surface_t * surface_;
cairo_pattern_t * pattern_;
};
class cairo_gradient : private mapnik::noncopyable
{
public:
cairo_gradient(const mapnik::gradient &grad, double opacity=1.0)
{
double x1,x2,y1,y2,rad;
grad.get_control_points(x1,y1,x2,y2,rad);
if (grad.get_gradient_type() == LINEAR)
{
pattern_ = cairo_pattern_create_linear(x1, y1, x2, y2);
}
else if (grad.get_gradient_type() == RADIAL)
{
pattern_ = cairo_pattern_create_radial(x1, y1, 0, x2, y2, rad);
}
units_ = grad.get_units();
BOOST_FOREACH ( mapnik::stop_pair const& st, grad.get_stop_array() )
{
mapnik::color const& stop_color = st.second;
double r= static_cast<double> (stop_color.red())/255.0;
double g= static_cast<double> (stop_color.green())/255.0;
double b= static_cast<double> (stop_color.blue())/255.0;
double a= static_cast<double> (stop_color.alpha())/255.0;
cairo_pattern_add_color_stop_rgba(pattern_,st.first, r, g, b, a*opacity);
}
double m[6];
agg::trans_affine tr = grad.get_transform();
tr.invert();
tr.store_to(m);
cairo_matrix_t matrix;
cairo_matrix_init(&matrix,m[0],m[1],m[2],m[3],m[4],m[5]);
cairo_pattern_set_matrix(pattern_, &matrix);
}
~cairo_gradient()
{
if (pattern_)
cairo_pattern_destroy(pattern_);
}
cairo_pattern_t * gradient() const
{
return pattern_;
}
gradient_unit_e units() const
{
return units_;
}
private:
cairo_pattern_t * pattern_;
gradient_unit_e units_;
};
struct cairo_closer
{
void operator() (cairo_t * obj)
{
if (obj) cairo_destroy(obj);
}
};
struct cairo_surface_closer
{
void operator() (cairo_surface_t * surface)
{
if (surface) cairo_surface_destroy(surface);
}
};
typedef boost::shared_ptr<cairo_t> cairo_ptr;
typedef boost::shared_ptr<cairo_surface_t> cairo_surface_ptr;
inline cairo_ptr create_context(cairo_surface_ptr const& surface)
{
return cairo_ptr(cairo_create(&*surface),cairo_closer());
}
class cairo_context : private mapnik::noncopyable
{
public:
cairo_context(cairo_ptr const& cairo);
inline ErrorStatus get_status() const
{
return cairo_status(cairo_.get());
}
void clip();
void show_page();
void set_color(color const &color, double opacity = 1.0);
void set_color(double r, double g, double b, double opacity = 1.0);
void set_operator(composite_mode_e comp_op);
void set_line_join(line_join_e join);
void set_line_cap(line_cap_e cap);
void set_miter_limit(double limit);
void set_line_width(double width);
void set_dash(dash_array const &dashes, double scale_factor);
void set_fill_rule(cairo_fill_rule_t fill_rule);
void move_to(double x, double y);
void curve_to(double ct1_x, double ct1_y, double ct2_x, double ct2_y, double end_x, double end_y);
void close_path();
void line_to(double x, double y);
void rectangle(double x, double y, double w, double h);
void stroke();
void fill();
void paint();
void set_pattern(cairo_pattern const& pattern);
void set_gradient(cairo_gradient const& pattern, box2d<double> const& bbox);
void add_image(double x, double y, image_data_32 & data, double opacity = 1.0);
void add_image(agg::trans_affine const& tr, image_data_32 & data, double opacity = 1.0);
void set_font_face(cairo_face_manager & manager, face_ptr face);
void set_font_matrix(cairo_matrix_t const& matrix);
void set_matrix(cairo_matrix_t const& matrix);
void transform(cairo_matrix_t const& matrix);
void translate(double x, double y);
void save();
void restore();
void show_glyph(unsigned long index, double x, double y);
void glyph_path(unsigned long index, double x, double y);
void add_text(text_path const& path,
cairo_face_manager & manager,
face_manager<freetype_engine> & font_manager,
double scale_factor = 1.0);
template <typename T>
void add_path(T& path, unsigned start_index = 0)
{
double x, y;
path.rewind(start_index);
for (unsigned cm = path.vertex(&x, &y); cm != SEG_END; cm = path.vertex(&x, &y))
{
if (cm == SEG_MOVETO)
{
move_to(x, y);
}
else if (cm == SEG_LINETO)
{
line_to(x, y);
}
else if (cm == SEG_CLOSE)
{
close_path();
}
}
}
template <typename T>
void add_agg_path(T& path, unsigned start_index = 0)
{
double x=0;
double y=0;
path.rewind(start_index);
for (unsigned cm = path.vertex(&x, &y); !agg::is_stop(cm); cm = path.vertex(&x, &y))
{
if (agg::is_move_to(cm))
{
move_to(x, y);
}
else if (agg::is_drawing(cm))
{
if (agg::is_curve3(cm))
{
double end_x=0;
double end_y=0;
MAPNIK_LOG_WARN(cairo_renderer) << "Curve 3 not implemented";
path.vertex(&end_x, &end_y);
curve_to(x,y,x,y,end_x,end_y);
}
else if (agg::is_curve4(cm))
{
double ct2_x=0;
double ct2_y=0;
double end_x=0;
double end_y=0;
path.vertex(&ct2_x, &ct2_y);
path.vertex(&end_x, &end_y);
curve_to(x,y,ct2_x,ct2_y,end_x,end_y);
}
else if (agg::is_line_to(cm))
{
line_to(x, y);
}
else
{
MAPNIK_LOG_WARN(cairo_renderer) << "Unimplemented drawing command: " << cm;
move_to(x, y);
}
}
else if (agg::is_close(cm))
{
close_path();
}
else
{
MAPNIK_LOG_WARN(cairo_renderer) << "Unimplemented path command: " << cm;
}
}
}
private:
cairo_ptr cairo_;
};
}
#endif // MAPNIK_CAIRO_CONTEXT_HPP
|