13 #include "classdesc.h" 16 #include <cairo/cairo.h> 36 cairo_surface_t* m_surface;
38 mutable double m_width, m_height;
40 void operator=(
const Surface&);
41 Surface(
const Surface&);
43 void computeSizes()
const {
44 if (m_surface && recomputeSizes)
45 switch (cairo_surface_get_type(m_surface))
47 case CAIRO_SURFACE_TYPE_IMAGE:
48 m_width=cairo_image_surface_get_width(m_surface);
49 m_height=cairo_image_surface_get_height(m_surface);
51 #if CAIRO_HAS_RECORDING_SURFACE 52 case CAIRO_SURFACE_TYPE_RECORDING:
55 cairo_recording_surface_ink_extents
56 (m_surface, &x, &y, &m_width, &m_height);
69 Surface(cairo_surface_t* s=NULL,
70 double width=-1,
double height=-1): m_surface(NULL), m_cairo(NULL)
71 {surface(s, width, height);}
73 if (m_cairo) cairo_destroy(m_cairo);
74 if (m_surface) cairo_surface_destroy(m_surface);
77 cairo_surface_t* surface()
const {
return m_surface;}
82 cairo_surface_t* surface(cairo_surface_t* s,
83 int width=-1,
int height=-1) {
84 if (m_cairo) cairo_destroy(m_cairo);
85 if (m_surface) cairo_surface_destroy(m_surface);
87 m_cairo = s? cairo_create(s): NULL;
88 m_width=width; m_height=height;
89 recomputeSizes=width<0 || height<0;
93 cairo_t* cairo()
const {
return m_cairo;}
94 double width()
const {computeSizes();
return m_width;}
95 double height()
const {computeSizes();
return m_height;}
99 #if CAIRO_HAS_RECORDING_SURFACE 101 cairo_surface_get_type(m_surface)==CAIRO_SURFACE_TYPE_RECORDING)
104 cairo_recording_surface_ink_extents(m_surface, &x, &y, &w, &h);
113 double left()
const {
114 #if CAIRO_HAS_RECORDING_SURFACE 116 cairo_surface_get_type(m_surface)==CAIRO_SURFACE_TYPE_RECORDING)
119 cairo_recording_surface_ink_extents(m_surface, &x, &y, &w, &h);
129 virtual void clear() {
137 virtual void blit(
unsigned x,
unsigned y,
unsigned width,
unsigned height) {}
139 virtual void blit() {}
141 virtual void resize(
size_t width,
size_t height) {}
143 virtual void requestRedraw() {}
148 typedef std::tr1::shared_ptr<Surface> SurfacePtr;
151 struct PhotoImageBlock:
public Tk_PhotoImageBlock
153 PhotoImageBlock(): transparency(true) {}
154 PhotoImageBlock(
int x,
int y,
bool transparency):
155 transparency(transparency)
160 cairo_format_t format = transparency?
161 CAIRO_FORMAT_ARGB32: CAIRO_FORMAT_RGB24;
162 pitch = cairo_format_stride_for_width(format, width);
164 for (
int i=0; i<3; ++i) offset[i]=2-i;
170 class TkPhotoSurface:
public Surface
172 Tk_PhotoHandle photo;
173 std::vector<unsigned char> imageData;
174 PhotoImageBlock imageBlock;
179 int width()
const {
return imageBlock.width;}
180 int height()
const {
return imageBlock.height;}
182 if (imageBlock.transparency)
184 memset(&imageData[0],0,imageData.size());
187 memset(&imageData[0],255,imageData.size());
189 TkPhotoSurface(Tk_PhotoHandle photo,
bool transparency=
true):
190 photo(photo), compositing(false)
195 void init(
bool transparency=
true)
198 Tk_PhotoGetSize(photo, &width, &height);
199 imageBlock = PhotoImageBlock(width, height, transparency);
200 imageData.resize(imageBlock.pitch * imageBlock.height);
201 imageBlock.pixelPtr = &imageData[0];
203 surface( cairo_image_surface_create_for_data
204 (&imageData[0], transparency?
205 CAIRO_FORMAT_ARGB32: CAIRO_FORMAT_RGB24,
206 width, height, imageBlock.pitch));
209 void resize(
size_t width,
size_t height)
211 #if TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION < 5 212 Tk_PhotoSetSize(photo, width, height);
214 Tk_PhotoSetSize(
interp(), photo, width, height);
216 init(imageBlock.transparency);
220 void blit(
unsigned x,
unsigned y,
unsigned width,
unsigned height)
222 imageBlock.pixelPtr=&imageData[x*imageBlock.pixelSize + y*imageBlock.pitch];
223 if (width !=
unsigned(imageBlock.width) ||
224 height !=
unsigned(imageBlock.height))
225 #if TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION < 5 226 Tk_PhotoSetSize(photo, width, height);
228 (photo, &imageBlock, 0, 0, width, height,
229 compositing? TK_PHOTO_COMPOSITE_OVERLAY:TK_PHOTO_COMPOSITE_SET);
231 Tk_PhotoSetSize(
interp(), photo, width, height);
232 Tk_PhotoPutBlock(
interp(), photo, &imageBlock, 0, 0, width, height,
233 compositing? TK_PHOTO_COMPOSITE_OVERLAY:TK_PHOTO_COMPOSITE_SET);
236 void blit() {blit(0,0,imageBlock.width, imageBlock.height);}
237 void requestRedraw() {blit();}
244 void resize(
size_t width,
size_t height);
251 double xScale, yScale;
252 double m_scale, rotation;
256 SurfacePtr cairoSurface;
258 static Tk_ConfigSpec configSpecs[];
266 void setMatrix(
double scale,
double rotation)
267 {m_scale=scale, this->rotation=rotation;}
270 void scale(
double xScale,
double yScale) {
271 if (xScale!=1 || yScale!=1)
272 {this->xScale*=xScale; this->yScale*=yScale;}
275 CairoImage(): xScale(1), yScale(1) {setMatrix(1,0);}
276 CairoImage(
const SurfacePtr& cairoSurface):
277 xScale(1), yScale(1), cairoSurface(cairoSurface) {setMatrix(1,0);}
279 virtual ~CairoImage() {}
281 void attachToImage(
const std::string& imgName);
284 double distanceFrom(
double x,
double y)
const;
287 virtual array_ns::array<double> boundingBox();
292 int inClip(
double x0,
double y0,
double x1,
double y1)
const;
295 virtual void draw()=0;
296 void redrawIfSurfaceTooSmall();
297 void blit() {cairoSurface->blit();}
304 cairo_path_t* m_path;
305 cairo_matrix_t m_transformation;
307 void operator=(
const Path&);
309 Path(cairo_t* cairo): m_path(cairo_copy_path(cairo))
310 {cairo_get_matrix(cairo, &m_transformation);}
311 ~Path() {cairo_path_destroy(m_path);}
312 void appendToCurrent(cairo_t* cairo) {
315 cairo_set_matrix(cairo,&m_transformation);
316 cairo_append_path(cairo,m_path);
317 cairo_restore(cairo);
342 char *activeImageString;
344 char *disabledImageString;
348 Tk_Image activeImage;
350 Tk_Image disabledImage;
353 CairoImage *cairoItem;
356 namespace TkImageCode
358 int CreateImage(Tcl_Interp *
interp, Tk_Canvas canvas, Tk_Item *itemPtr,
359 int objc,Tcl_Obj *CONST objv[],Tk_ConfigSpec[]);
360 void DeleteImage(Tk_Canvas canvas, Tk_Item *itemPtr, Display *display);
362 int configureCairoItem(Tcl_Interp *
interp, Tk_Canvas canvas,
363 Tk_Item *itemPtr,
int objc,
364 Tcl_Obj *CONST objv[],
365 int flags, Tk_ConfigSpec configSpecs[]);
366 void ComputeImageBbox(Tk_Canvas canvas, ImageItem *imgPtr);
372 int createImage(Tcl_Interp *
interp, Tk_Canvas canvas, Tk_Item *itemPtr,
373 int objc,Tcl_Obj *CONST objv[])
375 if (TkImageCode::CreateImage(interp,canvas,itemPtr,objc,objv,C::configSpecs)==TCL_OK)
377 ImageItem* imgPtr=(ImageItem*)(itemPtr);
378 imgPtr->cairoItem =
new C;
379 imgPtr->cairoItem->setMatrix(imgPtr->scale, imgPtr->rotation);
380 TkImageCode::ComputeImageBbox(canvas, imgPtr);
383 TkImageCode::DeleteImage(canvas, itemPtr,
384 Tk_Display(Tk_CanvasTkwin(canvas)));
388 const Tk_ItemType& cairoItemType();
Tcl_Interp * interp()
default interpreter. Set to NULL when interp() is destroyed
Definition: tcl++.h:222
_OPENMP
Definition: accessor.h:16