Debian Codegraph @

coders/wmf.c

raw symbols SCIP
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % W W M M FFFFF %
7 % W W MM MM F %
8 % W W W M M M FFF %
9 % WW WW M M F %
10 % W W M M F %
11 % %
12 % %
13 % Read Windows Metafile Format %
14 % %
15 % Software Design %
16 % Cristy %
17 % December 2000 %
18 % %
19 % %
20 % Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/license/ %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 */
36
37 /*
38 Include declarations.
39 */
40 #include "MagickCore/studio.h"
41 #include "MagickCore/property.h"
42 #include "MagickCore/blob.h"
43 #include "MagickCore/blob-private.h"
44 #include "MagickCore/color.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/constitute.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/log.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/paint.h"
58 #include "MagickCore/quantum-private.h"
59 #include "MagickCore/static.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/module.h"
62 #include "MagickCore/type.h"
63 #include "MagickCore/module.h"
64 #if defined(MAGICKCORE_WMF_DELEGATE)
65 #include "MagickWand/MagickWand.h"
66 #endif
67
68 #if defined(__CYGWIN__)
69 #undef MAGICKCORE_SANS_DELEGATE
70 #endif
71
72 #if defined(MAGICKCORE_SANS_DELEGATE)
73 #include "libwmf/api.h"
74 #include "libwmf/eps.h"
75
76 /*
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % %
79 % %
80 % %
81 % R e a d W M F I m a g e %
82 % %
83 % %
84 % %
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %
87 % ReadWMFImage() reads an Windows Metafile image file and returns it. It
88 % allocates the memory necessary for the new Image structure and returns a
89 % pointer to the new image.
90 %
91 % The format of the ReadWMFImage method is:
92 %
93 % Image *ReadWMFImage(const ImageInfo *image_info,ExceptionInfo *exception)
94 %
95 % A description of each parameter follows:
96 %
97 % o image_info: the image info.
98 %
99 % o exception: return any errors or warnings in this structure.
100 %
101 */
102
103 static int WMFReadBlob(void *image)
104 {
105 return(ReadBlobByte((Image *) image));
106 }
107
108 static int WMFSeekBlob(void *image,long offset)
109 {
110 return((int) SeekBlob((Image *) image,(MagickOffsetType) offset,SEEK_SET));
111 }
112
113 static long WMFTellBlob(void *image)
114 {
115 return((long) TellBlob((Image*) image));
116 }
117
118 static Image *ReadWMFImage(const ImageInfo *image_info,ExceptionInfo *exception)
119 {
120 char
121 filename[MagickPathExtent];
122
123 int
124 unique_file;
125
126 FILE
127 *file;
128
129 Image
130 *image;
131
132 ImageInfo
133 *read_info;
134
135 MagickBooleanType
136 status;
137
138 size_t
139 flags;
140
141 wmfAPI
142 *wmf_info;
143
144 wmfAPI_Options
145 options;
146
147 wmfD_Rect
148 bounding_box;
149
150 wmf_eps_t
151 *eps_info;
152
153 wmf_error_t
154 wmf_status;
155
156 /*
157 Read WMF image.
158 */
159 image=AcquireImage(image_info,exception);
160 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
161 if (status == MagickFalse)
162 {
163 image=DestroyImageList(image);
164 return((Image *) NULL);
165 }
166 wmf_info=(wmfAPI *) NULL;
167 flags=0;
168 flags|=WMF_OPT_IGNORE_NONFATAL;
169 flags|=WMF_OPT_FUNCTION;
170 options.function=wmf_eps_function;
171 wmf_status=wmf_api_create(&wmf_info,(unsigned long) flags,&options);
172 if (wmf_status != wmf_E_None)
173 {
174 if (wmf_info != (wmfAPI *) NULL)
175 wmf_api_destroy(wmf_info);
176 ThrowReaderException(DelegateError,"UnableToInitializeWMFLibrary");
177 }
178 wmf_status=wmf_bbuf_input(wmf_info,WMFReadBlob,WMFSeekBlob,WMFTellBlob,
179 (void *) image);
180 if (wmf_status != wmf_E_None)
181 {
182 ipa_device_close(wmf_info);
183 wmf_api_destroy(wmf_info);
184 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
185 image->filename);
186 image=DestroyImageList(image);
187 return((Image *) NULL);
188 }
189 wmf_status=wmf_scan(wmf_info,0,&bounding_box);
190 if (wmf_status != wmf_E_None)
191 {
192 ipa_device_close(wmf_info);
193 wmf_api_destroy(wmf_info);
194 ThrowReaderException(DelegateError,"FailedToScanFile");
195 }
196 eps_info=WMF_EPS_GetData(wmf_info);
197 file=(FILE *) NULL;
198 unique_file=AcquireUniqueFileResource(filename);
199 if (unique_file != -1)
200 file=fdopen(unique_file,"rb+");
201 if ((unique_file == -1) || (file == (FILE *) NULL))
202 {
203 ipa_device_close(wmf_info);
204 wmf_api_destroy(wmf_info);
205 ThrowReaderException(FileOpenError,"UnableToCreateTemporaryFile");
206 }
207 eps_info->out=wmf_stream_create(wmf_info,file);
208 eps_info->bbox=bounding_box;
209 wmf_status=wmf_play(wmf_info,0,&bounding_box);
210 if (wmf_status != wmf_E_None)
211 {
212 ipa_device_close(wmf_info);
213 wmf_api_destroy(wmf_info);
214 ThrowReaderException(DelegateError,"FailedToRenderFile");
215 }
216 if (fseek(file,0,SEEK_SET) != 0)
217 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
218 wmf_api_destroy(wmf_info);
219 (void) CloseBlob(image);
220 image=DestroyImage(image);
221 /*
222 Read EPS image.
223 */
224 read_info=CloneImageInfo(image_info);
225 read_info->file=file;
226 SetImageInfoBlob(read_info,(void *) NULL,0);
227 (void) FormatLocaleString(read_info->filename,MagickPathExtent,"eps:%s",
228 filename);
229 image=ReadImage(read_info,exception);
230 read_info=DestroyImageInfo(read_info);
231 if (image != (Image *) NULL)
232 {
233 (void) CopyMagickString(image->filename,image_info->filename,
234 MagickPathExtent);
235 (void) CopyMagickString(image->magick_filename,image_info->filename,
236 MagickPathExtent);
237 (void) CopyMagickString(image->magick,"WMF",MagickPathExtent);
238 }
239 (void) RelinquishUniqueFileResource(filename);
240 return(GetFirstImageInList(image));
241 }
242 #elif defined(MAGICKCORE_WMF_DELEGATE)
243
244 #define ERR(API) ((API)->err != wmf_E_None)
245 #define XC(x) ((double) (x))
246 #define YC(y) ((double) (y))
247
248 #if !defined(M_PI)
249 # define M_PI MagickPI
250 #endif
251
252 #if defined(MAGICKCORE_HAVE_FT2BUILD_H)
253 # include <ft2build.h>
254 #endif
255
256 #include "libwmf/fund.h"
257 #include "libwmf/types.h"
258 #include "libwmf/api.h"
259 #undef SRCCOPY
260 #undef SRCPAINT
261 #undef SRCAND
262 #undef SRCINVERT
263 #undef SRCERASE
264 #undef NOTSRCCOPY
265 #undef NOTSRCERASE
266 #undef MERGECOPY
267 #undef MERGEPAINT
268 #undef PATCOPY
269 #undef PATPAINT
270 #undef PATINVERT
271 #undef DSTINVERT
272 #undef BLACKNESS
273 #undef WHITENESS
274
275 /* The following additional undefs were required for MinGW */
276 #undef BS_HOLLOW
277 #undef PS_STYLE_MASK
278 #undef PS_ENDCAP_ROUND
279 #undef PS_ENDCAP_SQUARE
280 #undef PS_ENDCAP_FLAT
281 #undef PS_ENDCAP_MASK
282 #undef PS_JOIN_ROUND
283 #undef PS_JOIN_BEVEL
284 #undef PS_JOIN_MITER
285 #undef PS_COSMETIC
286 #undef PS_GEOMETRIC
287 #undef PS_TYPE_MASK
288 #undef STRETCH_ANDSCANS
289 #undef STRETCH_ORSCANS
290 #undef STRETCH_DELETESCANS
291 #undef STRETCH_HALFTONE
292 #undef ETO_OPAQUE
293 #undef ETO_CLIPPED
294 #undef ETO_GLYPH_INDEX
295 #undef ETO_RTLREADING
296
297 #include "libwmf/defs.h"
298 #include "libwmf/ipa.h"
299 #include "libwmf/color.h"
300 #include "libwmf/macro.h"
301
302 /* Unit conversions */
303 #define TWIPS_PER_INCH 1440
304 #define CENTIMETERS_PER_INCH 2.54
305 #define POINTS_PER_INCH 72
306
307 #if defined(MAGICKCORE_WMF_DELEGATE)
308 # define wmf_api_create(api,flags,options) wmf_lite_create(api,flags,options)
309 # define wmf_api_destroy(api) wmf_lite_destroy(api)
310 # undef WMF_FONT_PSNAME
311 # define WMF_FONT_PSNAME(F) ((F)->user_data ? ((wmf_magick_font_t*) (F)->user_data)->ps_name : 0)
312
313 typedef struct _wmf_magick_font_t wmf_magick_font_t;
314
315 struct _wmf_magick_font_t
316 {
317 char* ps_name;
318 double pointsize;
319 };
320
321 #endif
322
323 typedef struct _wmf_magick_t wmf_magick_t;
324
325 struct _wmf_magick_t
326 {
327 /* Bounding box */
328 wmfD_Rect
329 bbox;
330
331 /* Scale and translation factors */
332 double
333 scale_x,
334 scale_y,
335 translate_x,
336 translate_y,
337 rotate;
338
339 /* Vector output */
340 DrawingWand
341 *draw_wand;
342
343 ExceptionInfo
344 *exception;
345
346 /* ImageMagick image */
347 Image
348 *image;
349
350 /* ImageInfo */
351 const ImageInfo
352 *image_info;
353
354 /* DrawInfo */
355 DrawInfo
356 *draw_info;
357
358 /* Pattern ID */
359 unsigned long
360 pattern_id;
361
362 /* Clip path flag */
363 MagickBooleanType
364 clipping;
365
366 /* Clip path ID */
367 unsigned long
368 clip_mask_id;
369
370 /* Push depth */
371 long
372 push_depth;
373 };
374
375
376 #define WMF_MAGICK_GetData(Z) ((wmf_magick_t*)((Z)->device_data))
377 #define WMF_MAGICK_GetFontData(Z) \
378 ((wmf_magick_font_t*)((wmfFontData *)Z->font_data)->user_data)
379
380 #define WmfDrawingWand (((wmf_magick_t*)((API)->device_data))->draw_wand)
381
382 /* Enum to control whether util_set_brush applies brush to fill or
383 stroke. */
384 typedef enum
385 {
386 BrushApplyFill,
387 BrushApplyStroke
388 } BrushApply;
389
390
391 /* Enum to specify arc type */
392 typedef enum
393 {
394 magick_arc_ellipse = 0,
395 magick_arc_open,
396 magick_arc_pie,
397 magick_arc_chord
398 }
399 magick_arc_t;
400
401 #if defined(MAGICKCORE_WMF_DELEGATE)
402 static void lite_font_init (wmfAPI* API, wmfAPI_Options* options);
403 static void lite_font_map(wmfAPI* API,wmfFont* font);
404 static float lite_font_stringwidth(wmfAPI* API, wmfFont* font, char* str);
405 #endif
406
407 static void draw_fill_color_rgb(wmfAPI* API, const wmfRGB* rgb);
408 static void draw_stroke_color_rgb(wmfAPI* API, const wmfRGB* rgb);
409 static void draw_pattern_push(wmfAPI* API, unsigned long id, unsigned long columns, unsigned long rows);
410 static int ipa_blob_read(void* wand);
411 static int ipa_blob_seek(void* wand,long position);
412 static long ipa_blob_tell(void* wand);
413 static void ipa_bmp_draw(wmfAPI *API, wmfBMP_Draw_t *bmp_draw);
414 static void ipa_bmp_free(wmfAPI *API, wmfBMP *bmp);
415 static void ipa_bmp_read(wmfAPI *API, wmfBMP_Read_t *bmp_read);
416 static void ipa_device_begin(wmfAPI *API);
417 static void ipa_device_close(wmfAPI *API);
418 static void ipa_device_end(wmfAPI *API);
419 static void ipa_device_open(wmfAPI *API);
420 static void ipa_draw_arc(wmfAPI *API, wmfDrawArc_t *draw_arc);
421 static void ipa_draw_chord(wmfAPI *API, wmfDrawArc_t *draw_arc);
422 static void ipa_draw_ellipse(wmfAPI *API, wmfDrawArc_t *draw_arc);
423 static void ipa_draw_line(wmfAPI *API, wmfDrawLine_t *draw_line);
424 static void ipa_draw_pie(wmfAPI *API, wmfDrawArc_t *draw_arc);
425 static void ipa_draw_pixel(wmfAPI *API, wmfDrawPixel_t *draw_pixel);
426 static void ipa_draw_polygon(wmfAPI *API, wmfPolyLine_t *poly_line);
427 #if defined(MAGICKCORE_WMF_DELEGATE)
428 static void ipa_draw_polypolygon(wmfAPI *API, wmfPolyPoly_t* polypolygon);
429 #endif
430 static void ipa_draw_rectangle(wmfAPI *API, wmfDrawRectangle_t *draw_rect);
431 static void ipa_draw_text(wmfAPI *API, wmfDrawText_t *draw_text);
432 static void ipa_flood_exterior(wmfAPI *API, wmfFlood_t *flood);
433 static void ipa_flood_interior(wmfAPI *API, wmfFlood_t *flood);
434 static void ipa_functions(wmfAPI *API);
435 static void ipa_poly_line(wmfAPI *API, wmfPolyLine_t *poly_line);
436 static void ipa_region_clip(wmfAPI *API, wmfPolyRectangle_t *poly_rect);
437 static void ipa_region_frame(wmfAPI *API, wmfPolyRectangle_t *poly_rect);
438 static void ipa_region_paint(wmfAPI *API, wmfPolyRectangle_t *poly_rect);
439 static void ipa_rop_draw(wmfAPI *API, wmfROP_Draw_t *rop_draw);
440 static void ipa_udata_copy(wmfAPI *API, wmfUserData_t *userdata);
441 static void ipa_udata_free(wmfAPI *API, wmfUserData_t *userdata);
442 static void ipa_udata_init(wmfAPI *API, wmfUserData_t *userdata);
443 static void ipa_udata_set(wmfAPI *API, wmfUserData_t *userdata);
444 static int magick_progress_callback(void* wand,float quantum);
445 static void util_draw_arc(wmfAPI *API, wmfDrawArc_t *draw_arc,magick_arc_t finish);
446 static double util_pointsize(wmfAPI* API, wmfFont* font, char* str, double font_height, ExceptionInfo *);
447 static void util_set_brush(wmfAPI *API, wmfDC *dc, const BrushApply brush_apply);
448 static void util_set_pen(wmfAPI *API, wmfDC *dc);
449
450 /* Progress callback */
451 int magick_progress_callback(void *context,float quantum)
452 {
453 Image
454 *image;
455
456 MagickBooleanType
457 status;
458
459 (void) quantum;
460 image=(Image *) context;
461 assert(image->signature == MagickCoreSignature);
462 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
463 GetBlobSize(image));
464 return(status != MagickFalse ? 0 : 1);
465 }
466
467 /* Set fill color */
468 static void draw_fill_color_string(DrawingWand *drawing_wand,const char *color)
469 {
470 PixelWand
471 *fill_color;
472
473 fill_color=NewPixelWand();
474 PixelSetColor(fill_color,color);
475 DrawSetFillColor(drawing_wand,fill_color);
476 fill_color=DestroyPixelWand(fill_color);
477 }
478 static void draw_fill_color_rgb(wmfAPI* API, const wmfRGB* rgb)
479 {
480 PixelWand
481 *fill_color;
482
483 fill_color=NewPixelWand();
484 PixelSetRedQuantum(fill_color,ScaleCharToQuantum(rgb->r));
485 PixelSetGreenQuantum(fill_color,ScaleCharToQuantum(rgb->g));
486 PixelSetBlueQuantum(fill_color,ScaleCharToQuantum(rgb->b));
487 PixelSetAlphaQuantum(fill_color,OpaqueAlpha);
488 DrawSetFillColor(WmfDrawingWand,fill_color);
489 fill_color=DestroyPixelWand(fill_color);
490 }
491
492 /* Set stroke color */
493 static void draw_stroke_color_string(DrawingWand *drawing_wand,const char *color)
494 {
495 PixelWand
496 *stroke_color;
497
498 stroke_color=NewPixelWand();
499 PixelSetColor(stroke_color,color);
500 DrawSetStrokeColor(drawing_wand,stroke_color);
501 stroke_color=DestroyPixelWand(stroke_color);
502 }
503
504 static void draw_stroke_color_rgb(wmfAPI* API, const wmfRGB* rgb)
505 {
506 PixelWand
507 *stroke_color;
508
509 stroke_color=NewPixelWand();
510 PixelSetRedQuantum(stroke_color,ScaleCharToQuantum(rgb->r));
511 PixelSetGreenQuantum(stroke_color,ScaleCharToQuantum(rgb->g));
512 PixelSetBlueQuantum(stroke_color,ScaleCharToQuantum(rgb->b));
513 PixelSetAlphaQuantum(stroke_color,OpaqueAlpha);
514 DrawSetStrokeColor(WmfDrawingWand,stroke_color);
515 stroke_color=DestroyPixelWand(stroke_color);
516 }
517
518 /* Set under color */
519 static void draw_under_color_string(DrawingWand *drawing_wand,const char *color)
520 {
521 PixelWand
522 *under_color;
523
524 under_color=NewPixelWand();
525 PixelSetColor(under_color,color);
526 DrawSetTextUnderColor(drawing_wand,under_color);
527 under_color=DestroyPixelWand(under_color);
528 }
529
530 static void draw_pattern_push(wmfAPI* API,
531 unsigned long id,
532 unsigned long columns,
533 unsigned long rows)
534 {
535 char
536 pattern_id[MagickPathExtent];
537
538 (void) FormatLocaleString(pattern_id,MagickPathExtent,"brush_%lu",id);
539 (void) DrawPushPattern(WmfDrawingWand,pattern_id,0,0,columns,rows);
540 }
541
542 /* Pattern/Bit BLT with raster operation (ROP) support. Invoked by
543 META_PATBLT, which is equivalent to Windows PatBlt() call, or by
544 META_DIBBITBLT which is equivalent to Windows BitBlt() call. */
545
546 /* The BitBlt function transfers pixels from a rectangular area in one
547 device wand called the 'source', to a rectangular area of the
548 same size in another device wand, called the 'destination'. */
549
550 static void ipa_rop_draw(wmfAPI *API, wmfROP_Draw_t *rop_draw)
551 {
552 /* wmfBrush */
553 /* *brush = WMF_DC_BRUSH(rop_draw->dc); */
554
555 /* wmfBMP */
556 /* *brush_bmp = WMF_BRUSH_BITMAP(brush); */
557
558 if (TO_FILL(rop_draw) == 0)
559 return;
560
561 /* Save graphic wand */
562 (void) PushDrawingWand(WmfDrawingWand);
563
564 /* FIXME: finish implementing (once we know what it is supposed to do!) */
565
566 /*
567 struct _wmfROP_Draw_t
568 { wmfDC* dc;
569
570 wmfD_Coord TL;
571 wmfD_Coord BR;
572
573 U32 ROP;
574
575 double pixel_width;
576 double pixel_height;
577 };
578 */
579
580 /* if (brush_bmp && brush_bmp->data != 0) */
581 /* printf("Have an image!\n"); */
582
583 switch (rop_draw->ROP) /* Ternary raster operations */
584 {
585 case SRCCOPY: /* dest = source */
586 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw SRCCOPY ROP mode not implemented\n");
587 break;
588 case SRCPAINT: /* dest = source OR dest */
589 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw SRCPAINT ROP mode not implemented\n");
590 break;
591 case SRCAND: /* dest = source AND dest */
592 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw SRCAND ROP mode not implemented\n");
593 break;
594 case SRCINVERT: /* dest = source XOR dest */
595 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw SRCINVERT ROP mode not implemented\n");
596 break;
597 case SRCERASE: /* dest = source AND (NOT dest) */
598 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw SRCERASE ROP mode not implemented\n");
599 break;
600 case NOTSRCCOPY: /* dest = (NOT source) */
601 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw NOTSRCCOPY ROP mode not implemented\n");
602 break;
603 case NOTSRCERASE: /* dest = (NOT src) AND (NOT dest) */
604 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw NOTSRCERASE ROP mode not implemented\n");
605 break;
606 case MERGECOPY: /* dest = (source AND pattern) */
607 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw MERGECOPY ROP mode not implemented\n");
608 break;
609 case MERGEPAINT: /* dest = (NOT source) OR dest */
610 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw MERGEPAINT ROP mode not implemented\n");
611 break;
612 case PATCOPY: /* dest = pattern */
613 util_set_brush(API, rop_draw->dc, BrushApplyFill);
614 break;
615 case PATPAINT: /* dest = DPSnoo */
616 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw PATPAINT ROP mode not implemented\n");
617 break;
618 case PATINVERT: /* dest = pattern XOR dest */
619 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw PATINVERT ROP mode not implemented\n");
620 break;
621 case DSTINVERT: /* dest = (NOT dest) */
622 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw DSTINVERT ROP mode not implemented\n");
623 break;
624 case BLACKNESS: /* dest = BLACK */
625 draw_fill_color_string(WmfDrawingWand,"black");
626 break;
627 case WHITENESS: /* dest = WHITE */
628 draw_fill_color_string(WmfDrawingWand,"white");
629 break;
630 default:
631 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"ipa_rop_draw 0x%x ROP mode not implemented\n", rop_draw->ROP);
632 break;
633 }
634
635 DrawRectangle(WmfDrawingWand,
636 XC(rop_draw->TL.x), YC(rop_draw->TL.y),
637 XC(rop_draw->BR.x), YC(rop_draw->BR.y));
638
639 /* Restore graphic wand */
640 (void) PopDrawingWand(WmfDrawingWand);
641 }
642
643 static void ipa_bmp_draw(wmfAPI *API, wmfBMP_Draw_t *bmp_draw)
644 {
645 wmf_magick_t
646 *ddata = WMF_MAGICK_GetData(API);
647
648 ExceptionInfo
649 *exception;
650
651 Image
652 *image;
653
654 MagickWand
655 *magick_wand;
656
657 double
658 height,
659 width;
660
661 PixelInfo
662 white;
663
664 if (bmp_draw->bmp.data == 0)
665 return;
666
667 image = (Image*)bmp_draw->bmp.data;
668 if (!image)
669 return;
670
671 exception=ddata->exception;
672 if (bmp_draw->crop.x || bmp_draw->crop.y ||
673 (bmp_draw->crop.w != bmp_draw->bmp.width) ||
674 (bmp_draw->crop.h != bmp_draw->bmp.height))
675 {
676 /* Image needs to be cropped */
677 Image
678 *crop_image;
679
680 RectangleInfo
681 crop_info;
682
683 crop_info.x = bmp_draw->crop.x;
684 crop_info.y = bmp_draw->crop.y;
685 crop_info.width = bmp_draw->crop.w;
686 crop_info.height = bmp_draw->crop.h;
687
688 crop_image = CropImage(image, &crop_info, exception);
689 if (crop_image)
690 {
691 image=DestroyImageList(image);
692 image = crop_image;
693 bmp_draw->bmp.data = (void*)image;
694 }
695 }
696
697 QueryColorCompliance("white", AllCompliance, &white, exception);
698
699 if (ddata->image_info->texture ||
700 !(IsPixelInfoEquivalent(&ddata->image_info->background_color,&white)) ||
701 ddata->image_info->background_color.alpha != (double) OpaqueAlpha)
702 {
703 /*
704 Set image white background to transparent so that it may be
705 overlaid over non-white backgrounds.
706 */
707 QueryColorCompliance("white", AllCompliance, &white, exception);
708 TransparentPaintImage(image, &white, QuantumRange, MagickFalse, exception);
709 }
710
711 width = fabs(bmp_draw->pixel_width *(double) bmp_draw->crop.w);
712 height = fabs(bmp_draw->pixel_height *(double) bmp_draw->crop.h);
713 magick_wand=NewMagickWandFromImage(image);
714 (void) DrawComposite(WmfDrawingWand, CopyCompositeOp,
715 XC(bmp_draw->pt.x) *ddata->scale_x, YC(bmp_draw->pt.y) *ddata->scale_y,
716 width *ddata->scale_x, height *ddata->scale_y, magick_wand);
717 magick_wand=DestroyMagickWand(magick_wand);
718
719 #if 0
720 printf("bmp_draw->bmp.data = 0x%lx\n", (long)bmp_draw->bmp.data);
721 printf("registry id = %li\n", id);
722 /* printf("pixel_width = %g\n", bmp_draw->pixel_width); */
723 /* printf("pixel_height = %g\n", bmp_draw->pixel_height); */
724 printf("bmp_draw->bmp WxH = %ix%i\n", bmp_draw->bmp.width, bmp_draw->bmp.height);
725 printf("bmp_draw->crop WxH = %ix%i\n", bmp_draw->crop.w, bmp_draw->crop.h);
726 printf("bmp_draw->crop x,y = %i,%i\n", bmp_draw->crop.x, bmp_draw->crop.y);
727 printf("image size WxH = %lux%lu\n", image->columns, image->rows);
728 #endif
729 }
730
731 static void ipa_bmp_read(wmfAPI *API, wmfBMP_Read_t *bmp_read) {
732 wmf_magick_t
733 *ddata = WMF_MAGICK_GetData(API);
734
735 ExceptionInfo
736 *exception;
737
738 Image
739 *image;
740
741 ImageInfo
742 *image_info;
743
744 bmp_read->bmp.data = 0;
745
746 image_info=CloneImageInfo(ddata->image_info);
747 exception=ddata->exception;
748 (void) CopyMagickString(image_info->magick,"DIB",MagickPathExtent);
749 if (bmp_read->width || bmp_read->height)
750 {
751 char
752 size[MagickPathExtent];
753
754 (void) FormatLocaleString(size,MagickPathExtent,"%ux%u",bmp_read->width,
755 bmp_read->height);
756 CloneString(&image_info->size,size);
757 }
758 #if 0
759 printf("ipa_bmp_read: buffer=0x%lx length=%ld, width=%i, height=%i\n",
760 (long) bmp_read->buffer, bmp_read->length,
761 bmp_read->width, bmp_read->height);
762 #endif
763 image=BlobToImage(image_info, (const void *) bmp_read->buffer,
764 (size_t) bmp_read->length, exception);
765 image_info=DestroyImageInfo(image_info);
766 if (image != (Image *) NULL)
767 {
768 #if 0
769 printf("ipa_bmp_read: rows=%ld,columns=%ld\n\n", image->rows, image->columns);
770 #endif
771
772 bmp_read->bmp.data = (void*)image;
773 bmp_read->bmp.width = (U16)image->columns;
774 bmp_read->bmp.height = (U16)image->rows;
775 }
776 }
777
778 static void ipa_bmp_free(wmfAPI *API, wmfBMP *bmp)
779 {
780 (void) API;
781 DestroyImageList((Image*)bmp->data);
782 bmp->data = (void*) 0;
783 bmp->width = (U16) 0;
784 bmp->height = (U16) 0;
785 }
786
787 /*
788 This called by wmf_play() the *first* time the meta file is played
789 */
790 static void ipa_device_open(wmfAPI *API)
791 {
792 wmf_magick_t
793 *ddata = WMF_MAGICK_GetData (API);
794
795 ddata->pattern_id = 0;
796 ddata->clipping = MagickFalse;
797 ddata->clip_mask_id = 0;
798
799 ddata->push_depth = 0;
800
801 ddata->draw_wand = AcquireDrawingWand(ddata->draw_info,ddata->image);
802 }
803
804 /*
805 This called by wmf_api_destroy()
806 */
807 static void ipa_device_close(wmfAPI *API)
808 {
809 wmf_magick_t
810 *ddata = WMF_MAGICK_GetData(API);
811
812 if (ddata->draw_wand != (DrawingWand *) NULL)
813 {
814 DestroyDrawingWand(ddata->draw_wand);
815 ddata->draw_wand=(DrawingWand *) NULL;
816 }
817 if (ddata->draw_info != (DrawInfo *) NULL)
818 {
819 DestroyDrawInfo(ddata->draw_info);
820 ddata->draw_info=(DrawInfo *)NULL;
821 }
822 if (WMF_MAGICK_GetFontData(API)->ps_name)
823 WMF_MAGICK_GetFontData(API)->ps_name=(char *) RelinquishMagickMemory(
824 WMF_MAGICK_GetFontData(API)->ps_name);
825 }
826
827 /*
828 This called from the beginning of each play for initial page setup
829 */
830 static void ipa_device_begin(wmfAPI *API)
831 {
832 char
833 comment[MagickPathExtent];
834
835 wmf_magick_t
836 *ddata = WMF_MAGICK_GetData(API);
837
838 /* Make SVG output happy */
839 (void) PushDrawingWand(WmfDrawingWand);
840
841 DrawSetViewbox(WmfDrawingWand,0,0,ddata->image->columns,ddata->image->rows);
842
843 (void) FormatLocaleString(comment,MagickPathExtent,"Created by %s",
844 GetMagickVersion((size_t *) NULL));
845 DrawComment(WmfDrawingWand,comment);
846
847 /* Scale width and height to image */
848 DrawScale(WmfDrawingWand, ddata->scale_x, ddata->scale_y);
849
850 /* Translate to TL corner of bounding box */
851 DrawTranslate(WmfDrawingWand, ddata->translate_x, ddata->translate_y);
852
853 /* Apply rotation */
854 DrawRotate(WmfDrawingWand, ddata->rotate);
855
856 if (ddata->image_info->texture == NULL)
857 {
858 PixelWand
859 *background_color;
860
861 /* Draw rectangle in background color */
862 background_color=NewPixelWand();
863 PixelSetPixelColor(background_color,&ddata->image->background_color);
864 DrawSetFillColor(WmfDrawingWand,background_color);
865 background_color=DestroyPixelWand(background_color);
866 DrawRectangle(WmfDrawingWand,
867 XC(ddata->bbox.TL.x),YC(ddata->bbox.TL.y),
868 XC(ddata->bbox.BR.x),YC(ddata->bbox.BR.y));
869 }
870 else
871 {
872 /* Draw rectangle with texture image the SVG way */
873 Image
874 *image;
875
876 ImageInfo
877 *image_info;
878
879 ExceptionInfo
880 *exception;
881
882 exception=AcquireExceptionInfo();
883
884 image_info = CloneImageInfo((ImageInfo *) 0);
885 (void) CopyMagickString(image_info->filename,ddata->image_info->texture,
886 MagickPathExtent);
887 if (ddata->image_info->size)
888 CloneString(&image_info->size,ddata->image_info->size);
889
890 image = ReadImage(image_info,exception);
891 (void) DestroyExceptionInfo(exception);
892 image_info=DestroyImageInfo(image_info);
893 if (image)
894 {
895 char
896 pattern_id[MagickPathExtent];
897
898 MagickWand
899 *magick_wand;
900
901 (void) CopyMagickString(image->magick,"MIFF",MagickPathExtent);
902 DrawPushDefs(WmfDrawingWand);
903 draw_pattern_push(API,ddata->pattern_id,image->columns,image->rows);
904 magick_wand=NewMagickWandFromImage(image);
905 (void) DrawComposite(WmfDrawingWand,CopyCompositeOp,0,0,
906 image->columns,image->rows,magick_wand);
907 magick_wand=DestroyMagickWand(magick_wand);
908 (void) DrawPopPattern(WmfDrawingWand);
909 DrawPopDefs(WmfDrawingWand);
910 (void) FormatLocaleString(pattern_id,MagickPathExtent,"#brush_%lu",
911 ddata->pattern_id);
912 (void) DrawSetFillPatternURL(WmfDrawingWand,pattern_id);
913 ++ddata->pattern_id;
914 DrawRectangle(WmfDrawingWand,
915 XC(ddata->bbox.TL.x),YC(ddata->bbox.TL.y),
916 XC(ddata->bbox.BR.x),YC(ddata->bbox.BR.y));
917 image=DestroyImageList(image);
918 }
919 else
920 {
921 LogMagickEvent(CoderEvent,GetMagickModule(),
922 "reading texture image failed!");
923 }
924 }
925
926 DrawSetClipRule(WmfDrawingWand,EvenOddRule); /* Default for WMF is ALTERNATE polygon fill mode */
927 draw_fill_color_string(WmfDrawingWand,"none"); /* Default brush is WHITE_BRUSH */
928 draw_stroke_color_string(WmfDrawingWand,"none"); /* Default pen is BLACK_PEN */
929 DrawSetStrokeLineCap(WmfDrawingWand,ButtCap); /* Default linecap is PS_ENDCAP_FLAT */
930 DrawSetStrokeLineJoin(WmfDrawingWand,MiterJoin); /* Default linejoin is PS_JOIN_MITER */
931 draw_under_color_string(WmfDrawingWand,"white"); /* Default text box is white */
932 }
933
934 /*
935 This called from the end of each play for page termination
936 */
937 static void ipa_device_end(wmfAPI *API)
938 {
939 wmf_magick_t
940 *ddata = WMF_MAGICK_GetData(API);
941
942 /* Reset any existing clip paths by popping wand */
943 if (ddata->clipping)
944 (void) PopDrawingWand(WmfDrawingWand);
945 ddata->clipping = MagickFalse;
946
947 /* Make SVG output happy */
948 (void) PopDrawingWand(WmfDrawingWand);
949 }
950
951 static void ipa_flood_interior(wmfAPI *API, wmfFlood_t *flood)
952 {
953 /* Save graphic wand */
954 (void) PushDrawingWand(WmfDrawingWand);
955
956 draw_fill_color_rgb(API,&(flood->color));
957
958 DrawColor(WmfDrawingWand,XC(flood->pt.x), YC(flood->pt.y),
959 FillToBorderMethod);
960
961 /* Restore graphic wand */
962 (void) PopDrawingWand(WmfDrawingWand);
963 }
964
965 static void ipa_flood_exterior(wmfAPI *API, wmfFlood_t *flood)
966 {
967 /* Save graphic wand */
968 (void) PushDrawingWand(WmfDrawingWand);
969
970 draw_fill_color_rgb(API,&(flood->color));
971
972 if (flood->type == FLOODFILLSURFACE)
973 DrawColor(WmfDrawingWand, XC(flood->pt.x), YC(flood->pt.y),
974 FloodfillMethod);
975 else
976 DrawColor(WmfDrawingWand, XC(flood->pt.x), YC(flood->pt.y),
977 FillToBorderMethod);
978
979 /* Restore graphic wand */
980 (void) PopDrawingWand(WmfDrawingWand);
981 }
982
983 static void ipa_draw_pixel(wmfAPI *API, wmfDrawPixel_t *draw_pixel)
984 {
985 /* Save graphic wand */
986 (void) PushDrawingWand(WmfDrawingWand);
987
988 draw_stroke_color_string(WmfDrawingWand,"none");
989
990 draw_fill_color_rgb(API,&(draw_pixel->color));
991
992 DrawRectangle(WmfDrawingWand,
993 XC(draw_pixel->pt.x),
994 YC(draw_pixel->pt.y),
995 XC((double) draw_pixel->pt.x + draw_pixel->pixel_width),
996 YC((double) draw_pixel->pt.y + draw_pixel->pixel_height));
997
998 /* Restore graphic wand */
999 (void) PopDrawingWand(WmfDrawingWand);
1000 }
1001
1002 static void ipa_draw_pie(wmfAPI *API, wmfDrawArc_t *draw_arc)
1003 {
1004 util_draw_arc(API, draw_arc, magick_arc_pie);
1005 }
1006
1007 static void ipa_draw_chord(wmfAPI *API, wmfDrawArc_t *draw_arc)
1008 {
1009 util_draw_arc(API, draw_arc, magick_arc_chord);
1010 }
1011
1012 static void ipa_draw_arc(wmfAPI *API, wmfDrawArc_t *draw_arc)
1013 {
1014 util_draw_arc(API, draw_arc, magick_arc_open);
1015 }
1016
1017 static void ipa_draw_ellipse(wmfAPI *API, wmfDrawArc_t *draw_arc)
1018 {
1019 util_draw_arc(API, draw_arc, magick_arc_ellipse);
1020 }
1021
1022 static void util_draw_arc(wmfAPI *API,
1023 wmfDrawArc_t *draw_arc, magick_arc_t finish)
1024 {
1025 wmfD_Coord
1026 BR,
1027 O,
1028 TL,
1029 center,
1030 end,
1031 start;
1032
1033 double
1034 phi_e = 360,
1035 phi_s = 0;
1036
1037 double
1038 Rx,
1039 Ry;
1040
1041 /* Save graphic wand */
1042 (void) PushDrawingWand(WmfDrawingWand);
1043
1044 if (TO_FILL(draw_arc) || TO_DRAW(draw_arc))
1045 {
1046 center.x = (draw_arc->TL.x + draw_arc->BR.x) / 2;
1047 center.y = (draw_arc->TL.y + draw_arc->BR.y) / 2;
1048 start = center;
1049 end = center;
1050
1051 if (finish != magick_arc_ellipse)
1052 {
1053 draw_arc->start.x += center.x;
1054 draw_arc->start.y += center.y;
1055
1056 draw_arc->end.x += center.x;
1057 draw_arc->end.y += center.y;
1058 }
1059
1060 TL = draw_arc->TL;
1061 BR = draw_arc->BR;
1062
1063 O = center;
1064
1065 if (finish != magick_arc_ellipse)
1066 {
1067 start = draw_arc->start;
1068 end = draw_arc->end;
1069 }
1070
1071 Rx = (BR.x - TL.x) / 2;
1072 Ry = (BR.y - TL.y) / 2;
1073
1074 if (finish != magick_arc_ellipse)
1075 {
1076 start.x -= O.x;
1077 start.y -= O.y;
1078
1079 end.x -= O.x;
1080 end.y -= O.y;
1081
1082 phi_s = atan2((double) start.y, (double) start.x) *180 / MagickPI;
1083 phi_e = atan2((double) end.y, (double) end.x) *180 / MagickPI;
1084
1085 if (phi_e <= phi_s)
1086 phi_e += 360;
1087 }
1088
1089 util_set_pen(API, draw_arc->dc);
1090 if (finish == magick_arc_open)
1091 draw_fill_color_string(WmfDrawingWand,"none");
1092 else
1093 util_set_brush(API, draw_arc->dc, BrushApplyFill);
1094
1095 if (finish == magick_arc_ellipse)
1096 DrawEllipse(WmfDrawingWand, XC(O.x), YC(O.y), Rx, Ry, 0, 360);
1097 else if (finish == magick_arc_pie)
1098 {
1099 DrawPathStart(WmfDrawingWand);
1100 DrawPathMoveToAbsolute(WmfDrawingWand, XC(O.x+start.x),
1101 YC(O.y+start.y));
1102 DrawPathEllipticArcAbsolute(WmfDrawingWand, Rx, Ry, 0, MagickFalse,
1103 MagickTrue, XC(O.x+end.x), YC(O.y+end.y));
1104 DrawPathLineToAbsolute(WmfDrawingWand, XC(O.x), YC(O.y));
1105 DrawPathClose(WmfDrawingWand);
1106 DrawPathFinish(WmfDrawingWand);
1107 }
1108 else if (finish == magick_arc_chord)
1109 {
1110 DrawArc(WmfDrawingWand, XC(draw_arc->TL.x), YC(draw_arc->TL.y),
1111 XC(draw_arc->BR.x), XC(draw_arc->BR.y), phi_s, phi_e);
1112 DrawLine(WmfDrawingWand, XC(draw_arc->BR.x-start.x),
1113 YC(draw_arc->BR.y-start.y), XC(draw_arc->BR.x-end.x),
1114 YC(draw_arc->BR.y-end.y));
1115 }
1116 else /* if (finish == magick_arc_open) */
1117 DrawArc(WmfDrawingWand, XC(draw_arc->TL.x), YC(draw_arc->TL.y),
1118 XC(draw_arc->BR.x), XC(draw_arc->BR.y), phi_s, phi_e);
1119 }
1120
1121 /* Restore graphic wand */
1122 (void) PopDrawingWand(WmfDrawingWand);
1123 }
1124
1125 static void ipa_draw_line(wmfAPI *API, wmfDrawLine_t *draw_line)
1126 {
1127 /* Save graphic wand */
1128 (void) PushDrawingWand(WmfDrawingWand);
1129
1130 if (TO_DRAW(draw_line))
1131 {
1132 util_set_pen(API, draw_line->dc);
1133 DrawLine(WmfDrawingWand,
1134 XC(draw_line->from.x), YC(draw_line->from.y),
1135 XC(draw_line->to.x), YC(draw_line->to.y));
1136 }
1137
1138 /* Restore graphic wand */
1139 (void) PopDrawingWand(WmfDrawingWand);
1140 }
1141
1142 static void ipa_poly_line(wmfAPI *API, wmfPolyLine_t *polyline)
1143 {
1144 if (polyline->count <= 2)
1145 return;
1146
1147 if (TO_DRAW(polyline))
1148 {
1149 int
1150 point;
1151
1152 /* Save graphic wand */
1153 (void) PushDrawingWand(WmfDrawingWand);
1154
1155 util_set_pen(API, polyline->dc);
1156
1157 DrawPathStart(WmfDrawingWand);
1158 DrawPathMoveToAbsolute(WmfDrawingWand,
1159 XC(polyline->pt[0].x),
1160 YC(polyline->pt[0].y));
1161 for (point = 1; point < polyline->count; point++)
1162 {
1163 DrawPathLineToAbsolute(WmfDrawingWand,
1164 XC(polyline->pt[point].x),
1165 YC(polyline->pt[point].y));
1166 }
1167 DrawPathFinish(WmfDrawingWand);
1168
1169 /* Restore graphic wand */
1170 (void) PopDrawingWand(WmfDrawingWand);
1171 }
1172 }
1173
1174 static void ipa_draw_polygon(wmfAPI *API, wmfPolyLine_t *polyline)
1175 {
1176 if (polyline->count <= 2)
1177 return;
1178
1179 if (TO_FILL(polyline) || TO_DRAW(polyline))
1180 {
1181 int
1182 point;
1183
1184 /* Save graphic wand */
1185 (void) PushDrawingWand(WmfDrawingWand);
1186
1187 util_set_pen(API, polyline->dc);
1188 util_set_brush(API, polyline->dc, BrushApplyFill);
1189
1190 DrawPathStart(WmfDrawingWand);
1191 DrawPathMoveToAbsolute(WmfDrawingWand,
1192 XC(polyline->pt[0].x),
1193 YC(polyline->pt[0].y));
1194 for (point = 1; point < polyline->count; point++)
1195 {
1196 DrawPathLineToAbsolute(WmfDrawingWand,
1197 XC(polyline->pt[point].x),
1198 YC(polyline->pt[point].y));
1199 }
1200 DrawPathClose(WmfDrawingWand);
1201 DrawPathFinish(WmfDrawingWand);
1202
1203 /* Restore graphic wand */
1204 (void) PopDrawingWand(WmfDrawingWand);
1205 }
1206 }
1207
1208 /* Draw a polypolygon. A polypolygon is a list of polygons */
1209 #if defined(MAGICKCORE_WMF_DELEGATE)
1210 static void ipa_draw_polypolygon(wmfAPI *API, wmfPolyPoly_t* polypolygon)
1211 {
1212 if (TO_FILL(polypolygon) || TO_DRAW(polypolygon))
1213 {
1214 int
1215 polygon,
1216 point;
1217
1218 wmfPolyLine_t
1219 polyline;
1220
1221 /* Save graphic wand */
1222 (void) PushDrawingWand(WmfDrawingWand);
1223
1224 util_set_pen(API, polypolygon->dc);
1225 util_set_brush(API, polypolygon->dc, BrushApplyFill);
1226
1227 DrawPathStart(WmfDrawingWand);
1228 if (polypolygon->pt && polypolygon->count)
1229 {
1230 for (polygon = 0; polygon < polypolygon->npoly; polygon++)
1231 {
1232 polyline.dc = polypolygon->dc;
1233 polyline.pt = polypolygon->pt[polygon];
1234 polyline.count = polypolygon->count[polygon];
1235 if ((polyline.count > 2) && polyline.pt)
1236 {
1237 DrawPathMoveToAbsolute(WmfDrawingWand,
1238 XC(polyline.pt[0].x),
1239 YC(polyline.pt[0].y));
1240 for (point = 1; point < polyline.count; point++)
1241 {
1242 DrawPathLineToAbsolute(WmfDrawingWand,
1243 XC(polyline.pt[point].x),
1244 YC(polyline.pt[point].y));
1245 }
1246 DrawPathClose(WmfDrawingWand);
1247 }
1248 }
1249 }
1250 DrawPathFinish(WmfDrawingWand);
1251
1252 /* Restore graphic wand */
1253 (void) PopDrawingWand(WmfDrawingWand);
1254 }
1255 }
1256 #endif
1257
1258 static void ipa_draw_rectangle(wmfAPI *API, wmfDrawRectangle_t *draw_rect)
1259 {
1260 /* Save graphic wand */
1261 (void) PushDrawingWand(WmfDrawingWand);
1262
1263 if (TO_FILL(draw_rect) || TO_DRAW(draw_rect))
1264 {
1265 util_set_pen(API, draw_rect->dc);
1266 util_set_brush(API, draw_rect->dc, BrushApplyFill);
1267
1268 if ((draw_rect->width > 0) || (draw_rect->height > 0))
1269 DrawRoundRectangle(WmfDrawingWand,
1270 XC(draw_rect->TL.x), YC(draw_rect->TL.y),
1271 XC(draw_rect->BR.x), YC(draw_rect->BR.y),
1272 draw_rect->width / 2, draw_rect->height / 2);
1273 else
1274 DrawRectangle(WmfDrawingWand,
1275 XC(draw_rect->TL.x), YC(draw_rect->TL.y),
1276 XC(draw_rect->BR.x), YC(draw_rect->BR.y));
1277 }
1278
1279 /* Restore graphic wand */
1280 (void) PopDrawingWand(WmfDrawingWand);
1281 }
1282
1283 /* Draw an un-filled rectangle using the current brush */
1284 static void ipa_region_frame(wmfAPI *API, wmfPolyRectangle_t *poly_rect)
1285 {
1286 /* Save graphic wand */
1287 (void) PushDrawingWand(WmfDrawingWand);
1288
1289 if (TO_FILL(poly_rect) || TO_DRAW(poly_rect))
1290 {
1291 long
1292 i;
1293
1294 draw_fill_color_string(WmfDrawingWand,"none");
1295 util_set_brush(API, poly_rect->dc, BrushApplyStroke);
1296
1297 for (i = 0; i < (long) poly_rect->count; i++)
1298 {
1299 DrawRectangle(WmfDrawingWand,
1300 XC(poly_rect->TL[i].x), YC(poly_rect->TL[i].y),
1301 XC(poly_rect->BR[i].x), YC(poly_rect->BR[i].y));
1302 }
1303 }
1304
1305 /* Restore graphic wand */
1306 (void) PopDrawingWand(WmfDrawingWand);
1307 }
1308
1309 static void ipa_region_paint(wmfAPI *API, wmfPolyRectangle_t *poly_rect)
1310 {
1311
1312 if (poly_rect->count == 0)
1313 return;
1314
1315 /* Save graphic wand */
1316 (void) PushDrawingWand(WmfDrawingWand);
1317
1318 if (TO_FILL (poly_rect))
1319 {
1320 long
1321 i;
1322
1323 draw_stroke_color_string(WmfDrawingWand,"none");
1324 util_set_brush(API, poly_rect->dc, BrushApplyFill);
1325
1326 for (i = 0; i < (long) poly_rect->count; i++)
1327 {
1328 DrawRectangle(WmfDrawingWand,
1329 XC(poly_rect->TL[i].x), YC(poly_rect->TL[i].y),
1330 XC(poly_rect->BR[i].x), YC(poly_rect->BR[i].y));
1331 }
1332 }
1333
1334 /* Restore graphic wand */
1335 (void) PopDrawingWand(WmfDrawingWand);
1336 }
1337
1338 static void ipa_region_clip(wmfAPI *API, wmfPolyRectangle_t *poly_rect)
1339 {
1340 long
1341 i;
1342
1343 wmf_magick_t
1344 *ddata = WMF_MAGICK_GetData (API);
1345
1346 /* Reset any existing clip paths by popping wand */
1347 if (ddata->clipping)
1348 (void) PopDrawingWand(WmfDrawingWand);
1349 ddata->clipping = MagickFalse;
1350
1351 if (poly_rect->count > 0)
1352 {
1353 char
1354 clip_mask_id[MagickPathExtent];
1355
1356 /* Define clip path */
1357 ddata->clip_mask_id++;
1358 DrawPushDefs(WmfDrawingWand);
1359 (void) FormatLocaleString(clip_mask_id,MagickPathExtent,"clip_%lu",
1360 ddata->clip_mask_id);
1361 DrawPushClipPath(WmfDrawingWand,clip_mask_id);
1362 (void) PushDrawingWand(WmfDrawingWand);
1363 for (i = 0; i < (long) poly_rect->count; i++)
1364 {
1365 DrawRectangle(WmfDrawingWand,
1366 XC(poly_rect->TL[i].x), YC(poly_rect->TL[i].y),
1367 XC(poly_rect->BR[i].x), YC(poly_rect->BR[i].y));
1368 }
1369 (void) PopDrawingWand(WmfDrawingWand);
1370 DrawPopClipPath(WmfDrawingWand);
1371 DrawPopDefs(WmfDrawingWand);
1372
1373 /* Push wand for new clip paths */
1374 (void) PushDrawingWand(WmfDrawingWand);
1375 (void) DrawSetClipPath(WmfDrawingWand,clip_mask_id);
1376 ddata->clipping = MagickTrue;
1377 }
1378 }
1379
1380 static void ipa_functions(wmfAPI *API)
1381 {
1382 wmf_magick_t
1383 *ddata = 0;
1384
1385 wmfFunctionReference
1386 *FR = (wmfFunctionReference *) API->function_reference;
1387
1388 /*
1389 IPA function reference links
1390 */
1391 FR->device_open = ipa_device_open;
1392 FR->device_close = ipa_device_close;
1393 FR->device_begin = ipa_device_begin;
1394 FR->device_end = ipa_device_end;
1395 FR->flood_interior = ipa_flood_interior;
1396 FR->flood_exterior = ipa_flood_exterior;
1397 FR->draw_pixel = ipa_draw_pixel;
1398 FR->draw_pie = ipa_draw_pie;
1399 FR->draw_chord = ipa_draw_chord;
1400 FR->draw_arc = ipa_draw_arc;
1401 FR->draw_ellipse = ipa_draw_ellipse;
1402 FR->draw_line = ipa_draw_line;
1403 FR->poly_line = ipa_poly_line;
1404 FR->draw_polygon = ipa_draw_polygon;
1405 #if defined(MAGICKCORE_WMF_DELEGATE)
1406 FR->draw_polypolygon = ipa_draw_polypolygon;
1407 #endif
1408 FR->draw_rectangle = ipa_draw_rectangle;
1409 FR->rop_draw = ipa_rop_draw;
1410 FR->bmp_draw = ipa_bmp_draw;
1411 FR->bmp_read = ipa_bmp_read;
1412 FR->bmp_free = ipa_bmp_free;
1413 FR->draw_text = ipa_draw_text;
1414 FR->udata_init = ipa_udata_init;
1415 FR->udata_copy = ipa_udata_copy;
1416 FR->udata_set = ipa_udata_set;
1417 FR->udata_free = ipa_udata_free;
1418 FR->region_frame = ipa_region_frame;
1419 FR->region_paint = ipa_region_paint;
1420 FR->region_clip = ipa_region_clip;
1421
1422 /*
1423 Allocate device data structure
1424 */
1425 ddata = (wmf_magick_t *) wmf_malloc(API, sizeof(wmf_magick_t));
1426 if (ERR(API))
1427 return;
1428
1429 (void) memset((void *) ddata, 0, sizeof(wmf_magick_t));
1430 API->device_data = (void *) ddata;
1431
1432 /*
1433 Device data defaults
1434 */
1435 ddata->image = 0;
1436 }
1437
1438 static void ipa_draw_text(wmfAPI *API, wmfDrawText_t *draw_text)
1439 {
1440 double
1441 angle = 0.0, /* text rotation angle */
1442 pointsize = 0.0; /* pointsize to output font with desired height */
1443
1444 ExceptionInfo
1445 *exception;
1446
1447 TypeMetric
1448 metrics;
1449
1450 #if !defined(MAGICKCORE_WMF_DELEGATE)
1451 double
1452 bbox_height, /* bounding box height */
1453 bbox_width; /* bounding box width */
1454
1455 wmfD_Coord
1456 BL, /* bottom left of bounding box */
1457 BR, /* bottom right of bounding box */
1458 TL, /* top left of bounding box */
1459 TR; /* top right of bounding box */
1460 #endif
1461
1462 wmfD_Coord
1463 point; /* text placement point */
1464
1465 wmfFont
1466 *font;
1467
1468 wmf_magick_t
1469 *ddata = WMF_MAGICK_GetData(API);
1470
1471 point = draw_text->pt;
1472
1473 /* Choose bounding box and calculate its width and height */
1474 #if !defined(MAGICKCORE_WMF_DELEGATE)
1475 {
1476 double
1477 dx,
1478 dy;
1479
1480 if (draw_text->flags)
1481 {
1482 TL = draw_text->TL;
1483 BR = draw_text->BR;
1484 TR.x = draw_text->BR.x;
1485 TR.y = draw_text->TL.y;
1486 BL.x = draw_text->TL.x;
1487 BL.y = draw_text->BR.y;
1488 }
1489 else
1490 {
1491 TL = draw_text->bbox.TL;
1492 BR = draw_text->bbox.BR;
1493 TR = draw_text->bbox.TR;
1494 BL = draw_text->bbox.BL;
1495 }
1496 dx = ((TR.x - TL.x) + (BR.x - BL.x)) / 2;
1497 dy = ((TR.y - TL.y) + (BR.y - BL.y)) / 2;
1498 bbox_width = hypot(dx,dy);
1499 dx = ((BL.x - TL.x) + (BR.x - TR.x)) / 2;
1500 dy = ((BL.y - TL.y) + (BR.y - TR.y)) / 2;
1501 bbox_height = hypot(dx,dy);
1502 }
1503 #endif
1504
1505 font = WMF_DC_FONT(draw_text->dc);
1506
1507 if (!draw_text->str)
1508 return;
1509
1510 /* Convert font_height to equivalent pointsize */
1511 exception=ddata->exception;
1512 pointsize = util_pointsize(API, font, draw_text->str, draw_text->font_height, exception);
1513
1514 if (pointsize <= 0.0)
1515 return;
1516
1517 /* Save graphic wand */
1518 (void) PushDrawingWand(WmfDrawingWand);
1519
1520 #if 0
1521 printf("\nipa_draw_text\n");
1522 printf("Text = \"%s\"\n", draw_text->str);
1523 /* printf("WMF_FONT_NAME: = \"%s\"\n", WMF_FONT_NAME(font)); */
1524 printf("WMF_FONT_PSNAME: = \"%s\"\n", WMF_FONT_PSNAME(font));
1525 printf("Bounding box TL=%g,%g BR=%g,%g\n",
1526 TL.x, TL.y, BR.x, BR.y);
1527 /* printf("Text box = %gx%g\n", bbox_width, bbox_height); */
1528 /* printf("WMF_FONT_HEIGHT = %i\n", (int)WMF_FONT_HEIGHT(font)); */
1529 printf("Pointsize = %g\n", pointsize);
1530 fflush(stdout);
1531 #endif
1532
1533 /*
1534 *Obtain font metrics if required
1535 *
1536 */
1537 if ((WMF_DC_TEXTALIGN(draw_text->dc) & TA_CENTER) ||
1538 (WMF_TEXT_UNDERLINE(font)) || (WMF_TEXT_STRIKEOUT(font)))
1539 {
1540 Image
1541 *image = ddata->image;
1542
1543 DrawInfo
1544 *draw_info;
1545
1546 draw_info=ddata->draw_info;
1547 draw_info->font=WMF_FONT_PSNAME(font);
1548 draw_info->pointsize = pointsize;
1549 draw_info->text=draw_text->str;
1550
1551 if (GetTypeMetrics(image, draw_info, &metrics, exception) != MagickFalse)
1552 {
1553 /* Center the text if it is not yet centered and should be */
1554 if ((WMF_DC_TEXTALIGN(draw_text->dc) & TA_CENTER))
1555 {
1556 double
1557 text_width = metrics.width *(ddata->scale_y / ddata->scale_x);
1558
1559 #if defined(MAGICKCORE_WMF_DELEGATE)
1560 point.x -= (float) (text_width / 2.0);
1561 #else
1562 point.x += (float) (bbox_width / 2.0 - text_width / 2.0);
1563 #endif
1564 }
1565 }
1566 draw_info->font=NULL;
1567 draw_info->text=NULL;
1568 }
1569
1570 /* Set text background color */
1571 if (draw_text->flags & ETO_OPAQUE)
1572 {
1573 /* Draw bounding-box background color (META_EXTTEXTOUT mode) */
1574 draw_stroke_color_string(WmfDrawingWand,"none");
1575 draw_fill_color_rgb(API,WMF_DC_BACKGROUND(draw_text->dc));
1576 DrawRectangle(WmfDrawingWand,
1577 XC(draw_text->TL.x),YC(draw_text->TL.y),
1578 XC(draw_text->BR.x),YC(draw_text->BR.y));
1579 draw_fill_color_string(WmfDrawingWand,"none");
1580 }
1581 else
1582 {
1583 /* Set text undercolor */
1584 if (WMF_DC_OPAQUE(draw_text->dc))
1585 {
1586 wmfRGB
1587 *box = WMF_DC_BACKGROUND(draw_text->dc);
1588
1589 PixelWand
1590 *under_color;
1591
1592 under_color=NewPixelWand();
1593 PixelSetRedQuantum(under_color,ScaleCharToQuantum(box->r));
1594 PixelSetGreenQuantum(under_color,ScaleCharToQuantum(box->g));
1595 PixelSetBlueQuantum(under_color,ScaleCharToQuantum(box->b));
1596 PixelSetAlphaQuantum(under_color,OpaqueAlpha);
1597 DrawSetTextUnderColor(WmfDrawingWand,under_color);
1598 under_color=DestroyPixelWand(under_color);
1599 }
1600 else
1601 draw_under_color_string(WmfDrawingWand,"none");
1602 }
1603
1604 /* Set text clipping (META_EXTTEXTOUT mode) */
1605 if (draw_text->flags & ETO_CLIPPED)
1606 {
1607 }
1608
1609 /* Set stroke color */
1610 draw_stroke_color_string(WmfDrawingWand,"none");
1611
1612 /* Set fill color */
1613 draw_fill_color_rgb(API,WMF_DC_TEXTCOLOR(draw_text->dc));
1614
1615 /* Output font size */
1616 (void) DrawSetFontSize(WmfDrawingWand,pointsize);
1617
1618 /* Output Postscript font name */
1619 (void) DrawSetFont(WmfDrawingWand, WMF_FONT_PSNAME(font));
1620
1621 /* Translate coordinates so target is 0,0 */
1622 DrawTranslate(WmfDrawingWand, XC(point.x), YC(point.y));
1623
1624 /* Transform horizontal scale to draw text at 1:1 ratio */
1625 DrawScale(WmfDrawingWand, ddata->scale_y / ddata->scale_x, 1.0);
1626
1627 /* Apply rotation */
1628 /* ImageMagick's drawing rotation is clockwise from horizontal
1629 while WMF drawing rotation is counterclockwise from horizontal */
1630 angle=fabs(RadiansToDegrees(2.0*MagickPI-WMF_TEXT_ANGLE(font)));
1631 if (angle == 360)
1632 angle = 0;
1633 if (angle != 0)
1634 DrawRotate(WmfDrawingWand, angle);
1635
1636 /*
1637 *Render text
1638 *
1639 */
1640
1641 /* Output string */
1642 DrawAnnotation(WmfDrawingWand,0,0,(unsigned char *) draw_text->str);
1643
1644 /* Underline text the Windows way (at the bottom) */
1645 if (WMF_TEXT_UNDERLINE(font))
1646 {
1647 double
1648 line_height;
1649
1650 wmfD_Coord
1651 ulBR, /* bottom right of underline rectangle */
1652 ulTL; /* top left of underline rectangle */
1653
1654 line_height = ((double)1/(ddata->scale_x))*metrics.underline_thickness;
1655 if (metrics.underline_thickness < 1.5)
1656 line_height *= 0.55;
1657 ulTL.x = 0;
1658 ulTL.y = (float) fabs(metrics.descent) - line_height;
1659 ulBR.x = (float) metrics.width;
1660 ulBR.y = (float) fabs(metrics.descent);
1661
1662 DrawRectangle(WmfDrawingWand,
1663 XC(ulTL.x), YC(ulTL.y), XC(ulBR.x), YC(ulBR.y));
1664 }
1665
1666 /* Strikeout text the Windows way */
1667 if (WMF_TEXT_STRIKEOUT(font))
1668 {
1669 double line_height;
1670
1671 wmfD_Coord
1672 ulBR, /* bottom right of strikeout rectangle */
1673 ulTL; /* top left of strikeout rectangle */
1674
1675 line_height = ((double)1/(ddata->scale_x))*metrics.underline_thickness;
1676
1677 if (metrics.underline_thickness < 2.0)
1678 line_height *= 0.55;
1679 ulTL.x = 0;
1680 ulTL.y = -(((double) metrics.ascent) / 2 + line_height / 2);
1681 ulBR.x = metrics.width;
1682 ulBR.y = -(((double) metrics.ascent) / 2 - line_height / 2);
1683
1684 DrawRectangle(WmfDrawingWand,
1685 XC(ulTL.x), YC(ulTL.y), XC(ulBR.x), YC(ulBR.y));
1686
1687 }
1688
1689 /* Restore graphic wand */
1690 (void) PopDrawingWand(WmfDrawingWand);
1691
1692 #if 0
1693 (void) PushDrawingWand(WmfDrawingWand);
1694 draw_stroke_color_string(WmfDrawingWand,"red");
1695 draw_fill_color_string(WmfDrawingWand,"none");
1696 DrawRectangle(WmfDrawingWand,
1697 XC(TL.x), YC(TL.y),
1698 XC(BR.x), YC(BR.y));
1699 draw_stroke_color_string(WmfDrawingWand,"none");
1700 (void) PopDrawingWand(WmfDrawingWand);
1701 #endif
1702
1703 }
1704
1705 static void ipa_udata_init(wmfAPI *API, wmfUserData_t *userdata)
1706 {
1707 (void) API;
1708 (void) userdata;
1709 /* wmf_magick_t* ddata = WMF_MAGICK_GetData (API); */
1710
1711 }
1712
1713 static void ipa_udata_copy(wmfAPI *API, wmfUserData_t *userdata)
1714 {
1715 (void) API;
1716 (void) userdata;
1717 /* wmf_magick_t* ddata = WMF_MAGICK_GetData (API); */
1718
1719 }
1720
1721 static void ipa_udata_set(wmfAPI *API, wmfUserData_t *userdata)
1722 {
1723 (void) API;
1724 (void) userdata;
1725 /* wmf_magick_t* ddata = WMF_MAGICK_GetData (API); */
1726
1727 }
1728
1729 static void ipa_udata_free(wmfAPI *API, wmfUserData_t *userdata)
1730 {
1731 (void) API;
1732 (void) userdata;
1733 /* wmf_magick_t* ddata = WMF_MAGICK_GetData (API); */
1734
1735 }
1736
1737 static void util_set_brush(wmfAPI *API, wmfDC *dc,const BrushApply brush_apply)
1738 {
1739 wmf_magick_t
1740 *ddata = WMF_MAGICK_GetData(API);
1741
1742 wmfBrush
1743 *brush = WMF_DC_BRUSH(dc);
1744
1745 /* Set polygon fill rule */
1746 switch (WMF_DC_POLYFILL(dc)) /* Is this correct ?? */
1747 {
1748 case WINDING:
1749 DrawSetClipRule(WmfDrawingWand,NonZeroRule);
1750 break;
1751
1752 case ALTERNATE:
1753 default:
1754 DrawSetClipRule(WmfDrawingWand,EvenOddRule);
1755 break;
1756 }
1757
1758 switch (WMF_BRUSH_STYLE(brush))
1759 {
1760 case BS_SOLID /* 0 */:
1761 /* WMF_BRUSH_COLOR specifies brush color, WMF_BRUSH_HATCH
1762 ignored */
1763 {
1764 if (brush_apply == BrushApplyStroke)
1765 draw_stroke_color_rgb(API,WMF_BRUSH_COLOR(brush));
1766 else
1767 draw_fill_color_rgb(API,WMF_BRUSH_COLOR(brush));
1768 break;
1769 }
1770 case BS_HOLLOW /* 1 */: /* BS_HOLLOW & BS_NULL share enum */
1771 /* WMF_BRUSH_COLOR and WMF_BRUSH_HATCH ignored */
1772 {
1773 if (brush_apply == BrushApplyStroke)
1774 draw_stroke_color_string(WmfDrawingWand,"none");
1775 else
1776 draw_fill_color_string(WmfDrawingWand,"none");
1777 break;
1778 }
1779 case BS_HATCHED /* 2 */:
1780 /* WMF_BRUSH_COLOR specifies the hatch color, WMF_BRUSH_HATCH
1781 specifies the hatch brush style. If WMF_DC_OPAQUE, then
1782 WMF_DC_BACKGROUND specifies hatch background color. */
1783 {
1784 DrawPushDefs(WmfDrawingWand);
1785 draw_pattern_push(API, ddata->pattern_id, 8, 8);
1786 (void) PushDrawingWand(WmfDrawingWand);
1787
1788 if (WMF_DC_OPAQUE(dc))
1789 {
1790 if (brush_apply == BrushApplyStroke)
1791 draw_stroke_color_rgb(API,WMF_DC_BACKGROUND(dc));
1792 else
1793 draw_fill_color_rgb(API,WMF_DC_BACKGROUND(dc));
1794
1795 DrawRectangle(WmfDrawingWand, 0, 0, 7, 7);
1796 }
1797
1798 DrawSetStrokeAntialias(WmfDrawingWand, MagickFalse);
1799 DrawSetStrokeWidth(WmfDrawingWand, 1);
1800
1801 draw_stroke_color_rgb(API,WMF_BRUSH_COLOR(brush));
1802
1803 switch ((unsigned int) WMF_BRUSH_HATCH(brush))
1804 {
1805
1806 case HS_HORIZONTAL: /* ----- */
1807 {
1808 DrawLine(WmfDrawingWand, 0, 3, 7,3);
1809 break;
1810 }
1811 case HS_VERTICAL: /* ||||| */
1812 {
1813 DrawLine(WmfDrawingWand, 3, 0, 3, 7);
1814 break;
1815 }
1816 case HS_FDIAGONAL: /* \\\\\ */
1817 {
1818 DrawLine(WmfDrawingWand, 0, 0, 7, 7);
1819 break;
1820 }
1821 case HS_BDIAGONAL: /* / */
1822 {
1823 DrawLine(WmfDrawingWand, 0, 7, 7, 0);
1824 break;
1825 }
1826 case HS_CROSS: /* +++++ */
1827 {
1828 DrawLine(WmfDrawingWand, 0, 3, 7, 3);
1829 DrawLine(WmfDrawingWand, 3, 0, 3, 7);
1830 break;
1831 }
1832 case HS_DIAGCROSS: /* xxxxx */
1833 {
1834 DrawLine(WmfDrawingWand, 0, 0, 7, 7);
1835 DrawLine(WmfDrawingWand, 0, 7, 7, 0);
1836 break;
1837 }
1838 default:
1839 {
1840 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: unexpected brush hatch enumeration %u\n",
1841 (unsigned int)WMF_BRUSH_HATCH(brush));
1842 }
1843 }
1844 (void) PopDrawingWand(WmfDrawingWand);
1845 (void) DrawPopPattern(WmfDrawingWand);
1846 DrawPopDefs(WmfDrawingWand);
1847 {
1848 char
1849 pattern_id[MagickPathExtent];
1850
1851 (void) FormatLocaleString(pattern_id,MagickPathExtent,"#brush_%lu",
1852 ddata->pattern_id);
1853 if (brush_apply == BrushApplyStroke)
1854 (void) DrawSetStrokePatternURL(WmfDrawingWand,pattern_id);
1855 else
1856 (void) DrawSetFillPatternURL(WmfDrawingWand,pattern_id);
1857 ++ddata->pattern_id;
1858 }
1859 break;
1860 }
1861 case BS_PATTERN /* 3 */:
1862 /* WMF_BRUSH_COLOR ignored, WMF_BRUSH_HATCH provides handle to
1863 bitmap */
1864 {
1865 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: BS_PATTERN not supported\n");
1866 break;
1867 }
1868 case BS_INDEXED /* 4 */:
1869 {
1870 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: BS_INDEXED not supported\n");
1871 break;
1872 }
1873 case BS_DIBPATTERN /* 5 */:
1874 {
1875 wmfBMP
1876 *brush_bmp = WMF_BRUSH_BITMAP(brush);
1877
1878 if (brush_bmp && brush_bmp->data != 0)
1879 {
1880 CompositeOperator
1881 mode;
1882
1883 const Image
1884 *image;
1885
1886 MagickWand
1887 *magick_wand;
1888
1889 image = (Image*)brush_bmp->data;
1890
1891 mode = CopyCompositeOp; /* Default is copy */
1892 switch (WMF_DC_ROP(dc))
1893 {
1894 /* Binary raster ops */
1895 case R2_BLACK:
1896 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_BLACK ROP2 mode not supported!\n");
1897 break;
1898 case R2_NOTMERGEPEN:
1899 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_NOTMERGEPEN ROP2 mode not supported!\n");
1900 break;
1901 case R2_MASKNOTPEN:
1902 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush R2_MASKNOTPEN ROP2 mode not supported!\n");
1903 break;
1904 case R2_NOTCOPYPEN:
1905 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_NOTCOPYPEN ROP2 mode not supported!\n");
1906 break;
1907 case R2_MASKPENNOT:
1908 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_MASKPENNOT ROP2 mode not supported!\n");
1909 break;
1910 case R2_NOT:
1911 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_NOT ROP2 mode not supported!\n");
1912 break;
1913 case R2_XORPEN:
1914 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_XORPEN ROP2 mode not supported!\n");
1915 break;
1916 case R2_NOTMASKPEN:
1917 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_NOTMASKPEN ROP2 mode not supported!\n");
1918 break;
1919 case R2_MASKPEN:
1920 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_MASKPEN ROP2 mode not supported!\n");
1921 break;
1922 case R2_NOTXORPEN:
1923 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_NOTXORPEN ROP2 mode not supported!\n");
1924 break;
1925 case R2_NOP:
1926 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_NOP ROP2 mode not supported!\n");
1927 break;
1928 case R2_MERGENOTPEN:
1929 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_MERGENOTPEN ROP2 mode not supported!\n");
1930 break;
1931 case R2_COPYPEN:
1932 mode = CopyCompositeOp;
1933 break;
1934 case R2_MERGEPENNOT:
1935 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_MERGEPENNOT ROP2 mode not supported!\n");
1936 break;
1937 case R2_MERGEPEN:
1938 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_MERGEPEN ROP2 mode not supported!\n");
1939 break;
1940 case R2_WHITE:
1941 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: R2_WHITE ROP2 mode not supported!\n");
1942 break;
1943 default:
1944 {
1945 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: unexpected ROP2 enumeration %u!\n",
1946 (unsigned int)WMF_DC_ROP(dc));
1947 }
1948 }
1949
1950 DrawPushDefs(WmfDrawingWand);
1951 draw_pattern_push(API, ddata->pattern_id, brush_bmp->width,
1952 brush_bmp->height);
1953 magick_wand=NewMagickWandFromImage(image);
1954 (void) DrawComposite(WmfDrawingWand,mode, 0, 0, brush_bmp->width,
1955 brush_bmp->height, magick_wand);
1956 magick_wand=DestroyMagickWand(magick_wand);
1957 (void) DrawPopPattern(WmfDrawingWand);
1958 DrawPopDefs(WmfDrawingWand);
1959
1960 {
1961 char
1962 pattern_id[MagickPathExtent];
1963
1964 (void) FormatLocaleString(pattern_id,MagickPathExtent,"#brush_%lu",
1965 ddata->pattern_id);
1966 if (brush_apply == BrushApplyStroke)
1967 (void) DrawSetStrokePatternURL(WmfDrawingWand,pattern_id);
1968 else
1969 (void) DrawSetFillPatternURL(WmfDrawingWand,pattern_id);
1970 ++ddata->pattern_id;
1971 }
1972 }
1973 else
1974 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: no BMP image data!\n");
1975
1976 break;
1977 }
1978 case BS_DIBPATTERNPT /* 6 */:
1979 /* WMF_BRUSH_COLOR ignored, WMF_BRUSH_HATCH provides pointer to
1980 DIB */
1981 {
1982 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: BS_DIBPATTERNPT not supported\n");
1983 break;
1984 }
1985 case BS_PATTERN8X8 /* 7 */:
1986 {
1987 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: BS_PATTERN8X8 not supported\n");
1988 break;
1989 }
1990 case BS_DIBPATTERN8X8 /* 8 */:
1991 {
1992 LogMagickEvent(CoderEvent,GetMagickModule(),"util_set_brush: BS_DIBPATTERN8X8 not supported\n");
1993 break;
1994 }
1995 default:
1996 {
1997 }
1998 }
1999 }
2000
2001 static void util_set_pen(wmfAPI *API, wmfDC *dc)
2002 {
2003 wmf_magick_t
2004 *ddata = WMF_MAGICK_GetData(API);
2005
2006 wmfPen
2007 *pen = 0;
2008
2009 double
2010 pen_width,
2011 pixel_width;
2012
2013 unsigned int
2014 pen_style;
2015
2016 pen = WMF_DC_PEN(dc);
2017
2018 pen_width = (WMF_PEN_WIDTH(pen) + WMF_PEN_HEIGHT(pen)) / 2;
2019
2020 /* Pixel width is inverse of pixel scale */
2021 pixel_width = (((double) 1 / (ddata->scale_x)) +
2022 ((double) 1 / (ddata->scale_y))) / 2;
2023
2024 /* Don't allow pen_width to be much less than pixel_width in order
2025 to avoid disappearing or spider-web lines */
2026 pen_width = MagickMax(pen_width, pixel_width*0.8);
2027
2028 pen_style = (unsigned int) WMF_PEN_STYLE(pen);
2029
2030 /* Pen style specified? */
2031 if (pen_style == PS_NULL)
2032 {
2033 draw_stroke_color_string(WmfDrawingWand,"none");
2034 return;
2035 }
2036
2037 DrawSetStrokeAntialias(WmfDrawingWand, MagickTrue);
2038 DrawSetStrokeWidth(WmfDrawingWand,MagickMax(0.0, pen_width));
2039
2040 {
2041 LineCap
2042 linecap;
2043
2044 switch ((unsigned int) WMF_PEN_ENDCAP(pen))
2045 {
2046 case PS_ENDCAP_SQUARE:
2047 linecap = SquareCap;
2048 break;
2049 case PS_ENDCAP_ROUND:
2050 linecap = RoundCap;
2051 break;
2052 case PS_ENDCAP_FLAT:
2053 default:
2054 linecap = ButtCap;
2055 break;
2056 }
2057 DrawSetStrokeLineCap(WmfDrawingWand, linecap);
2058 }
2059
2060 {
2061 LineJoin
2062 linejoin;
2063
2064 switch ((unsigned int) WMF_PEN_JOIN(pen))
2065 {
2066 case PS_JOIN_BEVEL:
2067 linejoin = BevelJoin;
2068 break;
2069 case PS_JOIN_ROUND:
2070 linejoin = RoundJoin;
2071 break;
2072 case PS_JOIN_MITER:
2073 default:
2074 linejoin = MiterJoin;
2075 break;
2076 }
2077 DrawSetStrokeLineJoin(WmfDrawingWand,linejoin);
2078 }
2079
2080 {
2081 double
2082 dasharray[7];
2083
2084 switch (pen_style)
2085 {
2086 case PS_DASH: /* ------- */
2087 {
2088 /* Pattern 18,7 */
2089 dasharray[0] = pixel_width *18;
2090 dasharray[1] = pixel_width *7;
2091 dasharray[2] = 0;
2092
2093 DrawSetStrokeAntialias(WmfDrawingWand,MagickFalse);
2094 (void) DrawSetStrokeDashArray(WmfDrawingWand,2,dasharray);
2095 break;
2096 }
2097 case PS_ALTERNATE:
2098 case PS_DOT: /* ....... */
2099 {
2100 /* Pattern 3,3 */
2101 dasharray[0] = pixel_width *3;
2102 dasharray[1] = pixel_width *3;
2103 dasharray[2] = 0;
2104
2105 DrawSetStrokeAntialias(WmfDrawingWand,MagickFalse);
2106 (void) DrawSetStrokeDashArray(WmfDrawingWand,2,dasharray);
2107 break;
2108 }
2109 case PS_DASHDOT: /* _._._._ */
2110 {
2111 /* Pattern 9,6,3,6 */
2112 dasharray[0] = pixel_width *9;
2113 dasharray[1] = pixel_width *6;
2114 dasharray[2] = pixel_width *3;
2115 dasharray[3] = pixel_width *6;
2116 dasharray[4] = 0;
2117
2118 DrawSetStrokeAntialias(WmfDrawingWand,MagickFalse);
2119 (void) DrawSetStrokeDashArray(WmfDrawingWand,4,dasharray);
2120 break;
2121 }
2122 case PS_DASHDOTDOT: /* _.._.._ */
2123 {
2124 /* Pattern 9,3,3,3,3,3 */
2125 dasharray[0] = pixel_width *9;
2126 dasharray[1] = pixel_width *3;
2127 dasharray[2] = pixel_width *3;
2128 dasharray[3] = pixel_width *3;
2129 dasharray[4] = pixel_width *3;
2130 dasharray[5] = pixel_width *3;
2131 dasharray[6] = 0;
2132
2133 DrawSetStrokeAntialias(WmfDrawingWand,MagickFalse);
2134 (void) DrawSetStrokeDashArray(WmfDrawingWand,6,dasharray);
2135 break;
2136 }
2137 case PS_INSIDEFRAME: /* There is nothing to do in this case... */
2138 case PS_SOLID:
2139 default:
2140 {
2141 (void) DrawSetStrokeDashArray(WmfDrawingWand,0,(double *) NULL);
2142 break;
2143 }
2144 }
2145 }
2146
2147 draw_stroke_color_rgb(API,WMF_PEN_COLOR(pen));
2148 }
2149
2150 /* Estimate font pointsize based on Windows font parameters */
2151 static double util_pointsize(wmfAPI* API, wmfFont* font, char* str, double font_height, ExceptionInfo *exception)
2152 {
2153 wmf_magick_t
2154 *ddata = WMF_MAGICK_GetData(API);
2155
2156 Image
2157 *image = ddata->image;
2158
2159 TypeMetric
2160 metrics;
2161
2162 DrawInfo
2163 *draw_info;
2164
2165 double
2166 pointsize = 0;
2167
2168 draw_info=ddata->draw_info;
2169 if (draw_info == (const DrawInfo *) NULL)
2170 return 0;
2171
2172 draw_info->font=WMF_FONT_PSNAME(font);
2173 draw_info->pointsize=font_height;
2174 draw_info->text=str;
2175
2176 if (GetTypeMetrics(image, draw_info, &metrics, exception) != MagickFalse)
2177 {
2178
2179 if (strlen(str) == 1)
2180 {
2181 pointsize = (font_height *
2182 (font_height *MagickSafeReciprocal(metrics.ascent + fabs(metrics.descent))));
2183 draw_info->pointsize = pointsize;
2184 if (GetTypeMetrics(image, draw_info, &metrics, exception) != MagickFalse)
2185 pointsize *= (font_height *MagickSafeReciprocal(metrics.ascent + fabs(metrics.descent)));
2186 }
2187 else
2188 {
2189 pointsize = (font_height *(font_height *MagickSafeReciprocal(metrics.height)));
2190 draw_info->pointsize = pointsize;
2191 if (GetTypeMetrics(image, draw_info, &metrics, exception) != MagickFalse)
2192 pointsize *= (font_height *MagickSafeReciprocal((double) metrics.height));
2193
2194 }
2195 #if 0
2196 draw_info.pointsize = pointsize;
2197 if (GetTypeMetrics(image, &draw_info, &metrics, exception) != MagickFalse)
2198 pointsize *= (font_height / (metrics.ascent + fabs(metrics.descent)));
2199 pointsize *= 1.114286; /* Magic number computed through trial and error */
2200 #endif
2201 }
2202
2203 draw_info->font=NULL;
2204 draw_info->text=NULL;
2205 #if 0
2206 printf("String = %s\n", str);
2207 printf("Font = %s\n", WMF_FONT_PSNAME(font));
2208 printf("lfHeight = %g\n", font_height);
2209 printf("bounds = %g,%g %g,%g\n", metrics.bounds.x1, metrics.bounds.y1,
2210 metrics.bounds.x2,metrics.bounds.y2);
2211 printf("ascent = %g\n", metrics.ascent);
2212 printf("descent = %g\n", metrics.descent);
2213 printf("height = %g\n", metrics.height);
2214 printf("Pointsize = %g\n", pointsize);
2215 #endif
2216
2217 return floor(pointsize);
2218 }
2219
2220 #if defined(MAGICKCORE_WMF_DELEGATE)
2221 /*
2222 *Returns width of string in points, assuming (unstretched) font size of 1pt
2223 *(similar to wmf_ipa_font_stringwidth)
2224 *
2225 *This extremely odd at best, particularly since player/meta.h has access
2226 *to the corrected font_height (as drawtext.font_height) when it invokes the
2227 *stringwidth callback. It should be possible to compute the real stringwidth!
2228 */
2229 static float lite_font_stringwidth(wmfAPI* API, wmfFont* font, char* str)
2230 {
2231 #if 0
2232 wmf_magick_t
2233 *ddata = WMF_MAGICK_GetData(API);
2234
2235 Image
2236 *image = ddata->image;
2237
2238 DrawInfo
2239 *draw_info;
2240
2241 ExceptionInfo
2242 *exception;
2243
2244 TypeMetric
2245 metrics;
2246
2247 float
2248 stringwidth = 0;
2249
2250 double
2251 orig_x_resolution,
2252 orig_y_resolution;
2253
2254 ResolutionType
2255 orig_resolution_units;
2256
2257 orig_x_resolution = image->resolution.x;
2258 orig_y_resolution = image->resolution.y;
2259 orig_resolution_units = image->units;
2260
2261 draw_info=ddata->draw_info;
2262 if (draw_info == (const DrawInfo *) NULL)
2263 return 0;
2264
2265 draw_info->font=WMF_FONT_PSNAME(font);
2266 draw_info->pointsize=12;
2267 draw_info->text=str;
2268
2269 image->resolution.x = 72;
2270 image->resolution.y = 72;
2271 image->units = PixelsPerInchResolution;
2272
2273 exception=ddata->exception;
2274 if (GetTypeMetrics(image, draw_info, &metrics, exception) != MagickFalse)
2275 stringwidth = ((metrics.width *72)/(image->resolution.x *draw_info->pointsize)); /* *0.916348; */
2276
2277 draw_info->font=NULL;
2278 draw_info->text=NULL;
2279
2280 #if 0
2281 printf("\nlite_font_stringwidth\n");
2282 printf("string = \"%s\"\n", str);
2283 printf("WMF_FONT_NAME = \"%s\"\n", WMF_FONT_NAME(font));
2284 printf("WMF_FONT_PSNAME = \"%s\"\n", WMF_FONT_PSNAME(font));
2285 printf("stringwidth = %g\n", stringwidth);
2286 /* printf("WMF_FONT_HEIGHT = %i\n", (int)WMF_FONT_HEIGHT(font)); */
2287 /* printf("WMF_FONT_WIDTH = %i\n", (int)WMF_FONT_WIDTH(font)); */
2288 fflush(stdout);
2289 #endif
2290
2291 image->resolution.x = orig_x_resolution;
2292 image->resolution.y = orig_y_resolution;
2293 image->units = orig_resolution_units;
2294
2295 return stringwidth;
2296 #else
2297 (void) API;
2298 (void) font;
2299 (void) str;
2300
2301 return 0;
2302 #endif
2303 }
2304
2305 /* Map font (similar to wmf_ipa_font_map) */
2306
2307 /* Mappings to Postscript fonts: family, normal, italic, bold, bolditalic */
2308 static wmfFontMap WMFFontMap[] = {
2309 { (char *) "Courier", (char *) "Courier",
2310 (char *) "Courier-Oblique", (char *) "Courier-Bold",
2311 (char *) "Courier-BoldOblique" },
2312 { (char *) "Helvetica", (char *) "Helvetica",
2313 (char *) "Helvetica-Oblique", (char *) "Helvetica-Bold",
2314 (char *) "Helvetica-BoldOblique" },
2315 { (char *) "Modern", (char *) "Courier",
2316 (char *) "Courier-Oblique", (char *) "Courier-Bold",
2317 (char *) "Courier-BoldOblique" },
2318 { (char *) "Monotype Corsiva", (char *) "Courier",
2319 (char *) "Courier-Oblique", (char *) "Courier-Bold",
2320 (char *) "Courier-BoldOblique" },
2321 { (char *) "News Gothic", (char *) "Helvetica",
2322 (char *) "Helvetica-Oblique", (char *) "Helvetica-Bold",
2323 (char *) "Helvetica-BoldOblique" },
2324 { (char *) "Symbol", (char *) "Symbol",
2325 (char *) "Symbol", (char *) "Symbol",
2326 (char *) "Symbol" },
2327 { (char *) "System", (char *) "Courier",
2328 (char *) "Courier-Oblique", (char *) "Courier-Bold",
2329 (char *) "Courier-BoldOblique" },
2330 { (char *) "Times", (char *) "Times-Roman",
2331 (char *) "Times-Italic", (char *) "Times-Bold",
2332 (char *) "Times-BoldItalic" },
2333 { (char *) NULL, (char *) NULL,
2334 (char *) NULL, (char *) NULL,
2335 (char *) NULL }
2336 };
2337
2338
2339 /* Mapping between base name and Ghostscript family name */
2340 static wmfMapping SubFontMap[] =
2341 {
2342 { (char *) "Arial", (char *) "Helvetica", FT_ENCODING_NONE },
2343 { (char *) "Courier", (char *) "Courier", FT_ENCODING_NONE },
2344 { (char *) "Fixed", (char *) "Courier", FT_ENCODING_NONE },
2345 { (char *) "Helvetica", (char *) "Helvetica", FT_ENCODING_NONE },
2346 { (char *) "Sans", (char *) "Helvetica", FT_ENCODING_NONE },
2347 { (char *) "Sym", (char *) "Symbol", FT_ENCODING_NONE },
2348 { (char *) "Terminal", (char *) "Courier", FT_ENCODING_NONE },
2349 { (char *) "Times", (char *) "Times", FT_ENCODING_NONE },
2350 { (char *) "Wingdings", (char *) "Symbol", FT_ENCODING_NONE },
2351 { (char *) NULL, (char *) NULL, FT_ENCODING_NONE }
2352 };
2353
2354 static void lite_font_map(wmfAPI* API, wmfFont* font)
2355 {
2356 wmfFontData
2357 *font_data;
2358
2359 wmf_magick_font_t
2360 *magick_font;
2361
2362 wmf_magick_t
2363 *ddata = WMF_MAGICK_GetData(API);
2364
2365 ExceptionInfo
2366 *exception;
2367
2368 const TypeInfo
2369 *type_info,
2370 *type_info_base;
2371
2372 const char
2373 *wmf_font_name;
2374
2375 if (font == 0)
2376 return;
2377
2378 font_data = (wmfFontData*)API->font_data;
2379 font->user_data = font_data->user_data;
2380 magick_font = (wmf_magick_font_t*)font->user_data;
2381 wmf_font_name = WMF_FONT_NAME(font);
2382
2383 if (magick_font->ps_name != (char *) NULL)
2384 magick_font->ps_name=DestroyString(magick_font->ps_name);
2385
2386 exception=ddata->exception;
2387 type_info_base=GetTypeInfo("*",exception);
2388 if (type_info_base == 0)
2389 return;
2390
2391 /* Certain short-hand font names are not the proper Windows names
2392 and should be promoted to the proper names */
2393 if (LocaleCompare(wmf_font_name,"Times") == 0)
2394 wmf_font_name = "Times New Roman";
2395 else if (LocaleCompare(wmf_font_name,"Courier") == 0)
2396 wmf_font_name = "Courier New";
2397
2398 /* Look for a family-based best-match */
2399 if (!magick_font->ps_name)
2400 {
2401 int
2402 target_weight;
2403
2404 StyleType
2405 style = AnyStyle;
2406
2407 if (WMF_FONT_WEIGHT(font) == 0)
2408 target_weight = 400;
2409 else
2410 target_weight = WMF_FONT_WEIGHT(font);
2411 if (WMF_FONT_ITALIC(font))
2412 style=ItalicStyle;
2413 type_info=GetTypeInfoByFamily(wmf_font_name,style,AnyStretch,
2414 (size_t) target_weight,exception);
2415 if (type_info == (const TypeInfo *) NULL)
2416 type_info=GetTypeInfoByFamily(wmf_font_name,AnyStyle,AnyStretch,0,
2417 exception);
2418 if (type_info != (const TypeInfo *) NULL)
2419 CloneString(&magick_font->ps_name,type_info->name);
2420 }
2421
2422 /* Look for exact full match */
2423 if (!magick_font->ps_name)
2424 {
2425 type_info=GetTypeInfo(wmf_font_name,exception);
2426 if (type_info != (const TypeInfo *) NULL)
2427 CloneString(&magick_font->ps_name,type_info->name);
2428 }
2429
2430 /* Now let's try simple substitution mappings from WMFFontMap */
2431 if (!magick_font->ps_name)
2432 {
2433 char
2434 target[MagickPathExtent];
2435
2436 int
2437 target_weight = 400,
2438 want_italic = MagickFalse,
2439 want_bold = MagickFalse,
2440 i;
2441
2442 if (WMF_FONT_WEIGHT(font) != 0)
2443 target_weight = WMF_FONT_WEIGHT(font);
2444
2445 if ((target_weight > 550) || ((strstr(wmf_font_name,"Bold") ||
2446 strstr(wmf_font_name,"Heavy") ||
2447 strstr(wmf_font_name,"Black"))))
2448 want_bold = MagickTrue;
2449
2450 if ((WMF_FONT_ITALIC(font)) || ((strstr(wmf_font_name,"Italic") ||
2451 strstr(wmf_font_name,"Oblique"))))
2452 want_italic = MagickTrue;
2453
2454 (void) CopyMagickString(target,"Times",MagickPathExtent);
2455 for(i=0; SubFontMap[i].name != NULL; i++)
2456 {
2457 if (LocaleCompare(wmf_font_name, SubFontMap[i].name) == 0)
2458 {
2459 (void) CopyMagickString(target,SubFontMap[i].mapping,
2460 MagickPathExtent);
2461 break;
2462 }
2463 }
2464
2465 for(i=0; WMFFontMap[i].name != NULL; i++)
2466 {
2467 if (LocaleNCompare(WMFFontMap[i].name,target,strlen(WMFFontMap[i].name)) == 0)
2468 {
2469 if (want_bold && want_italic)
2470 CloneString(&magick_font->ps_name,WMFFontMap[i].bolditalic);
2471 else if (want_italic)
2472 CloneString(&magick_font->ps_name,WMFFontMap[i].italic);
2473 else if (want_bold)
2474 CloneString(&magick_font->ps_name,WMFFontMap[i].bold);
2475 else
2476 CloneString(&magick_font->ps_name,WMFFontMap[i].normal);
2477 }
2478 }
2479 }
2480
2481 #if 0
2482 printf("\nlite_font_map\n");
2483 printf("WMF_FONT_NAME = \"%s\"\n", WMF_FONT_NAME(font));
2484 printf("WMF_FONT_WEIGHT = %i\n", WMF_FONT_WEIGHT(font));
2485 printf("WMF_FONT_PSNAME = \"%s\"\n", WMF_FONT_PSNAME(font));
2486 fflush(stdout);
2487 #endif
2488
2489 }
2490
2491 /* Initialize API font structures */
2492 static void lite_font_init(wmfAPI* API, wmfAPI_Options* options)
2493 {
2494 wmfFontData
2495 *font_data;
2496
2497 (void) options;
2498 API->fonts = 0;
2499
2500 /* Allocate wmfFontData data structure */
2501 API->font_data = wmf_malloc(API,sizeof(wmfFontData));
2502 if (ERR (API))
2503 return;
2504
2505 font_data = (wmfFontData*)API->font_data;
2506
2507 /* Assign function to map font (type wmfMap) */
2508 font_data->map = lite_font_map;
2509
2510 /* Assign function to return string width in points (type wmfStringWidth) */
2511 font_data->stringwidth = lite_font_stringwidth;
2512
2513 /* Assign user data, not used by libwmflite (type void*) */
2514 font_data->user_data = wmf_malloc(API,sizeof(wmf_magick_font_t));
2515 if (ERR(API))
2516 return;
2517 ((wmf_magick_font_t*)font_data->user_data)->ps_name = 0;
2518 ((wmf_magick_font_t*)font_data->user_data)->pointsize = 0;
2519 }
2520
2521 #endif /* MAGICKCORE_WMF_DELEGATE */
2522
2523 /* BLOB read byte */
2524 static int ipa_blob_read(void* wand)
2525 {
2526 return ReadBlobByte((Image*)wand);
2527 }
2528
2529 /* BLOB seek */
2530 static int ipa_blob_seek(void* wand,long position)
2531 {
2532 return (int)SeekBlob((Image*)wand,(MagickOffsetType) position,SEEK_SET);
2533 }
2534
2535 /* BLOB tell */
2536 static long ipa_blob_tell(void* wand)
2537 {
2538 return (long)TellBlob((Image*)wand);
2539 }
2540
2541 static Image *ReadWMFImage(const ImageInfo *image_info,ExceptionInfo *exception)
2542 {
2543 double
2544 bounding_height,
2545 bounding_width,
2546 image_height,
2547 image_height_inch,
2548 image_width,
2549 image_width_inch,
2550 resolution_y,
2551 resolution_x,
2552 units_per_inch;
2553
2554 float
2555 wmf_width,
2556 wmf_height;
2557
2558 Image
2559 *image;
2560
2561 MagickBooleanType
2562 status;
2563
2564 unsigned long
2565 wmf_options_flags = 0;
2566
2567 wmf_error_t
2568 wmf_error;
2569
2570 wmf_magick_t
2571 *ddata = 0;
2572
2573 wmfAPI
2574 *API = 0;
2575
2576 wmfAPI_Options
2577 wmf_api_options;
2578
2579 wmfD_Rect
2580 bbox;
2581
2582 image=AcquireImage(image_info,exception);
2583 if (OpenBlob(image_info,image,ReadBinaryBlobMode,exception) == MagickFalse)
2584 {
2585 if (image->debug != MagickFalse)
2586 {
2587 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2588 " OpenBlob failed");
2589 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2590 "leave ReadWMFImage()");
2591 }
2592 image=DestroyImageList(image);
2593 return((Image *) NULL);
2594 }
2595
2596 /*
2597 *Create WMF API
2598 *
2599 */
2600
2601 /* Register callbacks */
2602 wmf_options_flags |= WMF_OPT_FUNCTION;
2603 (void) memset(&wmf_api_options, 0, sizeof(wmf_api_options));
2604 wmf_api_options.function = ipa_functions;
2605
2606 /* Ignore non-fatal errors */
2607 wmf_options_flags |= WMF_OPT_IGNORE_NONFATAL;
2608
2609 wmf_error = wmf_api_create(&API, wmf_options_flags, &wmf_api_options);
2610 if (wmf_error != wmf_E_None)
2611 {
2612 if (image->debug != MagickFalse)
2613 {
2614 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2615 " wmf_api_create failed");
2616 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2617 "leave ReadWMFImage()");
2618 }
2619 if (API)
2620 wmf_api_destroy(API);
2621 ThrowReaderException(DelegateError,"UnableToInitializeWMFLibrary");
2622 }
2623
2624 /* Register progress monitor */
2625 wmf_status_function(API,image,magick_progress_callback);
2626
2627 ddata=WMF_MAGICK_GetData(API);
2628 ddata->image=image;
2629 ddata->image_info=image_info;
2630 ddata->draw_info=CloneDrawInfo(image_info,(const DrawInfo *) NULL);
2631 ddata->exception=exception;
2632 ddata->draw_info->font=(char *)
2633 RelinquishMagickMemory(ddata->draw_info->font);
2634 ddata->draw_info->text=(char *)
2635 RelinquishMagickMemory(ddata->draw_info->text);
2636
2637 #if defined(MAGICKCORE_WMF_DELEGATE)
2638 /* Must initialize font subsystem for WMFlite interface */
2639 lite_font_init (API,&wmf_api_options); /* similar to wmf_ipa_font_init in src/font.c */
2640 /* wmf_arg_fontdirs (API,options); */ /* similar to wmf_arg_fontdirs in src/wmf.c */
2641
2642 #endif
2643
2644 /*
2645 *Open BLOB input via libwmf API
2646 *
2647 */
2648 wmf_error = wmf_bbuf_input(API,ipa_blob_read,ipa_blob_seek,
2649 ipa_blob_tell,(void*)image);
2650 if (wmf_error != wmf_E_None)
2651 {
2652 if (image->debug != MagickFalse)
2653 {
2654 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2655 " wmf_bbuf_input failed");
2656 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2657 "leave ReadWMFImage()");
2658 }
2659 wmf_api_destroy(API);
2660 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2661 image->filename);
2662 image=DestroyImageList(image);
2663 return((Image *) NULL);
2664 }
2665
2666 /*
2667 *Scan WMF file
2668 *
2669 */
2670 if (image->debug != MagickFalse)
2671 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2672 " Scanning WMF to obtain bounding box");
2673 wmf_error=wmf_scan(API, 0, &bbox);
2674 if (wmf_error != wmf_E_None)
2675 {
2676 if (image->debug != MagickFalse)
2677 {
2678 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2679 " wmf_scan failed with wmf_error %d", wmf_error);
2680 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2681 "leave ReadWMFImage()");
2682 }
2683 ipa_device_close(API);
2684 wmf_api_destroy(API);
2685 ThrowReaderException(DelegateError,"FailedToScanFile");
2686 }
2687
2688 /*
2689 *Compute dimensions and scale factors
2690 *
2691 */
2692
2693 ddata->bbox=bbox;
2694
2695 /* User specified resolution */
2696 resolution_y=DefaultResolution;
2697 if (image->resolution.y != 0.0)
2698 {
2699 resolution_y = image->resolution.y;
2700 if (image->units == PixelsPerCentimeterResolution)
2701 resolution_y *= CENTIMETERS_PER_INCH;
2702 }
2703 resolution_x=DefaultResolution;
2704 if (image->resolution.x != 0.0)
2705 {
2706 resolution_x = image->resolution.x;
2707 if (image->units == PixelsPerCentimeterResolution)
2708 resolution_x *= CENTIMETERS_PER_INCH;
2709 }
2710
2711 /* Obtain output size expressed in metafile units */
2712 wmf_error=wmf_size(API,&wmf_width,&wmf_height);
2713 if (wmf_error != wmf_E_None)
2714 {
2715 if (image->debug != MagickFalse)
2716 {
2717 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2718 " wmf_size failed with wmf_error %d", wmf_error);
2719 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2720 "leave ReadWMFImage()");
2721 }
2722 ipa_device_close(API);
2723 wmf_api_destroy(API);
2724 ThrowReaderException(DelegateError,"FailedToComputeOutputSize");
2725 }
2726
2727 /* Obtain (or guess) metafile units */
2728 if ((API)->File->placeable && (API)->File->pmh->Inch)
2729 units_per_inch=(API)->File->pmh->Inch;
2730 else if ((wmf_width*wmf_height) < 1024*1024)
2731 units_per_inch=POINTS_PER_INCH; /* MM_TEXT */
2732 else
2733 units_per_inch=TWIPS_PER_INCH; /* MM_TWIPS */
2734
2735 /* Calculate image width and height based on specified DPI
2736 resolution */
2737 image_width_inch = (double) wmf_width / units_per_inch;
2738 image_height_inch = (double) wmf_height / units_per_inch;
2739 image_width = image_width_inch *resolution_x;
2740 image_height = image_height_inch *resolution_y;
2741
2742 /* Compute bounding box scale factors and origin translations
2743 *
2744 *This all just a hack since libwmf does not currently seem to
2745 *provide the mapping between LOGICAL coordinates and DEVICE
2746 *coordinates. This mapping is necessary in order to know
2747 *where to place the logical bounding box within the image.
2748 *
2749 */
2750
2751 bounding_width = bbox.BR.x - bbox.TL.x;
2752 bounding_height = bbox.BR.y - bbox.TL.y;
2753 if ((bounding_width == 0) || (bounding_height == 0))
2754 {
2755 ipa_device_close(API);
2756 (void) wmf_api_destroy(API);
2757 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
2758 }
2759 ddata->scale_x = image_width/bounding_width;
2760 ddata->translate_x = 0-bbox.TL.x;
2761 ddata->rotate = 0;
2762
2763 /* Heuristic: guess that if the vertical coordinates mostly span
2764 negative values, then the image must be inverted. */
2765 if (fabs(bbox.BR.y) > fabs(bbox.TL.y))
2766 {
2767 /* Normal (Origin at top left of image) */
2768 ddata->scale_y = (image_height/bounding_height);
2769 ddata->translate_y = 0-bbox.TL.y;
2770 }
2771 else
2772 {
2773 /* Inverted (Origin at bottom left of image) */
2774 ddata->scale_y = (-image_height/bounding_height);
2775 ddata->translate_y = 0-bbox.BR.y;
2776 }
2777
2778 if (image->debug != MagickFalse)
2779 {
2780 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2781 " Placeable metafile: %s",
2782 (API)->File->placeable ? "Yes" : "No");
2783
2784 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2785 " Size in metafile units: %gx%g",(double) wmf_width,(double)
2786 wmf_height);
2787 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2788 " Metafile units/inch: %g",units_per_inch);
2789 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2790 " Size in inches: %gx%g",
2791 image_width_inch,image_height_inch);
2792 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2793 " Bounding Box: %g,%g %g,%g",(double) bbox.TL.x,
2794 (double) bbox.TL.y, (double) bbox.BR.x, (double) bbox.BR.y);
2795 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2796 " Bounding width x height: %gx%g",bounding_width,
2797 bounding_height);
2798 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2799 " Output resolution: %gx%g",resolution_x,resolution_y);
2800 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2801 " Image size: %gx%g",image_width,image_height);
2802 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2803 " Bounding box scale factor: %g,%g",ddata->scale_x,
2804 ddata->scale_y);
2805 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2806 " Translation: %g,%g",
2807 ddata->translate_x, ddata->translate_y);
2808 }
2809
2810 #if 0
2811 #if 0
2812 {
2813 typedef struct _wmfPlayer_t wmfPlayer_t;
2814 struct _wmfPlayer_t
2815 {
2816 wmfPen default_pen;
2817 wmfBrush default_brush;
2818 wmfFont default_font;
2819
2820 wmfDC* dc; /* current dc */
2821 };
2822
2823 wmfDC
2824 *dc;
2825
2826 #define WMF_ELICIT_DC(API) (((wmfPlayer_t*)((API)->player_data))->dc)
2827
2828 dc = WMF_ELICIT_DC(API);
2829
2830 printf("dc->Window.Ox = %d\n", dc->Window.Ox);
2831 printf("dc->Window.Oy = %d\n", dc->Window.Oy);
2832 printf("dc->Window.width = %d\n", dc->Window.width);
2833 printf("dc->Window.height = %d\n", dc->Window.height);
2834 printf("dc->pixel_width = %g\n", dc->pixel_width);
2835 printf("dc->pixel_height = %g\n", dc->pixel_height);
2836 #if defined(MAGICKCORE_WMF_DELEGATE) /* Only in libwmf 0.3 */
2837 printf("dc->Ox = %.d\n", dc->Ox);
2838 printf("dc->Oy = %.d\n", dc->Oy);
2839 printf("dc->width = %.d\n", dc->width);
2840 printf("dc->height = %.d\n", dc->height);
2841 #endif
2842
2843 }
2844 #endif
2845
2846 #endif
2847
2848 /*
2849 *Create canvas image
2850 *
2851 */
2852 image->rows=(unsigned long) ceil(image_height);
2853 image->columns=(unsigned long) ceil(image_width);
2854
2855 if (image_info->ping != MagickFalse)
2856 {
2857 ipa_device_close(API);
2858 wmf_api_destroy(API);
2859 (void) CloseBlob(image);
2860 if (image->debug != MagickFalse)
2861 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2862 "leave ReadWMFImage()");
2863 return(GetFirstImageInList(image));
2864 }
2865 status=SetImageExtent(image,image->columns,image->rows,exception);
2866 if (status == MagickFalse)
2867 return(DestroyImageList(image));
2868 if (image->debug != MagickFalse)
2869 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2870 " Creating canvas image with size %lux%lu",(unsigned long) image->rows,
2871 (unsigned long) image->columns);
2872
2873 /*
2874 *Set solid background color
2875 */
2876 {
2877 image->background_color = image_info->background_color;
2878 if (image->background_color.alpha != (double) OpaqueAlpha)
2879 image->alpha_trait=BlendPixelTrait;
2880 (void) SetImageBackgroundColor(image,exception);
2881 }
2882 /*
2883 *Play file to generate Vector drawing commands
2884 *
2885 */
2886
2887 if (image->debug != MagickFalse)
2888 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2889 " Playing WMF to prepare vectors");
2890
2891 wmf_error = wmf_play(API, 0, &bbox);
2892 if (wmf_error != wmf_E_None)
2893 {
2894 if (image->debug != MagickFalse)
2895 {
2896 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2897 " Playing WMF failed with wmf_error %d", wmf_error);
2898 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2899 "leave ReadWMFImage()");
2900 }
2901 ipa_device_close(API);
2902 wmf_api_destroy(API);
2903 ThrowReaderException(DelegateError,"FailedToRenderFile");
2904 }
2905
2906 /*
2907 *Scribble on canvas image
2908 *
2909 */
2910
2911 if (image->debug != MagickFalse)
2912 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2913 " Rendering WMF vectors");
2914 DrawRender(ddata->draw_wand);
2915
2916 if (image->debug != MagickFalse)
2917 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"leave ReadWMFImage()");
2918
2919 /* Cleanup allocated data */
2920 wmf_api_destroy(API);
2921 (void) CloseBlob(image);
2922
2923 /* Return image */
2924 return image;
2925 }
2926 #endif
2927
2928 /*
2929 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2930 % %
2931 % %
2932 % %
2933 % R e g i s t e r W M F I m a g e %
2934 % %
2935 % %
2936 % %
2937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2938 %
2939 % RegisterWMFImage() adds attributes for the WMF image format to
2940 % the list of supported formats. The attributes include the image format
2941 % tag, a method to read and/or write the format, whether the format
2942 % supports the saving of more than one frame to the same file or blob,
2943 % whether the format supports native in-memory I/O, and a brief
2944 % description of the format.
2945 %
2946 % The format of the RegisterWMFImage method is:
2947 %
2948 % size_t RegisterWMFImage(void)
2949 %
2950 */
2951 ModuleExport size_t RegisterWMFImage(void)
2952 {
2953 MagickInfo
2954 *entry;
2955
2956 entry=AcquireMagickInfo("WMF","WMZ","Compressed Windows Meta File");
2957 #if defined(MAGICKCORE_SANS_DELEGATE) || defined(MAGICKCORE_WMF_DELEGATE)
2958 entry->decoder=ReadWMFImage;
2959 #endif
2960 entry->flags|=CoderDecoderSeekableStreamFlag;
2961 (void) RegisterMagickInfo(entry);
2962 entry=AcquireMagickInfo("WMF","WMF","Windows Meta File");
2963 #if defined(MAGICKCORE_SANS_DELEGATE) || defined(MAGICKCORE_WMF_DELEGATE)
2964 entry->decoder=ReadWMFImage;
2965 #endif
2966 (void) RegisterMagickInfo(entry);
2967 return(MagickImageCoderSignature);
2968 }
2969
2970 /*
2971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2972 % %
2973 % %
2974 % %
2975 % U n r e g i s t e r W M F I m a g e %
2976 % %
2977 % %
2978 % %
2979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2980 %
2981 % UnregisterWMFImage() removes format registrations made by the
2982 % WMF module from the list of supported formats.
2983 %
2984 % The format of the UnregisterWMFImage method is:
2985 %
2986 % UnregisterWMFImage(void)
2987 %
2988 */
2989 ModuleExport void UnregisterWMFImage(void)
2990 {
2991 (void) UnregisterMagickInfo("WMZ");
2992 (void) UnregisterMagickInfo("WMF");
2993 }