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