]> 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-2012 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
100   const char
101     *gravity,
102     *option;
103
104   DrawInfo
105     *draw_info;
106
107   Image
108     *image;
109
110   MagickBooleanType
111     status;
112
113   register ssize_t
114     i;
115
116   size_t
117     height,
118     width;
119
120   TypeMetric
121     metrics;
122
123   /*
124     Initialize Image structure.
125   */
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");
137   /*
138     Format caption.
139   */
140   property=InterpretImageProperties(image_info,image,image_info->filename,
141     exception);
142   option=GetImageOption(image_info,"filename");
143   if (option == (const char *) NULL)
144     property=InterpretImageProperties(image_info,image,image_info->filename,
145       exception);
146   else
147     if (LocaleNCompare(option,"caption:",8) == 0)
148       property=InterpretImageProperties(image_info,image,option+8,exception);
149     else
150       property=InterpretImageProperties(image_info,image,option,exception);
151   (void) SetImageProperty(image,"caption",property,exception);
152   property=DestroyString(property);
153   caption=ConstantString(GetImageProperty(image,"caption",exception));
154   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
155   (void) CloneString(&draw_info->text,caption);
156   gravity=GetImageOption(image_info,"gravity");
157   if (gravity != (char *) NULL)
158     draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
159       MagickFalse,gravity);
160   if ((*caption != '\0') && (image->rows != 0) &&
161       (image_info->pointsize == 0.0))
162     {
163       char
164         *text;
165
166       /*
167         Scale text to fit bounding box.
168       */
169       for ( ; ; )
170       {
171         text=AcquireString(caption);
172         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
173           exception);
174         (void) CloneString(&draw_info->text,text);
175         text=DestroyString(text);
176         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
177           -metrics.bounds.x1,metrics.ascent);
178         if (draw_info->gravity == UndefinedGravity)
179           (void) CloneString(&draw_info->geometry,geometry);
180         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
181         (void) status;
182         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
183         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
184         if ((width > (image->columns+1)) || (height > (image->rows+1)))
185           break;
186         draw_info->pointsize*=2.0;
187       }
188       draw_info->pointsize/=2.0;
189       for ( ; ; )
190       {
191         text=AcquireString(caption);
192         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
193           exception);
194         (void) CloneString(&draw_info->text,text);
195         text=DestroyString(text);
196         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
197           -metrics.bounds.x1,metrics.ascent);
198         if (draw_info->gravity == UndefinedGravity)
199           (void) CloneString(&draw_info->geometry,geometry);
200         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
201         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
202         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
203         if ((width > (image->columns+1)) || (height > (image->rows+1)))
204           break;
205         draw_info->pointsize++;
206       }
207       draw_info->pointsize--;
208     }
209   i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception);
210   if (image->rows == 0)
211     image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
212       draw_info->interline_spacing+draw_info->stroke_width)+0.5);
213   if (image->rows == 0)
214     image->rows=(size_t) ((i+1)*draw_info->pointsize+
215       draw_info->interline_spacing+draw_info->stroke_width+0.5);
216   if (SetImageBackgroundColor(image,exception) == MagickFalse)
217     {
218       image=DestroyImageList(image);
219       return((Image *) NULL);
220     }
221   /*
222     Draw caption.
223   */
224   (void) CloneString(&draw_info->text,caption);
225   status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
226   if ((draw_info->gravity != UndefinedGravity) &&
227       (draw_info->direction != RightToLeftDirection))
228     image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
229   else
230     {
231       (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
232         -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
233         draw_info->stroke_width/2.0);
234       if (draw_info->direction == RightToLeftDirection)
235         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
236           image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
237           metrics.ascent+draw_info->stroke_width/2.0);
238       draw_info->geometry=AcquireString(geometry);
239     }
240   status=AnnotateImage(image,draw_info,exception);
241   draw_info=DestroyDrawInfo(draw_info);
242   caption=DestroyString(caption);
243   if (status == MagickFalse)
244     {
245       image=DestroyImageList(image);
246       return((Image *) NULL);
247     }
248   return(GetFirstImageInList(image));
249 }
250 \f
251 /*
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 %                                                                             %
254 %                                                                             %
255 %                                                                             %
256 %   R e g i s t e r C A P T I O N I m a g e                                   %
257 %                                                                             %
258 %                                                                             %
259 %                                                                             %
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 %
262 %  RegisterCAPTIONImage() adds attributes for the CAPTION image format to
263 %  the list of supported formats.  The attributes include the image format
264 %  tag, a method to read and/or write the format, whether the format
265 %  supports the saving of more than one frame to the same file or blob,
266 %  whether the format supports native in-memory I/O, and a brief
267 %  description of the format.
268 %
269 %  The format of the RegisterCAPTIONImage method is:
270 %
271 %      size_t RegisterCAPTIONImage(void)
272 %
273 */
274 ModuleExport size_t RegisterCAPTIONImage(void)
275 {
276   MagickInfo
277     *entry;
278
279   entry=SetMagickInfo("CAPTION");
280   entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
281   entry->description=ConstantString("Caption");
282   entry->adjoin=MagickFalse;
283   entry->module=ConstantString("CAPTION");
284   (void) RegisterMagickInfo(entry);
285   return(MagickImageCoderSignature);
286 }
287 \f
288 /*
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 %                                                                             %
291 %                                                                             %
292 %                                                                             %
293 %   U n r e g i s t e r C A P T I O N I m a g e                               %
294 %                                                                             %
295 %                                                                             %
296 %                                                                             %
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 %
299 %  UnregisterCAPTIONImage() removes format registrations made by the
300 %  CAPTION module from the list of supported formats.
301 %
302 %  The format of the UnregisterCAPTIONImage method is:
303 %
304 %      UnregisterCAPTIONImage(void)
305 %
306 */
307 ModuleExport void UnregisterCAPTIONImage(void)
308 {
309   (void) UnregisterMagickInfo("CAPTION");
310 }