gdi32: Avoid redundant computation of the gradient bounding rectangle.
[wine/multimedia.git] / dlls / gdi32 / enhmfdrv / objects.c
blob789c7a585a7960ed08b24896a73fa1177a18949c
1 /*
2 * Enhanced MetaFile objects
4 * Copyright 1999 Huw D M Davies
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.
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.
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
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "enhmfdrv/enhmetafiledrv.h"
26 #include "gdi_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
32 /******************************************************************
33 * EMFDRV_AddHandle
35 static UINT EMFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
37 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
38 UINT index;
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]));
48 physDev->handles[index] = obj;
50 physDev->cur_handles++;
51 if(physDev->cur_handles > physDev->emh->nHandles)
52 physDev->emh->nHandles++;
54 return index + 1; /* index 0 is reserved for the hmf, so we increment everything by 1 */
57 /******************************************************************
58 * EMFDRV_FindObject
60 static UINT EMFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
62 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
63 UINT index;
65 for(index = 0; index < physDev->handles_size; index++)
66 if(physDev->handles[index] == obj) break;
68 if(index == physDev->handles_size) return 0;
70 return index + 1;
74 /******************************************************************
75 * EMFDRV_DeleteObject
77 BOOL EMFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
79 EMRDELETEOBJECT emr;
80 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
81 UINT index;
82 BOOL ret = TRUE;
84 if(!(index = EMFDRV_FindObject(dev, obj))) return 0;
86 emr.emr.iType = EMR_DELETEOBJECT;
87 emr.emr.nSize = sizeof(emr);
88 emr.ihObject = index;
90 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
91 ret = FALSE;
93 physDev->handles[index - 1] = 0;
94 physDev->cur_handles--;
95 return ret;
99 /***********************************************************************
100 * EMFDRV_SelectBitmap
102 HBITMAP EMFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
104 return 0;
108 /***********************************************************************
109 * EMFDRV_CreateBrushIndirect
111 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
113 DWORD index = 0;
114 LOGBRUSH logbrush;
116 if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
118 switch (logbrush.lbStyle) {
119 case BS_SOLID:
120 case BS_HATCHED:
121 case BS_NULL:
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;
131 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
132 index = 0;
134 break;
135 case BS_PATTERN:
136 case BS_DIBPATTERN:
138 EMRCREATEDIBPATTERNBRUSHPT *emr;
139 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
140 BITMAPINFO *info = (BITMAPINFO *)buffer;
141 DWORD info_size;
142 void *bits;
143 UINT usage;
145 if (!get_brush_bitmap_info( hBrush, info, &bits, &usage )) break;
146 info_size = get_dib_info_size( info, usage );
148 emr = HeapAlloc( GetProcessHeap(), 0,
149 sizeof(EMRCREATEDIBPATTERNBRUSHPT)+info_size+info->bmiHeader.biSizeImage );
150 if(!emr) break;
152 if (logbrush.lbStyle == BS_PATTERN && info->bmiHeader.biBitCount == 1)
154 /* Presumably to reduce the size of the written EMF, MS supports an
155 * undocumented iUsage value of 2, indicating a mono bitmap without the
156 * 8 byte 2 entry black/white palette. Stupidly, they could have saved
157 * over 20 bytes more by also ignoring the BITMAPINFO fields that are
158 * irrelevant/constant for monochrome bitmaps.
159 * FIXME: It may be that the DIB functions themselves accept this value.
161 emr->emr.iType = EMR_CREATEMONOBRUSH;
162 usage = DIB_PAL_MONO;
163 /* FIXME: There is an extra DWORD written by native before the BMI.
164 * Not sure what its meant to contain.
166 emr->offBmi = sizeof( EMRCREATEDIBPATTERNBRUSHPT ) + sizeof(DWORD);
167 emr->cbBmi = sizeof( BITMAPINFOHEADER );
169 else
171 emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
172 emr->offBmi = sizeof( EMRCREATEDIBPATTERNBRUSHPT );
173 emr->cbBmi = info_size;
175 emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
176 emr->iUsage = usage;
177 emr->offBits = emr->offBmi + emr->cbBmi;
178 emr->cbBits = info->bmiHeader.biSizeImage;
179 emr->emr.nSize = emr->offBits + emr->cbBits;
181 memcpy( (BYTE *)emr + emr->offBmi, info, emr->cbBmi );
182 memcpy( (BYTE *)emr + emr->offBits, bits, emr->cbBits );
184 if(!EMFDRV_WriteRecord( dev, &emr->emr ))
185 index = 0;
186 HeapFree( GetProcessHeap(), 0, emr );
188 break;
190 default:
191 FIXME("Unknown style %x\n", logbrush.lbStyle);
192 break;
194 return index;
198 /***********************************************************************
199 * EMFDRV_SelectBrush
201 HBRUSH EMFDRV_SelectBrush( PHYSDEV dev, HBRUSH hBrush, const struct brush_pattern *pattern )
203 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
204 EMRSELECTOBJECT emr;
205 DWORD index;
206 int i;
208 if (physDev->restoring) return hBrush; /* don't output SelectObject records during RestoreDC */
210 /* If the object is a stock brush object, do not need to create it.
211 * See definitions in wingdi.h for range of stock brushes.
212 * We do however have to handle setting the higher order bit to
213 * designate that this is a stock object.
215 for (i = WHITE_BRUSH; i <= DC_BRUSH; i++)
217 if (hBrush == GetStockObject(i))
219 index = i | 0x80000000;
220 goto found;
223 if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
224 goto found;
226 if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
227 GDI_hdc_using_object(hBrush, dev->hdc);
229 found:
230 emr.emr.iType = EMR_SELECTOBJECT;
231 emr.emr.nSize = sizeof(emr);
232 emr.ihObject = index;
233 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
237 /******************************************************************
238 * EMFDRV_CreateFontIndirect
240 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
242 DWORD index = 0;
243 EMREXTCREATEFONTINDIRECTW emr;
244 int i;
246 if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
248 emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
249 emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
250 emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
251 emr.elfw.elfFullName[0] = '\0';
252 emr.elfw.elfStyle[0] = '\0';
253 emr.elfw.elfVersion = 0;
254 emr.elfw.elfStyleSize = 0;
255 emr.elfw.elfMatch = 0;
256 emr.elfw.elfReserved = 0;
257 for(i = 0; i < ELF_VENDOR_SIZE; i++)
258 emr.elfw.elfVendorId[i] = 0;
259 emr.elfw.elfCulture = PAN_CULTURE_LATIN;
260 emr.elfw.elfPanose.bFamilyType = PAN_NO_FIT;
261 emr.elfw.elfPanose.bSerifStyle = PAN_NO_FIT;
262 emr.elfw.elfPanose.bWeight = PAN_NO_FIT;
263 emr.elfw.elfPanose.bProportion = PAN_NO_FIT;
264 emr.elfw.elfPanose.bContrast = PAN_NO_FIT;
265 emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
266 emr.elfw.elfPanose.bArmStyle = PAN_NO_FIT;
267 emr.elfw.elfPanose.bLetterform = PAN_NO_FIT;
268 emr.elfw.elfPanose.bMidline = PAN_NO_FIT;
269 emr.elfw.elfPanose.bXHeight = PAN_NO_FIT;
271 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
272 index = 0;
273 return index;
277 /***********************************************************************
278 * EMFDRV_SelectFont
280 HFONT EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont )
282 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
283 EMRSELECTOBJECT emr;
284 DWORD index;
285 int i;
287 if (physDev->restoring) goto done; /* don't output SelectObject records during RestoreDC */
289 /* If the object is a stock font object, do not need to create it.
290 * See definitions in wingdi.h for range of stock fonts.
291 * We do however have to handle setting the higher order bit to
292 * designate that this is a stock object.
295 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
297 if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
299 index = i | 0x80000000;
300 goto found;
304 if((index = EMFDRV_FindObject(dev, hFont)) != 0)
305 goto found;
307 if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return 0;
308 GDI_hdc_using_object(hFont, dev->hdc);
310 found:
311 emr.emr.iType = EMR_SELECTOBJECT;
312 emr.emr.nSize = sizeof(emr);
313 emr.ihObject = index;
314 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
315 return 0;
316 done:
317 dev = GET_NEXT_PHYSDEV( dev, pSelectFont );
318 dev->funcs->pSelectFont( dev, hFont );
319 return hFont;
324 /******************************************************************
325 * EMFDRV_CreatePenIndirect
327 static DWORD EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen)
329 EMRCREATEPEN emr;
330 DWORD index = 0;
332 if (!GetObjectW( hPen, sizeof(emr.lopn), &emr.lopn ))
334 /* must be an extended pen */
335 EXTLOGPEN *elp;
336 INT size = GetObjectW( hPen, 0, NULL );
338 if (!size) return 0;
340 elp = HeapAlloc( GetProcessHeap(), 0, size );
342 GetObjectW( hPen, size, elp );
343 /* FIXME: add support for user style pens */
344 emr.lopn.lopnStyle = elp->elpPenStyle;
345 emr.lopn.lopnWidth.x = elp->elpWidth;
346 emr.lopn.lopnWidth.y = 0;
347 emr.lopn.lopnColor = elp->elpColor;
349 HeapFree( GetProcessHeap(), 0, elp );
352 emr.emr.iType = EMR_CREATEPEN;
353 emr.emr.nSize = sizeof(emr);
354 emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
356 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
357 index = 0;
358 return index;
361 /******************************************************************
362 * EMFDRV_SelectPen
364 HPEN EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen, const struct brush_pattern *pattern )
366 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
367 EMRSELECTOBJECT emr;
368 DWORD index;
369 int i;
371 if (physDev->restoring) return hPen; /* don't output SelectObject records during RestoreDC */
373 /* If the object is a stock pen object, do not need to create it.
374 * See definitions in wingdi.h for range of stock pens.
375 * We do however have to handle setting the higher order bit to
376 * designate that this is a stock object.
379 for (i = WHITE_PEN; i <= DC_PEN; i++)
381 if (hPen == GetStockObject(i))
383 index = i | 0x80000000;
384 goto found;
387 if((index = EMFDRV_FindObject(dev, hPen)) != 0)
388 goto found;
390 if (!(index = EMFDRV_CreatePenIndirect(dev, hPen))) return 0;
391 GDI_hdc_using_object(hPen, dev->hdc);
393 found:
394 emr.emr.iType = EMR_SELECTOBJECT;
395 emr.emr.nSize = sizeof(emr);
396 emr.ihObject = index;
397 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
401 /******************************************************************
402 * EMFDRV_CreatePalette
404 static DWORD EMFDRV_CreatePalette(PHYSDEV dev, HPALETTE hPal)
406 WORD i;
407 struct {
408 EMRCREATEPALETTE hdr;
409 PALETTEENTRY entry[255];
410 } pal;
412 memset( &pal, 0, sizeof(pal) );
414 if (!GetObjectW( hPal, sizeof(pal.hdr.lgpl) + sizeof(pal.entry), &pal.hdr.lgpl ))
415 return 0;
417 for (i = 0; i < pal.hdr.lgpl.palNumEntries; i++)
418 pal.hdr.lgpl.palPalEntry[i].peFlags = 0;
420 pal.hdr.emr.iType = EMR_CREATEPALETTE;
421 pal.hdr.emr.nSize = sizeof(pal.hdr) + pal.hdr.lgpl.palNumEntries * sizeof(PALETTEENTRY);
422 pal.hdr.ihPal = EMFDRV_AddHandle( dev, hPal );
424 if (!EMFDRV_WriteRecord( dev, &pal.hdr.emr ))
425 pal.hdr.ihPal = 0;
426 return pal.hdr.ihPal;
429 /******************************************************************
430 * EMFDRV_SelectPalette
432 HPALETTE EMFDRV_SelectPalette( PHYSDEV dev, HPALETTE hPal, BOOL force )
434 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
435 EMRSELECTPALETTE emr;
436 DWORD index;
438 if (physDev->restoring) return hPal; /* don't output SelectObject records during RestoreDC */
440 if (hPal == GetStockObject( DEFAULT_PALETTE ))
442 index = DEFAULT_PALETTE | 0x80000000;
443 goto found;
446 if ((index = EMFDRV_FindObject( dev, hPal )) != 0)
447 goto found;
449 if (!(index = EMFDRV_CreatePalette( dev, hPal ))) return 0;
450 GDI_hdc_using_object( hPal, dev->hdc );
452 found:
453 emr.emr.iType = EMR_SELECTPALETTE;
454 emr.emr.nSize = sizeof(emr);
455 emr.ihPal = index;
456 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPal : 0;
459 /******************************************************************
460 * EMFDRV_SetDCBrushColor
462 COLORREF EMFDRV_SetDCBrushColor( PHYSDEV dev, COLORREF color )
464 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
465 EMRSELECTOBJECT emr;
466 DWORD index;
468 if (GetCurrentObject( dev->hdc, OBJ_BRUSH ) != GetStockObject( DC_BRUSH )) return color;
470 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
471 if (!(physDev->dc_brush = CreateSolidBrush( color ))) return CLR_INVALID;
472 if (!(index = EMFDRV_CreateBrushIndirect(dev, physDev->dc_brush ))) return CLR_INVALID;
473 GDI_hdc_using_object( physDev->dc_brush, dev->hdc );
474 emr.emr.iType = EMR_SELECTOBJECT;
475 emr.emr.nSize = sizeof(emr);
476 emr.ihObject = index;
477 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
480 /******************************************************************
481 * EMFDRV_SetDCPenColor
483 COLORREF EMFDRV_SetDCPenColor( PHYSDEV dev, COLORREF color )
485 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
486 EMRSELECTOBJECT emr;
487 DWORD index;
488 LOGPEN logpen = { PS_SOLID, { 0, 0 }, color };
490 if (GetCurrentObject( dev->hdc, OBJ_PEN ) != GetStockObject( DC_PEN )) return color;
492 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
493 if (!(physDev->dc_pen = CreatePenIndirect( &logpen ))) return CLR_INVALID;
494 if (!(index = EMFDRV_CreatePenIndirect(dev, physDev->dc_pen))) return CLR_INVALID;
495 GDI_hdc_using_object( physDev->dc_pen, dev->hdc );
496 emr.emr.iType = EMR_SELECTOBJECT;
497 emr.emr.nSize = sizeof(emr);
498 emr.ihObject = index;
499 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
502 /******************************************************************
503 * EMFDRV_GdiComment
505 BOOL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, CONST BYTE *buffer)
507 EMRGDICOMMENT *emr;
508 UINT total, rounded_size;
509 BOOL ret;
511 rounded_size = (bytes+3) & ~3;
512 total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
514 emr = HeapAlloc(GetProcessHeap(), 0, total);
515 emr->emr.iType = EMR_GDICOMMENT;
516 emr->emr.nSize = total;
517 emr->cbData = bytes;
518 memset(&emr->Data[bytes], 0, rounded_size - bytes);
519 memcpy(&emr->Data[0], buffer, bytes);
521 ret = EMFDRV_WriteRecord( dev, &emr->emr );
523 HeapFree(GetProcessHeap(), 0, emr);
525 return ret;