]> granicus.if.org Git - imagemagick/blob - coders/caption.c
Remove --enable-zero-configuration as per https://github.com/ImageMagick/ImageMagick...
[imagemagick] / coders / caption.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
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                %
11 %                                                                             %
12 %                                                                             %
13 %                             Read Text Caption.                              %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                               February 2002                                 %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://www.imagemagick.org/script/license.php                           %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
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"
65 \f
66 /*
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %                                                                             %
69 %                                                                             %
70 %                                                                             %
71 %   R e a d C A P T I O N I m a g e                                           %
72 %                                                                             %
73 %                                                                             %
74 %                                                                             %
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %
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.
80 %
81 %  The format of the ReadCAPTIONImage method is:
82 %
83 %      Image *ReadCAPTIONImage(const ImageInfo *image_info,
84 %        ExceptionInfo *exception)
85 %
86 %  A description of each parameter follows:
87 %
88 %    o image_info: the image info.
89 %
90 %    o exception: return any errors or warnings in this structure.
91 %
92 */
93 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
94   ExceptionInfo *exception)
95 {
96   char
97     *caption,
98     geometry[MagickPathExtent],
99     *property,
100     *text;
101
102   const char
103     *gravity,
104     *option;
105
106   DrawInfo
107     *draw_info;
108
109   Image
110     *image;
111
112   MagickBooleanType
113     split,
114     status;
115
116   register ssize_t
117     i;
118
119   size_t
120     height,
121     width;
122
123   TypeMetric
124     metrics;
125
126   /*
127     Initialize Image structure.
128   */
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");
138   /*
139     Format caption.
140   */
141   option=GetImageOption(image_info,"filename");
142   if (option == (const char *) NULL)
143     property=InterpretImageProperties((ImageInfo *) image_info,image,
144       image_info->filename,exception);
145   else
146     if (LocaleNCompare(option,"caption:",8) == 0)
147       property=InterpretImageProperties((ImageInfo *) image_info,image,option+8,
148         exception);
149     else
150       property=InterpretImageProperties((ImageInfo *) image_info,image,option,
151         exception);
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);
161   split=MagickFalse;
162   status=MagickTrue;
163   if (image->columns == 0)
164     {
165       text=AcquireString(caption);
166       i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
167         exception);
168       (void) CloneString(&draw_info->text,text);
169       text=DestroyString(text);
170       (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
171         -metrics.bounds.x1,metrics.ascent);
172       if (draw_info->gravity == UndefinedGravity)
173         (void) CloneString(&draw_info->geometry,geometry);
174       status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
175       width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
176       image->columns=width;
177     }
178   if (image->rows == 0)
179     {
180       split=MagickTrue;
181       text=AcquireString(caption);
182       i=FormatMagickCaption(image,draw_info,split,&metrics,&text,exception);
183       (void) CloneString(&draw_info->text,text);
184       text=DestroyString(text);
185       (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
186         -metrics.bounds.x1,metrics.ascent);
187       if (draw_info->gravity == UndefinedGravity)
188         (void) CloneString(&draw_info->geometry,geometry);
189       status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
190       image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
191         draw_info->interline_spacing+draw_info->stroke_width)+0.5);
192     }
193   if (status != MagickFalse)
194     status=SetImageExtent(image,image->columns,image->rows,exception);
195   if (status == MagickFalse)
196     { 
197       draw_info=DestroyDrawInfo(draw_info);
198       return(DestroyImageList(image));
199     }
200   if (SetImageBackgroundColor(image,exception) == MagickFalse)
201     {
202       draw_info=DestroyDrawInfo(draw_info);
203       image=DestroyImageList(image);
204       return((Image *) NULL);
205     }
206   if ((fabs(image_info->pointsize) < MagickEpsilon) && (strlen(caption) > 0))
207     {
208       double
209         high,
210         low;
211
212       ssize_t
213         n;
214
215       /*
216         Auto fit text into bounding box.
217       */
218       for (n=0; n < 32; n++, draw_info->pointsize*=2.0)
219       {
220         text=AcquireString(caption);
221         i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
222           exception);
223         (void) CloneString(&draw_info->text,text);
224         text=DestroyString(text);
225         (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
226           -metrics.bounds.x1,metrics.ascent);
227         if (draw_info->gravity == UndefinedGravity)
228           (void) CloneString(&draw_info->geometry,geometry);
229         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
230         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
231         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
232         if ((image->columns != 0) && (image->rows != 0))
233           {
234             if ((width >= image->columns) && (height >= image->rows))
235               break;
236           }
237         else
238           if (((image->columns != 0) && (width >= image->columns)) ||
239               ((image->rows != 0) && (height >= image->rows)))
240             break;
241       }
242       high=draw_info->pointsize;
243       for (low=1.0; (high-low) > 0.5; )
244       {
245         draw_info->pointsize=(low+high)/2.0;
246         text=AcquireString(caption);
247         i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
248           exception);
249         (void) CloneString(&draw_info->text,text);
250         text=DestroyString(text);
251         (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
252           -metrics.bounds.x1,metrics.ascent);
253         if (draw_info->gravity == UndefinedGravity)
254           (void) CloneString(&draw_info->geometry,geometry);
255         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
256         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
257         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
258         if ((image->columns != 0) && (image->rows != 0))
259           {
260             if ((width < image->columns) && (height < image->rows))
261               low=draw_info->pointsize+0.5;
262             else
263               high=draw_info->pointsize-0.5;
264           }
265         else
266           if (((image->columns != 0) && (width < image->columns)) ||
267               ((image->rows != 0) && (height < image->rows)))
268             low=draw_info->pointsize+0.5;
269           else
270             high=draw_info->pointsize-0.5;
271       }
272       draw_info->pointsize=floor((low+high)/2.0-0.5);
273     }
274   /*
275     Draw caption.
276   */
277   i=FormatMagickCaption(image,draw_info,split,&metrics,&caption,exception);
278   (void) CloneString(&draw_info->text,caption);
279   (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",MagickMax(
280     draw_info->direction == RightToLeftDirection ? image->columns-
281     metrics.bounds.x2 : -metrics.bounds.x1,0.0),draw_info->gravity ==
282     UndefinedGravity ? metrics.ascent : 0.0);
283   (void) CloneString(&draw_info->geometry,geometry);
284   status=AnnotateImage(image,draw_info,exception);
285   if (image_info->pointsize == 0.0)
286     { 
287       char
288         pointsize[MagickPathExtent];
289       
290       (void) FormatLocaleString(pointsize,MagickPathExtent,"%.20g",
291         draw_info->pointsize);
292       (void) SetImageProperty(image,"caption:pointsize",pointsize,exception);
293     }
294   draw_info=DestroyDrawInfo(draw_info);
295   caption=DestroyString(caption);
296   if (status == MagickFalse)
297     {
298       image=DestroyImageList(image);
299       return((Image *) NULL);
300     }
301   return(GetFirstImageInList(image));
302 }
303 \f
304 /*
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 %                                                                             %
307 %                                                                             %
308 %                                                                             %
309 %   R e g i s t e r C A P T I O N I m a g e                                   %
310 %                                                                             %
311 %                                                                             %
312 %                                                                             %
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 %
315 %  RegisterCAPTIONImage() adds attributes for the CAPTION image format to
316 %  the list of supported formats.  The attributes include the image format
317 %  tag, a method to read and/or write the format, whether the format
318 %  supports the saving of more than one frame to the same file or blob,
319 %  whether the format supports native in-memory I/O, and a brief
320 %  description of the format.
321 %
322 %  The format of the RegisterCAPTIONImage method is:
323 %
324 %      size_t RegisterCAPTIONImage(void)
325 %
326 */
327 ModuleExport size_t RegisterCAPTIONImage(void)
328 {
329   MagickInfo
330     *entry;
331
332   entry=AcquireMagickInfo("CAPTION","CAPTION","Caption");
333   entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
334   entry->flags^=CoderAdjoinFlag;
335   (void) RegisterMagickInfo(entry);
336   return(MagickImageCoderSignature);
337 }
338 \f
339 /*
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 %                                                                             %
342 %                                                                             %
343 %                                                                             %
344 %   U n r e g i s t e r C A P T I O N I m a g e                               %
345 %                                                                             %
346 %                                                                             %
347 %                                                                             %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 %
350 %  UnregisterCAPTIONImage() removes format registrations made by the
351 %  CAPTION module from the list of supported formats.
352 %
353 %  The format of the UnregisterCAPTIONImage method is:
354 %
355 %      UnregisterCAPTIONImage(void)
356 %
357 */
358 ModuleExport void UnregisterCAPTIONImage(void)
359 {
360   (void) UnregisterMagickInfo("CAPTION");
361 }