]> granicus.if.org Git - imagemagick/blobdiff - coders/pango.c
(no commit message)
[imagemagick] / coders / pango.c
index 8ba9dc7bce20111a9dfd0d8014922b5c49617e3a..5d6221e40f6b55b88b506a460569257a47ef0d79 100644 (file)
@@ -17,7 +17,7 @@
 %                                 March 2012                                  %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 #include "MagickCore/image-private.h"
 #include "MagickCore/list.h"
 #include "MagickCore/magick.h"
-#include "MagickCore/module.h"
 #include "MagickCore/memory_.h"
+#include "MagickCore/module.h"
+#include "MagickCore/monitor.h"
+#include "MagickCore/monitor-private.h"
 #include "MagickCore/option.h"
 #include "MagickCore/pixel-accessor.h"
 #include "MagickCore/property.h"
 #include "MagickCore/quantum-private.h"
 #include "MagickCore/static.h"
 #include "MagickCore/string_.h"
-#if defined(MAGICKCORE_PANGOFT2_DELEGATE)
+#include "MagickCore/string-private.h"
+#include "MagickCore/token.h"
+#include "MagickCore/utility.h"
+#if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
 #include <pango/pango.h>
-#include <pango/pangoft2.h>
+#include <pango/pangocairo.h>
+#include <pango/pango-features.h>
 #endif
 \f
-#if defined(MAGICKCORE_PANGOFT2_DELEGATE)
+#if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
 static Image *ReadPANGOImage(const ImageInfo *image_info,
   ExceptionInfo *exception)
 {
+  cairo_font_options_t
+    *font_options;
+
+  cairo_surface_t
+    *surface;
+
   char
     *caption,
     *property;
 
+  cairo_t
+    *cairo_image;
+
+  const char
+    *option;
+
   DrawInfo
     *draw_info;
 
-  FT_Bitmap
-    *canvas;
-
   Image
     *image;
 
   MagickBooleanType
     status;
 
+  PangoAlignment
+    align;
+
   PangoContext
     *context;
 
-  PangoFontDescription
-    *description;
-
   PangoFontMap
     *fontmap;
 
+  PangoGravity
+    gravity;
+
   PangoLayout
     *layout;
 
+  PangoRectangle
+    extent;
+
   PixelInfo
     fill_color;
 
-  register Quantum
-    *q;
+  RectangleInfo
+    page;
 
   register unsigned char
     *p;
 
+  size_t
+    stride;
+
   ssize_t
     y;
 
+  unsigned char
+    *pixels;
+
   /*
     Initialize Image structure.
   */
@@ -145,66 +172,239 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
   image=AcquireImage(image_info,exception);
-  if ((image->columns == 0) || (image->rows == 0))
-    ThrowReaderException(OptionError,"MustSpecifyImageSize");
   (void) ResetImagePage(image,"0x0+0+0");
-  if (SetImageBackgroundColor(image,exception) == MagickFalse)
-    {
-      image=DestroyImageList(image);
-      return((Image *) NULL);
-    }
   /*
-    Get context.
+    Format caption.
   */
-  fontmap=(PangoFontMap *) pango_ft2_font_map_new();
-  pango_ft2_font_map_set_resolution((PangoFT2FontMap *) fontmap,
-    image->x_resolution,image->y_resolution);
-  pango_ft2_font_map_set_default_substitute((PangoFT2FontMap *) fontmap,NULL,
-    NULL,NULL);
-  context=pango_font_map_create_context(fontmap);
+  option=GetImageOption(image_info,"filename");
+  if (option == (const char *) NULL)
+    property=InterpretImageProperties(image_info,image,image_info->filename,
+      exception);
+  else
+    if (LocaleNCompare(option,"pango:",6) == 0)
+      property=InterpretImageProperties(image_info,image,option+6,exception);
+    else
+      property=InterpretImageProperties(image_info,image,option,exception);
+  (void) SetImageProperty(image,"caption",property,exception);
+  property=DestroyString(property);
+  caption=ConstantString(GetImageProperty(image,"caption",exception));
   /*
-    Create canvas.
+    Get context.
   */
-  canvas=(FT_Bitmap *) AcquireMagickMemory(sizeof(*canvas));
-  if (canvas == (FT_Bitmap *) NULL)
-    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-  canvas->width=image->columns;
-  canvas->pitch=(canvas->width+3) & ~3;
-  canvas->rows=image->rows;
-  canvas->buffer=(unsigned char *) AcquireQuantumMemory(canvas->pitch,
-    canvas->rows*sizeof(*canvas->buffer));
-  if (canvas->buffer == (unsigned char *) NULL)
+  fontmap=pango_cairo_font_map_new();
+  pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(fontmap),
+    image->resolution.x);
+  font_options=cairo_font_options_create();
+  option=GetImageOption(image_info,"pango:hinting");
+  if (option != (const char *) NULL)
     {
-      canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
-      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      if (LocaleCompare(option,"none") != 0)
+        cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_NONE);
+      if (LocaleCompare(option,"full") != 0)
+        cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_FULL);
+    }
+  context=pango_font_map_create_context(fontmap);
+  pango_cairo_context_set_font_options(context,font_options);
+  cairo_font_options_destroy(font_options);
+  option=GetImageOption(image_info,"pango:language");
+  if (option != (const char *) NULL)
+    pango_context_set_language(context,pango_language_from_string(option));
+  draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
+  pango_context_set_base_dir(context,draw_info->direction ==
+    RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
+  switch (draw_info->gravity)
+  {
+    case NorthGravity:
+    {
+      gravity=PANGO_GRAVITY_NORTH;
+      break;
+    }
+    case NorthWestGravity:
+    case WestGravity:
+    case SouthWestGravity:
+    {
+      gravity=PANGO_GRAVITY_WEST;
+      break;
+    }
+    case NorthEastGravity:
+    case EastGravity:
+    case SouthEastGravity:
+    {
+      gravity=PANGO_GRAVITY_EAST;
+      break;
+    }
+    case SouthGravity:
+    {
+      gravity=PANGO_GRAVITY_SOUTH;
+      break;
+    }
+    default:
+    {
+      gravity=PANGO_GRAVITY_AUTO;
+      break;
+    }
+  }
+  pango_context_set_base_gravity(context,gravity);
+  option=GetImageOption(image_info,"pango:gravity-hint");
+  if (option != (const char *) NULL)
+    {
+      if (LocaleCompare(option,"line") == 0)
+        pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE);
+      if (LocaleCompare(option,"natural") == 0)
+        pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL);
+      if (LocaleCompare(option,"strong") == 0)
+        pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG);
     }
