]> 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-2011 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/draw.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/property.h"
56 #include "MagickCore/quantum-private.h"
57 #include "MagickCore/static.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/module.h"
60 \f
61 /*
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %                                                                             %
64 %                                                                             %
65 %                                                                             %
66 %   R e a d C A P T I O N I m a g e                                           %
67 %                                                                             %
68 %                                                                             %
69 %                                                                             %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %
72 %  ReadCAPTIONImage() reads a CAPTION image file and returns it.  It
73 %  allocates the memory necessary for the new Image structure and returns a
74 %  pointer to the new image.
75 %
76 %  The format of the ReadCAPTIONImage method is:
77 %
78 %      Image *ReadCAPTIONImage(const ImageInfo *image_info,
79 %        ExceptionInfo *exception)
80 %
81 %  A description of each parameter follows:
82 %
83 %    o image_info: the image info.
84 %
85 %    o exception: return any errors or warnings in this structure.
86 %
87 */
88 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
89   ExceptionInfo *exception)
90 {
91   char
92     *caption,
93     geometry[MaxTextExtent],
94     *property;
95
96   const char
97     *gravity;
98
99   DrawInfo
100     *draw_info;
101
102   Image
103     *image;
104
105   MagickBooleanType
106     status;
107
108   register ssize_t
109     i;
110
111   size_t
112     height,
113     width;
114
115   TypeMetric
116     metrics;
117
118   /*
119     Initialize Image structure.
120   */
121   assert(image_info != (const ImageInfo *) NULL);
122   assert(image_info->signature == MagickSignature);
123   if (image_info->debug != MagickFalse)
124     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
125       image_info->filename);
126   assert(exception != (ExceptionInfo *) NULL);
127   assert(exception->signature == MagickSignature);
128   image=AcquireImage(image_info,exception);
129   if (image->columns == 0)
130     ThrowReaderException(OptionError,"MustSpecifyImageSize");
131   (void) ResetImagePage(image,"0x0+0+0");
132   /*
133     Format caption.
134   */
135   property=InterpretImageProperties(image_info,image,image_info->filename,
136     exception);
137   (void) SetImageProperty(image,"caption",property);
138   property=DestroyString(property);
139   caption=ConstantString(GetImageProperty(image,"caption"));
140   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
141   draw_info->text=ConstantString(caption);
142   gravity=GetImageOption(image_info,"gravity");
143   if (gravity != (char *) NULL)
144     draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
145       MagickFalse,gravity);
146   if ((*caption != '\0') && (image->rows != 0) &&
147       (image_info->pointsize == 0.0))
148     {
149       char
150         *text;
151
152       /*
153         Scale text to fit bounding box.
154       */
155       for ( ; ; )
156       {
157         text=AcquireString(caption);
158         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
159           exception);
160         (void) CloneString(&draw_info->text,text);
161         text=DestroyString(text);
162         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
163           -metrics.bounds.x1,metrics.ascent);
164         if (draw_info->gravity == UndefinedGravity)
165           (void) CloneString(&draw_info->geometry,geometry);
166         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
167         (void) status;
168         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
169         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
170         if ((width > (image->columns+1)) || (height > (image->rows+1)))
171           break;
172         draw_info->pointsize*=2.0;
173       }
174       draw_info->pointsize/=2.0;
175       for ( ; ; )
176       {
177         text=AcquireString(caption);
178         i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
179           exception);
180         (void) CloneString(&draw_info->text,text);
181         text=DestroyString(text);
182         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
183           -metrics.bounds.x1,metrics.ascent);
184         if (draw_info->gravity == UndefinedGravity)
185           (void) CloneString(&draw_info->geometry,geometry);
186         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
187         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
188         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
189         if ((width > (image->columns+1)) || (height > (image->rows+1)))
190           break;
191         draw_info->pointsize++;
192       }
193       draw_info->pointsize--;
194     }
195   i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception);
196   if (image->rows == 0)
197     image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
198       draw_info->interline_spacing+draw_info->stroke_width)+0.5);
199   if (image->rows == 0)
200     image->rows=(size_t) ((i+1)*draw_info->pointsize+
201       draw_info->interline_spacing+draw_info->stroke_width+0.5);
202   if (SetImageBackgroundColor(image) == MagickFalse)
203     {
204       InheritException(exception,&image->exception);
205       image=DestroyImageList(image);
206       return((Image *) NULL);
207     }
208   /*
209     Draw caption.
210   */
211   (void) CloneString(&draw_info->text,caption);
212   status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
213   if ((draw_info->gravity != UndefinedGravity) &&
214       (draw_info->direction != RightToLeftDirection))
215     image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
216   else
217     {
218       (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
219         -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
220         draw_info->stroke_width/2.0);
221       if (draw_info->direction == RightToLeftDirection)
222         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
223           image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
224           metrics.ascent+draw_info->stroke_width/2.0);
225       draw_info->geometry=AcquireString(geometry);
226     }
227   (void) AnnotateImage(image,draw_info,exception);
228   draw_info=DestroyDrawInfo(draw_info);
229   caption=DestroyString(caption);
230   return(GetFirstImageInList(image));
231 }
232 \f
233 /*
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 %                                                                             %
236 %                                                                             %
237 %                                                                             %
238 %   R e g i s t e r C A P T I O N I m a g e                                   %
239 %                                                                             %
240 %                                                                             %
241 %                                                                             %
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %
244 %  RegisterCAPTIONImage() adds attributes for the CAPTION image format to
245 %  the list of supported formats.  The attributes include the image format
246 %  tag, a method to read and/or write the format, whether the format
247 %  supports the saving of more than one frame to the same file or blob,
248 %  whether the format supports native in-memory I/O, and a brief
249 %  description of the format.
250 %
251 %  The format of the RegisterCAPTIONImage method is:
252 %
253 %      size_t RegisterCAPTIONImage(void)
254 %
255 */
256 ModuleExport size_t RegisterCAPTIONImage(void)
257 {
258   MagickInfo
259     *entry;
260
261   entry=SetMagickInfo("CAPTION");
262   entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
263   entry->adjoin=MagickFalse;
264   entry->format_type=ImplicitFormatType;
265   entry->description=ConstantString("Image caption");
266   entry->module=ConstantString("CAPTION");
267   (void) RegisterMagickInfo(entry);
268   return(MagickImageCoderSignature);
269 }
270 \f
271 /*
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 %                                                                             %
274 %                                                                             %
275 %                                                                             %
276 %   U n r e g i s t e r C A P T I O N I m a g e                               %
277 %                                                                             %
278 %                                                                             %
279 %                                                                             %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 %
282 %  UnregisterCAPTIONImage() removes format registrations made by the
283 %  CAPTION module from the list of supported formats.
284 %
285 %  The format of the UnregisterCAPTIONImage method is:
286 %
287 %      UnregisterCAPTIONImage(void)
288 %
289 */
290 ModuleExport void UnregisterCAPTIONImage(void)
291 {
292   (void) UnregisterMagickInfo("CAPTION");
293 }