1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                     PPPP    AAA   N   N   GGGG   OOO                        %
7 %                     P   P  A   A  NN  N  G      O   O                       %
8 %                     PPPP   AAAAA  N N N  G GGG  O   O                       %
9 %                     P      A   A  N  NN  G   G  O   O                       %
10 %                     P      A   A  N   N   GGGG   OOO                        %
11 %                                                                             %
12 %                                                                             %
13 %                     Read Pango Markup Language Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 March 2012                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2021 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/script/license.php                               %
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 
39 /*
40   Include declarations.
41 */
42 #include "magick/studio.h"
43 #include "magick/annotate.h"
44 #include "magick/artifact.h"
45 #include "magick/blob.h"
46 #include "magick/blob-private.h"
47 #include "magick/composite-private.h"
48 #include "magick/draw.h"
49 #include "magick/draw-private.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/module.h"
58 #include "magick/monitor.h"
59 #include "magick/monitor-private.h"
60 #include "magick/option.h"
61 #include "magick/pixel-accessor.h"
62 #include "magick/property.h"
63 #include "magick/quantum-private.h"
64 #include "magick/static.h"
65 #include "magick/string_.h"
66 #include "magick/string-private.h"
67 #include "magick/token.h"
68 #include "magick/utility.h"
69 #if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
70 #include <pango/pango.h>
71 #include <pango/pangocairo.h>
72 #include <pango/pango-features.h>
73 #endif
74 
75 /*
76   Define declarations.
77 */
78 #define DefaultPANGODensity  96.0
79 
80 #if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
81 /*
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %                                                                             %
84 %                                                                             %
85 %                                                                             %
86 %   R e a d P A N G O I m a g e                                               %
87 %                                                                             %
88 %                                                                             %
89 %                                                                             %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %
92 %  ReadPANGOImage() reads an image in the Pango Markup Language Format.
93 %
94 %  The format of the ReadPANGOImage method is:
95 %
96 %      Image *ReadPANGOImage(const ImageInfo *image_info,
97 %        ExceptionInfo *exception)
98 %
99 %  A description of each parameter follows:
100 %
101 %    o image_info: the image info.
102 %
103 %    o exception: return any errors or warnings in this structure.
104 %
105 */
ReadPANGOImage(const ImageInfo * image_info,ExceptionInfo * exception)106 static Image *ReadPANGOImage(const ImageInfo *image_info,
107   ExceptionInfo *exception)
108 {
109   cairo_font_options_t
110     *font_options;
111 
112   cairo_surface_t
113     *surface;
114 
115   char
116     *caption,
117     *property;
118 
119   cairo_t
120     *cairo_image;
121 
122   const char
123     *option;
124 
125   DrawInfo
126     *draw_info;
127 
128   Image
129     *image;
130 
131   MagickBooleanType
132     status;
133 
134   MemoryInfo
135     *pixel_info;
136 
137   PangoAlignment
138     align;
139 
140   PangoContext
141     *context;
142 
143   PangoFontDescription
144     *description;
145 
146   PangoFontMap
147     *fontmap;
148 
149   PangoGravity
150     gravity;
151 
152   PangoLayout
153     *layout;
154 
155   PangoRectangle
156     extent;
157 
158   PixelPacket
159     fill_color;
160 
161   RectangleInfo
162     page;
163 
164   unsigned char
165     *p;
166 
167   size_t
168     stride;
169 
170   ssize_t
171     y;
172 
173   unsigned char
174     *pixels;
175 
176   /*
177     Initialize Image structure.
178   */
179   assert(image_info != (const ImageInfo *) NULL);
180   assert(image_info->signature == MagickCoreSignature);
181   if (image_info->debug != MagickFalse)
182     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
183       image_info->filename);
184   assert(exception != (ExceptionInfo *) NULL);
185   assert(exception->signature == MagickCoreSignature);
186   image=AcquireImage(image_info);
187   (void) ResetImagePage(image,"0x0+0+0");
188   if ((image->columns != 0) && (image->rows != 0))
189     (void) SetImageBackgroundColor(image);
190   /*
191     Format caption.
192   */
193   option=GetImageOption(image_info,"filename");
194   if (option == (const char *) NULL)
195     property=InterpretImageProperties(image_info,image,image_info->filename);
196   else
197     if (LocaleNCompare(option,"pango:",6) == 0)
198       property=InterpretImageProperties(image_info,image,option+6);
199     else
200       property=InterpretImageProperties(image_info,image,option);
201   if (property != (char *) NULL)
202     {
203       (void) SetImageProperty(image,"caption",property);
204       property=DestroyString(property);
205     }
206   caption=ConstantString(GetImageProperty(image,"caption"));
207   /*
208     Get context.
209   */
210   fontmap=pango_cairo_font_map_new();
211   pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(fontmap),
212     image->x_resolution == 0.0 ? DefaultPANGODensity : image->x_resolution);
213   font_options=cairo_font_options_create();
214   option=GetImageOption(image_info,"pango:hinting");
215   if (option != (const char *) NULL)
216     {
217       if (LocaleCompare(option,"none") != 0)
218         cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_NONE);
219       if (LocaleCompare(option,"full") != 0)
220         cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_FULL);
221     }
222   context=pango_font_map_create_context(fontmap);
223   pango_cairo_context_set_font_options(context,font_options);
224   cairo_font_options_destroy(font_options);
225   option=GetImageOption(image_info,"pango:language");
226   if (option != (const char *) NULL)
227     pango_context_set_language(context,pango_language_from_string(option));
228   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
229   pango_context_set_base_dir(context,draw_info->direction ==
230     RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
231   switch (draw_info->gravity)
232   {
233     case NorthGravity:
234     {
235       gravity=PANGO_GRAVITY_NORTH;
236       break;
237     }
238     case NorthWestGravity:
239     case WestGravity:
240     case SouthWestGravity:
241     {
242       gravity=PANGO_GRAVITY_WEST;
243       break;
244     }
245     case NorthEastGravity:
246     case EastGravity:
247     case SouthEastGravity:
248     {
249       gravity=PANGO_GRAVITY_EAST;
250       break;
251     }
252     case SouthGravity:
253     {
254       gravity=PANGO_GRAVITY_SOUTH;
255       break;
256     }
257     default:
258     {
259       gravity=PANGO_GRAVITY_AUTO;
260       break;
261     }
262   }
263   pango_context_set_base_gravity(context,gravity);
264   option=GetImageOption(image_info,"pango:gravity-hint");
265   if (option != (const char *) NULL)
266     {
267       if (LocaleCompare(option,"line") == 0)
268         pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE);
269       if (LocaleCompare(option,"natural") == 0)
270         pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL);
271       if (LocaleCompare(option,"strong") == 0)
272         pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG);
273     }
274   /*
275     Configure layout.
276   */
277   layout=pango_layout_new(context);
278   option=GetImageOption(image_info,"pango:auto-dir");
279   if (option != (const char *) NULL)
280     pango_layout_set_auto_dir(layout,1);
281   option=GetImageOption(image_info,"pango:ellipsize");
282   if (option != (const char *) NULL)
283     {
284       if (LocaleCompare(option,"end") == 0)
285         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END);
286       if (LocaleCompare(option,"middle") == 0)
287         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE);
288       if (LocaleCompare(option,"none") == 0)
289         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE);
290       if (LocaleCompare(option,"start") == 0)
291         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START);
292     }
293   option=GetImageOption(image_info,"pango:justify");
294   if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
295     pango_layout_set_justify(layout,1);
296   option=GetImageOption(image_info,"pango:single-paragraph");
297   if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse))
298     pango_layout_set_single_paragraph_mode(layout,1);
299   option=GetImageOption(image_info,"pango:wrap");
300   if (option != (const char *) NULL)
301     {
302       if (LocaleCompare(option,"char") == 0)
303         pango_layout_set_wrap(layout,PANGO_WRAP_CHAR);
304       if (LocaleCompare(option,"word") == 0)
305         pango_layout_set_wrap(layout,PANGO_WRAP_WORD);
306       if (LocaleCompare(option,"word-char") == 0)
307         pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR);
308     }
309   option=GetImageOption(image_info,"pango:indent");
310   if (option != (const char *) NULL)
311     pango_layout_set_indent(layout,(int) ((StringToLong(option)*
312       (image->x_resolution == 0.0 ? DefaultPANGODensity : image->x_resolution)*
313       PANGO_SCALE+DefaultPANGODensity/2)/DefaultPANGODensity+0.5));
314   switch (draw_info->align)
315   {
316     case CenterAlign: align=PANGO_ALIGN_CENTER; break;
317     case RightAlign: align=PANGO_ALIGN_RIGHT; break;
318     case LeftAlign: align=PANGO_ALIGN_LEFT; break;
319     default:
320     {
321       if (draw_info->gravity == CenterGravity)
322         {
323           align=PANGO_ALIGN_CENTER;
324           break;
325         }
326       align=PANGO_ALIGN_LEFT;
327       break;
328     }
329   }
330   if ((align != PANGO_ALIGN_CENTER) &&
331       (draw_info->direction == RightToLeftDirection))
332     align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align);
333   option=GetImageOption(image_info,"pango:align");
334   if (option != (const char *) NULL)
335     {
336       if (LocaleCompare(option,"center") == 0)
337         align=PANGO_ALIGN_CENTER;
338       if (LocaleCompare(option,"left") == 0)
339         align=PANGO_ALIGN_LEFT;
340       if (LocaleCompare(option,"right") == 0)
341         align=PANGO_ALIGN_RIGHT;
342     }
343   pango_layout_set_alignment(layout,align);
344   if (draw_info->font == (char *) NULL)
345     description=pango_font_description_new();
346   else
347     description=pango_font_description_from_string(draw_info->font);
348   pango_font_description_set_size(description,(int) (PANGO_SCALE*
349     draw_info->pointsize+0.5));
350   pango_layout_set_font_description(layout,description);
351   pango_font_description_free(description);
352   option=GetImageOption(image_info,"pango:markup");
353   if ((option != (const char *) NULL) && (IsMagickTrue(option) == MagickFalse))
354     pango_layout_set_text(layout,caption,-1);
355   else
356     {
357       GError
358         *error;
359 
360       error=(GError *) NULL;
361       if (pango_parse_markup(caption,-1,0,NULL,NULL,NULL,&error) == 0)
362         (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
363           error->message,"`%s'",image_info->filename);
364       pango_layout_set_markup(layout,caption,-1);
365     }
366   pango_layout_context_changed(layout);
367   page.x=0;
368   page.y=0;
369   if (image_info->page != (char *) NULL)
370     (void) ParseAbsoluteGeometry(image_info->page,&page);
371   if (image->columns == 0)
372     {
373       pango_layout_get_extents(layout,NULL,&extent);
374       image->columns=(extent.x+extent.width+PANGO_SCALE/2)/PANGO_SCALE+2*page.x;
375     }
376   else
377     {
378       image->columns-=2*page.x;
379       pango_layout_set_width(layout,(int) ((PANGO_SCALE*image->columns*
380         (image->x_resolution == 0.0 ? DefaultPANGODensity :
381         image->x_resolution)+DefaultPANGODensity/2)/DefaultPANGODensity+0.5));
382     }
383   if (image->rows == 0)
384     {
385       pango_layout_get_extents(layout,NULL,&extent);
386       image->rows=(extent.y+extent.height+PANGO_SCALE/2)/PANGO_SCALE+2*page.y;
387     }
388   else
389     {
390       image->rows-=2*page.y;
391       pango_layout_set_height(layout,(int) ((PANGO_SCALE*image->rows*
392         (image->y_resolution == 0.0 ? DefaultPANGODensity :
393         image->y_resolution)+DefaultPANGODensity/2)/DefaultPANGODensity+0.5));
394     }
395   status=SetImageExtent(image,image->columns,image->rows);
396   if (status == MagickFalse)
397     {
398       InheritException(exception,&image->exception);
399       return(DestroyImageList(image));
400     }
401   /*
402     Render markup.
403   */
404   stride=(size_t) cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,(int)
405     image->columns);
406   pixel_info=AcquireVirtualMemory(image->rows,stride*sizeof(*pixels));
407   if (pixel_info == (MemoryInfo *) NULL)
408     {
409       draw_info=DestroyDrawInfo(draw_info);
410       caption=DestroyString(caption);
411       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
412     }
413   pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
414   surface=cairo_image_surface_create_for_data(pixels,CAIRO_FORMAT_ARGB32,
415     (int) image->columns,(int) image->rows,(int) stride);
416   cairo_image=cairo_create(surface);
417   cairo_set_operator(cairo_image,CAIRO_OPERATOR_CLEAR);
418   cairo_paint(cairo_image);
419   cairo_set_operator(cairo_image,CAIRO_OPERATOR_OVER);
420   cairo_translate(cairo_image,page.x,page.y);
421   cairo_set_source_rgba(cairo_image,QuantumScale*draw_info->fill.red,
422     QuantumScale*draw_info->fill.green,QuantumScale*draw_info->fill.blue,
423     QuantumScale*(QuantumRange-draw_info->fill.opacity));
424   pango_cairo_show_layout(cairo_image,layout);
425   cairo_destroy(cairo_image);
426   cairo_surface_destroy(surface);
427   g_object_unref(layout);
428   g_object_unref(fontmap);
429   /*
430     Convert surface to image.
431   */
432   (void) SetImageBackgroundColor(image);
433   p=pixels;
434   for (y=0; y < (ssize_t) image->rows; y++)
435   {
436     PixelPacket
437       *q;
438 
439     ssize_t
440       x;
441 
442     q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
443     if (q == (PixelPacket *) NULL)
444       break;
445     for (x=0; x < (ssize_t) image->columns; x++)
446     {
447       double
448         gamma;
449 
450       fill_color.blue=ScaleCharToQuantum(*p++);
451       fill_color.green=ScaleCharToQuantum(*p++);
452       fill_color.red=ScaleCharToQuantum(*p++);
453       fill_color.opacity=QuantumRange-ScaleCharToQuantum(*p++);
454       /*
455         Disassociate alpha.
456       */
457       gamma=1.0-QuantumScale*fill_color.opacity;
458       gamma=PerceptibleReciprocal(gamma);
459       fill_color.blue*=gamma;
460       fill_color.green*=gamma;
461       fill_color.red*=gamma;
462       MagickCompositeOver(&fill_color,fill_color.opacity,q,(MagickRealType)
463         q->opacity,q);
464       q++;
465     }
466     if (SyncAuthenticPixels(image,exception) == MagickFalse)
467       break;
468     if (image->previous == (Image *) NULL)
469       {
470         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
471         image->rows);
472         if (status == MagickFalse)
473           break;
474       }
475   }
476   /*
477     Relinquish resources.
478   */
479   pixel_info=RelinquishVirtualMemory(pixel_info);
480   draw_info=DestroyDrawInfo(draw_info);
481   caption=DestroyString(caption);
482   return(GetFirstImageInList(image));
483 }
484 #endif
485 
486 /*
487 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 %                                                                             %
489 %                                                                             %
490 %                                                                             %
491 %   R e g i s t e r P A N G O I m a g e                                       %
492 %                                                                             %
493 %                                                                             %
494 %                                                                             %
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 %
497 %  RegisterPANGOImage() adds attributes for the Pango Markup Language format to
498 %  the list of supported formats.  The attributes include the image format
499 %  tag, a method to read and/or write the format, whether the format
500 %  supports the saving of more than one frame to the same file or blob,
501 %  whether the format supports native in-memory I/O, and a brief
502 %  description of the format.
503 %
504 %  The format of the RegisterPANGOImage method is:
505 %
506 %      size_t RegisterPANGOImage(void)
507 %
508 */
RegisterPANGOImage(void)509 ModuleExport size_t RegisterPANGOImage(void)
510 {
511   char
512     version[MaxTextExtent];
513 
514   MagickInfo
515     *entry;
516 
517   *version='\0';
518 #if defined(PANGO_VERSION_STRING)
519   (void) FormatLocaleString(version,MaxTextExtent,"Pangocairo %s",
520     PANGO_VERSION_STRING);
521 #endif
522   entry=SetMagickInfo("PANGO");
523 #if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
524   entry->decoder=(DecodeImageHandler *) ReadPANGOImage;
525 #endif
526   entry->description=ConstantString("Pango Markup Language");
527   if (*version != '\0')
528     entry->version=ConstantString(version);
529   entry->adjoin=MagickFalse;
530   entry->thread_support=MagickFalse;
531   entry->magick_module=ConstantString("PANGO");
532   (void) RegisterMagickInfo(entry);
533   return(MagickImageCoderSignature);
534 }
535 
536 /*
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538 %                                                                             %
539 %                                                                             %
540 %                                                                             %
541 %   U n r e g i s t e r P A N G O I m a g e                                   %
542 %                                                                             %
543 %                                                                             %
544 %                                                                             %
545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
546 %
547 %  UnregisterPANGOImage() removes format registrations made by the Pango module
548 %  from the list of supported formats.
549 %
550 %  The format of the UnregisterPANGOImage method is:
551 %
552 %      UnregisterPANGOImage(void)
553 %
554 */
UnregisterPANGOImage(void)555 ModuleExport void UnregisterPANGOImage(void)
556 {
557   (void) UnregisterMagickInfo("PANGO");
558 }
559