-  canvas->num_grays=256;
-  canvas->pixel_mode=ft_pixel_mode_grays;
-  ResetMagickMemory(canvas->buffer,0x00,canvas->pitch*canvas->rows);
   /*
-    Render caption.
+    Configure layout.
   */
   layout=pango_layout_new(context);
-  description=pango_font_description_from_string("Arial,20");
-  pango_layout_set_font_description(layout,description);
-  pango_font_description_free(description);
-  property=InterpretImageProperties(image_info,image,image_info->filename,
-    exception);
-  (void) SetImageProperty(image,"caption",property,exception);
-  property=DestroyString(property);
-  caption=ConstantString(GetImageProperty(image,"caption",exception));
-  pango_layout_set_text(layout,caption,-1);
+  option=GetImageOption(image_info,"pango:auto-dir");
+  if (option != (const char *) NULL)
+    pango_layout_set_auto_dir(layout,1);
+  option=GetImageOption(image_info,"pango:ellipsize");
+  if (option != (const char *) NULL)
+    {
+      if (LocaleCompare(option,"end") == 0)
+        pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END);
+      if (LocaleCompare(option,"middle") == 0)
+        pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE);
+      if (LocaleCompare(option,"none") == 0)
+        pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE);
+      if (LocaleCompare(option,"start") == 0)
+        pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START);
+    }
+  option=GetImageOption(image_info,"pango:justify");
+  if (IfMagickTrue(IsStringTrue(option)))
+    pango_layout_set_justify(layout,1);
+  option=GetImageOption(image_info,"pango:single-paragraph");
+  if (IfMagickTrue(IsStringTrue(option)))
+    pango_layout_set_single_paragraph_mode(layout,1);
+  option=GetImageOption(image_info,"pango:wrap");
+  if (option != (const char *) NULL)
+    {
+      if (LocaleCompare(option,"char") == 0)
+        pango_layout_set_wrap(layout,PANGO_WRAP_CHAR);
+      if (LocaleCompare(option,"word") == 0)
+        pango_layout_set_wrap(layout,PANGO_WRAP_WORD);
+      if (LocaleCompare(option,"word-char") == 0)
+        pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR);
+    }
+  option=GetImageOption(image_info,"pango:indent");
+  if (option != (const char *) NULL)
+    pango_layout_set_indent(layout,(int) ((StringToLong(option)*
+      image->resolution.x*PANGO_SCALE+36)/72.0+0.5));
+  switch (draw_info->align)
+  {
+    case CenterAlign: align=PANGO_ALIGN_CENTER; break;
+    case RightAlign: align=PANGO_ALIGN_RIGHT; break;
+    case LeftAlign: align=PANGO_ALIGN_LEFT; break;
+    default:
+    {
+      if (draw_info->gravity == CenterGravity)
+        {
+          align=PANGO_ALIGN_CENTER;
+          break;
+        }
+      align=PANGO_ALIGN_LEFT;
+      break;
+    }
+  }
+  if ((align != PANGO_ALIGN_CENTER) &&
+      (draw_info->direction == RightToLeftDirection))
+    align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align);
+  pango_layout_set_alignment(layout,align);
+  if (draw_info->font != (char *) NULL)
+    {
+      PangoFontDescription
+        *description;
+
+      /*
+        Set font.
+      */
+      description=pango_font_description_from_string(draw_info->font);
+      pango_font_description_set_size(description,(int) (PANGO_SCALE*
+        draw_info->pointsize+0.5));
+      pango_layout_set_font_description(layout,description);
+      pango_font_description_free(description);
+    }
+  option=GetImageOption(image_info,"pango:markup");
+  if ((option != (const char *) NULL) && (IsStringTrue(option) == MagickFalse))
+    pango_layout_set_text(layout,caption,-1);
+  else
+    {
+      GError
+        *error;
+
+      error=(GError *) NULL;
+      if (pango_parse_markup(caption,-1,0,NULL,NULL,NULL,&error) == 0)
+        (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
+          error->message,"`%s'",image_info->filename);
+      pango_layout_set_markup(layout,caption,-1);
+    }
   pango_layout_context_changed(layout);
