2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC AAA PPPP TTTTT IIIII OOO N N %
7 % C A A P P T I O O NN N %
8 % C AAAAA PPPP T I O O N N N %
9 % C A A P T I O O N NN %
10 % CCCC A A P T IIIII OOO N N %
13 % Read Text Caption. %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/annotate.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/composite-private.h"
47 #include "MagickCore/draw.h"
48 #include "MagickCore/draw-private.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/module.h"
57 #include "MagickCore/option.h"
58 #include "MagickCore/property.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/string-private.h"
63 #include "MagickCore/utility.h"
64 #if defined(MAGICKCORE_PANGOFT2_DELEGATE)
65 #include <pango/pango.h>
66 #include <pango/pangoft2.h>
67 #include <pango/pango-features.h>
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e a d C A P T I O N I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % ReadCAPTIONImage() reads a CAPTION image file and returns it. It
82 % allocates the memory necessary for the new Image structure and returns a
83 % pointer to the new image.
85 % The format of the ReadCAPTIONImage method is:
87 % Image *ReadCAPTIONImage(const ImageInfo *image_info,
88 % ExceptionInfo *exception)
90 % A description of each parameter follows:
92 % o image_info: the image info.
94 % o exception: return any errors or warnings in this structure.
97 #if defined(MAGICKCORE_PANGOFT2_DELEGATE)
98 static void PangoSubstitute(FcPattern *pattern,void *context)
103 option=(const char *) context;
104 if (option == (const char *) NULL)
106 FcPatternDel(pattern,FC_HINTING);
107 FcPatternAddBool(pattern, FC_HINTING,LocaleCompare(option,"none") != 0);
108 FcPatternDel(pattern,FC_AUTOHINT);
109 FcPatternAddBool(pattern,FC_AUTOHINT,LocaleCompare(option,"auto") == 0);
112 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
113 ExceptionInfo *exception)
161 register unsigned char
168 Initialize Image structure.
170 assert(image_info != (const ImageInfo *) NULL);
171 assert(image_info->signature == MagickSignature);
172 if (image_info->debug != MagickFalse)
173 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
174 image_info->filename);
175 assert(exception != (ExceptionInfo *) NULL);
176 assert(exception->signature == MagickSignature);
177 image=AcquireImage(image_info,exception);
178 (void) ResetImagePage(image,"0x0+0+0");
182 fontmap=(PangoFontMap *) pango_ft2_font_map_new();
183 pango_ft2_font_map_set_resolution((PangoFT2FontMap *) fontmap,
184 image->resolution.x,image->resolution.y);
185 option=GetImageOption(image_info,"caption:hinting");
186 pango_ft2_font_map_set_default_substitute((PangoFT2FontMap *) fontmap,
187 PangoSubstitute,(char *) option,NULL);
188 context=pango_font_map_create_context(fontmap);
189 option=GetImageOption(image_info,"caption:language");
190 if (option != (const char *) NULL)
191 pango_context_set_language(context,pango_language_from_string(option));
192 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
193 pango_context_set_base_dir(context,draw_info->direction ==
194 RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
195 switch (draw_info->gravity)
197 case NorthGravity: gravity=PANGO_GRAVITY_NORTH; break;
198 case WestGravity: gravity=PANGO_GRAVITY_WEST; break;
199 case EastGravity: gravity=PANGO_GRAVITY_EAST; break;
200 case SouthGravity: gravity=PANGO_GRAVITY_SOUTH; break;
201 default: gravity=PANGO_GRAVITY_AUTO; break;
203 pango_context_set_base_gravity(context,gravity);
204 option=GetImageOption(image_info,"caption:gravity-hint");
205 if (option != (const char *) NULL)
207 if (LocaleCompare(option,"line") == 0)
208 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE);
209 if (LocaleCompare(option,"natural") == 0)
210 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL);
211 if (LocaleCompare(option,"strong") == 0)
212 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG);
217 layout=pango_layout_new(context);
218 option=GetImageOption(image_info,"caption:auto-dir");
219 if (option != (const char *) NULL)
220 pango_layout_set_auto_dir(layout,1);
221 option=GetImageOption(image_info,"caption:ellipsize");
222 if (option != (const char *) NULL)
224 if (LocaleCompare(option,"end") == 0)
225 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END);
226 if (LocaleCompare(option,"middle") == 0)
227 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE);
228 if (LocaleCompare(option,"none") == 0)
229 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE);
230 if (LocaleCompare(option,"start") == 0)
231 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START);
233 option=GetImageOption(image_info,"caption:justify");
234 if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
235 pango_layout_set_justify(layout,1);
236 option=GetImageOption(image_info,"caption:single-paragraph");
237 if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
238 pango_layout_set_single_paragraph_mode(layout,1);
239 option=GetImageOption(image_info,"caption:wrap");
240 if (option != (const char *) NULL)
242 if (LocaleCompare(option,"char") == 0)
243 pango_layout_set_wrap(layout,PANGO_WRAP_CHAR);
244 if (LocaleCompare(option,"word") == 0)
245 pango_layout_set_wrap(layout,PANGO_WRAP_WORD);
246 if (LocaleCompare(option,"word-char") == 0)
247 pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR);
249 option=GetImageOption(image_info,"caption:indent");
250 if (option != (const char *) NULL)
251 pango_layout_set_indent(layout,(StringToLong(option)*image->resolution.x*
253 switch (draw_info->align)
255 case CenterAlign: align=PANGO_ALIGN_CENTER; break;
256 case RightAlign: align=PANGO_ALIGN_RIGHT; break;
258 default: align=PANGO_ALIGN_LEFT; break;
260 if ((align != PANGO_ALIGN_CENTER) &&
261 (draw_info->direction == RightToLeftDirection))
262 align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align);
263 pango_layout_set_alignment(layout,align);
264 description=pango_font_description_from_string(draw_info->font ==
265 (char *) NULL ? "helvetica" : draw_info->font);
266 pango_font_description_set_size(description,PANGO_SCALE*draw_info->pointsize);
267 pango_layout_set_font_description(layout,description);
268 pango_font_description_free(description);
269 property=InterpretImageProperties(image_info,image,image_info->filename,
271 (void) SetImageProperty(image,"caption",property,exception);
272 property=DestroyString(property);
273 caption=ConstantString(GetImageProperty(image,"caption",exception));
277 option=GetImageOption(image_info,"caption:markup");
278 if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
279 pango_layout_set_markup(layout,caption,-1);
281 pango_layout_set_text(layout,caption,-1);
282 pango_layout_context_changed(layout);
285 if (image_info->page != (char *) NULL)
286 (void) ParseAbsoluteGeometry(image_info->page,&page);
287 if (image->columns == 0)
289 pango_layout_get_pixel_extents(layout,NULL,&extent);
290 image->columns=extent.x+extent.width;
294 image->columns-=2*page.x;
295 pango_layout_set_width(layout,(PANGO_SCALE*image->columns*
296 image->resolution.x+36.0)/72.0);
298 if (image->rows == 0)
300 pango_layout_get_pixel_extents(layout,NULL,&extent);
301 image->rows=extent.y+extent.height;
305 image->rows-=2*page.y;
306 pango_layout_set_height(layout,(PANGO_SCALE*image->rows*
307 image->resolution.y+36.0)/72.0);
312 canvas=(FT_Bitmap *) AcquireMagickMemory(sizeof(*canvas));
313 if (canvas == (FT_Bitmap *) NULL)
315 draw_info=DestroyDrawInfo(draw_info);
316 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
318 canvas->width=image->columns;
319 canvas->pitch=(canvas->width+3) & ~3;
320 canvas->rows=image->rows;
321 canvas->buffer=(unsigned char *) AcquireQuantumMemory(canvas->pitch,
322 canvas->rows*sizeof(*canvas->buffer));
323 if (canvas->buffer == (unsigned char *) NULL)
325 draw_info=DestroyDrawInfo(draw_info);
326 canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
327 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
329 canvas->num_grays=256;
330 canvas->pixel_mode=ft_pixel_mode_grays;
331 ResetMagickMemory(canvas->buffer,0x00,canvas->pitch*canvas->rows);
332 pango_ft2_render_layout(canvas,layout,0,0);
334 Convert caption to image.
336 image->columns+=2*page.x;
337 image->rows+=2*page.y;
338 if (SetImageBackgroundColor(image,exception) == MagickFalse)
340 draw_info=DestroyDrawInfo(draw_info);
341 canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer);
342 canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
343 caption=DestroyString(caption);
344 image=DestroyImageList(image);
345 return((Image *) NULL);
347 GetPixelInfo(image,&fill_color);
349 for (y=page.y; y < (ssize_t) (image->rows-page.y); y++)
354 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
355 if (q == (Quantum *) NULL)
357 q+=page.x*GetPixelChannels(image);
358 for (x=page.x; x < (ssize_t) (image->columns-page.x); x++)
363 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
364 fill_opacity=(*p)*fill_color.alpha/canvas->num_grays;
365 if (draw_info->text_antialias == MagickFalse)
366 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
367 CompositePixelOver(image,&fill_color,fill_opacity,q,
368 GetPixelAlpha(image,q),q);
370 q+=GetPixelChannels(image);
372 for ( ; x < (ssize_t) ((canvas->width+3) & ~3); x++)
376 Relinquish resources.
378 draw_info=DestroyDrawInfo(draw_info);
379 canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer);
380 canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
381 caption=DestroyString(caption);
382 return(GetFirstImageInList(image));
385 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
386 ExceptionInfo *exception)
390 geometry[MaxTextExtent],
416 Initialize Image structure.
418 assert(image_info != (const ImageInfo *) NULL);
419 assert(image_info->signature == MagickSignature);
420 if (image_info->debug != MagickFalse)
421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
422 image_info->filename);
423 assert(exception != (ExceptionInfo *) NULL);
424 assert(exception->signature == MagickSignature);
425 image=AcquireImage(image_info,exception);
426 if (image->columns == 0)
427 ThrowReaderException(OptionError,"MustSpecifyImageSize");
428 (void) ResetImagePage(image,"0x0+0+0");
432 property=InterpretImageProperties(image_info,image,image_info->filename,
434 (void) SetImageProperty(image,"caption",property,exception);
435 property=DestroyString(property);
436 caption=ConstantString(GetImageProperty(image,"caption",exception));
437 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
438 (void) CloneString(&draw_info->text,caption);
439 gravity=GetImageOption(image_info,"gravity");
440 if (gravity != (char *) NULL)
441 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
442 MagickFalse,gravity);
443 if ((*caption != '\0') && (image->rows != 0) &&
444 (image_info->pointsize == 0.0))
450 Scale text to fit bounding box.
454 text=AcquireString(caption);
455 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
457 (void) CloneString(&draw_info->text,text);
458 text=DestroyString(text);
459 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
460 -metrics.bounds.x1,metrics.ascent);
461 if (draw_info->gravity == UndefinedGravity)
462 (void) CloneString(&draw_info->geometry,geometry);
463 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
465 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
466 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
467 if ((width > (image->columns+1)) || (height > (image->rows+1)))
469 draw_info->pointsize*=2.0;
471 draw_info->pointsize/=2.0;
474 text=AcquireString(caption);
475 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
477 (void) CloneString(&draw_info->text,text);
478 text=DestroyString(text);
479 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
480 -metrics.bounds.x1,metrics.ascent);
481 if (draw_info->gravity == UndefinedGravity)
482 (void) CloneString(&draw_info->geometry,geometry);
483 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
484 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
485 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
486 if ((width > (image->columns+1)) || (height > (image->rows+1)))
488 draw_info->pointsize++;
490 draw_info->pointsize--;
492 i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception);
493 if (image->rows == 0)
494 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
495 draw_info->interline_spacing+draw_info->stroke_width)+0.5);
496 if (image->rows == 0)
497 image->rows=(size_t) ((i+1)*draw_info->pointsize+
498 draw_info->interline_spacing+draw_info->stroke_width+0.5);
499 if (SetImageBackgroundColor(image,exception) == MagickFalse)
501 image=DestroyImageList(image);
502 return((Image *) NULL);
507 (void) CloneString(&draw_info->text,caption);
508 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
509 if ((draw_info->gravity != UndefinedGravity) &&
510 (draw_info->direction != RightToLeftDirection))
511 image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
514 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
515 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
516 draw_info->stroke_width/2.0);
517 if (draw_info->direction == RightToLeftDirection)
518 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
519 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
520 metrics.ascent+draw_info->stroke_width/2.0);
521 draw_info->geometry=AcquireString(geometry);
523 (void) AnnotateImage(image,draw_info,exception);
524 draw_info=DestroyDrawInfo(draw_info);
525 caption=DestroyString(caption);
526 return(GetFirstImageInList(image));
531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 % R e g i s t e r C A P T I O N I m a g e %
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 % RegisterCAPTIONImage() adds attributes for the CAPTION image format to
542 % the list of supported formats. The attributes include the image format
543 % tag, a method to read and/or write the format, whether the format
544 % supports the saving of more than one frame to the same file or blob,
545 % whether the format supports native in-memory I/O, and a brief
546 % description of the format.
548 % The format of the RegisterCAPTIONImage method is:
550 % size_t RegisterCAPTIONImage(void)
553 ModuleExport size_t RegisterCAPTIONImage(void)
556 version[MaxTextExtent];
562 #if defined(PANGO_VERSION_STRING)
563 (void) FormatLocaleString(version,MaxTextExtent,"Pangoft2 %s",
564 PANGO_VERSION_STRING);
566 entry=SetMagickInfo("CAPTION");
567 entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
568 entry->description=ConstantString("Caption");
569 if (*version != '\0')
570 entry->version=ConstantString(version);
571 entry->adjoin=MagickFalse;
572 entry->module=ConstantString("CAPTION");
573 (void) RegisterMagickInfo(entry);
574 return(MagickImageCoderSignature);
578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 % U n r e g i s t e r C A P T I O N I m a g e %
586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 % UnregisterCAPTIONImage() removes format registrations made by the
589 % CAPTION module from the list of supported formats.
591 % The format of the UnregisterCAPTIONImage method is:
593 % UnregisterCAPTIONImage(void)
596 ModuleExport void UnregisterCAPTIONImage(void)
598 (void) UnregisterMagickInfo("CAPTION");