2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP AAA N N GGGG OOO %
8 % PPPP AAAAA N N N G GG O O %
13 % Read Text Caption. %
20 % Copyright 1999-2012 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 P A N G O I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % ReadPANGOImage() reads a PANGO 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 ReadPANGOImage method is:
87 % Image *ReadPANGOImage(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 MagickBooleanType PangoImage(const ImageInfo *image_info,Image *image,
113 const DrawInfo *draw_info,ExceptionInfo *exception)
151 register unsigned char
160 fontmap=(PangoFontMap *) pango_ft2_font_map_new();
161 pango_ft2_font_map_set_resolution((PangoFT2FontMap *) fontmap,
162 image->resolution.x,image->resolution.y);
163 option=GetImageOption(image_info,"pango:hinting");
164 pango_ft2_font_map_set_default_substitute((PangoFT2FontMap *) fontmap,
165 PangoSubstitute,(char *) option,NULL);
166 context=pango_font_map_create_context(fontmap);
167 option=GetImageOption(image_info,"pango:language");
168 if (option != (const char *) NULL)
169 pango_context_set_language(context,pango_language_from_string(option));
170 pango_context_set_base_dir(context,draw_info->direction ==
171 RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
172 switch (draw_info->gravity)
174 case NorthGravity: gravity=PANGO_GRAVITY_NORTH; break;
175 case WestGravity: gravity=PANGO_GRAVITY_WEST; break;
176 case EastGravity: gravity=PANGO_GRAVITY_EAST; break;
177 case SouthGravity: gravity=PANGO_GRAVITY_SOUTH; break;
178 default: gravity=PANGO_GRAVITY_AUTO; break;
180 pango_context_set_base_gravity(context,gravity);
181 option=GetImageOption(image_info,"pango:gravity-hint");
182 if (option != (const char *) NULL)
184 if (LocaleCompare(option,"line") == 0)
185 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE);
186 if (LocaleCompare(option,"natural") == 0)
187 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL);
188 if (LocaleCompare(option,"strong") == 0)
189 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG);
194 layout=pango_layout_new(context);
195 option=GetImageOption(image_info,"pango:auto-dir");
196 if (option != (const char *) NULL)
197 pango_layout_set_auto_dir(layout,1);
198 option=GetImageOption(image_info,"pango:ellipsize");
199 if (option != (const char *) NULL)
201 if (LocaleCompare(option,"end") == 0)
202 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END);
203 if (LocaleCompare(option,"middle") == 0)
204 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE);
205 if (LocaleCompare(option,"none") == 0)
206 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE);
207 if (LocaleCompare(option,"start") == 0)
208 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START);
210 option=GetImageOption(image_info,"pango:justify");
211 if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
212 pango_layout_set_justify(layout,1);
213 option=GetImageOption(image_info,"pango:single-paragraph");
214 if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
215 pango_layout_set_single_paragraph_mode(layout,1);
216 option=GetImageOption(image_info,"pango:wrap");
217 if (option != (const char *) NULL)
219 if (LocaleCompare(option,"char") == 0)
220 pango_layout_set_wrap(layout,PANGO_WRAP_CHAR);
221 if (LocaleCompare(option,"word") == 0)
222 pango_layout_set_wrap(layout,PANGO_WRAP_WORD);
223 if (LocaleCompare(option,"word-char") == 0)
224 pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR);
226 option=GetImageOption(image_info,"pango:indent");
227 if (option != (const char *) NULL)
228 pango_layout_set_indent(layout,(StringToLong(option)*image->resolution.x*
230 switch (draw_info->align)
232 case CenterAlign: align=PANGO_ALIGN_CENTER; break;
233 case RightAlign: align=PANGO_ALIGN_RIGHT; break;
234 case LeftAlign: align=PANGO_ALIGN_LEFT; break;
237 if (draw_info->gravity == CenterGravity)
239 align=PANGO_ALIGN_CENTER;
242 align=PANGO_ALIGN_LEFT;
246 if ((align != PANGO_ALIGN_CENTER) &&
247 (draw_info->direction == RightToLeftDirection))
248 align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align);
249 pango_layout_set_alignment(layout,align);
250 description=pango_font_description_from_string(draw_info->font ==
251 (char *) NULL ? "helvetica" : draw_info->font);
252 pango_font_description_set_size(description,(int) (0.815*PANGO_SCALE*
253 draw_info->pointsize+0.5));
254 pango_layout_set_font_description(layout,description);
255 pango_font_description_free(description);
259 option=GetImageOption(image_info,"pango:markup");
260 if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
261 pango_layout_set_markup(layout,draw_info->text,-1);
263 pango_layout_set_text(layout,draw_info->text,-1);
264 pango_layout_context_changed(layout);
267 if (image_info->page != (char *) NULL)
268 (void) ParseAbsoluteGeometry(image_info->page,&page);
269 if (image->columns == 0)
271 pango_layout_get_pixel_extents(layout,NULL,&extent);
272 image->columns=extent.x+extent.width;
276 image->columns-=2*page.x;
277 pango_layout_set_width(layout,(PANGO_SCALE*image->columns*
278 image->resolution.x+36.0)/72.0);
280 if (image->rows == 0)
282 pango_layout_get_pixel_extents(layout,NULL,&extent);
283 image->rows=extent.y+extent.height;
287 image->rows-=2*page.y;
288 pango_layout_set_height(layout,(PANGO_SCALE*image->rows*
289 image->resolution.y+36.0)/72.0);
294 canvas=(FT_Bitmap *) AcquireMagickMemory(sizeof(*canvas));
295 if (canvas == (FT_Bitmap *) NULL)
296 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
298 canvas->width=image->columns;
299 canvas->pitch=(canvas->width+3) & ~3;
300 canvas->rows=image->rows;
301 canvas->buffer=(unsigned char *) AcquireQuantumMemory(canvas->pitch,
302 canvas->rows*sizeof(*canvas->buffer));
303 if (canvas->buffer == (unsigned char *) NULL)
305 canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
306 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
309 canvas->num_grays=256;
310 canvas->pixel_mode=ft_pixel_mode_grays;
311 ResetMagickMemory(canvas->buffer,0x00,canvas->pitch*canvas->rows);
312 pango_ft2_render_layout(canvas,layout,0,0);
314 Convert caption to image.
316 image->columns+=2*page.x;
317 image->rows+=2*page.y;
318 if (SetImageBackgroundColor(image,exception) == MagickFalse)
320 canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer);
321 canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
322 image=DestroyImageList(image);
325 GetPixelInfo(image,&fill_color);
327 for (y=page.y; y < (ssize_t) (image->rows-page.y); y++)
332 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
333 if (q == (Quantum *) NULL)
335 q+=page.x*GetPixelChannels(image);
336 for (x=page.x; x < (ssize_t) (image->columns-page.x); x++)
341 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
342 fill_alpha=(MagickRealType) (*p)/(canvas->num_grays-1);
343 if (draw_info->text_antialias == MagickFalse)
344 fill_alpha=fill_alpha >= 0.5 ? 1.0 : 0.0;
345 fill_alpha=fill_alpha*fill_color.alpha;
346 CompositePixelOver(image,&fill_color,fill_alpha,q,GetPixelAlpha(image,q),
349 q+=GetPixelChannels(image);
351 for ( ; x < (ssize_t) ((canvas->width+3) & ~3); x++)
355 Relinquish resources.
357 canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer);
358 canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
363 static Image *ReadPANGOImage(const ImageInfo *image_info,
364 ExceptionInfo *exception)
368 geometry[MaxTextExtent],
395 Initialize Image structure.
397 assert(image_info != (const ImageInfo *) NULL);
398 assert(image_info->signature == MagickSignature);
399 if (image_info->debug != MagickFalse)
400 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
401 image_info->filename);
402 assert(exception != (ExceptionInfo *) NULL);
403 assert(exception->signature == MagickSignature);
404 image=AcquireImage(image_info,exception);
405 if (image->columns == 0)
406 ThrowReaderException(OptionError,"MustSpecifyImageSize");
407 (void) ResetImagePage(image,"0x0+0+0");
411 property=InterpretImageProperties(image_info,image,image_info->filename,
413 option=GetImageOption(image_info,"filename");
414 if (option == (const char *) NULL)
415 property=InterpretImageProperties(image_info,image,image_info->filename,
418 if (LocaleNCompare(option,"pango:",8) == 0)
419 property=InterpretImageProperties(image_info,image,option+8,exception);
421 property=InterpretImageProperties(image_info,image,option,exception);
422 (void) SetImageProperty(image,"caption",property,exception);
423 property=DestroyString(property);
424 caption=ConstantString(GetImageProperty(image,"caption",exception));
425 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
426 (void) CloneString(&draw_info->text,caption);
427 gravity=GetImageOption(image_info,"gravity");
428 if (gravity != (char *) NULL)
429 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
430 MagickFalse,gravity);
431 if ((*caption != '\0') && (image->rows != 0) &&
432 (image_info->pointsize == 0.0))
438 Scale text to fit bounding box.
442 text=AcquireString(caption);
443 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
445 (void) CloneString(&draw_info->text,text);
446 text=DestroyString(text);
447 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
448 -metrics.bounds.x1,metrics.ascent);
449 if (draw_info->gravity == UndefinedGravity)
450 (void) CloneString(&draw_info->geometry,geometry);
451 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
453 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
454 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
455 if ((width > (image->columns+1)) || (height > (image->rows+1)))
457 draw_info->pointsize*=2.0;
459 draw_info->pointsize/=2.0;
462 text=AcquireString(caption);
463 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
465 (void) CloneString(&draw_info->text,text);
466 text=DestroyString(text);
467 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
468 -metrics.bounds.x1,metrics.ascent);
469 if (draw_info->gravity == UndefinedGravity)
470 (void) CloneString(&draw_info->geometry,geometry);
471 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
472 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
473 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
474 if ((width > (image->columns+1)) || (height > (image->rows+1)))
476 draw_info->pointsize++;
478 draw_info->pointsize--;
480 i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception);
481 if (image->rows == 0)
482 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
483 draw_info->interline_spacing+draw_info->stroke_width)+0.5);
484 if (image->rows == 0)
485 image->rows=(size_t) ((i+1)*draw_info->pointsize+
486 draw_info->interline_spacing+draw_info->stroke_width+0.5);
487 if (SetImageBackgroundColor(image,exception) == MagickFalse)
489 image=DestroyImageList(image);
490 return((Image *) NULL);
495 (void) CloneString(&draw_info->text,caption);
496 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
497 if ((draw_info->gravity != UndefinedGravity) &&
498 (draw_info->direction != RightToLeftDirection))
499 image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
502 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
503 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
504 draw_info->stroke_width/2.0);
505 if (draw_info->direction == RightToLeftDirection)
506 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
507 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
508 metrics.ascent+draw_info->stroke_width/2.0);
509 draw_info->geometry=AcquireString(geometry);
511 #if defined(MAGICKCORE_PANGOFT2_DELEGATE)
512 status=PangoImage(image_info,image,draw_info,exception);
514 status=AnnotateImage(image,draw_info,exception);
516 draw_info=DestroyDrawInfo(draw_info);
517 caption=DestroyString(caption);
518 if (status == MagickFalse)
520 image=DestroyImageList(image);
521 return((Image *) NULL);
523 return(GetFirstImageInList(image));
527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
531 % R e g i s t e r P A N G O I m a g e %
535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 % RegisterPANGOImage() adds attributes for the PANGO image format to
538 % the list of supported formats. The attributes include the image format
539 % tag, a method to read and/or write the format, whether the format
540 % supports the saving of more than one frame to the same file or blob,
541 % whether the format supports native in-memory I/O, and a brief
542 % description of the format.
544 % The format of the RegisterPANGOImage method is:
546 % size_t RegisterPANGOImage(void)
549 ModuleExport size_t RegisterPANGOImage(void)
552 version[MaxTextExtent];
558 #if defined(PANGO_VERSION_STRING)
559 (void) FormatLocaleString(version,MaxTextExtent,"Pangoft2 %s",
560 PANGO_VERSION_STRING);
562 entry=SetMagickInfo("PANGO");
563 entry->decoder=(DecodeImageHandler *) ReadPANGOImage;
564 entry->description=ConstantString("Caption");
565 if (*version != '\0')
566 entry->version=ConstantString(version);
567 entry->adjoin=MagickFalse;
568 entry->module=ConstantString("PANGO");
569 (void) RegisterMagickInfo(entry);
570 return(MagickImageCoderSignature);
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 % U n r e g i s t e r P A N G O I m a g e %
582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 % UnregisterPANGOImage() removes format registrations made by the
585 % PANGO module from the list of supported formats.
587 % The format of the UnregisterPANGOImage method is:
589 % UnregisterPANGOImage(void)
592 ModuleExport void UnregisterPANGOImage(void)
594 (void) UnregisterMagickInfo("PANGO");