2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC AAA PPPP TTTTT IIIII OOO N N %
7 % C A A P P T I O O NN N %
8 % C AAAAA PPPP T I O O N N N %
9 % C A A P T I O O N NN %
10 % CCCC A A P T IIIII OOO N N %
13 % Read Text Caption. %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/annotate.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/composite-private.h"
47 #include "MagickCore/draw.h"
48 #include "MagickCore/draw-private.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/module.h"
57 #include "MagickCore/option.h"
58 #include "MagickCore/property.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/string-private.h"
63 #include "MagickCore/utility.h"
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 % R e a d C A P T I O N I m a g e %
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % ReadCAPTIONImage() reads a CAPTION image file and returns it. It
77 % allocates the memory necessary for the new Image structure and returns a
78 % pointer to the new image.
80 % The format of the ReadCAPTIONImage method is:
82 % Image *ReadCAPTIONImage(const ImageInfo *image_info,
83 % ExceptionInfo *exception)
85 % A description of each parameter follows:
87 % o image_info: the image info.
89 % o exception: return any errors or warnings in this structure.
92 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
93 ExceptionInfo *exception)
97 geometry[MaxTextExtent],
124 Initialize Image structure.
126 assert(image_info != (const ImageInfo *) NULL);
127 assert(image_info->signature == MagickSignature);
128 if (image_info->debug != MagickFalse)
129 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
130 image_info->filename);
131 assert(exception != (ExceptionInfo *) NULL);
132 assert(exception->signature == MagickSignature);
133 image=AcquireImage(image_info,exception);
134 if (image->columns == 0)
135 ThrowReaderException(OptionError,"MustSpecifyImageSize");
136 (void) ResetImagePage(image,"0x0+0+0");
140 option=GetImageOption(image_info,"filename");
141 if (option == (const char *) NULL)
142 property=InterpretImageProperties(image_info,image,image_info->filename,
145 if (LocaleNCompare(option,"caption:",8) == 0)
146 property=InterpretImageProperties(image_info,image,option+8,exception);
148 property=InterpretImageProperties(image_info,image,option,exception);
149 (void) SetImageProperty(image,"caption",property,exception);
150 property=DestroyString(property);
151 caption=ConstantString(GetImageProperty(image,"caption",exception));
152 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
153 (void) CloneString(&draw_info->text,caption);
154 gravity=GetImageOption(image_info,"gravity");
155 if (gravity != (char *) NULL)
156 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
157 MagickFalse,gravity);
158 if ((*caption != '\0') && (image->rows != 0) &&
159 (image_info->pointsize == 0.0))
169 Auto fit text into bounding box.
173 text=AcquireString(caption);
174 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
176 (void) CloneString(&draw_info->text,text);
177 text=DestroyString(text);
178 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
179 -metrics.bounds.x1,metrics.ascent);
180 if (draw_info->gravity == UndefinedGravity)
181 (void) CloneString(&draw_info->geometry,geometry);
182 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
184 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
185 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
186 if ((width > image->columns) && (height > image->rows))
188 draw_info->pointsize*=2.0;
190 high=draw_info->pointsize/2.0;
192 while ((high-low) > 1.0)
194 draw_info->pointsize=(low+high)/2.0;
195 text=AcquireString(caption);
196 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
198 (void) CloneString(&draw_info->text,text);
199 text=DestroyString(text);
200 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
201 -metrics.bounds.x1,metrics.ascent);
202 if (draw_info->gravity == UndefinedGravity)
203 (void) CloneString(&draw_info->geometry,geometry);
204 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
205 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
206 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
207 if ((width <= image->columns) && (height <= image->rows))
208 low=draw_info->pointsize+1.0;
210 high=draw_info->pointsize-1.0;
212 for (draw_info->pointsize=(low+high)/2.0; ; )
214 text=AcquireString(caption);
215 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
217 (void) CloneString(&draw_info->text,text);
218 text=DestroyString(text);
219 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
220 -metrics.bounds.x1,metrics.ascent);
221 if (draw_info->gravity == UndefinedGravity)
222 (void) CloneString(&draw_info->geometry,geometry);
223 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
224 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
225 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
226 if ((width <= image->columns) && (height <= image->rows))
228 draw_info->pointsize--;
230 draw_info->pointsize=floor(draw_info->pointsize);
232 i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception);
233 if (image->rows == 0)
234 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
235 draw_info->interline_spacing+draw_info->stroke_width)+0.5);
236 if (image->rows == 0)
237 image->rows=(size_t) ((i+1)*draw_info->pointsize+
238 draw_info->interline_spacing+draw_info->stroke_width+0.5);
239 if (SetImageBackgroundColor(image,exception) == MagickFalse)
241 image=DestroyImageList(image);
242 return((Image *) NULL);
247 (void) CloneString(&draw_info->text,caption);
248 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
249 if ((draw_info->gravity != UndefinedGravity) &&
250 (draw_info->direction != RightToLeftDirection))
251 image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
254 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
255 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
256 draw_info->stroke_width/2.0);
257 if (draw_info->direction == RightToLeftDirection)
258 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
259 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
260 metrics.ascent+draw_info->stroke_width/2.0);
261 draw_info->geometry=AcquireString(geometry);
263 status=AnnotateImage(image,draw_info,exception);
264 draw_info=DestroyDrawInfo(draw_info);
265 caption=DestroyString(caption);
266 if (status == MagickFalse)
268 image=DestroyImageList(image);
269 return((Image *) NULL);
271 return(GetFirstImageInList(image));
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % R e g i s t e r C A P T I O N I m a g e %
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % RegisterCAPTIONImage() adds attributes for the CAPTION image format to
286 % the list of supported formats. The attributes include the image format
287 % tag, a method to read and/or write the format, whether the format
288 % supports the saving of more than one frame to the same file or blob,
289 % whether the format supports native in-memory I/O, and a brief
290 % description of the format.
292 % The format of the RegisterCAPTIONImage method is:
294 % size_t RegisterCAPTIONImage(void)
297 ModuleExport size_t RegisterCAPTIONImage(void)
302 entry=SetMagickInfo("CAPTION");
303 entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
304 entry->description=ConstantString("Caption");
305 entry->adjoin=MagickFalse;
306 entry->module=ConstantString("CAPTION");
307 (void) RegisterMagickInfo(entry);
308 return(MagickImageCoderSignature);
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % U n r e g i s t e r C A P T I O N I m a g e %
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % UnregisterCAPTIONImage() removes format registrations made by the
323 % CAPTION module from the list of supported formats.
325 % The format of the UnregisterCAPTIONImage method is:
327 % UnregisterCAPTIONImage(void)
330 ModuleExport void UnregisterCAPTIONImage(void)
332 (void) UnregisterMagickInfo("CAPTION");