-  /* wrapping: pango_layout_set_width(layout,72*image->columns); */
-  pango_ft2_render_layout(canvas,layout,0,0);
+  page.x=0;
+  page.y=0;
+  if (image_info->page != (char *) NULL)
+    (void) ParseAbsoluteGeometry(image_info->page,&page);
+  if (image->columns == 0)
+    {
+      pango_layout_get_pixel_extents(layout,NULL,&extent);
+      image->columns=extent.x+extent.width+2*page.x;
+    }
+  else
+    {
+      image->columns-=2*page.x;
+      pango_layout_set_width(layout,(int) ((PANGO_SCALE*image->columns*
+        image->resolution.x+36.0)/72.0+0.5));
+    }
+  if (image->rows == 0)
+    {
+      pango_layout_get_pixel_extents(layout,NULL,&extent);
+      image->rows=extent.y+extent.height+2*page.y;
+    }
+  else
+    {
+      image->rows-=2*page.y;
+      pango_layout_set_height(layout,(int) ((PANGO_SCALE*image->rows*
+        image->resolution.y+36.0)/72.0+0.5));
+    }
   /*
-    Convert caption to image.
+    Render markup.
   */
-  draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
+  stride=(size_t) cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
+    image->columns);
+  pixels=(unsigned char *) AcquireQuantumMemory(image->rows,stride*
+    sizeof(*pixels));
+  if (pixels == (unsigned char *) NULL)
+    {
+      draw_info=DestroyDrawInfo(draw_info);
+      caption=DestroyString(caption);
+      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+    }
+  surface=cairo_image_surface_create_for_data(pixels,CAIRO_FORMAT_ARGB32,
+    image->columns,image->rows,stride);
+  cairo_image=cairo_create(surface);
+  cairo_set_operator(cairo_image,CAIRO_OPERATOR_CLEAR);
+  cairo_paint(cairo_image);
+  cairo_set_operator(cairo_image,CAIRO_OPERATOR_OVER);
+  cairo_translate(cairo_image,page.x,page.y);
+  pango_cairo_show_layout(cairo_image,layout);
+  cairo_destroy(cairo_image);
+  cairo_surface_destroy(surface);
+  g_object_unref(layout);
+  g_object_unref(fontmap);
+  /*
+    Convert surface to image.
+  */
+  (void) SetImageBackgroundColor(image,exception);
+  p=pixels;
   GetPixelInfo(image,&fill_color);
