% 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 %
*/
#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"
char
*caption,
geometry[MaxTextExtent],
- *property;
+ *property,
+ *text;
const char
*gravity,
*image;
MagickBooleanType
+ split,
status;
register ssize_t
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));
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;
/*
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);
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);
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);