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-2017 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/artifact.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/composite-private.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/draw-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/module.h"
58 #include "MagickCore/option.h"
59 #include "MagickCore/property.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/string-private.h"
64 #include "MagickCore/utility.h"
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 % R e a d C A P T I O N I m a g e %
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % ReadCAPTIONImage() reads a CAPTION image file and returns it. It
78 % allocates the memory necessary for the new Image structure and returns a
79 % pointer to the new image.
81 % The format of the ReadCAPTIONImage method is:
83 % Image *ReadCAPTIONImage(const ImageInfo *image_info,
84 % ExceptionInfo *exception)
86 % A description of each parameter follows:
88 % o image_info: the image info.
90 % o exception: return any errors or warnings in this structure.
93 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
94 ExceptionInfo *exception)
98 geometry[MagickPathExtent],
127 Initialize Image structure.
129 assert(image_info != (const ImageInfo *) NULL);
130 assert(image_info->signature == MagickCoreSignature);
131 if (image_info->debug != MagickFalse)
132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
133 image_info->filename);
134 assert(exception != (ExceptionInfo *) NULL);
135 assert(exception->signature == MagickCoreSignature);
136 image=AcquireImage(image_info,exception);
137 (void) ResetImagePage(image,"0x0+0+0");
141 option=GetImageOption(image_info,"filename");
142 if (option == (const char *) NULL)
143 property=InterpretImageProperties((ImageInfo *) image_info,image,
144 image_info->filename,exception);
146 if (LocaleNCompare(option,"caption:",8) == 0)
147 property=InterpretImageProperties((ImageInfo *) image_info,image,option+8,
150 property=InterpretImageProperties((ImageInfo *) image_info,image,option,
152 (void) SetImageProperty(image,"caption",property,exception);
153 property=DestroyString(property);
154 caption=ConstantString(GetImageProperty(image,"caption",exception));
155 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
156 (void) CloneString(&draw_info->text,caption);
157 gravity=GetImageOption(image_info,"gravity");
158 if (gravity != (char *) NULL)
159 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
160 MagickFalse,gravity);
162 if (image->columns == 0)
164 text=AcquireString(caption);
165 i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
167 (void) CloneString(&draw_info->text,text);
168 text=DestroyString(text);
169 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
170 -metrics.bounds.x1,metrics.ascent);
171 if (draw_info->gravity == UndefinedGravity)
172 (void) CloneString(&draw_info->geometry,geometry);
173 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
174 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
175 image->columns=width;
177 if (image->rows == 0)
180 text=AcquireString(caption);
181 i=FormatMagickCaption(image,draw_info,split,&metrics,&text,exception);
182 (void) CloneString(&draw_info->text,text);
183 text=DestroyString(text);
184 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
185 -metrics.bounds.x1,metrics.ascent);
186 if (draw_info->gravity == UndefinedGravity)
187 (void) CloneString(&draw_info->geometry,geometry);
188 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
189 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
190 draw_info->interline_spacing+draw_info->stroke_width)+0.5);
192 status=SetImageExtent(image,image->columns,image->rows,exception);
193 if (status == MagickFalse)
195 draw_info=DestroyDrawInfo(draw_info);
196 return(DestroyImageList(image));
198 if (SetImageBackgroundColor(image,exception) == MagickFalse)
200 draw_info=DestroyDrawInfo(draw_info);
201 image=DestroyImageList(image);
202 return((Image *) NULL);
204 if ((fabs(image_info->pointsize) < MagickEpsilon) && (strlen(caption) > 0))
211 Auto fit text into bounding box.
213 for ( ; ; draw_info->pointsize*=2.0)
215 text=AcquireString(caption);
216 i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
218 (void) CloneString(&draw_info->text,text);
219 text=DestroyString(text);
220 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
221 -metrics.bounds.x1,metrics.ascent);
222 if (draw_info->gravity == UndefinedGravity)
223 (void) CloneString(&draw_info->geometry,geometry);
224 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
225 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
226 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
227 if ((image->columns != 0) && (image->rows != 0))
229 if ((width >= image->columns) && (height >= image->rows))
233 if (((image->columns != 0) && (width >= image->columns)) ||
234 ((image->rows != 0) && (height >= image->rows)))
237 high=draw_info->pointsize;
238 for (low=1.0; (high-low) > 0.5; )
240 draw_info->pointsize=(low+high)/2.0;
241 text=AcquireString(caption);
242 i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
244 (void) CloneString(&draw_info->text,text);
245 text=DestroyString(text);
246 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
247 -metrics.bounds.x1,metrics.ascent);
248 if (draw_info->gravity == UndefinedGravity)
249 (void) CloneString(&draw_info->geometry,geometry);
250 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
251 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
252 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
253 if ((image->columns != 0) && (image->rows != 0))
255 if ((width < image->columns) && (height < image->rows))
256 low=draw_info->pointsize+0.5;
258 high=draw_info->pointsize-0.5;
261 if (((image->columns != 0) && (width < image->columns)) ||
262 ((image->rows != 0) && (height < image->rows)))
263 low=draw_info->pointsize+0.5;
265 high=draw_info->pointsize-0.5;
267 draw_info->pointsize=floor((low+high)/2.0-0.5);
272 i=FormatMagickCaption(image,draw_info,split,&metrics,&caption,exception);
273 (void) CloneString(&draw_info->text,caption);
274 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",MagickMax(
275 draw_info->direction == RightToLeftDirection ? image->columns-
276 metrics.bounds.x2 : -metrics.bounds.x1,0.0),draw_info->gravity ==
277 UndefinedGravity ? metrics.ascent : 0.0);
278 (void) CloneString(&draw_info->geometry,geometry);
279 status=AnnotateImage(image,draw_info,exception);
280 if (image_info->pointsize == 0.0)
283 pointsize[MagickPathExtent];
285 (void) FormatLocaleString(pointsize,MagickPathExtent,"%.20g",
286 draw_info->pointsize);
287 (void) SetImageProperty(image,"caption:pointsize",pointsize,exception);
289 draw_info=DestroyDrawInfo(draw_info);
290 caption=DestroyString(caption);
291 if (status == MagickFalse)
293 image=DestroyImageList(image);
294 return((Image *) NULL);
296 return(GetFirstImageInList(image));
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % R e g i s t e r C A P T I O N I m a g e %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 % RegisterCAPTIONImage() adds attributes for the CAPTION image format to
311 % the list of supported formats. The attributes include the image format
312 % tag, a method to read and/or write the format, whether the format
313 % supports the saving of more than one frame to the same file or blob,
314 % whether the format supports native in-memory I/O, and a brief
315 % description of the format.
317 % The format of the RegisterCAPTIONImage method is:
319 % size_t RegisterCAPTIONImage(void)
322 ModuleExport size_t RegisterCAPTIONImage(void)
327 entry=AcquireMagickInfo("CAPTION","CAPTION","Caption");
328 entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
329 entry->flags^=CoderAdjoinFlag;
330 (void) RegisterMagickInfo(entry);
331 return(MagickImageCoderSignature);
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 % U n r e g i s t e r C A P T I O N I m a g e %
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 % UnregisterCAPTIONImage() removes format registrations made by the
346 % CAPTION module from the list of supported formats.
348 % The format of the UnregisterCAPTIONImage method is:
350 % UnregisterCAPTIONImage(void)
353 ModuleExport void UnregisterCAPTIONImage(void)
355 (void) UnregisterMagickInfo("CAPTION");