-  p=canvas->buffer;
   for (y=0; y < (ssize_t) image->rows; y++)
   {
+    register Quantum
+      *q;
+
     register ssize_t
       x;
 
@@ -213,25 +413,40 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
       break;
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      MagickRealType
-        fill_opacity;
+      double
+        gamma;
 
-      (void) GetFillColor(draw_info,x,y,&fill_color,exception);
-      fill_opacity=(*p)*fill_color.alpha/canvas->num_grays;
-      if (draw_info->text_antialias == MagickFalse)
-        fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
-      CompositePixelOver(image,&fill_color,fill_opacity,q,
-         GetPixelAlpha(image,q),q);
-      p++;
+      fill_color.blue=(MagickRealType) ScaleCharToQuantum(*p++);
+      fill_color.green=(MagickRealType) ScaleCharToQuantum(*p++);
+      fill_color.red=(MagickRealType) ScaleCharToQuantum(*p++);
+      fill_color.alpha=(MagickRealType) ScaleCharToQuantum(*p++);
+      /*
+        Disassociate alpha.
+      */
+      gamma=1.0-QuantumScale*fill_color.alpha;
+      gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
+      fill_color.blue*=gamma;
+      fill_color.green*=gamma;
+      fill_color.red*=gamma;
+      CompositePixelOver(image,&fill_color,fill_color.alpha,q,(MagickRealType)
+        GetPixelAlpha(image,q),q);
       q+=GetPixelChannels(image);
     }
+    if (SyncAuthenticPixels(image,exception) == MagickFalse)
+      break;
+    if (image->previous == (Image *) NULL)
+      {
+        status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+        image->rows);
+        if (status == MagickFalse)
+          break;
+      }
   }
   /*
     Relinquish resources.
   */
+  pixels=(unsigned char *) RelinquishMagickMemory(pixels);
   draw_info=DestroyDrawInfo(draw_info);
-  canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer);
-  canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
   caption=DestroyString(caption);
   return(GetFirstImageInList(image));
 }
@@ -262,14 +477,24 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
 */
 ModuleExport size_t RegisterPANGOImage(void)
 {
+  char
+    version[MaxTextExtent];
+
   MagickInfo
     *entry;
 
+  *version='\0';
+#if defined(PANGO_VERSION_STRING)
+  (void) FormatLocaleString(version,MaxTextExtent,"Pangocairo %s",
+    PANGO_VERSION_STRING);
+#endif
   entry=SetMagickInfo("PANGO");
-#if defined(MAGICKCORE_PANGOFT2_DELEGATE)
+#if defined(MAGICKCORE_PANGOCAIRO_DELEGATE)
   entry->decoder=(DecodeImageHandler *) ReadPANGOImage;
 #endif
   entry->description=ConstantString("Pango Markup Language");
+  if (*version != '\0')
+    entry->version=ConstantString(version);
   entry->adjoin=MagickFalse;
   entry->module=ConstantString("PANGO");
   (void) RegisterMagickInfo(entry);