Merge the following revisions from kernel-fun branch:
[reactos.git] / reactos / win32ss / gdi / gdi32 / wine / enhmfdrv / objects.c
1 /*
2 * Enhanced MetaFile objects
3 *
4 * Copyright 1999 Huw D M Davies
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "enhmfdrv/enhmetafiledrv.h"
26 #include "gdi_private.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
30
31
32 /******************************************************************
33 * EMFDRV_AddHandle
34 */
35 static UINT EMFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
36 {
37 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
38 UINT index;
39
40 for(index = 0; index < physDev->handles_size; index++)
41 if(physDev->handles[index] == 0) break;
42 if(index == physDev->handles_size) {
43 physDev->handles_size += HANDLE_LIST_INC;
44 physDev->handles = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
45 physDev->handles,
46 physDev->handles_size * sizeof(physDev->handles[0]));
47 }
48 physDev->handles[index] = get_full_gdi_handle( obj );
49
50 physDev->cur_handles++;
51 if(physDev->cur_handles > physDev->emh->nHandles)
52 physDev->emh->nHandles++;
53
54 return index + 1; /* index 0 is reserved for the hmf, so we increment everything by 1 */
55 }
56
57 /******************************************************************
58 * EMFDRV_FindObject
59 */
60 static UINT EMFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
61 {
62 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
63 UINT index;
64
65 for(index = 0; index < physDev->handles_size; index++)
66 if(physDev->handles[index] == obj) break;
67
68 if(index == physDev->handles_size) return 0;
69
70 return index + 1;
71 }
72
73
74 /******************************************************************
75 * EMFDRV_DeleteObject
76 */
77 BOOL EMFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
78 {
79 EMRDELETEOBJECT emr;
80 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
81 UINT index;
82 BOOL ret = TRUE;
83
84 if(!(index = EMFDRV_FindObject(dev, obj))) return FALSE;
85
86 emr.emr.iType = EMR_DELETEOBJECT;
87 emr.emr.nSize = sizeof(emr);
88 emr.ihObject = index;
89
90 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
91 ret = FALSE;
92
93 physDev->handles[index - 1] = 0;
94 physDev->cur_handles--;
95 return ret;
96 }
97
98
99 /***********************************************************************
100 * EMFDRV_SelectBitmap
101 */
102 HBITMAP EMFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
103 {
104 return 0;
105 }
106
107
108 /***********************************************************************
109 * EMFDRV_CreateBrushIndirect
110 */
111 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
112 {
113 DWORD index = 0;
114 LOGBRUSH logbrush;
115
116 if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
117
118 switch (logbrush.lbStyle) {
119 case BS_SOLID:
120 case BS_HATCHED:
121 case BS_NULL:
122 {
123 EMRCREATEBRUSHINDIRECT emr;
124 emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
125 emr.emr.nSize = sizeof(emr);
126 emr.ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
127 emr.lb.lbStyle = logbrush.lbStyle;
128 emr.lb.lbColor = logbrush.lbColor;
129 emr.lb.lbHatch = logbrush.lbHatch;
130
131 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
132 index = 0;
133 }
134 break;
135 case BS_PATTERN:
136 case BS_DIBPATTERN:
137 {
138 EMRCREATEDIBPATTERNBRUSHPT *emr;
139 char buffer[sizeof(BITMAPINFO) + 255 * sizeof(RGBQUAD)]; // ros!
140 //char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
141 BITMAPINFO *info = (BITMAPINFO *)buffer;
142 DWORD info_size;
143 void *bits;
144 UINT usage;
145
146 if (!get_brush_bitmap_info( hBrush, info, &bits, &usage )) break;
147 info_size = get_dib_info_size( info, usage );
148
149 emr = HeapAlloc( GetProcessHeap(), 0,
150 sizeof(EMRCREATEDIBPATTERNBRUSHPT)+info_size+info->bmiHeader.biSizeImage );
151 if(!emr) break;
152
153 if (logbrush.lbStyle == BS_PATTERN && info->bmiHeader.biBitCount == 1)
154 {
155 /* Presumably to reduce the size of the written EMF, MS supports an
156 * undocumented iUsage value of 2, indicating a mono bitmap without the
157 * 8 byte 2 entry black/white palette. Stupidly, they could have saved
158 * over 20 bytes more by also ignoring the BITMAPINFO fields that are
159 * irrelevant/constant for monochrome bitmaps.
160 * FIXME: It may be that the DIB functions themselves accept this value.
161 */
162 emr->emr.iType = EMR_CREATEMONOBRUSH;
163 usage = DIB_PAL_MONO;
164 /* FIXME: There is an extra DWORD written by native before the BMI.
165 * Not sure what its meant to contain.
166 */
167 emr->offBmi = sizeof( EMRCREATEDIBPATTERNBRUSHPT ) + sizeof(DWORD);
168 emr->cbBmi = sizeof( BITMAPINFOHEADER );
169 }
170 else
171 {
172 emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
173 emr->offBmi = sizeof( EMRCREATEDIBPATTERNBRUSHPT );
174 emr->cbBmi = info_size;
175 }
176 emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
177 emr->iUsage = usage;
178 emr->offBits = emr->offBmi + emr->cbBmi;
179 emr->cbBits = info->bmiHeader.biSizeImage;
180 emr->emr.nSize = emr->offBits + emr->cbBits;
181
182 memcpy( (BYTE *)emr + emr->offBmi, info, emr->cbBmi );
183 memcpy( (BYTE *)emr + emr->offBits, bits, emr->cbBits );
184
185 if(!EMFDRV_WriteRecord( dev, &emr->emr ))
186 index = 0;
187 HeapFree( GetProcessHeap(), 0, emr );
188 }
189 break;
190
191 default:
192 FIXME("Unknown style %x\n", logbrush.lbStyle);
193 break;
194 }
195 return index;
196 }
197
198
199 /***********************************************************************
200 * EMFDRV_SelectBrush
201 */
202 HBRUSH EMFDRV_SelectBrush( PHYSDEV dev, HBRUSH hBrush, const struct brush_pattern *pattern )
203 {
204 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
205 EMRSELECTOBJECT emr;
206 DWORD index;
207 int i;
208
209 if (physDev->restoring) return hBrush; /* don't output SelectObject records during RestoreDC */
210
211 /* If the object is a stock brush object, do not need to create it.
212 * See definitions in wingdi.h for range of stock brushes.
213 * We do however have to handle setting the higher order bit to
214 * designate that this is a stock object.
215 */
216 for (i = WHITE_BRUSH; i <= DC_BRUSH; i++)
217 {
218 if (hBrush == GetStockObject(i))
219 {
220 index = i | 0x80000000;
221 goto found;
222 }
223 }
224 if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
225 goto found;
226
227 if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
228 GDI_hdc_using_object(hBrush, dev->hdc);
229
230 found:
231 emr.emr.iType = EMR_SELECTOBJECT;
232 emr.emr.nSize = sizeof(emr);
233 emr.ihObject = index;
234 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
235 }
236
237
238 /******************************************************************
239 * EMFDRV_CreateFontIndirect
240 */
241 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
242 {
243 DWORD index = 0;
244 EMREXTCREATEFONTINDIRECTW emr;
245 int i;
246
247 if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return FALSE;
248
249 emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
250 emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
251 emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
252 emr.elfw.elfFullName[0] = '\0';
253 emr.elfw.elfStyle[0] = '\0';
254 emr.elfw.elfVersion = 0;
255 emr.elfw.elfStyleSize = 0;
256 emr.elfw.elfMatch = 0;
257 emr.elfw.elfReserved = 0;
258 for(i = 0; i < ELF_VENDOR_SIZE; i++)
259 emr.elfw.elfVendorId[i] = 0;
260 emr.elfw.elfCulture = PAN_CULTURE_LATIN;
261 emr.elfw.elfPanose.bFamilyType = PAN_NO_FIT;
262 emr.elfw.elfPanose.bSerifStyle = PAN_NO_FIT;
263 emr.elfw.elfPanose.bWeight = PAN_NO_FIT;
264 emr.elfw.elfPanose.bProportion = PAN_NO_FIT;
265 emr.elfw.elfPanose.bContrast = PAN_NO_FIT;
266 emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
267 emr.elfw.elfPanose.bArmStyle = PAN_NO_FIT;
268 emr.elfw.elfPanose.bLetterform = PAN_NO_FIT;
269 emr.elfw.elfPanose.bMidline = PAN_NO_FIT;
270 emr.elfw.elfPanose.bXHeight = PAN_NO_FIT;
271
272 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
273 index = 0;
274 return index;
275 }
276
277
278 /***********************************************************************
279 * EMFDRV_SelectFont
280 */
281 HFONT EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont, UINT *aa_flags )
282 {
283 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
284 EMRSELECTOBJECT emr;
285 DWORD index;
286 int i;
287
288 if (physDev->restoring) goto done; /* don't output SelectObject records during RestoreDC */
289
290 /* If the object is a stock font object, do not need to create it.
291 * See definitions in wingdi.h for range of stock fonts.
292 * We do however have to handle setting the higher order bit to
293 * designate that this is a stock object.
294 */
295
296 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
297 {
298 if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
299 {
300 index = i | 0x80000000;
301 goto found;
302 }
303 }
304
305 if((index = EMFDRV_FindObject(dev, hFont)) != 0)
306 goto found;
307
308 if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return 0;
309 GDI_hdc_using_object(hFont, dev->hdc);
310
311 found:
312 emr.emr.iType = EMR_SELECTOBJECT;
313 emr.emr.nSize = sizeof(emr);
314 emr.ihObject = index;
315 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
316 return 0;
317 done:
318 *aa_flags = GGO_BITMAP; /* no point in anti-aliasing on metafiles */
319 dev = GET_NEXT_PHYSDEV( dev, pSelectFont );
320 dev->funcs->pSelectFont( dev, hFont, aa_flags );
321 return hFont;
322 }
323
324
325
326 /******************************************************************
327 * EMFDRV_CreatePenIndirect
328 */
329 static DWORD EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen)
330 {
331 EMRCREATEPEN emr;
332 DWORD index = 0;
333
334 if (!GetObjectW( hPen, sizeof(emr.lopn), &emr.lopn ))
335 {
336 /* must be an extended pen */
337 EXTLOGPEN *elp;
338 INT size = GetObjectW( hPen, 0, NULL );
339
340 if (!size) return 0;
341
342 elp = HeapAlloc( GetProcessHeap(), 0, size );
343
344 GetObjectW( hPen, size, elp );
345 /* FIXME: add support for user style pens */
346 emr.lopn.lopnStyle = elp->elpPenStyle;
347 emr.lopn.lopnWidth.x = elp->elpWidth;
348 emr.lopn.lopnWidth.y = 0;
349 emr.lopn.lopnColor = elp->elpColor;
350
351 HeapFree( GetProcessHeap(), 0, elp );
352 }
353
354 emr.emr.iType = EMR_CREATEPEN;
355 emr.emr.nSize = sizeof(emr);
356 emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
357
358 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
359 index = 0;
360 return index;
361 }
362
363 /******************************************************************
364 * EMFDRV_SelectPen
365 */
366 HPEN EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen, const struct brush_pattern *pattern )
367 {
368 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
369 EMRSELECTOBJECT emr;
370 DWORD index;
371 int i;
372
373 if (physDev->restoring) return hPen; /* don't output SelectObject records during RestoreDC */
374
375 /* If the object is a stock pen object, do not need to create it.
376 * See definitions in wingdi.h for range of stock pens.
377 * We do however have to handle setting the higher order bit to
378 * designate that this is a stock object.
379 */
380
381 for (i = WHITE_PEN; i <= DC_PEN; i++)
382 {
383 if (hPen == GetStockObject(i))
384 {
385 index = i | 0x80000000;
386 goto found;
387 }
388 }
389 if((index = EMFDRV_FindObject(dev, hPen)) != 0)
390 goto found;
391
392 if (!(index = EMFDRV_CreatePenIndirect(dev, hPen))) return 0;
393 GDI_hdc_using_object(hPen, dev->hdc);
394
395 found:
396 emr.emr.iType = EMR_SELECTOBJECT;
397 emr.emr.nSize = sizeof(emr);
398 emr.ihObject = index;
399 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
400 }
401
402
403 /******************************************************************
404 * EMFDRV_CreatePalette
405 */
406 static DWORD EMFDRV_CreatePalette(PHYSDEV dev, HPALETTE hPal)
407 {
408 WORD i;
409 struct {
410 EMRCREATEPALETTE hdr;
411 PALETTEENTRY entry[255];
412 } pal;
413
414 memset( &pal, 0, sizeof(pal) );
415
416 if (!GetObjectW( hPal, sizeof(pal.hdr.lgpl) + sizeof(pal.entry), &pal.hdr.lgpl ))
417 return 0;
418
419 for (i = 0; i < pal.hdr.lgpl.palNumEntries; i++)
420 pal.hdr.lgpl.palPalEntry[i].peFlags = 0;
421
422 pal.hdr.emr.iType = EMR_CREATEPALETTE;
423 pal.hdr.emr.nSize = sizeof(pal.hdr) + pal.hdr.lgpl.palNumEntries * sizeof(PALETTEENTRY);
424 pal.hdr.ihPal = EMFDRV_AddHandle( dev, hPal );
425
426 if (!EMFDRV_WriteRecord( dev, &pal.hdr.emr ))
427 pal.hdr.ihPal = 0;
428 return pal.hdr.ihPal;
429 }
430
431 /******************************************************************
432 * EMFDRV_SelectPalette
433 */
434 HPALETTE EMFDRV_SelectPalette( PHYSDEV dev, HPALETTE hPal, BOOL force )
435 {
436 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
437 EMRSELECTPALETTE emr;
438 DWORD index;
439
440 if (physDev->restoring) return hPal; /* don't output SelectObject records during RestoreDC */
441
442 if (hPal == GetStockObject( DEFAULT_PALETTE ))
443 {
444 index = DEFAULT_PALETTE | 0x80000000;
445 goto found;
446 }
447
448 if ((index = EMFDRV_FindObject( dev, hPal )) != 0)
449 goto found;
450
451 if (!(index = EMFDRV_CreatePalette( dev, hPal ))) return 0;
452 GDI_hdc_using_object( hPal, dev->hdc );
453
454 found:
455 emr.emr.iType = EMR_SELECTPALETTE;
456 emr.emr.nSize = sizeof(emr);
457 emr.ihPal = index;
458 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPal : 0;
459 }
460
461 /******************************************************************
462 * EMFDRV_SetDCBrushColor
463 */
464 COLORREF EMFDRV_SetDCBrushColor( PHYSDEV dev, COLORREF color )
465 {
466 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
467 EMRSELECTOBJECT emr;
468 DWORD index;
469
470 if (GetCurrentObject( dev->hdc, OBJ_BRUSH ) != GetStockObject( DC_BRUSH )) return color;
471
472 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
473 if (!(physDev->dc_brush = CreateSolidBrush( color ))) return CLR_INVALID;
474 if (!(index = EMFDRV_CreateBrushIndirect(dev, physDev->dc_brush ))) return CLR_INVALID;
475 GDI_hdc_using_object( physDev->dc_brush, dev->hdc );
476 emr.emr.iType = EMR_SELECTOBJECT;
477 emr.emr.nSize = sizeof(emr);
478 emr.ihObject = index;
479 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
480 }
481
482 /******************************************************************
483 * EMFDRV_SetDCPenColor
484 */
485 COLORREF EMFDRV_SetDCPenColor( PHYSDEV dev, COLORREF color )
486 {
487 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
488 EMRSELECTOBJECT emr;
489 DWORD index;
490 LOGPEN logpen = { PS_SOLID, { 0, 0 }, color };
491
492 if (GetCurrentObject( dev->hdc, OBJ_PEN ) != GetStockObject( DC_PEN )) return color;
493
494 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
495 if (!(physDev->dc_pen = CreatePenIndirect( &logpen ))) return CLR_INVALID;
496 if (!(index = EMFDRV_CreatePenIndirect(dev, physDev->dc_pen))) return CLR_INVALID;
497 GDI_hdc_using_object( physDev->dc_pen, dev->hdc );
498 emr.emr.iType = EMR_SELECTOBJECT;
499 emr.emr.nSize = sizeof(emr);
500 emr.ihObject = index;
501 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
502 }
503
504 /******************************************************************
505 * EMFDRV_GdiComment
506 */
507 BOOL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, const BYTE *buffer)
508 {
509 EMRGDICOMMENT *emr;
510 UINT total, rounded_size;
511 BOOL ret;
512
513 rounded_size = (bytes+3) & ~3;
514 total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
515
516 emr = HeapAlloc(GetProcessHeap(), 0, total);
517 emr->emr.iType = EMR_GDICOMMENT;
518 emr->emr.nSize = total;
519 emr->cbData = bytes;
520 memset(&emr->Data[bytes], 0, rounded_size - bytes);
521 memcpy(&emr->Data[0], buffer, bytes);
522
523 ret = EMFDRV_WriteRecord( dev, &emr->emr );
524
525 HeapFree(GetProcessHeap(), 0, emr);
526
527 return ret;
528 }