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 "MagickCore/studio.h"
43 #include "MagickCore/annotate.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/composite-private.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/draw-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/module.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/option.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/string-private.h"
67 #include "MagickCore/token.h"
68 #include "MagickCore/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 DefaultSVGDensity  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   PixelInfo
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,exception);
187   (void) ResetImagePage(image,"0x0+0+0");
188   if ((image->columns != 0) && (image->rows != 0))
189     (void) SetImageBackgroundColor(image,exception);
190   /*
191     Format caption.
192   */
193   option=GetImageOption(image_info,"filename");
194   if (option == (const char *) NULL)
195     property=InterpretImageProperties((ImageInfo *) image_info,image,
196       image_info->filename,exception);
197   else
198     if (LocaleNCompare(option,"pango:",6) == 0)
199       property=InterpretImageProperties((ImageInfo *) image_info,image,option+6,
200         exception);
201     else
202       property=InterpretImageProperties((ImageInfo *) image_info,image,option,
203         exception);
204   if (property != (char *) NULL)
205     {
206       (void) SetImageProperty(image,"caption",property,exception);
207       property=DestroyString(property);
208     }
209   caption=ConstantString(GetImageProperty(image,"caption",exception));
210   /*
211     Get context.
212   */
213   fontmap=pango_cairo_font_map_new();
214   pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(fontmap),
215     image->resolution.x == 0.0 ? DefaultSVGDensity : image->resolution.x);
216   font_options=cairo_font_options_create();
217   option=GetImageOption(image_info,"pango:hinting");
218   if (option != (const char *) NULL)
219     {
220       if (LocaleCompare(option,"none") != 0)
221         cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_NONE);
222       if (LocaleCompare(option,"full") != 0)
223         cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_FULL);
224     }
225   context=pango_font_map_create_context(fontmap);
226   pango_cairo_context_set_font_options(context,font_options);
227   cairo_font_options_destroy(font_options);
228   option=GetImageOption(image_info,"pango:language");
229   if (option != (const char *) NULL)
230     pango_context_set_language(context,pango_language_from_string(option));
231   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
232   pango_context_set_base_dir(context,draw_info->direction ==
233     RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
234   switch (draw_info->gravity)
235   {
236     case NorthGravity:
237     {
238       gravity=PANGO_GRAVITY_NORTH;
239       break;
240     }
241     case NorthWestGravity:
242     case WestGravity:
243     case SouthWestGravity:
244     {
245       gravity=PANGO_GRAVITY_WEST;
246       break;
247     }
248     case NorthEastGravity:
249     case EastGravity:
250     case SouthEastGravity:
251     {
252       gravity=PANGO_GRAVITY_EAST;
253       break;
254     }
255     case SouthGravity:
256     {
257       gravity=PANGO_GRAVITY_SOUTH;
258       break;
259     }
260     default:
261     {
262       gravity=PANGO_GRAVITY_AUTO;
263       break;
264     }
265   }
266   pango_context_set_base_gravity(context,gravity);
267   option=GetImageOption(image_info,"pango:gravity-hint");
268   if (option != (const char *) NULL)
269     {
270       if (LocaleCompare(option,"line") == 0)
271         pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE);
272       if (LocaleCompare(option,"natural") == 0)
273         pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL);
274       if (LocaleCompare(option,"strong") == 0)
275         pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG);
276     }
277   /*
278     Configure layout.
279   */
280   layout=pango_layout_new(context);
281   option=GetImageOption(image_info,"pango:auto-dir");
282   if (option != (const char *) NULL)
283     pango_layout_set_auto_dir(layout,1);
284   option=GetImageOption(image_info,"pango:ellipsize");
285   if (option != (const char *) NULL)
286     {
287       if (LocaleCompare(option,"end") == 0)
288         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END);
289       if (LocaleCompare(option,"middle") == 0)
290         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE);
291       if (LocaleCompare(option,"none") == 0)
292         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE);
293       if (LocaleCompare(option,"start") == 0)
294         pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START);
295     }
296   option=GetImageOption(image_info,"pango:justify");
297   if (IsStringTrue(option) != MagickFalse)
298     pango_layout_set_justify(layout,1);
299   option=GetImageOption(image_info,"pango:single-paragraph");
300   if (IsStringTrue(option) != MagickFalse)
301     pango_layout_set_single_paragraph_mode(layout,1);
302   option=GetImageOption(image_info,"pango:wrap");
303   if (option != (const char *) NULL)
304     {
305       if (LocaleCompare(option,"char") == 0)
306         pango_layout_set_wrap(layout,PANGO_WRAP_CHAR);
307       if (LocaleCompare(option,"word") == 0)
308         pango_layout_set_wrap(layout,PANGO_WRAP_WORD);
309       if (LocaleCompare(option,"word-char") == 0)
310         pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR);
311     }
312   option=GetImageOption(image_info,"pango:indent");
313   if (option != (const char *) NULL)
314     pango_layout_set_indent(layout,(int) ((StringToLong(option)*
315       (image->resolution.x == 0.0 ? DefaultSVGDensity : image->resolution.x)*
316       PANGO_SCALE+DefaultSVGDensity/2)/DefaultSVGDensity+0.5));
317   switch (draw_info->align)
318   {
319     case CenterAlign: align=PANGO_ALIGN_CENTER; break;
320     case RightAlign: align=PANGO_ALIGN_RIGHT; break;
321     case LeftAlign: align=PANGO_ALIGN_LEFT; break;
322     default:
323     {
324       if (draw_info->gravity == CenterGravity)
325         {
326           align=PANGO_ALIGN_CENTER;
327           break;
328         }
329       align=PANGO_ALIGN_LEFT;
330       break;
331     }
332   }
333   if ((align != PANGO_ALIGN_CENTER) &&
334       (draw_info->direction == RightToLeftDirection))
335     align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align);
336   option=GetImageOption(image_info,"pango:align");
337   if (option != (const char *) NULL)
338     {
339       if (LocaleCompare(option,"center") == 0)
340         align=PANGO_ALIGN_CENTER;
341       if (LocaleCompare(option,"left") == 0)
342         align=PANGO_ALIGN_LEFT;
343       if (LocaleCompare(option,"right") == 0)
344         align=PANGO_ALIGN_RIGHT;
345     }
346   pango_layout_set_alignment(layout,align);
347   if (draw_info->font == (char *) NULL)
348     description=pango_font_description_new();
349   else
350     description=pango_font_description_from_string(draw_info->font);
351   pango_font_description_set_size(description,(int) (PANGO_SCALE*
352     draw_info->pointsize+0.5));
353   pango_layout_set_font_description(layout,description);
354   pango_font_description_free(description);
355   option=GetImageOption(image_info,"pango:markup");
356   if ((option != (const char *) NULL) && (IsStringTrue(option) == MagickFalse))
357     pango_layout_set_text(layout,caption,-1);
358   else
359     {
360       GError
361         *error;
362 
363       error=(GError *) NULL;
364       if (pango_parse_markup(caption,-1,0,NULL,NULL,NULL,&error) == 0)
365         (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
366           error->message,"`%s'",image_info->filename);
367       pango_layout_set_markup(layout,caption,-1);
368     }
369   pango_layout_context_changed(layout);
370   page.x=0;
371   page.y=0;
372   if (image_info->page != (char *) NULL)
373     (void) ParseAbsoluteGeometry(image_info->page,&page);
374   if (image->columns == 0)
375     {
376       pango_layout_get_extents(layout,NULL,&extent);
377       image->columns=(extent.x+extent.width+PANGO_SCALE/2)/PANGO_SCALE+2*page.x;
378     }
379   else
380     {
381       image->columns-=2*page.x;
382       pango_layout_set_width(layout,(int) ((PANGO_SCALE*image->columns*
383         (image->resolution.x == 0.0 ? DefaultSVGDensity : image->resolution.x)+
384         DefaultSVGDensity/2)/DefaultSVGDensity+0.5));
385     }
386   if (image->rows == 0)
387     {
388       pango_layout_get_extents(layout,NULL,&extent);
389       image->rows=(extent.y+extent.height+PANGO_SCALE/2)/PANGO_SCALE+2*page.y;
390     }
391   else
392     {
393       image->rows-=2*page.y;
394       pango_layout_set_height(layout,(int) ((PANGO_SCALE*image->rows*
395         (image->resolution.y == 0.0 ? DefaultSVGDensity : image->resolution.y)+
396         DefaultSVGDensity/2)/DefaultSVGDensity+0.5));
397     }
398   status=SetImageExtent(image,image->columns,image->rows,exception);
399   if (status == MagickFalse)
400     return(DestroyImageList(image));
401   /*
402     Render markup.
403   */
404   stride=(size_t) cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
405     (int) 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*draw_info->fill.alpha);
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,exception);
433   p=pixels;
434   GetPixelInfo(image,&fill_color);
435   for (y=0; y < (ssize_t) image->rows; y++)
436   {
437     Quantum
438       *q;
439 
440     ssize_t
441       x;
442 
443     q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
444     if (q == (Quantum *) NULL)
445       break;
446     for (x=0; x < (ssize_t) image->columns; x++)
447     {
448       double
449         gamma;
450 
451       fill_color.blue=(double) ScaleCharToQuantum(*p++);
452       fill_color.green=(double) ScaleCharToQuantum(*p++);
453       fill_color.red=(double) ScaleCharToQuantum(*p++);
454       fill_color.alpha=(double) ScaleCharToQuantum(*p++);
455       /*
456         Disassociate alpha.
457       */
458       gamma=QuantumScale*fill_color.alpha;
459       gamma=PerceptibleReciprocal(gamma);
460       fill_color.blue*=gamma;
461       fill_color.green*=gamma;
462       fill_color.red*=gamma;
463       CompositePixelOver(image,&fill_color,fill_color.alpha,q,(double)
464         GetPixelAlpha(image,q),q);
465       q+=GetPixelChannels(image);
466     }
467     if (SyncAuthenticPixels(image,exception) == MagickFalse)
468       break;
469     if (image->previous == (Image *) NULL)
470       {
471         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
472         image->rows);
473         if (status == MagickFalse)
474           break;
475       }
476   }
477   /*
478     Relinquish resources.
479   */
480   pixel_info=RelinquishVirtualMemory(pixel_info);
481   draw_info=DestroyDrawInfo(draw_info);
482   caption=DestroyString(caption);
483   return(GetFirstImageInList(image));
484 }
485 #endif
486 
487 /*
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
489 %                                                                             %
490 %                                                                             %
491 %                                                                             %
492 %   R e g i s t e r P A N G O I m a g e                                       %
493 %                                                                             %
494 %                                                                             %
495 %                                                                             %
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497 %
498 %  RegisterPANGOImage() adds attributes for the Pango Markup Language format to
499 %  the list of supported formats.  The attributes include the image format
500 %  tag, a method to read and/or write the format, whether the format
501 %  supports the saving of more than one frame to the same file or blob,
502 %  whether the format supports native in-memory I/O, and a brief
503 %  description of the format.
504 %
505 %  The format of the RegisterPANGOImage method is:
506 %
507 %      size_t RegisterPANGOImage(void)
508 %
509 */
RegisterPANGOImage(void)510 ModuleExport size_t RegisterPANGOImage(void)
511 {
512   char
513     version[MagickPathExtent];
514 
515   MagickInfo
516     *entry;
517 
518   *version='\0';
519 #if defined(PANGO_VERSION_STRING)
520   (void) FormatLocaleString(version,MagickPathExtent,"Pangocairo %s",
521     PANGO_VERSION_STRING);
522 #endif
523   entry=AcquireMagickInfo("PANGO","PANGO","Pango Markup Language");
524 #if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
525   entry->decoder=(DecodeImageHandler *) ReadPANGOImage;
526 #endif
527   if (*version != '\0')
528     entry->version=ConstantString(version);
529   entry->flags^=CoderAdjoinFlag;
530   entry->flags^=CoderDecoderThreadSupportFlag;
531   (void) RegisterMagickInfo(entry);
532   return(MagickImageCoderSignature);
533 }
534 
535 /*
536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 %                                                                             %
538 %                                                                             %
539 %                                                                             %
540 %   U n r e g i s t e r P A N G O I m a g e                                   %
541 %                                                                             %
542 %                                                                             %
543 %                                                                             %
544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545 %
546 %  UnregisterPANGOImage() removes format registrations made by the Pango module
547 %  from the list of supported formats.
548 %
549 %  The format of the UnregisterPANGOImage method is:
550 %
551 %      UnregisterPANGOImage(void)
552 %
553 */
UnregisterPANGOImage(void)554 ModuleExport void UnregisterPANGOImage(void)
555 {
556   (void) UnregisterMagickInfo("PANGO");
557 }
558