1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* Cairo - a vector graphics library with display and print output
3  *
4  * Copyright © 2005 Red Hat, Inc.
5  * Copyright © 2012 Intel Corporation
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it either under the terms of the GNU Lesser General Public
9  * License version 2.1 as published by the Free Software Foundation
10  * (the "LGPL") or, at your option, under the terms of the Mozilla
11  * Public License Version 1.1 (the "MPL"). If you do not alter this
12  * notice, a recipient may use your version of this file under either
13  * the MPL or the LGPL.
14  *
15  * You should have received a copy of the LGPL along with this library
16  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18  * You should have received a copy of the MPL along with this library
19  * in the file COPYING-MPL-1.1
20  *
21  * The contents of this file are subject to the Mozilla Public License
22  * Version 1.1 (the "License"); you may not use this file except in
23  * compliance with the License. You may obtain a copy of the License at
24  * http://www.mozilla.org/MPL/
25  *
26  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28  * the specific language governing rights and limitations.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is Red Hat, Inc.
33  *
34  * Contributor(s):
35  *	Owen Taylor <otaylor@redhat.com>
36  *	Stuart Parmenter <stuart@mozilla.com>
37  *	Vladimir Vukicevic <vladimir@pobox.com>
38  */
39 
40 #define WIN32_LEAN_AND_MEAN
41 /* We require Windows 2000 features such as ETO_PDY */
42 #if !defined(WINVER) || (WINVER < 0x0500)
43 # define WINVER 0x0500
44 #endif
45 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
46 # define _WIN32_WINNT 0x0500
47 #endif
48 
49 #include "cairoint.h"
50 
51 #include "cairo-backend-private.h"
52 #include "cairo-default-context-private.h"
53 #include "cairo-error-private.h"
54 #include "cairo-image-surface-private.h"
55 #include "cairo-paginated-private.h"
56 #include "cairo-pattern-private.h"
57 #include "cairo-win32-private.h"
58 #include "cairo-scaled-font-subsets-private.h"
59 #include "cairo-surface-fallback-private.h"
60 #include "cairo-surface-backend-private.h"
61 
62 #include <wchar.h>
63 #include <windows.h>
64 
65 #if defined(__MINGW32__) && !defined(ETO_PDY)
66 # define ETO_PDY 0x2000
67 #endif
68 
69 /**
70  * SECTION:cairo-win32
71  * @Title: Win32 Surfaces
72  * @Short_Description: Microsoft Windows surface support
73  * @See_Also: #cairo_surface_t
74  *
75  * The Microsoft Windows surface is used to render cairo graphics to
76  * Microsoft Windows windows, bitmaps, and printing device contexts.
77  *
78  * The surface returned by cairo_win32_printing_surface_create() is of surface
79  * type %CAIRO_SURFACE_TYPE_WIN32_PRINTING and is a multi-page vector surface
80  * type.
81  *
82  * The surface returned by the other win32 constructors is of surface type
83  * %CAIRO_SURFACE_TYPE_WIN32 and is a raster surface type.
84  **/
85 
86 /**
87  * CAIRO_HAS_WIN32_SURFACE:
88  *
89  * Defined if the Microsoft Windows surface backend is available.
90  * This macro can be used to conditionally compile backend-specific code.
91  *
92  * Since: 1.0
93  **/
94 
95 /**
96  * _cairo_win32_print_gdi_error:
97  * @context: context string to display along with the error
98  *
99  * Helper function to dump out a human readable form of the
100  * current error code.
101  *
102  * Return value: A cairo status code for the error code
103  **/
104 cairo_status_t
_cairo_win32_print_gdi_error(const char * context)105 _cairo_win32_print_gdi_error (const char *context)
106 {
107     void *lpMsgBuf;
108     DWORD last_error = GetLastError ();
109 
110     if (!FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
111 			 FORMAT_MESSAGE_FROM_SYSTEM,
112 			 NULL,
113 			 last_error,
114 			 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
115 			 (LPWSTR) &lpMsgBuf,
116 			 0, NULL)) {
117 	fprintf (stderr, "%s: Unknown GDI error", context);
118     } else {
119 	fprintf (stderr, "%s: %S", context, (wchar_t *)lpMsgBuf);
120 
121 	LocalFree (lpMsgBuf);
122     }
123 
124     fflush (stderr);
125 
126     return _cairo_error (CAIRO_STATUS_WIN32_GDI_ERROR);
127 }
128 
129 cairo_bool_t
_cairo_win32_surface_get_extents(void * abstract_surface,cairo_rectangle_int_t * rectangle)130 _cairo_win32_surface_get_extents (void		          *abstract_surface,
131 				  cairo_rectangle_int_t   *rectangle)
132 {
133     cairo_win32_surface_t *surface = abstract_surface;
134 
135     *rectangle = surface->extents;
136     return TRUE;
137 }
138 
139 /**
140  * cairo_win32_surface_get_dc:
141  * @surface: a #cairo_surface_t
142  *
143  * Returns the HDC associated with this surface, or %NULL if none.
144  * Also returns %NULL if the surface is not a win32 surface.
145  *
146  * A call to cairo_surface_flush() is required before using the HDC to
147  * ensure that all pending drawing operations are finished and to
148  * restore any temporary modification cairo has made to its state. A
149  * call to cairo_surface_mark_dirty() is required after the state or
150  * the content of the HDC has been modified.
151  *
152  * Return value: HDC or %NULL if no HDC available.
153  *
154  * Since: 1.2
155  **/
156 HDC
cairo_win32_surface_get_dc(cairo_surface_t * surface)157 cairo_win32_surface_get_dc (cairo_surface_t *surface)
158 {
159     if (surface->backend == NULL)
160 	return NULL;
161 
162     if (surface->backend->type == CAIRO_SURFACE_TYPE_WIN32)
163 	return to_win32_surface(surface)->dc;
164 
165     if (_cairo_surface_is_paginated (surface)) {
166 	cairo_surface_t *target = _cairo_paginated_surface_get_target (surface);
167 	if (target->backend->type == CAIRO_SURFACE_TYPE_WIN32_PRINTING)
168 	    return to_win32_surface(target)->dc;
169     }
170 
171     return NULL;
172 }
173 
174 HDC
cairo_win32_get_dc_with_clip(cairo_t * cr)175 cairo_win32_get_dc_with_clip (cairo_t *cr)
176 {
177     cairo_surface_t *surface = cairo_get_target (cr);
178     if (cr->backend->type == CAIRO_TYPE_DEFAULT) {
179         cairo_default_context_t *c = (cairo_default_context_t *) cr;
180         cairo_clip_t *clip = _cairo_clip_copy (_cairo_gstate_get_clip (c->gstate));
181         if (_cairo_surface_is_win32 (surface)) {
182             cairo_win32_display_surface_t *winsurf = (cairo_win32_display_surface_t *) surface;
183 
184             _cairo_win32_display_surface_set_clip (winsurf, clip);
185 
186             _cairo_clip_destroy (clip);
187             return winsurf->win32.dc;
188         }
189 
190         if (_cairo_surface_is_paginated (surface)) {
191             cairo_surface_t *target;
192 
193             target = _cairo_paginated_surface_get_target (surface);
194 
195 #ifndef CAIRO_OMIT_WIN32_PRINTING
196             if (_cairo_surface_is_win32_printing (target)) {
197                 cairo_status_t status;
198                 cairo_win32_printing_surface_t *psurf = (cairo_win32_printing_surface_t *) target;
199 
200                 status = _cairo_surface_clipper_set_clip (&psurf->clipper, clip);
201 
202                 _cairo_clip_destroy (clip);
203 
204                 if (status)
205                     return NULL;
206 
207                 return psurf->win32.dc;
208             }
209 #endif
210         }
211         _cairo_clip_destroy (clip);
212     }
213     return NULL;
214 }
215 
216 /**
217  * _cairo_surface_is_win32:
218  * @surface: a #cairo_surface_t
219  *
220  * Checks if a surface is an #cairo_win32_surface_t
221  *
222  * Return value: %TRUE if the surface is an win32 surface
223  **/
224 cairo_bool_t
_cairo_surface_is_win32(const cairo_surface_t * surface)225 _cairo_surface_is_win32 (const cairo_surface_t *surface)
226 {
227     /* _cairo_surface_nil sets a NULL backend so be safe */
228     return surface->backend && surface->backend->type == CAIRO_SURFACE_TYPE_WIN32;
229 }
230 
231 /**
232  * cairo_win32_surface_get_image:
233  * @surface: a #cairo_surface_t
234  *
235  * Returns a #cairo_surface_t image surface that refers to the same bits
236  * as the DIB of the Win32 surface.  If the passed-in win32 surface
237  * is not a DIB surface, %NULL is returned.
238  *
239  * Return value: a #cairo_surface_t (owned by the win32 #cairo_surface_t),
240  * or %NULL if the win32 surface is not a DIB.
241  *
242  * Since: 1.4
243  **/
244 cairo_surface_t *
cairo_win32_surface_get_image(cairo_surface_t * surface)245 cairo_win32_surface_get_image (cairo_surface_t *surface)
246 {
247 
248     if (! _cairo_surface_is_win32 (surface)) {
249         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
250     }
251 
252     GdiFlush();
253     return to_win32_display_surface(surface)->image;
254 }
255 
256 #define STACK_GLYPH_SIZE 256
257 cairo_int_status_t
_cairo_win32_surface_emit_glyphs(cairo_win32_surface_t * dst,const cairo_pattern_t * source,cairo_glyph_t * glyphs,int num_glyphs,cairo_scaled_font_t * scaled_font,cairo_bool_t glyph_indexing)258 _cairo_win32_surface_emit_glyphs (cairo_win32_surface_t *dst,
259 				  const cairo_pattern_t *source,
260 				  cairo_glyph_t	 *glyphs,
261 				  int			  num_glyphs,
262 				  cairo_scaled_font_t	 *scaled_font,
263 				  cairo_bool_t		  glyph_indexing)
264 {
265 #if CAIRO_HAS_DWRITE_FONT
266     if (scaled_font->backend->type == CAIRO_FONT_TYPE_DWRITE) {
267         if (!glyph_indexing) return CAIRO_INT_STATUS_UNSUPPORTED;
268 
269         // FIXME: fake values for params that aren't currently passed in here
270         cairo_operator_t op = CAIRO_OPERATOR_SOURCE;
271         cairo_clip_t *clip = NULL;
272         return _cairo_dwrite_show_glyphs_on_surface (dst, op, source, glyphs, num_glyphs, scaled_font, clip /* , glyph_indexing */ );
273     }
274 #endif
275 #if CAIRO_HAS_WIN32_FONT
276     WORD glyph_buf_stack[STACK_GLYPH_SIZE];
277     WORD *glyph_buf = glyph_buf_stack;
278     int dxy_buf_stack[2 * STACK_GLYPH_SIZE];
279     int *dxy_buf = dxy_buf_stack;
280 
281     BOOL win_result = 0;
282     int i, j;
283 
284     cairo_solid_pattern_t *solid_pattern;
285     COLORREF color;
286 
287     cairo_matrix_t device_to_logical;
288 
289     int start_x, start_y;
290     double user_x, user_y;
291     int logical_x, logical_y;
292     unsigned int glyph_index_option;
293 
294     /* We can only handle win32 fonts */
295     assert (cairo_scaled_font_get_type (scaled_font) == CAIRO_FONT_TYPE_WIN32);
296 
297     /* We can only handle opaque solid color sources and destinations */
298     assert (_cairo_pattern_is_opaque_solid(source));
299     assert (dst->format == CAIRO_FORMAT_RGB24);
300 
301     solid_pattern = (cairo_solid_pattern_t *)source;
302     color = RGB(((int)solid_pattern->color.red_short) >> 8,
303 		((int)solid_pattern->color.green_short) >> 8,
304 		((int)solid_pattern->color.blue_short) >> 8);
305 
306     cairo_win32_scaled_font_get_device_to_logical(scaled_font, &device_to_logical);
307 
308     SaveDC(dst->dc);
309 
310     cairo_win32_scaled_font_select_font(scaled_font, dst->dc);
311     SetTextColor(dst->dc, color);
312     SetTextAlign(dst->dc, TA_BASELINE | TA_LEFT);
313     SetBkMode(dst->dc, TRANSPARENT);
314 
315     if (num_glyphs > STACK_GLYPH_SIZE) {
316 	glyph_buf = (WORD *) _cairo_malloc_ab (num_glyphs, sizeof(WORD));
317         dxy_buf = (int *) _cairo_malloc_abc (num_glyphs, sizeof(int), 2);
318     }
319 
320     /* It is vital that dx values for dxy_buf are calculated from the delta of
321      * _logical_ x coordinates (not user x coordinates) or else the sum of all
322      * previous dx values may start to diverge from the current glyph's x
323      * coordinate due to accumulated rounding error. As a result strings could
324      * be painted shorter or longer than expected. */
325 
326     user_x = glyphs[0].x;
327     user_y = glyphs[0].y;
328 
329     cairo_matrix_transform_point(&device_to_logical,
330                                  &user_x, &user_y);
331 
332     logical_x = _cairo_lround (user_x);
333     logical_y = _cairo_lround (user_y);
334 
335     start_x = logical_x;
336     start_y = logical_y;
337 
338     for (i = 0, j = 0; i < num_glyphs; ++i, j = 2 * i) {
339         glyph_buf[i] = (WORD) glyphs[i].index;
340         if (i == num_glyphs - 1) {
341             dxy_buf[j] = 0;
342             dxy_buf[j+1] = 0;
343         } else {
344             double next_user_x = glyphs[i+1].x;
345             double next_user_y = glyphs[i+1].y;
346             int next_logical_x, next_logical_y;
347 
348             cairo_matrix_transform_point(&device_to_logical,
349                                          &next_user_x, &next_user_y);
350 
351             next_logical_x = _cairo_lround (next_user_x);
352             next_logical_y = _cairo_lround (next_user_y);
353 
354             dxy_buf[j] = _cairo_lround (next_logical_x - logical_x);
355             dxy_buf[j+1] = _cairo_lround (next_logical_y - logical_y);
356 
357             logical_x = next_logical_x;
358             logical_y = next_logical_y;
359         }
360     }
361 
362     if (glyph_indexing)
363 	glyph_index_option = ETO_GLYPH_INDEX;
364     else
365 	glyph_index_option = 0;
366 
367     win_result = ExtTextOutW(dst->dc,
368                              start_x,
369                              start_y,
370                              glyph_index_option | ETO_PDY,
371                              NULL,
372                              glyph_buf,
373                              num_glyphs,
374                              dxy_buf);
375     if (!win_result) {
376         _cairo_win32_print_gdi_error("_cairo_win32_surface_show_glyphs(ExtTextOutW failed)");
377     }
378 
379     RestoreDC(dst->dc, -1);
380 
381     if (glyph_buf != glyph_buf_stack) {
382 	free(glyph_buf);
383         free(dxy_buf);
384     }
385     return (win_result) ? CAIRO_STATUS_SUCCESS : CAIRO_INT_STATUS_UNSUPPORTED;
386 #else
387     return CAIRO_INT_STATUS_UNSUPPORTED;
388 #endif
389 }
390 #undef STACK_GLYPH_SIZE
391 
392 cairo_status_t
cairo_win32_surface_get_size(const cairo_surface_t * surface,int * width,int * height)393 cairo_win32_surface_get_size (const cairo_surface_t *surface, int *width, int *height)
394 {
395     if (surface->type != CAIRO_SURFACE_TYPE_WIN32)
396         return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
397 
398     const cairo_win32_surface_t *winsurface = (const cairo_win32_surface_t *) surface;
399 
400     *width = winsurface->extents.width;
401     *height = winsurface->extents.height;
402 
403     return CAIRO_STATUS_SUCCESS;
404 }
405 
406