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