]> granicus.if.org Git - imagemagick/blob - coders/caption.c
(no commit message)
[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 %                                John Cristy                                  %
17 %                               February 2002                                 %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 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 %    http://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/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"
64 \f
65 /*
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %                                                                             %
68 %                                                                             %
69 %                                                                             %
70 %   R e a d C A P T I O N I m a g e                                           %
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %
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.
79 %
80 %  The format of the ReadCAPTIONImage method is:
81 %
82 %      Image *ReadCAPTIONImage(const ImageInfo *image_info,
83 %        ExceptionInfo *exception)
84 %
85 %  A description of each parameter follows:
86 %
87 %    o image_info: the image info.
88 %
89 %    o exception: return any errors or warnings in this structure.
90 %
91 */
92 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
93   ExceptionInfo *exception)
94 {
95   char
96     *caption,
97     geometry[MaxTextExtent],
98     *property,
99     *text;
100
101   const char
102     *gravity,
103     *option;
104
105   DrawInfo
106     *draw_info;
107
108   Image
109     *image;
110
111   MagickBooleanType
112     status;
113
114   register ssize_t
115     i;
116
117   size_t
118     height,
119     width;
120
121   TypeMetric
122     metrics;
123
124   /*
125     Initialize Image structure.
126   */
127   assert(image_info != (const ImageInfo *) NULL);
128   assert(image_info->signature == MagickSignature);
129   if (image_info->debug != MagickFalse)
130     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
131       image_info->filename);
132   assert(exception != (ExceptionInfo *) NULL);
133   assert(exception->signature == MagickSignature);
134   image=AcquireImage(image_info,exception);
135   (void) ResetImagePage(image,"0x0+0+0");
136   /*
137     Format caption.
138   */
139   option=GetImageOption(image_info,"filename");
140   if (option == (const char *) NULL)
141     property=InterpretImageProperties(image_info,image,image_info->filename,
142       exception);
143   else
144     if (LocaleNCompare(option,"caption:",8) == 0)
145       property=InterpretImageProperties(image_info,image,option+8,exception);
146     else
147       property=InterpretImageProperties(image_info,image,option,exception);
148   (void) SetImageProperty(image,"caption",property,exception);
149   property=DestroyString(property);
150   caption=ConstantString(GetImageProperty(image,"caption",exception));
151   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
152   (void) CloneString(&draw_info->text,caption);
153   gravity=GetImageOption(image_info,"gravity");
154   if (gravity != (char *) NULL)
155     draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
156       MagickFalse,gravity);
157   if (image->columns == 0)
158     {
159       for ( ; ; draw_info->pointsize*=2.0)
160       {
161         text=AcquireString(caption);
162         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
163           exception);
164         (void) CloneString(&draw_info->text,text);
165         text=DestroyString(text);
166         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
167           -metrics.bounds.x1,metrics.ascent);
168         if (draw_info->gravity == UndefinedGravity)
169           (void) CloneString(&draw_info->geometry,geometry);
170         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
171         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
172         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
173         if ((width >= image->columns) || (image_info->pointsize != 0.0))
174           break;
175       }
176       image->columns=width;
177     }
178   if (image->rows == 0)
179     {
180       for ( ; ; draw_info->pointsize*=2.0)
181       {
182         text=AcquireString(caption);
183         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
184           exception);
185         (void) CloneString(&draw_info->text,text);
186         text=DestroyString(text);
187         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
188           -metrics.bounds.x1,metrics.ascent);
189         if (draw_info->gravity == UndefinedGravity)
190           (void) CloneString(&draw_info->geometry,geometry);
191         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
192         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
193         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
194         if ((width >= image->columns) || (image_info->pointsize != 0.0))
195           break;
196       }
197       image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
198         draw_info->interline_spacing+draw_info->stroke_width)+0.5);
199     }
200   if (image_info->pointsize != 0.0)
201     i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,
202       exception);
203   else
204     {
205       double
206         high,
207         low;
208
209       /*
210         Auto fit text into bounding box.
211       */
212       for ( ; ; draw_info->pointsize*=2.0)
213       {
214         text=AcquireString(caption);
215         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
216           exception);
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))
227           break;
228         if ((width >= (image->columns << 1)) || (height >= (image->rows << 1)))
229           break;
230       }
231       high=draw_info->pointsize/2.0;
232       for (low=high/2.0; (high-low) > 1.0; )
233       {
234         draw_info->pointsize=(low+high)/2.0;
235         text=AcquireString(caption);
236         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
237           exception);
238         (void) CloneString(&draw_info->text,text);
239         text=DestroyString(text);
240         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
241           -metrics.bounds.x1,metrics.ascent);
242         if (draw_info->gravity == UndefinedGravity)
243           (void) CloneString(&draw_info->geometry,geometry);
244         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
245         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
246         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
247         if ((width <= image->columns) && (height <= image->rows))
248           low=draw_info->pointsize+1.0;
249         else
250           high=draw_info->pointsize-1.0;
251       }
252       for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; )
253       {
254         text=AcquireString(caption);
255         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
256           exception);
257         (void) CloneString(&draw_info->text,text);
258         text=DestroyString(text);
259         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
260           -metrics.bounds.x1,metrics.ascent);
261         if (draw_info->gravity == UndefinedGravity)
262           (void) CloneString(&draw_info->geometry,geometry);
263         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
264         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
265         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
266         if ((width <= image->columns) && (height <= image->rows))
267           break;
268         draw_info->pointsize--;
269       }
270       draw_info->pointsize=floor(draw_info->pointsize+0.5);
271       i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&caption,
272         exception);
273     }
274   if (SetImageBackgroundColor(image,exception) == MagickFalse)
275     {
276       image=DestroyImageList(image);
277       return((Image *) NULL);
278     }
279   /*
280     Draw caption.
281   */
282   (void) CloneString(&draw_info->text,caption);
283   status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
284   if ((draw_info->gravity != UndefinedGravity) &&
285       (draw_info->direction != RightToLeftDirection))
286     image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
287   else
288     {
289       (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
290         -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
291         draw_info->stroke_width/2.0);
292       if (draw_info->direction == RightToLeftDirection)
293         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
294           image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
295           metrics.ascent+draw_info->stroke_width/2.0);
296       draw_info->geometry=AcquireString(geometry);
297     }
298   status=AnnotateImage(image,draw_info,exception);
299   draw_info=DestroyDrawInfo(draw_info);
300   caption=DestroyString(caption);
301   if (status == MagickFalse)
302     {
303       image=DestroyImageList(image);
304       return((Image *) NULL);
305     }
306   return(GetFirstImageInList(image));
307 }
308 \f
309 /*
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 %                                                                             %
312 %                                                                             %
313 %                                                                             %
314 %   R e g i s t e r C A P T I O N I m a g e                                   %
315 %                                                                             %
316 %                                                                             %
317 %                                                                             %
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 %
320 %  RegisterCAPTIONImage() adds attributes for the CAPTION image format to
321 %  the list of supported formats.  The attributes include the image format
322 %  tag, a method to read and/or write the format, whether the format
323 %  supports the saving of more than one frame to the same file or blob,
324 %  whether the format supports native in-memory I/O, and a brief
325 %  description of the format.
326 %
327 %  The format of the RegisterCAPTIONImage method is:
328 %
329 %      size_t RegisterCAPTIONImage(void)
330 %
331 */
332 ModuleExport size_t RegisterCAPTIONImage(void)
333 {
334   MagickInfo
335     *entry;
336
337   entry=SetMagickInfo("CAPTION");
338   entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
339   entry->description=ConstantString("Caption");
340   entry->adjoin=MagickFalse;
341   entry->module=ConstantString("CAPTION");
342   (void) RegisterMagickInfo(entry);
343   return(MagickImageCoderSignature);
344 }
345 \f
346 /*
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 %                                                                             %
349 %                                                                             %
350 %                                                                             %
351 %   U n r e g i s t e r C A P T I O N I m a g e                               %
352 %                                                                             %
353 %                                                                             %
354 %                                                                             %
355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356 %
357 %  UnregisterCAPTIONImage() removes format registrations made by the
358 %  CAPTION module from the list of supported formats.
359 %
360 %  The format of the UnregisterCAPTIONImage method is:
361 %
362 %      UnregisterCAPTIONImage(void)
363 %
364 */
365 ModuleExport void UnregisterCAPTIONImage(void)
366 {
367   (void) UnregisterMagickInfo("CAPTION");
368 }