caption.c revision 092ec8d083fedaedfb7792995e7ea42164553cff
13ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
23ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
43ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
53ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
63ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%               CCCC   AAA   PPPP   TTTTT  IIIII   OOO   N   N                %
73ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%              C      A   A  P   P    T      I    O   O  NN  N                %
83ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%              C      AAAAA  PPPP     T      I    O   O  N N N                %
93ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%              C      A   A  P        T      I    O   O  N  NN                %
103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%               CCCC  A   A  P        T    IIIII   OOO   N   N                %
113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                             Read Text Caption.                              %
143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              Software Design                                %
163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                John Cristy                                  %
173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                               February 2002                                 %
183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2045ef08fd6a09813e4a8f5ddadf85ba9e0ec2cdc7cristy%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  dedicated to making software imaging solutions freely available.           %
223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  You may not use this file except in compliance with the License.  You may  %
243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  obtain a copy of the License at                                            %
253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    http://www.imagemagick.org/script/license.php                            %
273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Unless required by applicable law or agreed to in writing, software        %
293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  distributed under the License is distributed on an "AS IS" BASIS,          %
303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  See the License for the specific language governing permissions and        %
323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  limitations under the License.                                             %
333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Include declarations.
413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
424c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/studio.h"
434c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/annotate.h"
4476ce6e193e5a305875173a744bbd59d27ddb3148cristy#include "MagickCore/artifact.h"
454c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob.h"
464c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob-private.h"
478a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy#include "MagickCore/composite-private.h"
484c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/draw.h"
498a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy#include "MagickCore/draw-private.h"
504c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception.h"
514c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception-private.h"
524c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image.h"
534c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image-private.h"
544c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/list.h"
554c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/magick.h"
564c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/memory_.h"
578a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy#include "MagickCore/module.h"
584c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/option.h"
594c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/property.h"
604c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/quantum-private.h"
614c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/static.h"
624c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/string_.h"
638a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy#include "MagickCore/string-private.h"
648a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy#include "MagickCore/utility.h"
653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e a d C A P T I O N I m a g e                                           %
723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  ReadCAPTIONImage() reads a CAPTION image file and returns it.  It
783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  allocates the memory necessary for the new Image structure and returns a
793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  pointer to the new image.
803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the ReadCAPTIONImage method is:
823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      Image *ReadCAPTIONImage(const ImageInfo *image_info,
843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%        ExceptionInfo *exception)
853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  A description of each parameter follows:
873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image_info: the image info.
893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o exception: return any errors or warnings in this structure.
913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
933ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic Image *ReadCAPTIONImage(const ImageInfo *image_info,
943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ExceptionInfo *exception)
953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  char
973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *caption,
983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    geometry[MaxTextExtent],
9954ec42737fdec7527afa06ca95be82c8ca0bd866cristy    *property,
10054ec42737fdec7527afa06ca95be82c8ca0bd866cristy    *text;
1013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  const char
103ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    *gravity,
104ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    *option;
1053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  DrawInfo
1073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *draw_info;
1083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Image
1103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *image;
1113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickBooleanType
1133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    status;
1143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
115bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  register ssize_t
1163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i;
1173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
118bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  size_t
1193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    height,
1203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    width;
1213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1224e82e51d7ebce7b4ef0f808d906124dd6f812248cristy  TypeMetric
1234e82e51d7ebce7b4ef0f808d906124dd6f812248cristy    metrics;
1244e82e51d7ebce7b4ef0f808d906124dd6f812248cristy
1253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
1263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Initialize Image structure.
1273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
1283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image_info != (const ImageInfo *) NULL);
1293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image_info->signature == MagickSignature);
1303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (image_info->debug != MagickFalse)
1313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      image_info->filename);
1333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(exception != (ExceptionInfo *) NULL);
1343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(exception->signature == MagickSignature);
1359950d57e1124b73f684fb5946e206994cefda628cristy  image=AcquireImage(image_info,exception);
1363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) ResetImagePage(image,"0x0+0+0");
1373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
1383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Format caption.
1393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
140092ec8d083fedaedfb7792995e7ea42164553cffcristy  option=GetImageOption(image_info,"filename");
141ab272ac4f115daf25c032de9dbb49a399eebe49bcristy  if (option == (const char *) NULL)
142ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    property=InterpretImageProperties(image_info,image,image_info->filename,
143ab272ac4f115daf25c032de9dbb49a399eebe49bcristy      exception);
144ab272ac4f115daf25c032de9dbb49a399eebe49bcristy  else
145ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    if (LocaleNCompare(option,"caption:",8) == 0)
146ab272ac4f115daf25c032de9dbb49a399eebe49bcristy      property=InterpretImageProperties(image_info,image,option+8,exception);
147ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    else
148ab272ac4f115daf25c032de9dbb49a399eebe49bcristy      property=InterpretImageProperties(image_info,image,option,exception);
149d15e65928aec551b7388c2863de3e3e628e2e0ddcristy  (void) SetImageProperty(image,"caption",property,exception);
1503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  property=DestroyString(property);
151d15e65928aec551b7388c2863de3e3e628e2e0ddcristy  caption=ConstantString(GetImageProperty(image,"caption",exception));
1523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
1536fc392931fb03ddff4853716af6d51e329f59696cristy  (void) CloneString(&draw_info->text,caption);
154092ec8d083fedaedfb7792995e7ea42164553cffcristy  gravity=GetImageOption(image_info,"gravity");
1553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (gravity != (char *) NULL)
156042ee78fa9004bf1ac6a95f09d9d1faca631dda1cristy    draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
1573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      MagickFalse,gravity);
1588db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy  if (image->columns == 0)
1598db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    {
160e275ada1a7f4f90e73328c35e9f02464bafed8becristy      text=AcquireString(caption);
161e275ada1a7f4f90e73328c35e9f02464bafed8becristy      i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
162e275ada1a7f4f90e73328c35e9f02464bafed8becristy        exception);
163e275ada1a7f4f90e73328c35e9f02464bafed8becristy      (void) CloneString(&draw_info->text,text);
164e275ada1a7f4f90e73328c35e9f02464bafed8becristy      text=DestroyString(text);
165e275ada1a7f4f90e73328c35e9f02464bafed8becristy      (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
166e275ada1a7f4f90e73328c35e9f02464bafed8becristy        -metrics.bounds.x1,metrics.ascent);
167e275ada1a7f4f90e73328c35e9f02464bafed8becristy      if (draw_info->gravity == UndefinedGravity)
168e275ada1a7f4f90e73328c35e9f02464bafed8becristy        (void) CloneString(&draw_info->geometry,geometry);
169b712c6e549e8dbd76a125beb14182d41cc8027eecristy      status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
170a492593efccb08b72392d64966b5c7728e56f16acristy      width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
1718db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy      image->columns=width;
1728db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    }
1738db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy  if (image->rows == 0)
1748db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    {
175e275ada1a7f4f90e73328c35e9f02464bafed8becristy      text=AcquireString(caption);
176e275ada1a7f4f90e73328c35e9f02464bafed8becristy      i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
177e275ada1a7f4f90e73328c35e9f02464bafed8becristy        exception);
178e275ada1a7f4f90e73328c35e9f02464bafed8becristy      (void) CloneString(&draw_info->text,text);
179e275ada1a7f4f90e73328c35e9f02464bafed8becristy      text=DestroyString(text);
180e275ada1a7f4f90e73328c35e9f02464bafed8becristy      (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
181e275ada1a7f4f90e73328c35e9f02464bafed8becristy        -metrics.bounds.x1,metrics.ascent);
182e275ada1a7f4f90e73328c35e9f02464bafed8becristy      if (draw_info->gravity == UndefinedGravity)
183e275ada1a7f4f90e73328c35e9f02464bafed8becristy        (void) CloneString(&draw_info->geometry,geometry);
184a492593efccb08b72392d64966b5c7728e56f16acristy      status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
18554ec42737fdec7527afa06ca95be82c8ca0bd866cristy      image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
18654ec42737fdec7527afa06ca95be82c8ca0bd866cristy        draw_info->interline_spacing+draw_info->stroke_width)+0.5);
1878db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    }
188a492593efccb08b72392d64966b5c7728e56f16acristy  if (fabs(image_info->pointsize) < MagickEpsilon)
1893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
190bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy      double
191bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        high,
192bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        low;
193bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy
1943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      /*
195bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        Auto fit text into bounding box.
1963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      */
1978592b059b2b4f03c0280b0e1146b225207e624f8cristy      for ( ; ; draw_info->pointsize*=2.0)
1983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
1993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        text=AcquireString(caption);
2009f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy        i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
2015cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy          exception);
2023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        (void) CloneString(&draw_info->text,text);
2033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        text=DestroyString(text);
204b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy        (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
2053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          -metrics.bounds.x1,metrics.ascent);
2063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (draw_info->gravity == UndefinedGravity)
2073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          (void) CloneString(&draw_info->geometry,geometry);
2085cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy        status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
209bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
2104e82e51d7ebce7b4ef0f808d906124dd6f812248cristy        height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
21154ec42737fdec7527afa06ca95be82c8ca0bd866cristy        if ((width >= image->columns) && (height >= image->rows))
2123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
21354ec42737fdec7527afa06ca95be82c8ca0bd866cristy        if ((width >= (image->columns << 1)) || (height >= (image->rows << 1)))
2148b235b2d0ea1bce8f9743d2d25bd6ddc9f72ff1dcristy          break;
2153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
2160839c55af3e04dec06756f60da81a58210557c29cristy      high=draw_info->pointsize;
217e275ada1a7f4f90e73328c35e9f02464bafed8becristy      for (low=1.0; (high-low) > 1.0; )
2183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
219bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        draw_info->pointsize=(low+high)/2.0;
2203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        text=AcquireString(caption);
2219f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy        i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
2225cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy          exception);
2233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        (void) CloneString(&draw_info->text,text);
2243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        text=DestroyString(text);
225b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy        (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
2263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          -metrics.bounds.x1,metrics.ascent);
2273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (draw_info->gravity == UndefinedGravity)
2283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          (void) CloneString(&draw_info->geometry,geometry);
2295cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy        status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
230bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
2314e82e51d7ebce7b4ef0f808d906124dd6f812248cristy        height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
23242f5a6a8af8b1e4fd5f341e709c4d06778b8d85fcristy        if ((width <= image->columns) && (height <= image->rows))
233bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy          low=draw_info->pointsize+1.0;
234bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        else
235bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy          high=draw_info->pointsize-1.0;
2363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
2376cd29479f50f920eb9e65e97ba682037204f7708cristy      for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; )
23817386ff059907b275aeb762297179b2d26d95a33cristy      {
23917386ff059907b275aeb762297179b2d26d95a33cristy        text=AcquireString(caption);
2409f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy        i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
24117386ff059907b275aeb762297179b2d26d95a33cristy          exception);
24217386ff059907b275aeb762297179b2d26d95a33cristy        (void) CloneString(&draw_info->text,text);
24317386ff059907b275aeb762297179b2d26d95a33cristy        text=DestroyString(text);
24417386ff059907b275aeb762297179b2d26d95a33cristy        (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
24517386ff059907b275aeb762297179b2d26d95a33cristy          -metrics.bounds.x1,metrics.ascent);
24617386ff059907b275aeb762297179b2d26d95a33cristy        if (draw_info->gravity == UndefinedGravity)
24717386ff059907b275aeb762297179b2d26d95a33cristy          (void) CloneString(&draw_info->geometry,geometry);
24817386ff059907b275aeb762297179b2d26d95a33cristy        status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
24917386ff059907b275aeb762297179b2d26d95a33cristy        width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
25017386ff059907b275aeb762297179b2d26d95a33cristy        height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
25117386ff059907b275aeb762297179b2d26d95a33cristy        if ((width <= image->columns) && (height <= image->rows))
25217386ff059907b275aeb762297179b2d26d95a33cristy          break;
2536cd29479f50f920eb9e65e97ba682037204f7708cristy        draw_info->pointsize--;
25417386ff059907b275aeb762297179b2d26d95a33cristy      }
255ec0bf4bde1f4ae360eaad31b98fb9ffe06d3b35dcristy      draw_info->pointsize--;
2563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
257a492593efccb08b72392d64966b5c7728e56f16acristy  (void) CloneString(&draw_info->text,caption);
258a492593efccb08b72392d64966b5c7728e56f16acristy  i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&caption,
259a492593efccb08b72392d64966b5c7728e56f16acristy    exception);
260ea1a8aa04a9fe1500104284407c1cc06d20da699cristy  if (SetImageBackgroundColor(image,exception) == MagickFalse)
26195524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy    {
26295524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy      image=DestroyImageList(image);
26395524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy      return((Image *) NULL);
26495524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy    }
2653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
2663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Draw caption.
2673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
2683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CloneString(&draw_info->text,caption);
2695cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy  status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
270024843f38984cd26602f0b3b28a0768dfa056bb5cristy  if ((draw_info->gravity != UndefinedGravity) &&
271024843f38984cd26602f0b3b28a0768dfa056bb5cristy      (draw_info->direction != RightToLeftDirection))
272bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
2733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  else
2743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
275b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy      (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
2763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
2773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        draw_info->stroke_width/2.0);
278024843f38984cd26602f0b3b28a0768dfa056bb5cristy      if (draw_info->direction == RightToLeftDirection)
279024843f38984cd26602f0b3b28a0768dfa056bb5cristy        (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
280024843f38984cd26602f0b3b28a0768dfa056bb5cristy          image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
281024843f38984cd26602f0b3b28a0768dfa056bb5cristy          metrics.ascent+draw_info->stroke_width/2.0);
282024843f38984cd26602f0b3b28a0768dfa056bb5cristy      draw_info->geometry=AcquireString(geometry);
2833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
284ab272ac4f115daf25c032de9dbb49a399eebe49bcristy  status=AnnotateImage(image,draw_info,exception);
2853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  draw_info=DestroyDrawInfo(draw_info);
2863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  caption=DestroyString(caption);
287ab272ac4f115daf25c032de9dbb49a399eebe49bcristy  if (status == MagickFalse)
288ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    {
289ab272ac4f115daf25c032de9dbb49a399eebe49bcristy      image=DestroyImageList(image);
290ab272ac4f115daf25c032de9dbb49a399eebe49bcristy      return((Image *) NULL);
291ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    }
2923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(GetFirstImageInList(image));
2933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
2943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
2963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e g i s t e r C A P T I O N I m a g e                                   %
3013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
3063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  RegisterCAPTIONImage() adds attributes for the CAPTION image format to
3073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  the list of supported formats.  The attributes include the image format
3083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  tag, a method to read and/or write the format, whether the format
3093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  supports the saving of more than one frame to the same file or blob,
3103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  whether the format supports native in-memory I/O, and a brief
3113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  description of the format.
3123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
3133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the RegisterCAPTIONImage method is:
3143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
315bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy%      size_t RegisterCAPTIONImage(void)
3163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
3173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
318bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristyModuleExport size_t RegisterCAPTIONImage(void)
3193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
3203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickInfo
3213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *entry;
3223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry=SetMagickInfo("CAPTION");
3243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
3258a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy  entry->description=ConstantString("Caption");
3263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->adjoin=MagickFalse;
3273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->module=ConstantString("CAPTION");
3283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
3293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(MagickImageCoderSignature);
3303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
3313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
3333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   U n r e g i s t e r C A P T I O N I m a g e                               %
3383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
3413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
3433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  UnregisterCAPTIONImage() removes format registrations made by the
3443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  CAPTION module from the list of supported formats.
3453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
3463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the UnregisterCAPTIONImage method is:
3473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
3483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      UnregisterCAPTIONImage(void)
3493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
3503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
3513ed852eea50f9d4cd633efb8c2b054b8e33c253cristyModuleExport void UnregisterCAPTIONImage(void)
3523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
3533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("CAPTION");
3543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
355