1 /*
2  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
3  * and associated documentation files (the "Software"), to deal in the Software without restriction,
4  * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5  * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
6  * subject to the following conditions:
7  *
8  * The above copyright notice and this permission notice shall be included in all copies or substantial
9  * portions of the Software.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
12  * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
13  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
14  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
15  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16  *
17  * Authors:
18  *      Alexandre Pigolkine (pigolkine@gmx.de)
19  *      Duncan Mak (duncan@ximian.com)
20  *      Miguel de Icaza (miguel@ximian.com)
21  *      Ravindra (rkumar@novell.com)
22  *  	Sanjay Gupta (gsanjay@novell.com)
23  *	Vladimir Vukicevic (vladimir@pobox.com)
24  *	Geoff Norton (gnorton@customerdna.com)
25  *      Jonathan Gilbert (logic@deltaq.org)
26  *	Sebastien Pouliot  <sebastien@ximian.com>
27  *	Frederik Carlier <frederik.carlier@quamotion.mobi>
28  *
29  * Copyright (C) 2003-2007 Novell, Inc (http://www.novell.com)
30  */
31 
32 /*
33  * NOTE: This is a private header files and everything is subject to changes.
34  */
35 
36 #ifndef __GRAPHICS_PRIVATE_H__
37 #define __GRAPHICS_PRIVATE_H__
38 
39 #include "gdiplus-private.h"
40 
41 #include "imageattributes-private.h"
42 #include "pen-private.h"
43 #include "region-private.h"
44 #include "metafile.h"
45 
46 #if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1,6,0)
47 	#define CAIRO_AA_OFFSET_X		1
48 	#define CAIRO_AA_OFFSET_Y		0.5
49 #else
50 	#define CAIRO_AA_OFFSET_X		0.5
51 	#define CAIRO_AA_OFFSET_Y		0.5
52 #endif
53 
54 #define CURVE_MIN_TERMS			1
55 #define CURVE_MAX_TERMS			7
56 
57 #define DEFAULT_TEXT_CONTRAST		4
58 
59 /* Recursion depth of the flattening algorithm */
60 #define FLATTEN_RECURSION_LIMIT		10
61 
62 /* not 100% identical to MS GDI+ which varies a little from int and float, but still around 0x40000000 */
63 #define GDIP_MAX_COORD			1073741824
64 #define gdip_is_overflow(a)		(((a) < -GDIP_MAX_COORD) || ((a) > GDIP_MAX_COORD))
65 
66 typedef enum {
67         CURVE_OPEN,
68         CURVE_CLOSE
69 } _CurveType;
70 
71 typedef enum {
72 	gtUndefined,
73 	gtX11Drawable,
74 	gtMemoryBitmap,
75 	gtOSXDrawable,
76 	gtPostScript
77 } GraphicsType;
78 
79 typedef enum {
80 	GraphicsBackEndInvalid	= -1,
81 	GraphicsBackEndCairo	= 0,
82 	GraphicsBackEndMetafile	= 1
83 } GraphicsBackEnd;
84 
85 typedef struct {
86 	cairo_matrix_t		matrix;
87 	cairo_matrix_t		previous_matrix;
88 	GpRegion*		clip;
89 	cairo_matrix_t		clip_matrix;
90 	CompositingMode    	composite_mode;
91 	CompositingQuality 	composite_quality;
92 	InterpolationMode 	interpolation;
93 	GpUnit			page_unit;
94 	float			scale;
95 	SmoothingMode		draw_mode;
96 	TextRenderingHint 	text_mode;
97 	PixelOffsetMode 	pixel_mode;
98 	int			org_x;
99 	int			org_y;
100 	int			text_contrast;
101 } GpState;
102 
103 typedef enum {
104 	GraphicsStateValid = 0,
105 	GraphicsStateBusy = 1
106 } GraphicsInternalState;
107 
108 typedef struct _Graphics {
109 	GraphicsBackEnd		backend;
110 	/* cairo-specific stuff */
111 	cairo_t			*ct;
112 	GpMatrix		*copy_of_ctm;
113 	cairo_matrix_t		previous_matrix;
114 #if defined(HAVE_X11)
115 #ifdef CAIRO_HAS_XLIB_SURFACE
116 	Display			*display;
117 	Drawable		drawable;
118 #endif
119 #endif
120 	void			*image;
121 	int			type;
122 	GpPen*			last_pen;	/* caching pen and brush to avoid unnecessary sets */
123 	GpBrush*		last_brush;
124 	float			aa_offset_x;
125 	float			aa_offset_y;
126 	/* metafile-specific stuff */
127 	EmfType			emf_type;
128 	GpMetafile		*metafile;
129 	cairo_surface_t		*metasurface;	/* bogus surface to satisfy some API calls */
130 	/* common-stuff */
131 	GpRegion*		clip;
132 	GpMatrix*		clip_matrix;
133 	GpRect			bounds;
134 	GpRect			orig_bounds;
135 	GpUnit			page_unit;
136 	float			scale;
137 	InterpolationMode	interpolation;
138 	SmoothingMode		draw_mode;
139 	TextRenderingHint	text_mode;
140 	GpState*		saved_status;
141 	int			saved_status_pos;
142 	CompositingMode		composite_mode;
143 	CompositingQuality	composite_quality;
144 	PixelOffsetMode		pixel_mode;
145 	int			render_origin_x;
146 	int			render_origin_y;
147 	float			dpi_x;
148 	float			dpi_y;
149 	int			text_contrast;
150 	GraphicsInternalState		state;
151 #ifdef CAIRO_HAS_QUARTZ_SURFACE
152 	void		*cg_context;
153 #endif
154 } Graphics;
155 
156 float gdip_unit_conversion (Unit from, Unit to, float dpi, GraphicsType type, float nSrc) GDIP_INTERNAL;
157 
158 void gdip_set_cairo_clipping (GpGraphics *graphics) GDIP_INTERNAL;
159 
160 GpGraphics* gdip_graphics_new (cairo_surface_t *surface) GDIP_INTERNAL;
161 GpGraphics* gdip_metafile_graphics_new (GpMetafile *metafile) GDIP_INTERNAL;
162 
163 BOOL gdip_is_scaled (GpGraphics *graphics) GDIP_INTERNAL;
164 
165 /* prototypes for cairo wrappers to deal with coordonates limits, unit conversion and antialiasing) */
166 void gdip_cairo_rectangle (GpGraphics *graphics, double x, double y, double width, double height, BOOL antialiasing) GDIP_INTERNAL;
167 void gdip_cairo_move_to (GpGraphics *graphics, double x, double y, BOOL convert_units, BOOL antialiasing) GDIP_INTERNAL;
168 void gdip_cairo_line_to (GpGraphics *graphics, double x, double y, BOOL convert_units, BOOL antialiasing) GDIP_INTERNAL;
169 void gdip_cairo_curve_to (GpGraphics *graphics, double x1, double y1, double x2, double y2, double x3, double y3,
170 	BOOL convert_units, BOOL antialiasing) GDIP_INTERNAL;
171 
172 void gdip_cairo_set_matrix (GpGraphics *graphics, GpMatrix *matrixPageUnits) GDIP_INTERNAL;
173 
174 #ifdef CAIRO_HAS_QUARTZ_SURFACE
175 
176 #if __i386__
177 typedef float CGFloat;
178 #else
179 typedef double CGFloat;
180 #endif
181 
182 // For the Quartz backend to function we need a few structures and function declarations.
183 // Unfortunately including the headers causes conflicts with internal types.  This must
184 // be kept in sync with any changes that might happen (albeit unlikely) to apples structures
185 struct CGPoint {
186 	CGFloat x;
187 	CGFloat y;
188 };
189 
190 typedef struct CGPoint CGPoint;
191 
192 struct CGSize {
193 	CGFloat width;
194 	CGFloat height;
195 };
196 
197 typedef struct CGSize CGSize;
198 
199 struct CGRect {
200    CGPoint origin;
201    CGSize size;
202 };
203 
204 typedef struct CGRect CGRect;
205 
206 void *CGBitmapContextCreateImage (void *context);
207 void CGContextDrawImage (void *context, CGRect rect, void *image);
208 void CGImageRelease (void *image);
209 void *cairo_quartz_surface_get_cg_context(cairo_surface_t *surface);
210 cairo_surface_t *cairo_quartz_surface_create (int format, int width, int height);
211 cairo_surface_t *cairo_quartz_surface_create_for_cg_context (void *ctx, int width, int height);
212 #endif
213 
214 /* include public API */
215 #include "image.h"
216 #include "graphics.h"
217 
218 #endif
219