]> granicus.if.org Git - imagemagick/blobdiff - coders/caption.c
(no commit message)
[imagemagick] / coders / caption.c
index 7a3443fa9d414c32577b14aae3e48b7d7bf744a1..602839d7b3994bf32ccf1af6a0d595c6706c1a56 100644 (file)
 %                             Read Text Caption.                              %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                               February 2002                                 %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 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  %
@@ -41,6 +41,7 @@
 */
 #include "MagickCore/studio.h"
 #include "MagickCore/annotate.h"
+#include "MagickCore/artifact.h"
 #include "MagickCore/blob.h"
 #include "MagickCore/blob-private.h"
 #include "MagickCore/composite-private.h"
@@ -95,7 +96,8 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
   char
     *caption,
     geometry[MaxTextExtent],
-    *property;
+    *property,
+    *text;
 
   const char
     *gravity,
@@ -108,6 +110,7 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
     *image;
 
   MagickBooleanType
+    split,
     status;
 
   register ssize_t
@@ -131,21 +134,21 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
   image=AcquireImage(image_info,exception);
-  if (image->columns == 0)
-    ThrowReaderException(OptionError,"MustSpecifyImageSize");
   (void) ResetImagePage(image,"0x0+0+0");
   /*
     Format caption.
   */
   option=GetImageOption(image_info,"filename");
   if (option == (const char *) NULL)
-    property=InterpretImageProperties(image_info,image,image_info->filename,
-      exception);
+    property=InterpretImageProperties((ImageInfo *) image_info,image,
+      image_info->filename,exception);
   else
     if (LocaleNCompare(option,"caption:",8) == 0)
-      property=InterpretImageProperties(image_info,image,option+8,exception);
+      property=InterpretImageProperties((ImageInfo *) image_info,image,option+8,
+        exception);
     else
-      property=InterpretImageProperties(image_info,image,option,exception);
+      property=InterpretImageProperties((ImageInfo *) image_info,image,option,
+        exception);
   (void) SetImageProperty(image,"caption",property,exception);
   property=DestroyString(property);
   caption=ConstantString(GetImageProperty(image,"caption",exception));
@@ -155,12 +158,40 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
   if (gravity != (char *) NULL)
     draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
       MagickFalse,gravity);
-  if ((*caption != '\0') && (image->rows != 0) &&
-      (image_info->pointsize == 0.0))
+  split=MagickFalse;
+  if (image->columns == 0)
+    {
+      text=AcquireString(caption);
+      i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
+        exception);
+      (void) CloneString(&draw_info->text,text);
+      text=DestroyString(text);
+      (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
+        -metrics.bounds.x1,metrics.ascent);
+      if (draw_info->gravity == UndefinedGravity)
+        (void) CloneString(&draw_info->geometry,geometry);
+      status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
+      width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
+      image->columns=width;
+    }
+  if (image->rows == 0)
+    {
+      split=MagickTrue;
+      text=AcquireString(caption);
+      i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
+        exception);
+      (void) CloneString(&draw_info->text,text);
+      text=DestroyString(text);
+      (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
+        -metrics.bounds.x1,metrics.ascent);
+      if (draw_info->gravity == UndefinedGravity)
+        (void) CloneString(&draw_info->geometry,geometry);
+      status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
+      image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
+        draw_info->interline_spacing+draw_info->stroke_width)+0.5);
+    }
+  if (fabs(image_info->pointsize) < MagickEpsilon)
     {
-      char
-        *text;
-
       double
         high,
         low;
@@ -168,10 +199,10 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
       /*
         Auto fit text into bounding box.
       */
-      for ( ; ; )
+      for ( ; ; draw_info->pointsize*=2.0)
       {
         text=AcquireString(caption);
-        i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&text,
+        i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
           exception);
         (void) CloneString(&draw_info->text,text);
         text=DestroyString(text);
@@ -180,20 +211,24 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
         if (draw_info->gravity == UndefinedGravity)
           (void) CloneString(&draw_info->geometry,geometry);
         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
-        (void) status;
         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
-        if ((width > image->columns) && (height > image->rows))
-          break;
-        draw_info->pointsize*=2.0;
+        if ((image->columns != 0) && (image->rows != 0))
+          {
+            if ((width >= image->columns) && (height >= image->rows))
+              break;
+          }
+        else
+          if (((image->columns != 0) && (width >= image->columns)) ||
+              ((image->rows != 0) && (height >= image->rows)))
+            break;
       }
-      high=draw_info->pointsize/2.0;
-      low=high/2.0;
-      while ((high-low) > 1.0)
+      high=draw_info->pointsize;
+      for (low=1.0; (high-low) > 0.5; )
       {
         draw_info->pointsize=(low+high)/2.0;
         text=AcquireString(caption);
-        i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&text,
+        i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
           exception);
         (void) CloneString(&draw_info->text,text);
         text=DestroyString(text);
@@ -204,38 +239,24 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
-        if ((width <= image->columns) && (height <= image->rows))
-          low=draw_info->pointsize+1.0;
+        if ((image->columns != 0) && (image->rows != 0))
+          {
+            if ((width < image->columns) && (height < image->rows))
+              low=draw_info->pointsize+0.5;
+            else
+              high=draw_info->pointsize-0.5;
+          }
         else
-          high=draw_info->pointsize-1.0;
+          if (((image->columns != 0) && (width < image->columns)) ||
+              ((image->rows != 0) && (height < image->rows)))
+            low=draw_info->pointsize+0.5;
+          else
+            high=draw_info->pointsize-0.5;
       }
-      for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; )
-      {
-        text=AcquireString(caption);
-        i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&text,
-          exception);
-        (void) CloneString(&draw_info->text,text);
-        text=DestroyString(text);
-        (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
-          -metrics.bounds.x1,metrics.ascent);
-        if (draw_info->gravity == UndefinedGravity)
-          (void) CloneString(&draw_info->geometry,geometry);
-        status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
-        width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
-        height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
-        if ((width <= image->columns) && (height <= image->rows))
-          break;
-        draw_info->pointsize--;
-      }
-      draw_info->pointsize=floor(draw_info->pointsize);
+      draw_info->pointsize=(low+high)/2.0-0.5;
     }
-  i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception);
-  if (image->rows == 0)
-    image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
-      draw_info->interline_spacing+draw_info->stroke_width)+0.5);
-  if (image->rows == 0)
-    image->rows=(size_t) ((i+1)*draw_info->pointsize+
-      draw_info->interline_spacing+draw_info->stroke_width+0.5);
+  (void) CloneString(&draw_info->text,caption);
+  i=FormatMagickCaption(image,draw_info,split,&metrics,&caption,exception);
   if (SetImageBackgroundColor(image,exception) == MagickFalse)
     {
       image=DestroyImageList(image);