cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | % % |
cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/annotate.h" |
| 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 46 | #include "MagickCore/composite-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 47 | #include "MagickCore/draw.h" |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 48 | #include "MagickCore/draw-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 56 | #include "MagickCore/module.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 62 | #include "MagickCore/string-private.h" |
| 63 | #include "MagickCore/utility.h" |
| 64 | #if defined(MAGICKCORE_PANGOFT2_DELEGATE) |
| 65 | #include <pango/pango.h> |
| 66 | #include <pango/pangoft2.h> |
| 67 | #include <pango/pango-features.h> |
| 68 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | % % |
| 73 | % % |
| 74 | % % |
| 75 | % R e a d C A P T I O N I m a g e % |
| 76 | % % |
| 77 | % % |
| 78 | % % |
| 79 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 80 | % |
| 81 | % ReadCAPTIONImage() reads a CAPTION image file and returns it. It |
| 82 | % allocates the memory necessary for the new Image structure and returns a |
| 83 | % pointer to the new image. |
| 84 | % |
| 85 | % The format of the ReadCAPTIONImage method is: |
| 86 | % |
| 87 | % Image *ReadCAPTIONImage(const ImageInfo *image_info, |
| 88 | % ExceptionInfo *exception) |
| 89 | % |
| 90 | % A description of each parameter follows: |
| 91 | % |
| 92 | % o image_info: the image info. |
| 93 | % |
| 94 | % o exception: return any errors or warnings in this structure. |
| 95 | % |
| 96 | */ |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 97 | #if defined(MAGICKCORE_PANGOFT2_DELEGATE) |
| 98 | static void PangoSubstitute(FcPattern *pattern,void *context) |
| 99 | { |
| 100 | const char |
| 101 | *option; |
| 102 | |
| 103 | option=(const char *) context; |
| 104 | if (option == (const char *) NULL) |
| 105 | return; |
| 106 | FcPatternDel(pattern,FC_HINTING); |
| 107 | FcPatternAddBool(pattern, FC_HINTING,LocaleCompare(option,"none") != 0); |
| 108 | FcPatternDel(pattern,FC_AUTOHINT); |
| 109 | FcPatternAddBool(pattern,FC_AUTOHINT,LocaleCompare(option,"auto") == 0); |
| 110 | } |
| 111 | |
| 112 | static Image *ReadCAPTIONImage(const ImageInfo *image_info, |
| 113 | ExceptionInfo *exception) |
| 114 | { |
| 115 | char |
| 116 | *caption, |
| 117 | *property; |
| 118 | |
| 119 | const char |
| 120 | *option; |
| 121 | |
| 122 | DrawInfo |
| 123 | *draw_info; |
| 124 | |
| 125 | FT_Bitmap |
| 126 | *canvas; |
| 127 | |
| 128 | Image |
| 129 | *image; |
| 130 | |
| 131 | PangoAlignment |
| 132 | align; |
| 133 | |
| 134 | PangoContext |
| 135 | *context; |
| 136 | |
| 137 | PangoFontDescription |
| 138 | *description; |
| 139 | |
| 140 | PangoFontMap |
| 141 | *fontmap; |
| 142 | |
| 143 | PangoGravity |
| 144 | gravity; |
| 145 | |
| 146 | PangoLayout |
| 147 | *layout; |
| 148 | |
| 149 | PangoRectangle |
| 150 | extent; |
| 151 | |
| 152 | PixelInfo |
| 153 | fill_color; |
| 154 | |
| 155 | RectangleInfo |
| 156 | page; |
| 157 | |
| 158 | register Quantum |
| 159 | *q; |
| 160 | |
| 161 | register unsigned char |
| 162 | *p; |
| 163 | |
| 164 | ssize_t |
| 165 | y; |
| 166 | |
| 167 | /* |
| 168 | Initialize Image structure. |
| 169 | */ |
| 170 | assert(image_info != (const ImageInfo *) NULL); |
| 171 | assert(image_info->signature == MagickSignature); |
| 172 | if (image_info->debug != MagickFalse) |
| 173 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 174 | image_info->filename); |
| 175 | assert(exception != (ExceptionInfo *) NULL); |
| 176 | assert(exception->signature == MagickSignature); |
| 177 | image=AcquireImage(image_info,exception); |
| 178 | (void) ResetImagePage(image,"0x0+0+0"); |
| 179 | /* |
| 180 | Get context. |
| 181 | */ |
| 182 | fontmap=(PangoFontMap *) pango_ft2_font_map_new(); |
| 183 | pango_ft2_font_map_set_resolution((PangoFT2FontMap *) fontmap, |
| 184 | image->resolution.x,image->resolution.y); |
| 185 | option=GetImageOption(image_info,"caption:hinting"); |
| 186 | pango_ft2_font_map_set_default_substitute((PangoFT2FontMap *) fontmap, |
| 187 | PangoSubstitute,(char *) option,NULL); |
| 188 | context=pango_font_map_create_context(fontmap); |
| 189 | option=GetImageOption(image_info,"caption:language"); |
| 190 | if (option != (const char *) NULL) |
| 191 | pango_context_set_language(context,pango_language_from_string(option)); |
| 192 | draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); |
| 193 | pango_context_set_base_dir(context,draw_info->direction == |
| 194 | RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR); |
| 195 | switch (draw_info->gravity) |
| 196 | { |
| 197 | case NorthGravity: gravity=PANGO_GRAVITY_NORTH; break; |
| 198 | case WestGravity: gravity=PANGO_GRAVITY_WEST; break; |
| 199 | case EastGravity: gravity=PANGO_GRAVITY_EAST; break; |
| 200 | case SouthGravity: gravity=PANGO_GRAVITY_SOUTH; break; |
| 201 | default: gravity=PANGO_GRAVITY_AUTO; break; |
| 202 | } |
| 203 | pango_context_set_base_gravity(context,gravity); |
| 204 | option=GetImageOption(image_info,"caption:gravity-hint"); |
| 205 | if (option != (const char *) NULL) |
| 206 | { |
| 207 | if (LocaleCompare(option,"line") == 0) |
| 208 | pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE); |
| 209 | if (LocaleCompare(option,"natural") == 0) |
| 210 | pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL); |
| 211 | if (LocaleCompare(option,"strong") == 0) |
| 212 | pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG); |
| 213 | } |
| 214 | /* |
| 215 | Configure layout. |
| 216 | */ |
| 217 | layout=pango_layout_new(context); |
| 218 | option=GetImageOption(image_info,"caption:auto-dir"); |
| 219 | if (option != (const char *) NULL) |
| 220 | pango_layout_set_auto_dir(layout,1); |
| 221 | option=GetImageOption(image_info,"caption:ellipsize"); |
| 222 | if (option != (const char *) NULL) |
| 223 | { |
| 224 | if (LocaleCompare(option,"end") == 0) |
| 225 | pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END); |
| 226 | if (LocaleCompare(option,"middle") == 0) |
| 227 | pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE); |
| 228 | if (LocaleCompare(option,"none") == 0) |
| 229 | pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE); |
| 230 | if (LocaleCompare(option,"start") == 0) |
| 231 | pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START); |
| 232 | } |
| 233 | option=GetImageOption(image_info,"caption:justify"); |
| 234 | if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse)) |
| 235 | pango_layout_set_justify(layout,1); |
| 236 | option=GetImageOption(image_info,"caption:single-paragraph"); |
| 237 | if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse)) |
| 238 | pango_layout_set_single_paragraph_mode(layout,1); |
| 239 | option=GetImageOption(image_info,"caption:wrap"); |
| 240 | if (option != (const char *) NULL) |
| 241 | { |
| 242 | if (LocaleCompare(option,"char") == 0) |
| 243 | pango_layout_set_wrap(layout,PANGO_WRAP_CHAR); |
| 244 | if (LocaleCompare(option,"word") == 0) |
| 245 | pango_layout_set_wrap(layout,PANGO_WRAP_WORD); |
| 246 | if (LocaleCompare(option,"word-char") == 0) |
| 247 | pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR); |
| 248 | } |
| 249 | option=GetImageOption(image_info,"caption:indent"); |
| 250 | if (option != (const char *) NULL) |
| 251 | pango_layout_set_indent(layout,(StringToLong(option)*image->resolution.x* |
| 252 | PANGO_SCALE+36)/72); |
| 253 | switch (draw_info->align) |
| 254 | { |
| 255 | case CenterAlign: align=PANGO_ALIGN_CENTER; break; |
| 256 | case RightAlign: align=PANGO_ALIGN_RIGHT; break; |
| 257 | case LeftAlign: |
| 258 | default: align=PANGO_ALIGN_LEFT; break; |
| 259 | } |
| 260 | if ((align != PANGO_ALIGN_CENTER) && |
| 261 | (draw_info->direction == RightToLeftDirection)) |
cristy | 3fac9ec | 2011-11-17 18:04:39 +0000 | [diff] [blame] | 262 | align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align); |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 263 | pango_layout_set_alignment(layout,align); |
| 264 | description=pango_font_description_from_string(draw_info->font == |
| 265 | (char *) NULL ? "helvetica" : draw_info->font); |
| 266 | pango_font_description_set_size(description,PANGO_SCALE*draw_info->pointsize); |
| 267 | pango_layout_set_font_description(layout,description); |
| 268 | pango_font_description_free(description); |
| 269 | property=InterpretImageProperties(image_info,image,image_info->filename, |
| 270 | exception); |
| 271 | (void) SetImageProperty(image,"caption",property,exception); |
| 272 | property=DestroyString(property); |
| 273 | caption=ConstantString(GetImageProperty(image,"caption",exception)); |
| 274 | /* |
| 275 | Render caption. |
| 276 | */ |
| 277 | option=GetImageOption(image_info,"caption:markup"); |
| 278 | if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse)) |
| 279 | pango_layout_set_markup(layout,caption,-1); |
| 280 | else |
| 281 | pango_layout_set_text(layout,caption,-1); |
| 282 | pango_layout_context_changed(layout); |
| 283 | page.x=0; |
| 284 | page.y=0; |
| 285 | if (image_info->page != (char *) NULL) |
| 286 | (void) ParseAbsoluteGeometry(image_info->page,&page); |
| 287 | if (image->columns == 0) |
| 288 | { |
| 289 | pango_layout_get_pixel_extents(layout,NULL,&extent); |
| 290 | image->columns=extent.x+extent.width; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | image->columns-=2*page.x; |
| 295 | pango_layout_set_width(layout,(PANGO_SCALE*image->columns* |
| 296 | image->resolution.x+36.0)/72.0); |
| 297 | } |
| 298 | if (image->rows == 0) |
| 299 | { |
| 300 | pango_layout_get_pixel_extents(layout,NULL,&extent); |
| 301 | image->rows=extent.y+extent.height; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | image->rows-=2*page.y; |
| 306 | pango_layout_set_height(layout,(PANGO_SCALE*image->rows* |
| 307 | image->resolution.y+36.0)/72.0); |
| 308 | } |
| 309 | /* |
| 310 | Create canvas. |
| 311 | */ |
| 312 | canvas=(FT_Bitmap *) AcquireMagickMemory(sizeof(*canvas)); |
| 313 | if (canvas == (FT_Bitmap *) NULL) |
| 314 | { |
| 315 | draw_info=DestroyDrawInfo(draw_info); |
| 316 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 317 | } |
| 318 | canvas->width=image->columns; |
| 319 | canvas->pitch=(canvas->width+3) & ~3; |
| 320 | canvas->rows=image->rows; |
| 321 | canvas->buffer=(unsigned char *) AcquireQuantumMemory(canvas->pitch, |
| 322 | canvas->rows*sizeof(*canvas->buffer)); |
| 323 | if (canvas->buffer == (unsigned char *) NULL) |
| 324 | { |
| 325 | draw_info=DestroyDrawInfo(draw_info); |
| 326 | canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); |
| 327 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 328 | } |
| 329 | canvas->num_grays=256; |
| 330 | canvas->pixel_mode=ft_pixel_mode_grays; |
| 331 | ResetMagickMemory(canvas->buffer,0x00,canvas->pitch*canvas->rows); |
| 332 | pango_ft2_render_layout(canvas,layout,0,0); |
| 333 | /* |
| 334 | Convert caption to image. |
| 335 | */ |
| 336 | image->columns+=2*page.x; |
| 337 | image->rows+=2*page.y; |
| 338 | if (SetImageBackgroundColor(image,exception) == MagickFalse) |
| 339 | { |
| 340 | draw_info=DestroyDrawInfo(draw_info); |
| 341 | canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer); |
| 342 | canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); |
| 343 | caption=DestroyString(caption); |
| 344 | image=DestroyImageList(image); |
| 345 | return((Image *) NULL); |
| 346 | } |
| 347 | GetPixelInfo(image,&fill_color); |
| 348 | p=canvas->buffer; |
| 349 | for (y=page.y; y < (ssize_t) (image->rows-page.y); y++) |
| 350 | { |
| 351 | register ssize_t |
| 352 | x; |
| 353 | |
| 354 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 355 | if (q == (Quantum *) NULL) |
| 356 | break; |
| 357 | q+=page.x*GetPixelChannels(image); |
| 358 | for (x=page.x; x < (ssize_t) (image->columns-page.x); x++) |
| 359 | { |
| 360 | MagickRealType |
cristy | f5d0a1a | 2012-02-03 12:33:10 +0000 | [diff] [blame] | 361 | fill_alpha; |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 362 | |
| 363 | (void) GetFillColor(draw_info,x,y,&fill_color,exception); |
cristy | f5d0a1a | 2012-02-03 12:33:10 +0000 | [diff] [blame] | 364 | fill_alpha=(MagickRealType) (*p)/(canvas->num_grays-1); |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 365 | if (draw_info->text_antialias == MagickFalse) |
cristy | f5d0a1a | 2012-02-03 12:33:10 +0000 | [diff] [blame] | 366 | fill_alpha=fill_alpha >= 0.5 ? 1.0 : 0.0; |
| 367 | fill_alpha=fill_alpha*fill_color.alpha; |
| 368 | CompositePixelOver(image,&fill_color,fill_alpha,q,GetPixelAlpha(image,q), |
| 369 | q); |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 370 | p++; |
| 371 | q+=GetPixelChannels(image); |
| 372 | } |
| 373 | for ( ; x < (ssize_t) ((canvas->width+3) & ~3); x++) |
| 374 | p++; |
| 375 | } |
| 376 | /* |
| 377 | Relinquish resources. |
| 378 | */ |
| 379 | draw_info=DestroyDrawInfo(draw_info); |
| 380 | canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer); |
| 381 | canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); |
| 382 | caption=DestroyString(caption); |
| 383 | return(GetFirstImageInList(image)); |
| 384 | } |
| 385 | #else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 386 | static Image *ReadCAPTIONImage(const ImageInfo *image_info, |
| 387 | ExceptionInfo *exception) |
| 388 | { |
| 389 | char |
| 390 | *caption, |
| 391 | geometry[MaxTextExtent], |
| 392 | *property; |
| 393 | |
| 394 | const char |
| 395 | *gravity; |
| 396 | |
| 397 | DrawInfo |
| 398 | *draw_info; |
| 399 | |
| 400 | Image |
| 401 | *image; |
| 402 | |
| 403 | MagickBooleanType |
| 404 | status; |
| 405 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 406 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 407 | i; |
| 408 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 409 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 410 | height, |
| 411 | width; |
| 412 | |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 413 | TypeMetric |
| 414 | metrics; |
| 415 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | /* |
| 417 | Initialize Image structure. |
| 418 | */ |
| 419 | assert(image_info != (const ImageInfo *) NULL); |
| 420 | assert(image_info->signature == MagickSignature); |
| 421 | if (image_info->debug != MagickFalse) |
| 422 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 423 | image_info->filename); |
| 424 | assert(exception != (ExceptionInfo *) NULL); |
| 425 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 426 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 427 | if (image->columns == 0) |
| 428 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
| 429 | (void) ResetImagePage(image,"0x0+0+0"); |
| 430 | /* |
| 431 | Format caption. |
| 432 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 433 | property=InterpretImageProperties(image_info,image,image_info->filename, |
| 434 | exception); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 435 | (void) SetImageProperty(image,"caption",property,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 436 | property=DestroyString(property); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 437 | caption=ConstantString(GetImageProperty(image,"caption",exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 438 | draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); |
cristy | 6fc3929 | 2011-10-23 14:26:30 +0000 | [diff] [blame] | 439 | (void) CloneString(&draw_info->text,caption); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 440 | gravity=GetImageOption(image_info,"gravity"); |
| 441 | if (gravity != (char *) NULL) |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 442 | draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 443 | MagickFalse,gravity); |
| 444 | if ((*caption != '\0') && (image->rows != 0) && |
| 445 | (image_info->pointsize == 0.0)) |
| 446 | { |
| 447 | char |
| 448 | *text; |
| 449 | |
| 450 | /* |
| 451 | Scale text to fit bounding box. |
| 452 | */ |
| 453 | for ( ; ; ) |
| 454 | { |
| 455 | text=AcquireString(caption); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 456 | i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text, |
| 457 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 458 | (void) CloneString(&draw_info->text,text); |
| 459 | text=DestroyString(text); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 460 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 461 | -metrics.bounds.x1,metrics.ascent); |
| 462 | if (draw_info->gravity == UndefinedGravity) |
| 463 | (void) CloneString(&draw_info->geometry,geometry); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 464 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 465 | (void) status; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 466 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 467 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 468 | if ((width > (image->columns+1)) || (height > (image->rows+1))) |
| 469 | break; |
| 470 | draw_info->pointsize*=2.0; |
| 471 | } |
| 472 | draw_info->pointsize/=2.0; |
| 473 | for ( ; ; ) |
| 474 | { |
| 475 | text=AcquireString(caption); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 476 | i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text, |
| 477 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 478 | (void) CloneString(&draw_info->text,text); |
| 479 | text=DestroyString(text); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 480 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 481 | -metrics.bounds.x1,metrics.ascent); |
| 482 | if (draw_info->gravity == UndefinedGravity) |
| 483 | (void) CloneString(&draw_info->geometry,geometry); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 484 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 485 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 486 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 487 | if ((width > (image->columns+1)) || (height > (image->rows+1))) |
| 488 | break; |
| 489 | draw_info->pointsize++; |
| 490 | } |
| 491 | draw_info->pointsize--; |
| 492 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 493 | i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 494 | if (image->rows == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 495 | image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+ |
cristy | cc2fb02 | 2011-04-01 14:08:37 +0000 | [diff] [blame] | 496 | draw_info->interline_spacing+draw_info->stroke_width)+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | if (image->rows == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 498 | image->rows=(size_t) ((i+1)*draw_info->pointsize+ |
cristy | cc2fb02 | 2011-04-01 14:08:37 +0000 | [diff] [blame] | 499 | draw_info->interline_spacing+draw_info->stroke_width+0.5); |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 500 | if (SetImageBackgroundColor(image,exception) == MagickFalse) |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 501 | { |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 502 | image=DestroyImageList(image); |
| 503 | return((Image *) NULL); |
| 504 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 505 | /* |
| 506 | Draw caption. |
| 507 | */ |
| 508 | (void) CloneString(&draw_info->text,caption); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 509 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 510 | if ((draw_info->gravity != UndefinedGravity) && |
| 511 | (draw_info->direction != RightToLeftDirection)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 512 | image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 513 | else |
| 514 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 515 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 516 | -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ |
| 517 | draw_info->stroke_width/2.0); |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 518 | if (draw_info->direction == RightToLeftDirection) |
| 519 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
| 520 | image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0), |
| 521 | metrics.ascent+draw_info->stroke_width/2.0); |
| 522 | draw_info->geometry=AcquireString(geometry); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 523 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 524 | (void) AnnotateImage(image,draw_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 525 | draw_info=DestroyDrawInfo(draw_info); |
| 526 | caption=DestroyString(caption); |
| 527 | return(GetFirstImageInList(image)); |
| 528 | } |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 529 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 530 | |
| 531 | /* |
| 532 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 533 | % % |
| 534 | % % |
| 535 | % % |
| 536 | % R e g i s t e r C A P T I O N I m a g e % |
| 537 | % % |
| 538 | % % |
| 539 | % % |
| 540 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 541 | % |
| 542 | % RegisterCAPTIONImage() adds attributes for the CAPTION image format to |
| 543 | % the list of supported formats. The attributes include the image format |
| 544 | % tag, a method to read and/or write the format, whether the format |
| 545 | % supports the saving of more than one frame to the same file or blob, |
| 546 | % whether the format supports native in-memory I/O, and a brief |
| 547 | % description of the format. |
| 548 | % |
| 549 | % The format of the RegisterCAPTIONImage method is: |
| 550 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 551 | % size_t RegisterCAPTIONImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 552 | % |
| 553 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 554 | ModuleExport size_t RegisterCAPTIONImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 555 | { |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 556 | char |
| 557 | version[MaxTextExtent]; |
| 558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 559 | MagickInfo |
| 560 | *entry; |
| 561 | |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 562 | *version='\0'; |
| 563 | #if defined(PANGO_VERSION_STRING) |
cristy | c3f3335 | 2011-11-19 03:01:16 +0000 | [diff] [blame] | 564 | (void) FormatLocaleString(version,MaxTextExtent,"Pangoft2 %s", |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 565 | PANGO_VERSION_STRING); |
| 566 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 567 | entry=SetMagickInfo("CAPTION"); |
| 568 | entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage; |
cristy | 8a3ce7f | 2011-10-30 02:28:03 +0000 | [diff] [blame] | 569 | entry->description=ConstantString("Caption"); |
| 570 | if (*version != '\0') |
| 571 | entry->version=ConstantString(version); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 572 | entry->adjoin=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 573 | entry->module=ConstantString("CAPTION"); |
| 574 | (void) RegisterMagickInfo(entry); |
| 575 | return(MagickImageCoderSignature); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 580 | % % |
| 581 | % % |
| 582 | % % |
| 583 | % U n r e g i s t e r C A P T I O N I m a g e % |
| 584 | % % |
| 585 | % % |
| 586 | % % |
| 587 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 588 | % |
| 589 | % UnregisterCAPTIONImage() removes format registrations made by the |
| 590 | % CAPTION module from the list of supported formats. |
| 591 | % |
| 592 | % The format of the UnregisterCAPTIONImage method is: |
| 593 | % |
| 594 | % UnregisterCAPTIONImage(void) |
| 595 | % |
| 596 | */ |
| 597 | ModuleExport void UnregisterCAPTIONImage(void) |
| 598 | { |
| 599 | (void) UnregisterMagickInfo("CAPTION"); |
| 600 | } |