2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP AAA N N GGGG OOO %
8 % PPPP AAAAA N N N G GGG O O %
9 % P M A A N NN G G O O %
10 % P A A N N GGGG OOO %
13 % Read Pango Markup Language Format %
20 % Copyright 1999-2013 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/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/option.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/property.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/string-private.h"
66 #include "MagickCore/token.h"
67 #include "MagickCore/utility.h"
68 #if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
69 #include <pango/pango.h>
70 #include <pango/pangocairo.h>
71 #include <pango/pango-features.h>
74 #if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % R e a d P A N G O I m a g e %
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % ReadPANGOImage() reads an image in the Pango Markup Language Format.
88 % The format of the ReadPANGOImage method is:
90 % Image *ReadPANGOImage(const ImageInfo *image_info,
91 % ExceptionInfo *exception)
93 % A description of each parameter follows:
95 % o image_info: the image info.
97 % o exception: return any errors or warnings in this structure.
100 static Image *ReadPANGOImage(const ImageInfo *image_info,
101 ExceptionInfo *exception)
152 register unsigned char
165 Initialize Image structure.
167 assert(image_info != (const ImageInfo *) NULL);
168 assert(image_info->signature == MagickSignature);
169 if (image_info->debug != MagickFalse)
170 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
171 image_info->filename);
172 assert(exception != (ExceptionInfo *) NULL);
173 assert(exception->signature == MagickSignature);
174 image=AcquireImage(image_info,exception);
175 (void) ResetImagePage(image,"0x0+0+0");
179 option=GetImageOption(image_info,"filename");
180 if (option == (const char *) NULL)
181 property=InterpretImageProperties(image_info,image,image_info->filename,
184 if (LocaleNCompare(option,"pango:",6) == 0)
185 property=InterpretImageProperties(image_info,image,option+6,exception);
187 property=InterpretImageProperties(image_info,image,option,exception);
188 (void) SetImageProperty(image,"caption",property,exception);
189 property=DestroyString(property);
190 caption=ConstantString(GetImageProperty(image,"caption",exception));
194 fontmap=pango_cairo_font_map_new();
195 pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(fontmap),
196 image->resolution.x == 0.0 ? 90.0 : image->resolution.x);
197 font_options=cairo_font_options_create();
198 option=GetImageOption(image_info,"pango:hinting");
199 if (option != (const char *) NULL)
201 if (LocaleCompare(option,"none") != 0)
202 cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_NONE);
203 if (LocaleCompare(option,"full") != 0)
204 cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_FULL);
206 context=pango_font_map_create_context(fontmap);
207 pango_cairo_context_set_font_options(context,font_options);
208 cairo_font_options_destroy(font_options);
209 option=GetImageOption(image_info,"pango:language");
210 if (option != (const char *) NULL)
211 pango_context_set_language(context,pango_language_from_string(option));
212 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
213 pango_context_set_base_dir(context,draw_info->direction ==
214 RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
215 switch (draw_info->gravity)
219 gravity=PANGO_GRAVITY_NORTH;
222 case NorthWestGravity:
224 case SouthWestGravity:
226 gravity=PANGO_GRAVITY_WEST;
229 case NorthEastGravity:
231 case SouthEastGravity:
233 gravity=PANGO_GRAVITY_EAST;
238 gravity=PANGO_GRAVITY_SOUTH;
243 gravity=PANGO_GRAVITY_AUTO;
247 pango_context_set_base_gravity(context,gravity);
248 option=GetImageOption(image_info,"pango:gravity-hint");
249 if (option != (const char *) NULL)
251 if (LocaleCompare(option,"line") == 0)
252 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE);
253 if (LocaleCompare(option,"natural") == 0)
254 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL);
255 if (LocaleCompare(option,"strong") == 0)
256 pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG);
261 layout=pango_layout_new(context);
262 option=GetImageOption(image_info,"pango:auto-dir");
263 if (option != (const char *) NULL)
264 pango_layout_set_auto_dir(layout,1);
265 option=GetImageOption(image_info,"pango:ellipsize");
266 if (option != (const char *) NULL)
268 if (LocaleCompare(option,"end") == 0)
269 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END);
270 if (LocaleCompare(option,"middle") == 0)
271 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE);
272 if (LocaleCompare(option,"none") == 0)
273 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE);
274 if (LocaleCompare(option,"start") == 0)
275 pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START);
277 option=GetImageOption(image_info,"pango:justify");
278 if (IfMagickTrue(IsStringTrue(option)))
279 pango_layout_set_justify(layout,1);
280 option=GetImageOption(image_info,"pango:single-paragraph");
281 if (IfMagickTrue(IsStringTrue(option)))
282 pango_layout_set_single_paragraph_mode(layout,1);
283 option=GetImageOption(image_info,"pango:wrap");
284 if (option != (const char *) NULL)
286 if (LocaleCompare(option,"char") == 0)
287 pango_layout_set_wrap(layout,PANGO_WRAP_CHAR);
288 if (LocaleCompare(option,"word") == 0)
289 pango_layout_set_wrap(layout,PANGO_WRAP_WORD);
290 if (LocaleCompare(option,"word-char") == 0)
291 pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR);
293 option=GetImageOption(image_info,"pango:indent");
294 if (option != (const char *) NULL)
295 pango_layout_set_indent(layout,(int) ((StringToLong(option)*
296 (image->resolution.x == 0.0 ? 90.0 : image->resolution.x)*PANGO_SCALE+36)/
298 switch (draw_info->align)
300 case CenterAlign: align=PANGO_ALIGN_CENTER; break;
301 case RightAlign: align=PANGO_ALIGN_RIGHT; break;
302 case LeftAlign: align=PANGO_ALIGN_LEFT; break;
305 if (draw_info->gravity == CenterGravity)
307 align=PANGO_ALIGN_CENTER;
310 align=PANGO_ALIGN_LEFT;
314 if ((align != PANGO_ALIGN_CENTER) &&
315 (draw_info->direction == RightToLeftDirection))
316 align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align);
317 pango_layout_set_alignment(layout,align);
318 if (draw_info->font != (char *) NULL)
326 description=pango_font_description_from_string(draw_info->font);
327 pango_font_description_set_size(description,(int) (PANGO_SCALE*
328 draw_info->pointsize+0.5));
329 pango_layout_set_font_description(layout,description);
330 pango_font_description_free(description);
332 option=GetImageOption(image_info,"pango:markup");
333 if ((option != (const char *) NULL) && (IsStringTrue(option) == MagickFalse))
334 pango_layout_set_text(layout,caption,-1);
340 error=(GError *) NULL;
341 if (pango_parse_markup(caption,-1,0,NULL,NULL,NULL,&error) == 0)
342 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
343 error->message,"`%s'",image_info->filename);
344 pango_layout_set_markup(layout,caption,-1);
346 pango_layout_context_changed(layout);
349 if (image_info->page != (char *) NULL)
350 (void) ParseAbsoluteGeometry(image_info->page,&page);
351 if (image->columns == 0)
353 pango_layout_get_extents(layout,NULL,&extent);
354 image->columns=(extent.x+extent.width+PANGO_SCALE/2)/PANGO_SCALE+2*page.x;
358 image->columns-=2*page.x;
359 pango_layout_set_width(layout,(int) ((PANGO_SCALE*image->columns*
360 (image->resolution.x == 0.0 ? 90.0 : image->resolution.x)+45.0)/90.0+
363 if (image->rows == 0)
365 pango_layout_get_extents(layout,NULL,&extent);
366 image->rows=(extent.y+extent.height+PANGO_SCALE/2)/PANGO_SCALE+2*page.y;
370 image->rows-=2*page.y;
371 pango_layout_set_height(layout,(int) ((PANGO_SCALE*image->rows*
372 (image->resolution.y == 0.0 ? 90.0 : image->resolution.y)+45.0)/90.0+
378 stride=(size_t) cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
380 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,stride*
382 if (pixels == (unsigned char *) NULL)
384 draw_info=DestroyDrawInfo(draw_info);
385 caption=DestroyString(caption);
386 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
388 surface=cairo_image_surface_create_for_data(pixels,CAIRO_FORMAT_ARGB32,
389 image->columns,image->rows,stride);
390 cairo_image=cairo_create(surface);
391 cairo_set_operator(cairo_image,CAIRO_OPERATOR_CLEAR);
392 cairo_paint(cairo_image);
393 cairo_set_operator(cairo_image,CAIRO_OPERATOR_OVER);
394 cairo_translate(cairo_image,page.x,page.y);
395 pango_cairo_show_layout(cairo_image,layout);
396 cairo_destroy(cairo_image);
397 cairo_surface_destroy(surface);
398 g_object_unref(layout);
399 g_object_unref(fontmap);
401 Convert surface to image.
403 (void) SetImageBackgroundColor(image,exception);
405 GetPixelInfo(image,&fill_color);
406 for (y=0; y < (ssize_t) image->rows; y++)
414 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
415 if (q == (Quantum *) NULL)
417 for (x=0; x < (ssize_t) image->columns; x++)
422 fill_color.blue=(double) ScaleCharToQuantum(*p++);
423 fill_color.green=(double) ScaleCharToQuantum(*p++);
424 fill_color.red=(double) ScaleCharToQuantum(*p++);
425 fill_color.alpha=(double) ScaleCharToQuantum(*p++);
429 gamma=1.0-QuantumScale*fill_color.alpha;
430 gamma=PerceptibleReciprocal(gamma);
431 fill_color.blue*=gamma;
432 fill_color.green*=gamma;
433 fill_color.red*=gamma;
434 CompositePixelOver(image,&fill_color,fill_color.alpha,q,(double)
435 GetPixelAlpha(image,q),q);
436 q+=GetPixelChannels(image);
438 if (SyncAuthenticPixels(image,exception) == MagickFalse)
440 if (image->previous == (Image *) NULL)
442 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
444 if (status == MagickFalse)
449 Relinquish resources.
451 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
452 draw_info=DestroyDrawInfo(draw_info);
453 caption=DestroyString(caption);
454 return(GetFirstImageInList(image));
459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % R e g i s t e r P A N G O I m a g e %
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % RegisterPANGOImage() adds attributes for the Pango Markup Language format to
470 % the list of supported formats. The attributes include the image format
471 % tag, a method to read and/or write the format, whether the format
472 % supports the saving of more than one frame to the same file or blob,
473 % whether the format supports native in-memory I/O, and a brief
474 % description of the format.
476 % The format of the RegisterPANGOImage method is:
478 % size_t RegisterPANGOImage(void)
481 ModuleExport size_t RegisterPANGOImage(void)
484 version[MaxTextExtent];
490 #if defined(PANGO_VERSION_STRING)
491 (void) FormatLocaleString(version,MaxTextExtent,"Pangocairo %s",
492 PANGO_VERSION_STRING);
494 entry=SetMagickInfo("PANGO");
495 #if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
496 entry->decoder=(DecodeImageHandler *) ReadPANGOImage;
498 entry->description=ConstantString("Pango Markup Language");
499 if (*version != '\0')
500 entry->version=ConstantString(version);
501 entry->adjoin=MagickFalse;
502 entry->module=ConstantString("PANGO");
503 (void) RegisterMagickInfo(entry);
504 return(MagickImageCoderSignature);
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 % U n r e g i s t e r P A N G O I m a g e %
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 % UnregisterPANGOImage() removes format registrations made by the Pango module
519 % from the list of supported formats.
521 % The format of the UnregisterPANGOImage method is:
523 % UnregisterPANGOImage(void)
526 ModuleExport void UnregisterPANGOImage(void)
528 (void) UnregisterMagickInfo("PANGO");