cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % AAA N N N N OOO TTTTT AAA TTTTT EEEEE % |
| 7 | % A A NN N NN N O O T A A T E % |
| 8 | % AAAAA N N N N N N O O T AAAAA T EEE % |
| 9 | % A A N NN N NN O O T A A T E % |
| 10 | % A A N N N N OOO T A A T EEEEE % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Image Annotation Methods % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
| 20 | % Copyright 1999-2009 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 | % Digital Applications (www.digapp.com) contributed the stroked text algorithm. |
| 37 | % It was written by Leonard Rosenthol. |
| 38 | % |
| 39 | % |
| 40 | */ |
| 41 | |
| 42 | /* |
| 43 | Include declarations. |
| 44 | */ |
| 45 | #include "magick/studio.h" |
| 46 | #include "magick/annotate.h" |
| 47 | #include "magick/cache-view.h" |
| 48 | #include "magick/client.h" |
| 49 | #include "magick/color.h" |
| 50 | #include "magick/color-private.h" |
| 51 | #include "magick/composite.h" |
| 52 | #include "magick/composite-private.h" |
| 53 | #include "magick/constitute.h" |
| 54 | #include "magick/draw.h" |
| 55 | #include "magick/draw-private.h" |
| 56 | #include "magick/exception.h" |
| 57 | #include "magick/exception-private.h" |
| 58 | #include "magick/gem.h" |
| 59 | #include "magick/geometry.h" |
| 60 | #include "magick/image-private.h" |
| 61 | #include "magick/log.h" |
| 62 | #include "magick/quantum.h" |
| 63 | #include "magick/quantum-private.h" |
| 64 | #include "magick/property.h" |
| 65 | #include "magick/resource_.h" |
| 66 | #include "magick/statistic.h" |
| 67 | #include "magick/string_.h" |
| 68 | #include "magick/token-private.h" |
| 69 | #include "magick/transform.h" |
| 70 | #include "magick/type.h" |
| 71 | #include "magick/utility.h" |
| 72 | #include "magick/xwindow-private.h" |
| 73 | #if defined(MAGICKCORE_FREETYPE_DELEGATE) |
| 74 | #if defined(__MINGW32__) |
| 75 | # undef interface |
| 76 | #endif |
| 77 | #if defined(MAGICKCORE_HAVE_FT2BUILD_H) |
| 78 | # include <ft2build.h> |
| 79 | #endif |
| 80 | #if defined(FT_FREETYPE_H) |
| 81 | # include FT_FREETYPE_H |
| 82 | #else |
| 83 | # include <freetype/freetype.h> |
| 84 | #endif |
| 85 | #if defined(FT_GLYPH_H) |
| 86 | # include FT_GLYPH_H |
| 87 | #else |
| 88 | # include <freetype/ftglyph.h> |
| 89 | #endif |
| 90 | #if defined(FT_OUTLINE_H) |
| 91 | # include FT_OUTLINE_H |
| 92 | #else |
| 93 | # include <freetype/ftoutln.h> |
| 94 | #endif |
| 95 | #if defined(FT_BBOX_H) |
| 96 | # include FT_BBOX_H |
| 97 | #else |
| 98 | # include <freetype/ftbbox.h> |
| 99 | #endif /* defined(FT_BBOX_H) */ |
| 100 | #endif |
| 101 | |
| 102 | /* |
| 103 | Forward declarations. |
| 104 | */ |
| 105 | static MagickBooleanType |
| 106 | RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *), |
| 107 | RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *), |
| 108 | RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *, |
| 109 | TypeMetric *), |
| 110 | RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *); |
| 111 | |
| 112 | /* |
| 113 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 114 | % % |
| 115 | % % |
| 116 | % % |
| 117 | % A n n o t a t e I m a g e % |
| 118 | % % |
| 119 | % % |
| 120 | % % |
| 121 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 122 | % |
| 123 | % AnnotateImage() annotates an image with text. Optionally you can include |
| 124 | % any of the following bits of information about the image by embedding |
| 125 | % the appropriate special characters: |
| 126 | % |
| 127 | % %b file size in bytes. |
| 128 | % %c comment. |
| 129 | % %d directory in which the image resides. |
| 130 | % %e extension of the image file. |
| 131 | % %f original filename of the image. |
| 132 | % %h height of image. |
| 133 | % %i filename of the image. |
| 134 | % %k number of unique colors. |
| 135 | % %l image label. |
| 136 | % %m image file format. |
| 137 | % %n number of images in a image sequence. |
| 138 | % %o output image filename. |
| 139 | % %p page number of the image. |
| 140 | % %q image depth (8 or 16). |
| 141 | % %q image depth (8 or 16). |
| 142 | % %s image scene number. |
| 143 | % %t image filename without any extension. |
| 144 | % %u a unique temporary filename. |
| 145 | % %w image width. |
| 146 | % %x x resolution of the image. |
| 147 | % %y y resolution of the image. |
| 148 | % |
| 149 | % The format of the AnnotateImage method is: |
| 150 | % |
| 151 | % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info) |
| 152 | % |
| 153 | % A description of each parameter follows: |
| 154 | % |
| 155 | % o image: the image. |
| 156 | % |
| 157 | % o draw_info: the draw info. |
| 158 | % |
| 159 | */ |
| 160 | MagickExport MagickBooleanType AnnotateImage(Image *image, |
| 161 | const DrawInfo *draw_info) |
| 162 | { |
| 163 | char |
| 164 | primitive[MaxTextExtent], |
| 165 | **textlist; |
| 166 | |
| 167 | DrawInfo |
| 168 | *annotate, |
| 169 | *annotate_info; |
| 170 | |
| 171 | GeometryInfo |
| 172 | geometry_info; |
| 173 | |
| 174 | MagickBooleanType |
| 175 | status; |
| 176 | |
| 177 | PointInfo |
| 178 | offset; |
| 179 | |
| 180 | RectangleInfo |
| 181 | geometry; |
| 182 | |
| 183 | register long |
| 184 | i; |
| 185 | |
| 186 | size_t |
| 187 | length; |
| 188 | |
| 189 | TypeMetric |
| 190 | metrics; |
| 191 | |
| 192 | unsigned long |
cristy | 5846039 | 2009-09-09 01:52:06 +0000 | [diff] [blame] | 193 | height, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 194 | number_lines; |
| 195 | |
| 196 | assert(image != (Image *) NULL); |
| 197 | assert(image->signature == MagickSignature); |
| 198 | if (image->debug != MagickFalse) |
| 199 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 200 | assert(draw_info != (DrawInfo *) NULL); |
| 201 | assert(draw_info->signature == MagickSignature); |
| 202 | if (draw_info->text == (char *) NULL) |
| 203 | return(MagickFalse); |
| 204 | if (*draw_info->text == '\0') |
| 205 | return(MagickTrue); |
| 206 | textlist=StringToList(draw_info->text); |
| 207 | if (textlist == (char **) NULL) |
| 208 | return(MagickFalse); |
| 209 | length=strlen(textlist[0]); |
| 210 | for (i=1; textlist[i] != (char *) NULL; i++) |
| 211 | if (strlen(textlist[i]) > length) |
| 212 | length=strlen(textlist[i]); |
| 213 | number_lines=(unsigned long) i; |
| 214 | annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 215 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 216 | SetGeometry(image,&geometry); |
| 217 | SetGeometryInfo(&geometry_info); |
| 218 | if (annotate_info->geometry != (char *) NULL) |
| 219 | { |
| 220 | (void) ParsePageGeometry(image,annotate_info->geometry,&geometry, |
| 221 | &image->exception); |
| 222 | (void) ParseGeometry(annotate_info->geometry,&geometry_info); |
| 223 | } |
| 224 | if (SetImageStorageClass(image,DirectClass) == MagickFalse) |
| 225 | return(MagickFalse); |
| 226 | status=MagickTrue; |
| 227 | for (i=0; textlist[i] != (char *) NULL; i++) |
| 228 | { |
| 229 | /* |
| 230 | Position text relative to image. |
| 231 | */ |
| 232 | annotate_info->affine.tx=geometry_info.xi-image->page.x; |
| 233 | annotate_info->affine.ty=geometry_info.psi-image->page.y; |
| 234 | (void) CloneString(&annotate->text,textlist[i]); |
| 235 | (void) GetTypeMetrics(image,annotate,&metrics); |
cristy | 5846039 | 2009-09-09 01:52:06 +0000 | [diff] [blame] | 236 | height=(long) (metrics.ascent-metrics.descent+ |
| 237 | draw_info->interline_spacing+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | switch (annotate->gravity) |
| 239 | { |
| 240 | case UndefinedGravity: |
| 241 | default: |
| 242 | { |
| 243 | offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height; |
| 244 | offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height; |
| 245 | break; |
| 246 | } |
| 247 | case NorthWestGravity: |
| 248 | { |
| 249 | offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i* |
| 250 | annotate_info->affine.ry*height+annotate_info->affine.ry* |
| 251 | (metrics.ascent+metrics.descent); |
| 252 | offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i* |
| 253 | annotate_info->affine.sy*height+annotate_info->affine.sy* |
| 254 | metrics.ascent; |
| 255 | break; |
| 256 | } |
| 257 | case NorthGravity: |
| 258 | { |
| 259 | offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+ |
| 260 | geometry.width/2.0+i*annotate_info->affine.ry*height- |
| 261 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+ |
| 262 | annotate_info->affine.ry*(metrics.ascent+metrics.descent); |
| 263 | offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i* |
| 264 | annotate_info->affine.sy*height+annotate_info->affine.sy* |
| 265 | metrics.ascent-annotate_info->affine.rx*(metrics.width- |
| 266 | metrics.bounds.x1)/2.0; |
| 267 | break; |
| 268 | } |
| 269 | case NorthEastGravity: |
| 270 | { |
| 271 | offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+ |
| 272 | geometry.width+i*annotate_info->affine.ry*height- |
| 273 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+ |
| 274 | annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0; |
| 275 | offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i* |
| 276 | annotate_info->affine.sy*height+annotate_info->affine.sy* |
| 277 | metrics.ascent-annotate_info->affine.rx*(metrics.width- |
| 278 | metrics.bounds.x1); |
| 279 | break; |
| 280 | } |
| 281 | case WestGravity: |
| 282 | { |
| 283 | offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i* |
| 284 | annotate_info->affine.ry*height+annotate_info->affine.ry* |
| 285 | (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0; |
| 286 | offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+ |
| 287 | geometry.height/2.0+i*annotate_info->affine.sy*height+ |
| 288 | annotate_info->affine.sy*(metrics.ascent+metrics.descent- |
| 289 | (number_lines-1.0)*height)/2.0; |
| 290 | break; |
| 291 | } |
| 292 | case StaticGravity: |
| 293 | case CenterGravity: |
| 294 | { |
| 295 | offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+ |
| 296 | geometry.width/2.0+i*annotate_info->affine.ry*height- |
| 297 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+ |
| 298 | annotate_info->affine.ry*(metrics.ascent+metrics.descent- |
| 299 | (number_lines-1)*height)/2.0; |
| 300 | offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+ |
| 301 | geometry.height/2.0+i*annotate_info->affine.sy*height- |
| 302 | annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+ |
| 303 | annotate_info->affine.sy*(metrics.ascent+metrics.descent- |
| 304 | (number_lines-1.0)*height)/2.0; |
| 305 | break; |
| 306 | } |
| 307 | case EastGravity: |
| 308 | { |
| 309 | offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+ |
| 310 | geometry.width+i*annotate_info->affine.ry*height- |
| 311 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+ |
| 312 | annotate_info->affine.ry*(metrics.ascent+metrics.descent- |
| 313 | (number_lines-1.0)*height)/2.0-1.0; |
| 314 | offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+ |
| 315 | geometry.height/2.0+i*annotate_info->affine.sy*height- |
| 316 | annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+ |
| 317 | annotate_info->affine.sy*(metrics.ascent+metrics.descent- |
| 318 | (number_lines-1.0)*height)/2.0; |
| 319 | break; |
| 320 | } |
| 321 | case SouthWestGravity: |
| 322 | { |
| 323 | offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i* |
| 324 | annotate_info->affine.ry*height-annotate_info->affine.ry* |
| 325 | (number_lines-1.0)*height; |
| 326 | offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+ |
| 327 | geometry.height+i*annotate_info->affine.sy*height- |
| 328 | annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent; |
| 329 | break; |
| 330 | } |
| 331 | case SouthGravity: |
| 332 | { |
| 333 | offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+ |
| 334 | geometry.width/2.0+i*annotate_info->affine.ry*height- |
| 335 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0- |
| 336 | annotate_info->affine.ry*(number_lines-1.0)*height/2.0; |
| 337 | offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+ |
| 338 | geometry.height+i*annotate_info->affine.sy*height- |
| 339 | annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0- |
| 340 | annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent; |
| 341 | break; |
| 342 | } |
| 343 | case SouthEastGravity: |
| 344 | { |
| 345 | offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+ |
| 346 | geometry.width+i*annotate_info->affine.ry*height- |
| 347 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)- |
| 348 | annotate_info->affine.ry*(number_lines-1.0)*height-1.0; |
| 349 | offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+ |
| 350 | geometry.height+i*annotate_info->affine.sy*height- |
| 351 | annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)- |
| 352 | annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | switch (annotate->align) |
| 357 | { |
| 358 | case LeftAlign: |
| 359 | { |
| 360 | offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height; |
| 361 | offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height; |
| 362 | break; |
| 363 | } |
| 364 | case CenterAlign: |
| 365 | { |
| 366 | offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height- |
| 367 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0; |
| 368 | offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height- |
| 369 | annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0; |
| 370 | break; |
| 371 | } |
| 372 | case RightAlign: |
| 373 | { |
| 374 | offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height- |
| 375 | annotate_info->affine.sx*(metrics.width+metrics.bounds.x1); |
| 376 | offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height- |
| 377 | annotate_info->affine.rx*(metrics.width+metrics.bounds.x1); |
| 378 | break; |
| 379 | } |
| 380 | default: |
| 381 | break; |
| 382 | } |
| 383 | if (draw_info->undercolor.opacity != TransparentOpacity) |
| 384 | { |
| 385 | DrawInfo |
| 386 | *undercolor_info; |
| 387 | |
| 388 | /* |
| 389 | Text box. |
| 390 | */ |
| 391 | undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL); |
| 392 | undercolor_info->fill=draw_info->undercolor; |
| 393 | undercolor_info->affine=draw_info->affine; |
| 394 | undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent; |
| 395 | undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent; |
| 396 | (void) FormatMagickString(primitive,MaxTextExtent, |
cristy | 5846039 | 2009-09-09 01:52:06 +0000 | [diff] [blame] | 397 | "rectangle 0,0 %g,%lu",metrics.origin.x,height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 398 | (void) CloneString(&undercolor_info->primitive,primitive); |
| 399 | (void) DrawImage(image,undercolor_info); |
| 400 | (void) DestroyDrawInfo(undercolor_info); |
| 401 | } |
| 402 | annotate_info->affine.tx=offset.x; |
| 403 | annotate_info->affine.ty=offset.y; |
| 404 | (void) FormatMagickString(primitive,MaxTextExtent,"stroke-width %g " |
| 405 | "line 0,0 %g,0",metrics.underline_thickness,metrics.width); |
| 406 | if (annotate->decorate == OverlineDecoration) |
| 407 | { |
| 408 | annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+ |
| 409 | metrics.descent-metrics.underline_position)); |
| 410 | (void) CloneString(&annotate_info->primitive,primitive); |
| 411 | (void) DrawImage(image,annotate_info); |
| 412 | } |
| 413 | else |
| 414 | if (annotate->decorate == UnderlineDecoration) |
| 415 | { |
| 416 | annotate_info->affine.ty-=(draw_info->affine.sy* |
| 417 | metrics.underline_position); |
| 418 | (void) CloneString(&annotate_info->primitive,primitive); |
| 419 | (void) DrawImage(image,annotate_info); |
| 420 | } |
| 421 | /* |
| 422 | Annotate image with text. |
| 423 | */ |
| 424 | status=RenderType(image,annotate,&offset,&metrics); |
| 425 | if (status == MagickFalse) |
| 426 | break; |
| 427 | if (annotate->decorate == LineThroughDecoration) |
| 428 | { |
| 429 | annotate_info->affine.ty-=(draw_info->affine.sy*(height+ |
| 430 | metrics.underline_position+metrics.descent)/2.0); |
| 431 | (void) CloneString(&annotate_info->primitive,primitive); |
| 432 | (void) DrawImage(image,annotate_info); |
| 433 | } |
| 434 | } |
| 435 | /* |
| 436 | Relinquish resources. |
| 437 | */ |
| 438 | annotate_info=DestroyDrawInfo(annotate_info); |
| 439 | annotate=DestroyDrawInfo(annotate); |
| 440 | for (i=0; textlist[i] != (char *) NULL; i++) |
| 441 | textlist[i]=DestroyString(textlist[i]); |
| 442 | textlist=(char **) RelinquishMagickMemory(textlist); |
| 443 | return(status); |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 448 | % % |
| 449 | % % |
| 450 | % % |
| 451 | % F o r m a t M a g i c k C a p t i o n % |
| 452 | % % |
| 453 | % % |
| 454 | % % |
| 455 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 456 | % |
| 457 | % FormatMagickCaption() formats a caption so that it fits within the image |
| 458 | % width. It returns the number of lines in the formatted caption. |
| 459 | % |
| 460 | % The format of the FormatMagickCaption method is: |
| 461 | % |
| 462 | % long FormatMagickCaption(Image *image,DrawInfo *draw_info, |
| 463 | % TypeMetric *metrics,char **caption) |
| 464 | % |
| 465 | % A description of each parameter follows. |
| 466 | % |
| 467 | % o image: The image. |
| 468 | % |
| 469 | % o caption: the caption. |
| 470 | % |
| 471 | % o draw_info: the draw info. |
| 472 | % |
| 473 | % o metrics: Return the font metrics in this structure. |
| 474 | % |
| 475 | */ |
| 476 | MagickExport long FormatMagickCaption(Image *image,DrawInfo *draw_info, |
| 477 | TypeMetric *metrics,char **caption) |
| 478 | { |
| 479 | MagickBooleanType |
| 480 | status; |
| 481 | |
| 482 | register char |
| 483 | *p, |
| 484 | *q, |
| 485 | *s; |
| 486 | |
| 487 | register long |
| 488 | i; |
| 489 | |
| 490 | unsigned long |
| 491 | width; |
| 492 | |
| 493 | q=draw_info->text; |
| 494 | s=(char *) NULL; |
| 495 | for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p)) |
| 496 | { |
| 497 | if (IsUTFSpace(GetUTFCode(p)) != MagickFalse) |
| 498 | s=p; |
| 499 | for (i=0; i < (long) GetUTFOctets(p); i++) |
| 500 | *q++=(*(p+i)); |
| 501 | *q='\0'; |
| 502 | status=GetTypeMetrics(image,draw_info,metrics); |
| 503 | if (status == MagickFalse) |
| 504 | break; |
| 505 | width=(unsigned long) (metrics->width+0.5); |
| 506 | if (GetUTFCode(p) != '\n') |
| 507 | if (width <= image->columns) |
| 508 | continue; |
| 509 | if (s == (char *) NULL) |
| 510 | { |
| 511 | s=p; |
| 512 | while ((IsUTFSpace(GetUTFCode(s)) == MagickFalse) && |
| 513 | (GetUTFCode(s) != 0)) |
| 514 | s+=GetUTFOctets(s); |
| 515 | } |
| 516 | if (GetUTFCode(s) != 0) |
| 517 | { |
| 518 | *s='\n'; |
| 519 | p=s; |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | char |
| 524 | *target; |
| 525 | |
| 526 | long |
| 527 | n; |
| 528 | |
| 529 | /* |
| 530 | No convenient line breaks-- insert newline. |
| 531 | */ |
| 532 | target=AcquireString(*caption); |
| 533 | n=p-(*caption); |
| 534 | CopyMagickString(target,*caption,n+1); |
| 535 | ConcatenateMagickString(target,"\n",strlen(*caption)+1); |
| 536 | ConcatenateMagickString(target,p,strlen(*caption)+2); |
| 537 | (void) DestroyString(*caption); |
| 538 | *caption=target; |
| 539 | p=(*caption)+n; |
| 540 | } |
| 541 | s=(char *) NULL; |
| 542 | q=draw_info->text; |
| 543 | } |
| 544 | i=0; |
| 545 | for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p)) |
| 546 | if (GetUTFCode(p) == '\n') |
| 547 | i++; |
| 548 | return(i); |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 553 | % % |
| 554 | % % |
| 555 | % % |
| 556 | % G e t M u l t i l i n e T y p e M e t r i c s % |
| 557 | % % |
| 558 | % % |
| 559 | % % |
| 560 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 561 | % |
| 562 | % GetMultilineTypeMetrics() returns the following information for the |
| 563 | % specified font and text: |
| 564 | % |
| 565 | % character width |
| 566 | % character height |
| 567 | % ascender |
| 568 | % descender |
| 569 | % text width |
| 570 | % text height |
| 571 | % maximum horizontal advance |
| 572 | % bounds: x1 |
| 573 | % bounds: y1 |
| 574 | % bounds: x2 |
| 575 | % bounds: y2 |
| 576 | % origin: x |
| 577 | % origin: y |
| 578 | % underline position |
| 579 | % underline thickness |
| 580 | % |
| 581 | % This method is like GetTypeMetrics() but it returns the maximum text width |
| 582 | % and height for multiple lines of text. |
| 583 | % |
| 584 | % The format of the GetMultilineTypeMetrics method is: |
| 585 | % |
| 586 | % MagickBooleanType GetMultilineTypeMetrics(Image *image, |
| 587 | % const DrawInfo *draw_info,TypeMetric *metrics) |
| 588 | % |
| 589 | % A description of each parameter follows: |
| 590 | % |
| 591 | % o image: the image. |
| 592 | % |
| 593 | % o draw_info: the draw info. |
| 594 | % |
| 595 | % o metrics: Return the font metrics in this structure. |
| 596 | % |
| 597 | */ |
| 598 | MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image, |
| 599 | const DrawInfo *draw_info,TypeMetric *metrics) |
| 600 | { |
| 601 | char |
| 602 | **textlist; |
| 603 | |
| 604 | DrawInfo |
| 605 | *annotate_info; |
| 606 | |
| 607 | MagickBooleanType |
| 608 | status; |
| 609 | |
| 610 | register long |
| 611 | i; |
| 612 | |
| 613 | TypeMetric |
| 614 | extent; |
| 615 | |
| 616 | unsigned long |
| 617 | number_lines; |
| 618 | |
| 619 | assert(image != (Image *) NULL); |
| 620 | assert(image->signature == MagickSignature); |
| 621 | if (image->debug != MagickFalse) |
| 622 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 623 | assert(draw_info != (DrawInfo *) NULL); |
| 624 | assert(draw_info->text != (char *) NULL); |
| 625 | assert(draw_info->signature == MagickSignature); |
| 626 | if (*draw_info->text == '\0') |
| 627 | return(MagickFalse); |
| 628 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 629 | annotate_info->text=DestroyString(annotate_info->text); |
| 630 | /* |
| 631 | Convert newlines to multiple lines of text. |
| 632 | */ |
| 633 | textlist=StringToList(draw_info->text); |
| 634 | if (textlist == (char **) NULL) |
| 635 | return(MagickFalse); |
| 636 | annotate_info->render=MagickFalse; |
| 637 | (void) ResetMagickMemory(metrics,0,sizeof(*metrics)); |
| 638 | (void) ResetMagickMemory(&extent,0,sizeof(extent)); |
| 639 | /* |
| 640 | Find the widest of the text lines. |
| 641 | */ |
| 642 | annotate_info->text=textlist[0]; |
| 643 | status=GetTypeMetrics(image,annotate_info,&extent); |
| 644 | *metrics=extent; |
| 645 | for (i=1; textlist[i] != (char *) NULL; i++) |
| 646 | { |
| 647 | annotate_info->text=textlist[i]; |
| 648 | status=GetTypeMetrics(image,annotate_info,&extent); |
| 649 | if (extent.width > metrics->width) |
| 650 | *metrics=extent; |
| 651 | } |
| 652 | number_lines=(unsigned long) i; |
| 653 | metrics->height=(double) number_lines*(long) (metrics->ascent- |
cristy | 5846039 | 2009-09-09 01:52:06 +0000 | [diff] [blame] | 654 | metrics->descent+draw_info->interline_spacing+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 655 | /* |
| 656 | Relinquish resources. |
| 657 | */ |
| 658 | annotate_info->text=(char *) NULL; |
| 659 | annotate_info=DestroyDrawInfo(annotate_info); |
| 660 | for (i=0; textlist[i] != (char *) NULL; i++) |
| 661 | textlist[i]=DestroyString(textlist[i]); |
| 662 | textlist=(char **) RelinquishMagickMemory(textlist); |
| 663 | return(status); |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 668 | % % |
| 669 | % % |
| 670 | % % |
| 671 | % G e t T y p e M e t r i c s % |
| 672 | % % |
| 673 | % % |
| 674 | % % |
| 675 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 676 | % |
| 677 | % GetTypeMetrics() returns the following information for the specified font |
| 678 | % and text: |
| 679 | % |
| 680 | % character width |
| 681 | % character height |
| 682 | % ascender |
| 683 | % descender |
| 684 | % text width |
| 685 | % text height |
| 686 | % maximum horizontal advance |
| 687 | % bounds: x1 |
| 688 | % bounds: y1 |
| 689 | % bounds: x2 |
| 690 | % bounds: y2 |
| 691 | % origin: x |
| 692 | % origin: y |
| 693 | % underline position |
| 694 | % underline thickness |
| 695 | % |
| 696 | % The format of the GetTypeMetrics method is: |
| 697 | % |
| 698 | % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info, |
| 699 | % TypeMetric *metrics) |
| 700 | % |
| 701 | % A description of each parameter follows: |
| 702 | % |
| 703 | % o image: the image. |
| 704 | % |
| 705 | % o draw_info: the draw info. |
| 706 | % |
| 707 | % o metrics: Return the font metrics in this structure. |
| 708 | % |
| 709 | */ |
| 710 | MagickExport MagickBooleanType GetTypeMetrics(Image *image, |
| 711 | const DrawInfo *draw_info,TypeMetric *metrics) |
| 712 | { |
| 713 | DrawInfo |
| 714 | *annotate_info; |
| 715 | |
| 716 | MagickBooleanType |
| 717 | status; |
| 718 | |
| 719 | PointInfo |
| 720 | offset; |
| 721 | |
| 722 | assert(image != (Image *) NULL); |
| 723 | assert(image->signature == MagickSignature); |
| 724 | if (image->debug != MagickFalse) |
| 725 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 726 | assert(draw_info != (DrawInfo *) NULL); |
| 727 | assert(draw_info->text != (char *) NULL); |
| 728 | assert(draw_info->signature == MagickSignature); |
| 729 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 730 | annotate_info->render=MagickFalse; |
| 731 | (void) ResetMagickMemory(metrics,0,sizeof(*metrics)); |
| 732 | offset.x=0.0; |
| 733 | offset.y=0.0; |
| 734 | status=RenderType(image,annotate_info,&offset,metrics); |
| 735 | if (image->debug != MagickFalse) |
| 736 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; " |
| 737 | "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; " |
| 738 | "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; " |
| 739 | "underline position: %g; underline thickness: %g",annotate_info->text, |
| 740 | metrics->width,metrics->height,metrics->ascent,metrics->descent, |
| 741 | metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1, |
| 742 | metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y, |
| 743 | metrics->pixels_per_em.x,metrics->pixels_per_em.y, |
| 744 | metrics->underline_position,metrics->underline_thickness); |
| 745 | annotate_info=DestroyDrawInfo(annotate_info); |
| 746 | return(status); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 751 | % % |
| 752 | % % |
| 753 | % % |
| 754 | + R e n d e r T y p e % |
| 755 | % % |
| 756 | % % |
| 757 | % % |
| 758 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 759 | % |
| 760 | % RenderType() renders text on the image. It also returns the bounding box of |
| 761 | % the text relative to the image. |
| 762 | % |
| 763 | % The format of the RenderType method is: |
| 764 | % |
| 765 | % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info, |
| 766 | % const PointInfo *offset,TypeMetric *metrics) |
| 767 | % |
| 768 | % A description of each parameter follows: |
| 769 | % |
| 770 | % o image: the image. |
| 771 | % |
| 772 | % o draw_info: the draw info. |
| 773 | % |
| 774 | % o offset: (x,y) location of text relative to image. |
| 775 | % |
| 776 | % o metrics: bounding box of text. |
| 777 | % |
| 778 | */ |
| 779 | static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info, |
| 780 | const PointInfo *offset,TypeMetric *metrics) |
| 781 | { |
| 782 | const TypeInfo |
| 783 | *type_info; |
| 784 | |
| 785 | DrawInfo |
| 786 | *annotate_info; |
| 787 | |
| 788 | MagickBooleanType |
| 789 | status; |
| 790 | |
| 791 | type_info=(const TypeInfo *) NULL; |
| 792 | if (draw_info->font != (char *) NULL) |
| 793 | { |
| 794 | if (*draw_info->font == '@') |
| 795 | { |
| 796 | status=RenderFreetype(image,draw_info,draw_info->encoding,offset, |
| 797 | metrics); |
| 798 | return(status); |
| 799 | } |
| 800 | if (*draw_info->font == '-') |
| 801 | return(RenderX11(image,draw_info,offset,metrics)); |
| 802 | if (IsPathAccessible(draw_info->font) != MagickFalse) |
| 803 | { |
| 804 | status=RenderFreetype(image,draw_info,draw_info->encoding,offset, |
| 805 | metrics); |
| 806 | return(status); |
| 807 | } |
| 808 | type_info=GetTypeInfo(draw_info->font,&image->exception); |
| 809 | if (type_info == (const TypeInfo *) NULL) |
| 810 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 811 | TypeWarning,"UnableToReadFont","`%s'",draw_info->font); |
| 812 | } |
| 813 | if ((type_info == (const TypeInfo *) NULL) && |
| 814 | (draw_info->family != (const char *) NULL)) |
| 815 | { |
| 816 | type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style, |
| 817 | draw_info->stretch,draw_info->weight,&image->exception); |
| 818 | if (type_info == (const TypeInfo *) NULL) |
| 819 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 820 | TypeWarning,"UnableToReadFont","`%s'",draw_info->family); |
| 821 | } |
| 822 | if (type_info == (const TypeInfo *) NULL) |
| 823 | type_info=GetTypeInfoByFamily("arial",draw_info->style, |
| 824 | draw_info->stretch,draw_info->weight,&image->exception); |
| 825 | if (type_info == (const TypeInfo *) NULL) |
| 826 | type_info=GetTypeInfoByFamily("helvetica",draw_info->style, |
| 827 | draw_info->stretch,draw_info->weight,&image->exception); |
| 828 | if (type_info == (const TypeInfo *) NULL) |
| 829 | type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style, |
| 830 | draw_info->stretch,draw_info->weight,&image->exception); |
| 831 | if (type_info == (const TypeInfo *) NULL) |
| 832 | { |
| 833 | status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics); |
| 834 | return(status); |
| 835 | } |
| 836 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 837 | annotate_info->face=type_info->face; |
| 838 | if (type_info->metrics != (char *) NULL) |
| 839 | (void) CloneString(&annotate_info->metrics,type_info->metrics); |
| 840 | if (type_info->glyphs != (char *) NULL) |
| 841 | (void) CloneString(&annotate_info->font,type_info->glyphs); |
| 842 | status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics); |
| 843 | annotate_info=DestroyDrawInfo(annotate_info); |
| 844 | return(status); |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 849 | % % |
| 850 | % % |
| 851 | % % |
| 852 | + R e n d e r F r e e t y p e % |
| 853 | % % |
| 854 | % % |
| 855 | % % |
| 856 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 857 | % |
| 858 | % RenderFreetype() renders text on the image with a Truetype font. It also |
| 859 | % returns the bounding box of the text relative to the image. |
| 860 | % |
| 861 | % The format of the RenderFreetype method is: |
| 862 | % |
| 863 | % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info, |
| 864 | % const char *encoding,const PointInfo *offset,TypeMetric *metrics) |
| 865 | % |
| 866 | % A description of each parameter follows: |
| 867 | % |
| 868 | % o image: the image. |
| 869 | % |
| 870 | % o draw_info: the draw info. |
| 871 | % |
| 872 | % o encoding: the font encoding. |
| 873 | % |
| 874 | % o offset: (x,y) location of text relative to image. |
| 875 | % |
| 876 | % o metrics: bounding box of text. |
| 877 | % |
| 878 | */ |
| 879 | |
| 880 | #if defined(MAGICKCORE_FREETYPE_DELEGATE) |
| 881 | static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to, |
| 882 | DrawInfo *draw_info) |
| 883 | { |
| 884 | AffineMatrix |
| 885 | affine; |
| 886 | |
| 887 | char |
| 888 | path[MaxTextExtent]; |
| 889 | |
| 890 | affine=draw_info->affine; |
| 891 | (void) FormatMagickString(path,MaxTextExtent,"C%g,%g %g,%g %g,%g", |
| 892 | affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0, |
| 893 | affine.ty-q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0); |
| 894 | (void) ConcatenateString(&draw_info->primitive,path); |
| 895 | return(0); |
| 896 | } |
| 897 | |
| 898 | static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info) |
| 899 | { |
| 900 | AffineMatrix |
| 901 | affine; |
| 902 | |
| 903 | char |
| 904 | path[MaxTextExtent]; |
| 905 | |
| 906 | affine=draw_info->affine; |
| 907 | (void) FormatMagickString(path,MaxTextExtent,"L%g,%g",affine.tx+to->x/64.0, |
| 908 | affine.ty-to->y/64.0); |
| 909 | (void) ConcatenateString(&draw_info->primitive,path); |
| 910 | return(0); |
| 911 | } |
| 912 | |
| 913 | static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info) |
| 914 | { |
| 915 | AffineMatrix |
| 916 | affine; |
| 917 | |
| 918 | char |
| 919 | path[MaxTextExtent]; |
| 920 | |
| 921 | affine=draw_info->affine; |
| 922 | (void) FormatMagickString(path,MaxTextExtent,"M%g,%g",affine.tx+to->x/64.0, |
| 923 | affine.ty-to->y/64.0); |
| 924 | (void) ConcatenateString(&draw_info->primitive,path); |
| 925 | return(0); |
| 926 | } |
| 927 | |
| 928 | static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to, |
| 929 | DrawInfo *draw_info) |
| 930 | { |
| 931 | AffineMatrix |
| 932 | affine; |
| 933 | |
| 934 | char |
| 935 | path[MaxTextExtent]; |
| 936 | |
| 937 | affine=draw_info->affine; |
| 938 | (void) FormatMagickString(path,MaxTextExtent,"Q%g,%g %g,%g", |
| 939 | affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0, |
| 940 | affine.ty-to->y/64.0); |
| 941 | (void) ConcatenateString(&draw_info->primitive,path); |
| 942 | return(0); |
| 943 | } |
| 944 | |
| 945 | static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info, |
| 946 | const char *encoding,const PointInfo *offset,TypeMetric *metrics) |
| 947 | { |
| 948 | #if !defined(FT_OPEN_PATHNAME) |
| 949 | #define FT_OPEN_PATHNAME ft_open_pathname |
| 950 | #endif |
| 951 | |
| 952 | typedef struct _GlyphInfo |
| 953 | { |
| 954 | FT_UInt |
| 955 | id; |
| 956 | |
| 957 | FT_Vector |
| 958 | origin; |
| 959 | |
| 960 | FT_Glyph |
| 961 | image; |
| 962 | } GlyphInfo; |
| 963 | |
| 964 | const char |
| 965 | *value; |
| 966 | |
| 967 | DrawInfo |
| 968 | *annotate_info; |
| 969 | |
| 970 | FT_BBox |
| 971 | bounds; |
| 972 | |
| 973 | FT_BitmapGlyph |
| 974 | bitmap; |
| 975 | |
| 976 | FT_Encoding |
| 977 | encoding_type; |
| 978 | |
| 979 | FT_Error |
| 980 | status; |
| 981 | |
| 982 | FT_Face |
| 983 | face; |
| 984 | |
| 985 | FT_Int32 |
| 986 | flags; |
| 987 | |
| 988 | FT_Library |
| 989 | library; |
| 990 | |
| 991 | FT_Matrix |
| 992 | affine; |
| 993 | |
| 994 | FT_Open_Args |
| 995 | args; |
| 996 | |
| 997 | FT_Vector |
| 998 | origin; |
| 999 | |
| 1000 | GlyphInfo |
| 1001 | glyph, |
| 1002 | last_glyph; |
| 1003 | |
| 1004 | long |
| 1005 | code, |
| 1006 | y; |
| 1007 | |
| 1008 | PointInfo |
| 1009 | point, |
| 1010 | resolution; |
| 1011 | |
| 1012 | register char |
| 1013 | *p; |
| 1014 | |
| 1015 | static FT_Outline_Funcs |
| 1016 | OutlineMethods = |
| 1017 | { |
| 1018 | (FT_Outline_MoveTo_Func) TraceMoveTo, |
| 1019 | (FT_Outline_LineTo_Func) TraceLineTo, |
| 1020 | (FT_Outline_ConicTo_Func) TraceQuadraticBezier, |
| 1021 | (FT_Outline_CubicTo_Func) TraceCubicBezier, |
| 1022 | 0, 0 |
| 1023 | }; |
| 1024 | |
| 1025 | /* |
| 1026 | Initialize Truetype library. |
| 1027 | */ |
| 1028 | status=FT_Init_FreeType(&library); |
| 1029 | if (status != 0) |
| 1030 | ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary", |
| 1031 | image->filename); |
| 1032 | args.flags=FT_OPEN_PATHNAME; |
| 1033 | if (draw_info->font == (char *) NULL) |
| 1034 | args.pathname=ConstantString("helvetica"); |
| 1035 | else |
| 1036 | if (*draw_info->font != '@') |
| 1037 | args.pathname=ConstantString(draw_info->font); |
| 1038 | else |
| 1039 | args.pathname=ConstantString(draw_info->font+1); |
| 1040 | face=(FT_Face) NULL; |
| 1041 | status=FT_Open_Face(library,&args,draw_info->face,&face); |
| 1042 | args.pathname=DestroyString(args.pathname); |
| 1043 | if (status != 0) |
| 1044 | { |
| 1045 | (void) FT_Done_FreeType(library); |
| 1046 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1047 | TypeError,"UnableToReadFont","`%s'",draw_info->font); |
| 1048 | return(RenderPostscript(image,draw_info,offset,metrics)); |
| 1049 | } |
| 1050 | if ((draw_info->metrics != (char *) NULL) && |
| 1051 | (IsPathAccessible(draw_info->metrics) != MagickFalse)) |
| 1052 | (void) FT_Attach_File(face,draw_info->metrics); |
| 1053 | encoding_type=ft_encoding_unicode; |
| 1054 | status=FT_Select_Charmap(face,encoding_type); |
| 1055 | if ((status != 0) && (face->num_charmaps != 0)) |
| 1056 | status=FT_Set_Charmap(face,face->charmaps[0]); |
| 1057 | if (encoding != (const char *) NULL) |
| 1058 | { |
| 1059 | if (LocaleCompare(encoding,"AdobeCustom") == 0) |
| 1060 | encoding_type=ft_encoding_adobe_custom; |
| 1061 | if (LocaleCompare(encoding,"AdobeExpert") == 0) |
| 1062 | encoding_type=ft_encoding_adobe_expert; |
| 1063 | if (LocaleCompare(encoding,"AdobeStandard") == 0) |
| 1064 | encoding_type=ft_encoding_adobe_standard; |
| 1065 | if (LocaleCompare(encoding,"AppleRoman") == 0) |
| 1066 | encoding_type=ft_encoding_apple_roman; |
| 1067 | if (LocaleCompare(encoding,"BIG5") == 0) |
| 1068 | encoding_type=ft_encoding_big5; |
| 1069 | if (LocaleCompare(encoding,"GB2312") == 0) |
| 1070 | encoding_type=ft_encoding_gb2312; |
| 1071 | if (LocaleCompare(encoding,"Johab") == 0) |
| 1072 | encoding_type=ft_encoding_johab; |
| 1073 | #if defined(ft_encoding_latin_1) |
| 1074 | if (LocaleCompare(encoding,"Latin-1") == 0) |
| 1075 | encoding_type=ft_encoding_latin_1; |
| 1076 | #endif |
| 1077 | if (LocaleCompare(encoding,"Latin-2") == 0) |
| 1078 | encoding_type=ft_encoding_latin_2; |
| 1079 | if (LocaleCompare(encoding,"None") == 0) |
| 1080 | encoding_type=ft_encoding_none; |
| 1081 | if (LocaleCompare(encoding,"SJIScode") == 0) |
| 1082 | encoding_type=ft_encoding_sjis; |
| 1083 | if (LocaleCompare(encoding,"Symbol") == 0) |
| 1084 | encoding_type=ft_encoding_symbol; |
| 1085 | if (LocaleCompare(encoding,"Unicode") == 0) |
| 1086 | encoding_type=ft_encoding_unicode; |
| 1087 | if (LocaleCompare(encoding,"Wansung") == 0) |
| 1088 | encoding_type=ft_encoding_wansung; |
| 1089 | status=FT_Select_Charmap(face,encoding_type); |
| 1090 | if (status != 0) |
| 1091 | ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding); |
| 1092 | } |
| 1093 | /* |
| 1094 | Set text size. |
| 1095 | */ |
| 1096 | resolution.x=DefaultResolution; |
| 1097 | resolution.y=DefaultResolution; |
| 1098 | if (draw_info->density != (char *) NULL) |
| 1099 | { |
| 1100 | GeometryInfo |
| 1101 | geometry_info; |
| 1102 | |
| 1103 | MagickStatusType |
| 1104 | flags; |
| 1105 | |
| 1106 | flags=ParseGeometry(draw_info->density,&geometry_info); |
| 1107 | resolution.x=geometry_info.rho; |
| 1108 | resolution.y=geometry_info.sigma; |
| 1109 | if ((flags & SigmaValue) == 0) |
| 1110 | resolution.y=resolution.x; |
| 1111 | } |
| 1112 | status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize), |
| 1113 | (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x, |
| 1114 | (FT_UInt) resolution.y); |
| 1115 | metrics->pixels_per_em.x=face->size->metrics.x_ppem; |
| 1116 | metrics->pixels_per_em.y=face->size->metrics.y_ppem; |
| 1117 | metrics->ascent=(double) face->size->metrics.ascender/64.0; |
| 1118 | metrics->descent=(double) face->size->metrics.descender/64.0; |
| 1119 | metrics->width=0; |
| 1120 | metrics->origin.x=0; |
| 1121 | metrics->origin.y=0; |
| 1122 | metrics->height=(double) face->size->metrics.height/64.0; |
| 1123 | metrics->max_advance=0.0; |
| 1124 | if (face->size->metrics.max_advance > MagickEpsilon) |
| 1125 | metrics->max_advance=(double) face->size->metrics.max_advance/64.0; |
| 1126 | metrics->bounds.x1=0.0; |
| 1127 | metrics->bounds.y1=metrics->descent; |
| 1128 | metrics->bounds.x2=metrics->ascent+metrics->descent; |
| 1129 | metrics->bounds.y2=metrics->ascent+metrics->descent; |
| 1130 | metrics->underline_position=face->underline_position/64.0; |
| 1131 | metrics->underline_thickness=face->underline_thickness/64.0; |
| 1132 | if (*draw_info->text == '\0') |
| 1133 | { |
| 1134 | (void) FT_Done_Face(face); |
| 1135 | (void) FT_Done_FreeType(library); |
| 1136 | return(MagickTrue); |
| 1137 | } |
| 1138 | /* |
| 1139 | Compute bounding box. |
| 1140 | */ |
| 1141 | if (image->debug != MagickFalse) |
| 1142 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; " |
| 1143 | "font-encoding %s; text-encoding %s; pointsize %g", |
| 1144 | draw_info->font != (char *) NULL ? draw_info->font : "none", |
| 1145 | encoding != (char *) NULL ? encoding : "none", |
| 1146 | draw_info->encoding != (char *) NULL ? draw_info->encoding : "none", |
| 1147 | draw_info->pointsize); |
| 1148 | flags=FT_LOAD_NO_BITMAP; |
| 1149 | value=GetImageProperty(image,"type:hinting"); |
| 1150 | if (LocaleCompare(value,"off") == 0) |
| 1151 | flags|=FT_LOAD_NO_HINTING; |
| 1152 | glyph.id=0; |
| 1153 | glyph.image=NULL; |
| 1154 | last_glyph.id=0; |
| 1155 | last_glyph.image=NULL; |
| 1156 | origin.x=0; |
| 1157 | origin.y=0; |
| 1158 | affine.xx=65536L; |
| 1159 | affine.yx=0L; |
| 1160 | affine.xy=0L; |
| 1161 | affine.yy=65536L; |
| 1162 | if (draw_info->render != MagickFalse) |
| 1163 | { |
| 1164 | affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5); |
| 1165 | affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5); |
| 1166 | affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5); |
| 1167 | affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5); |
| 1168 | } |
| 1169 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 1170 | (void) CloneString(&annotate_info->primitive,"path '"); |
| 1171 | if (draw_info->render != MagickFalse) |
| 1172 | { |
| 1173 | if (image->storage_class != DirectClass) |
| 1174 | (void) SetImageStorageClass(image,DirectClass); |
| 1175 | if (image->matte == MagickFalse) |
| 1176 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel); |
| 1177 | } |
| 1178 | point.x=0.0; |
| 1179 | point.y=0.0; |
| 1180 | code=0; |
| 1181 | for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p)) |
| 1182 | { |
| 1183 | glyph.id=FT_Get_Char_Index(face,GetUTFCode(p)); |
| 1184 | if (glyph.id == 0) |
| 1185 | glyph.id=FT_Get_Char_Index(face,'?'); |
| 1186 | if ((glyph.id != 0) && (last_glyph.id != 0)) |
| 1187 | { |
| 1188 | if (draw_info->kerning != 0.0) |
| 1189 | origin.x+=64.0*draw_info->kerning; |
| 1190 | else |
| 1191 | if (FT_HAS_KERNING(face)) |
| 1192 | { |
| 1193 | FT_Vector |
| 1194 | kerning; |
| 1195 | |
| 1196 | status=FT_Get_Kerning(face,last_glyph.id,glyph.id, |
| 1197 | ft_kerning_default,&kerning); |
| 1198 | if (status == 0) |
| 1199 | origin.x+=kerning.x; |
| 1200 | } |
| 1201 | } |
| 1202 | glyph.origin=origin; |
| 1203 | status=FT_Load_Glyph(face,glyph.id,flags); |
| 1204 | if (status != 0) |
| 1205 | continue; |
| 1206 | status=FT_Get_Glyph(face->glyph,&glyph.image); |
| 1207 | if (status != 0) |
| 1208 | continue; |
| 1209 | status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline, |
| 1210 | &bounds); |
| 1211 | if (status != 0) |
| 1212 | continue; |
| 1213 | if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1)) |
| 1214 | metrics->bounds.x1=bounds.xMin; |
| 1215 | if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1)) |
| 1216 | metrics->bounds.y1=bounds.yMin; |
| 1217 | if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2)) |
| 1218 | metrics->bounds.x2=bounds.xMax; |
| 1219 | if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2)) |
| 1220 | metrics->bounds.y2=bounds.yMax; |
| 1221 | if (draw_info->render != MagickFalse) |
| 1222 | if ((draw_info->stroke.opacity != TransparentOpacity) || |
| 1223 | (draw_info->stroke_pattern != (Image *) NULL)) |
| 1224 | { |
| 1225 | /* |
| 1226 | Trace the glyph. |
| 1227 | */ |
| 1228 | annotate_info->affine.tx=glyph.origin.x/64.0; |
| 1229 | annotate_info->affine.ty=glyph.origin.y/64.0; |
| 1230 | (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->outline, |
| 1231 | &OutlineMethods,annotate_info); |
| 1232 | } |
| 1233 | FT_Vector_Transform(&glyph.origin,&affine); |
| 1234 | (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin); |
| 1235 | status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal, |
| 1236 | (FT_Vector *) NULL,MagickTrue); |
| 1237 | if (status != 0) |
| 1238 | continue; |
| 1239 | bitmap=(FT_BitmapGlyph) glyph.image; |
| 1240 | point.x=offset->x+bitmap->left; |
| 1241 | point.y=offset->y-bitmap->top; |
| 1242 | if (draw_info->render != MagickFalse) |
| 1243 | { |
| 1244 | CacheView |
| 1245 | *image_view; |
| 1246 | |
| 1247 | ExceptionInfo |
| 1248 | *exception; |
| 1249 | |
| 1250 | MagickBooleanType |
| 1251 | status; |
| 1252 | |
| 1253 | /* |
| 1254 | Rasterize the glyph. |
| 1255 | */ |
| 1256 | status=MagickTrue; |
| 1257 | exception=(&image->exception); |
| 1258 | image_view=AcquireCacheView(image); |
| 1259 | for (y=0; y < (long) bitmap->bitmap.rows; y++) |
| 1260 | { |
| 1261 | long |
| 1262 | x_offset, |
| 1263 | y_offset; |
| 1264 | |
| 1265 | MagickBooleanType |
| 1266 | active, |
| 1267 | sync; |
| 1268 | |
| 1269 | MagickRealType |
| 1270 | fill_opacity; |
| 1271 | |
| 1272 | PixelPacket |
| 1273 | fill_color; |
| 1274 | |
| 1275 | register long |
| 1276 | x; |
| 1277 | |
| 1278 | register PixelPacket |
| 1279 | *__restrict q; |
| 1280 | |
| 1281 | register unsigned char |
| 1282 | *p; |
| 1283 | |
| 1284 | if (status == MagickFalse) |
| 1285 | continue; |
| 1286 | x_offset=(long) (point.x+0.5); |
| 1287 | y_offset=(long) (point.y+y+0.5); |
| 1288 | if ((y_offset < 0) || (y_offset >= (long) image->rows)) |
| 1289 | continue; |
| 1290 | q=(PixelPacket *) NULL; |
| 1291 | if ((x_offset < 0) || (x_offset >= (long) image->columns)) |
| 1292 | active=MagickFalse; |
| 1293 | else |
| 1294 | { |
| 1295 | q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset, |
| 1296 | bitmap->bitmap.width,1,exception); |
| 1297 | active=q != (PixelPacket *) NULL ? MagickTrue : MagickFalse; |
| 1298 | } |
| 1299 | p=bitmap->bitmap.buffer+y*bitmap->bitmap.width; |
| 1300 | for (x=0; x < (long) bitmap->bitmap.width; x++) |
| 1301 | { |
| 1302 | x_offset++; |
| 1303 | if ((*p == 0) || (x_offset < 0) || |
| 1304 | (x_offset >= (long) image->columns)) |
| 1305 | { |
| 1306 | p++; |
| 1307 | q++; |
| 1308 | continue; |
| 1309 | } |
| 1310 | fill_opacity=(MagickRealType) (*p)/(bitmap->bitmap.num_grays-1); |
| 1311 | if (draw_info->text_antialias == MagickFalse) |
| 1312 | fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0; |
| 1313 | if (active == MagickFalse) |
| 1314 | q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1, |
| 1315 | exception); |
| 1316 | if (q == (PixelPacket *) NULL) |
| 1317 | { |
| 1318 | p++; |
| 1319 | q++; |
| 1320 | continue; |
| 1321 | } |
| 1322 | (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color); |
| 1323 | fill_opacity=QuantumRange-fill_opacity*(QuantumRange- |
| 1324 | fill_color.opacity); |
| 1325 | MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q); |
| 1326 | if (active == MagickFalse) |
| 1327 | { |
| 1328 | sync=SyncCacheViewAuthenticPixels(image_view,exception); |
| 1329 | if (sync == MagickFalse) |
| 1330 | status=MagickFalse; |
| 1331 | } |
| 1332 | p++; |
| 1333 | q++; |
| 1334 | } |
| 1335 | sync=SyncCacheViewAuthenticPixels(image_view,exception); |
| 1336 | if (sync == MagickFalse) |
| 1337 | status=MagickFalse; |
| 1338 | } |
| 1339 | image_view=DestroyCacheView(image_view); |
| 1340 | } |
| 1341 | if ((bitmap->left+bitmap->bitmap.width) > metrics->width) |
| 1342 | metrics->width=bitmap->left+bitmap->bitmap.width; |
| 1343 | if ((draw_info->interword_spacing != 0.0) && |
| 1344 | (IsUTFSpace(GetUTFCode(p)) != MagickFalse) && |
| 1345 | (IsUTFSpace(code) == MagickFalse)) |
| 1346 | origin.x+=64.0*draw_info->interword_spacing; |
| 1347 | else |
| 1348 | origin.x+=face->glyph->advance.x; |
| 1349 | metrics->origin.x=origin.x; |
| 1350 | metrics->origin.y=origin.y; |
| 1351 | if (last_glyph.id != 0) |
| 1352 | FT_Done_Glyph(last_glyph.image); |
| 1353 | last_glyph=glyph; |
| 1354 | code=GetUTFCode(p); |
| 1355 | } |
| 1356 | if (last_glyph.id != 0) |
| 1357 | FT_Done_Glyph(last_glyph.image); |
| 1358 | if ((draw_info->stroke.opacity != TransparentOpacity) || |
| 1359 | (draw_info->stroke_pattern != (Image *) NULL)) |
| 1360 | { |
| 1361 | if (draw_info->render != MagickFalse) |
| 1362 | { |
| 1363 | /* |
| 1364 | Draw text stroke. |
| 1365 | */ |
| 1366 | annotate_info->linejoin=RoundJoin; |
| 1367 | annotate_info->affine.tx=offset->x; |
| 1368 | annotate_info->affine.ty=offset->y; |
| 1369 | (void) ConcatenateString(&annotate_info->primitive,"'"); |
| 1370 | (void) DrawImage(image,annotate_info); |
| 1371 | } |
| 1372 | } |
| 1373 | /* |
| 1374 | Determine font metrics. |
| 1375 | */ |
| 1376 | glyph.id=FT_Get_Char_Index(face,'_'); |
| 1377 | glyph.origin=origin; |
| 1378 | status=FT_Load_Glyph(face,glyph.id,flags); |
| 1379 | if (status == 0) |
| 1380 | { |
| 1381 | status=FT_Get_Glyph(face->glyph,&glyph.image); |
| 1382 | if (status == 0) |
| 1383 | { |
| 1384 | status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)-> |
| 1385 | outline,&bounds); |
| 1386 | if (status == 0) |
| 1387 | { |
| 1388 | FT_Vector_Transform(&glyph.origin,&affine); |
| 1389 | (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin); |
| 1390 | status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal, |
| 1391 | (FT_Vector *) NULL,MagickTrue); |
| 1392 | bitmap=(FT_BitmapGlyph) glyph.image; |
| 1393 | if (bitmap->left > metrics->width) |
| 1394 | metrics->width=bitmap->left; |
| 1395 | } |
| 1396 | } |
| 1397 | if (glyph.id != 0) |
| 1398 | FT_Done_Glyph(glyph.image); |
| 1399 | } |
| 1400 | metrics->width-=metrics->bounds.x1/64.0; |
| 1401 | metrics->bounds.x1/=64.0; |
| 1402 | metrics->bounds.y1/=64.0; |
| 1403 | metrics->bounds.x2/=64.0; |
| 1404 | metrics->bounds.y2/=64.0; |
| 1405 | metrics->origin.x/=64.0; |
| 1406 | metrics->origin.y/=64.0; |
| 1407 | /* |
| 1408 | Relinquish resources. |
| 1409 | */ |
| 1410 | annotate_info=DestroyDrawInfo(annotate_info); |
| 1411 | (void) FT_Done_Face(face); |
| 1412 | (void) FT_Done_FreeType(library); |
| 1413 | return(MagickTrue); |
| 1414 | } |
| 1415 | #else |
| 1416 | static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info, |
| 1417 | const char *magick_unused(encoding),const PointInfo *offset, |
| 1418 | TypeMetric *metrics) |
| 1419 | { |
| 1420 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1421 | MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)", |
| 1422 | draw_info->font); |
| 1423 | return(RenderPostscript(image,draw_info,offset,metrics)); |
| 1424 | } |
| 1425 | #endif |
| 1426 | |
| 1427 | /* |
| 1428 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1429 | % % |
| 1430 | % % |
| 1431 | % % |
| 1432 | + R e n d e r P o s t s c r i p t % |
| 1433 | % % |
| 1434 | % % |
| 1435 | % % |
| 1436 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1437 | % |
| 1438 | % RenderPostscript() renders text on the image with a Postscript font. It |
| 1439 | % also returns the bounding box of the text relative to the image. |
| 1440 | % |
| 1441 | % The format of the RenderPostscript method is: |
| 1442 | % |
| 1443 | % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info, |
| 1444 | % const PointInfo *offset,TypeMetric *metrics) |
| 1445 | % |
| 1446 | % A description of each parameter follows: |
| 1447 | % |
| 1448 | % o image: the image. |
| 1449 | % |
| 1450 | % o draw_info: the draw info. |
| 1451 | % |
| 1452 | % o offset: (x,y) location of text relative to image. |
| 1453 | % |
| 1454 | % o metrics: bounding box of text. |
| 1455 | % |
| 1456 | */ |
| 1457 | |
| 1458 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 1459 | { |
| 1460 | if (x < y) |
| 1461 | return(x); |
| 1462 | return(y); |
| 1463 | } |
| 1464 | |
| 1465 | static char *EscapeParenthesis(const char *text) |
| 1466 | { |
| 1467 | char |
| 1468 | *buffer; |
| 1469 | |
| 1470 | register char |
| 1471 | *p; |
| 1472 | |
| 1473 | register long |
| 1474 | i; |
| 1475 | |
| 1476 | size_t |
| 1477 | escapes; |
| 1478 | |
| 1479 | escapes=0; |
| 1480 | buffer=AcquireString(text); |
| 1481 | p=buffer; |
| 1482 | for (i=0; i < (long) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++) |
| 1483 | { |
| 1484 | if ((text[i] == '(') || (text[i] == ')')) |
| 1485 | { |
| 1486 | *p++='\\'; |
| 1487 | escapes++; |
| 1488 | } |
| 1489 | *p++=text[i]; |
| 1490 | } |
| 1491 | *p='\0'; |
| 1492 | return(buffer); |
| 1493 | } |
| 1494 | |
| 1495 | static MagickBooleanType RenderPostscript(Image *image, |
| 1496 | const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics) |
| 1497 | { |
| 1498 | char |
| 1499 | filename[MaxTextExtent], |
| 1500 | geometry[MaxTextExtent], |
| 1501 | *text; |
| 1502 | |
| 1503 | FILE |
| 1504 | *file; |
| 1505 | |
| 1506 | Image |
| 1507 | *annotate_image; |
| 1508 | |
| 1509 | ImageInfo |
| 1510 | *annotate_info; |
| 1511 | |
| 1512 | int |
| 1513 | unique_file; |
| 1514 | |
| 1515 | long |
| 1516 | y; |
| 1517 | |
| 1518 | MagickBooleanType |
| 1519 | identity; |
| 1520 | |
| 1521 | PointInfo |
| 1522 | extent, |
| 1523 | point, |
| 1524 | resolution; |
| 1525 | |
| 1526 | register long |
| 1527 | i; |
| 1528 | |
| 1529 | /* |
| 1530 | Render label with a Postscript font. |
| 1531 | */ |
| 1532 | if (image->debug != MagickFalse) |
| 1533 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(), |
| 1534 | "Font %s; pointsize %g",draw_info->font != (char *) NULL ? |
| 1535 | draw_info->font : "none",draw_info->pointsize); |
| 1536 | file=(FILE *) NULL; |
| 1537 | unique_file=AcquireUniqueFileResource(filename); |
| 1538 | if (unique_file != -1) |
| 1539 | file=fdopen(unique_file,"wb"); |
| 1540 | if ((unique_file == -1) || (file == (FILE *) NULL)) |
| 1541 | { |
| 1542 | ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile", |
| 1543 | filename); |
| 1544 | return(MagickFalse); |
| 1545 | } |
| 1546 | (void) fprintf(file,"%%!PS-Adobe-3.0\n"); |
| 1547 | (void) fprintf(file,"/ReencodeType\n"); |
| 1548 | (void) fprintf(file,"{\n"); |
| 1549 | (void) fprintf(file," findfont dup length\n"); |
| 1550 | (void) fprintf(file, |
| 1551 | " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n"); |
| 1552 | (void) fprintf(file, |
| 1553 | " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n"); |
| 1554 | (void) fprintf(file,"} bind def\n"); |
| 1555 | /* |
| 1556 | Sample to compute bounding box. |
| 1557 | */ |
| 1558 | identity=(draw_info->affine.sx == draw_info->affine.sy) && |
| 1559 | (draw_info->affine.rx == 0.0) && (draw_info->affine.ry == 0.0) ? |
| 1560 | MagickTrue : MagickFalse; |
| 1561 | extent.x=0.0; |
| 1562 | extent.y=0.0; |
| 1563 | for (i=0; i <= (long) (strlen(draw_info->text)+2); i++) |
| 1564 | { |
| 1565 | point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+ |
| 1566 | draw_info->affine.ry*2.0*draw_info->pointsize); |
| 1567 | point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+ |
| 1568 | draw_info->affine.sy*2.0*draw_info->pointsize); |
| 1569 | if (point.x > extent.x) |
| 1570 | extent.x=point.x; |
| 1571 | if (point.y > extent.y) |
| 1572 | extent.y=point.y; |
| 1573 | } |
| 1574 | (void) fprintf(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 : |
| 1575 | extent.x/2.0,extent.y/2.0); |
| 1576 | (void) fprintf(file,"%g %g scale\n",draw_info->pointsize, |
| 1577 | draw_info->pointsize); |
| 1578 | if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') || |
| 1579 | (strchr(draw_info->font,'/') != (char *) NULL)) |
| 1580 | (void) fprintf(file, |
| 1581 | "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n"); |
| 1582 | else |
| 1583 | (void) fprintf(file,"/%s-ISO dup /%s ReencodeType findfont setfont\n", |
| 1584 | draw_info->font,draw_info->font); |
| 1585 | (void) fprintf(file,"[%g %g %g %g 0 0] concat\n",draw_info->affine.sx, |
| 1586 | -draw_info->affine.rx,-draw_info->affine.ry,draw_info->affine.sy); |
| 1587 | text=EscapeParenthesis(draw_info->text); |
| 1588 | if (identity == MagickFalse) |
| 1589 | (void) fprintf(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",text); |
| 1590 | (void) fprintf(file,"(%s) show\n",text); |
| 1591 | text=DestroyString(text); |
| 1592 | (void) fprintf(file,"showpage\n"); |
| 1593 | (void) fclose(file); |
| 1594 | (void) FormatMagickString(geometry,MaxTextExtent,"%ldx%ld+0+0!",(long) |
| 1595 | (extent.x+0.5),(long) (extent.y+0.5)); |
| 1596 | annotate_info=AcquireImageInfo(); |
| 1597 | (void) FormatMagickString(annotate_info->filename,MaxTextExtent,"ps:%s", |
| 1598 | filename); |
| 1599 | (void) CloneString(&annotate_info->page,geometry); |
| 1600 | if (draw_info->density != (char *) NULL) |
| 1601 | (void) CloneString(&annotate_info->density,draw_info->density); |
| 1602 | annotate_info->antialias=draw_info->text_antialias; |
| 1603 | annotate_image=ReadImage(annotate_info,&image->exception); |
| 1604 | CatchException(&image->exception); |
| 1605 | annotate_info=DestroyImageInfo(annotate_info); |
| 1606 | (void) RelinquishUniqueFileResource(filename); |
| 1607 | if (annotate_image == (Image *) NULL) |
| 1608 | return(MagickFalse); |
| 1609 | resolution.x=DefaultResolution; |
| 1610 | resolution.y=DefaultResolution; |
| 1611 | if (draw_info->density != (char *) NULL) |
| 1612 | { |
| 1613 | GeometryInfo |
| 1614 | geometry_info; |
| 1615 | |
| 1616 | MagickStatusType |
| 1617 | flags; |
| 1618 | |
| 1619 | flags=ParseGeometry(draw_info->density,&geometry_info); |
| 1620 | resolution.x=geometry_info.rho; |
| 1621 | resolution.y=geometry_info.sigma; |
| 1622 | if ((flags & SigmaValue) == 0) |
| 1623 | resolution.y=resolution.x; |
| 1624 | } |
| 1625 | if (identity == MagickFalse) |
| 1626 | (void) TransformImage(&annotate_image,"0x0",(char *) NULL); |
| 1627 | else |
| 1628 | { |
| 1629 | RectangleInfo |
| 1630 | crop_info; |
| 1631 | |
| 1632 | crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception); |
| 1633 | crop_info.height=(unsigned long) ((resolution.y/DefaultResolution)* |
| 1634 | ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5); |
| 1635 | crop_info.y=(long) ((resolution.y/DefaultResolution)*extent.y/8.0+0.5); |
| 1636 | (void) FormatMagickString(geometry,MaxTextExtent,"%lux%lu%+ld%+ld", |
| 1637 | crop_info.width,crop_info.height,crop_info.x,crop_info.y); |
| 1638 | (void) TransformImage(&annotate_image,geometry,(char *) NULL); |
| 1639 | } |
| 1640 | metrics->pixels_per_em.x=(resolution.y/DefaultResolution)* |
| 1641 | ExpandAffine(&draw_info->affine)*draw_info->pointsize; |
| 1642 | metrics->pixels_per_em.y=metrics->pixels_per_em.x; |
| 1643 | metrics->ascent=metrics->pixels_per_em.x; |
| 1644 | metrics->descent=metrics->pixels_per_em.y/-5.0; |
| 1645 | metrics->width=(double) annotate_image->columns/ |
| 1646 | ExpandAffine(&draw_info->affine); |
| 1647 | metrics->height=1.152*metrics->pixels_per_em.x; |
| 1648 | metrics->max_advance=metrics->pixels_per_em.x; |
| 1649 | metrics->bounds.x1=0.0; |
| 1650 | metrics->bounds.y1=metrics->descent; |
| 1651 | metrics->bounds.x2=metrics->ascent+metrics->descent; |
| 1652 | metrics->bounds.y2=metrics->ascent+metrics->descent; |
| 1653 | metrics->underline_position=(-2.0); |
| 1654 | metrics->underline_thickness=1.0; |
| 1655 | if (draw_info->render == MagickFalse) |
| 1656 | { |
| 1657 | annotate_image=DestroyImage(annotate_image); |
| 1658 | return(MagickTrue); |
| 1659 | } |
| 1660 | if (draw_info->fill.opacity != TransparentOpacity) |
| 1661 | { |
| 1662 | ExceptionInfo |
| 1663 | *exception; |
| 1664 | |
| 1665 | MagickBooleanType |
| 1666 | sync; |
| 1667 | |
| 1668 | PixelPacket |
| 1669 | fill_color; |
| 1670 | |
| 1671 | CacheView |
| 1672 | *annotate_view; |
| 1673 | |
| 1674 | /* |
| 1675 | Render fill color. |
| 1676 | */ |
| 1677 | if (image->matte == MagickFalse) |
| 1678 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel); |
| 1679 | if (annotate_image->matte == MagickFalse) |
| 1680 | (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel); |
| 1681 | fill_color=draw_info->fill; |
| 1682 | exception=(&image->exception); |
| 1683 | annotate_view=AcquireCacheView(annotate_image); |
| 1684 | for (y=0; y < (long) annotate_image->rows; y++) |
| 1685 | { |
| 1686 | register long |
| 1687 | x; |
| 1688 | |
| 1689 | register PixelPacket |
| 1690 | *__restrict q; |
| 1691 | |
| 1692 | q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns, |
| 1693 | 1,exception); |
| 1694 | if (q == (PixelPacket *) NULL) |
| 1695 | break; |
| 1696 | for (x=0; x < (long) annotate_image->columns; x++) |
| 1697 | { |
| 1698 | (void) GetFillColor(draw_info,x,y,&fill_color); |
| 1699 | q->opacity=RoundToQuantum(QuantumRange-(((QuantumRange- |
| 1700 | (MagickRealType) PixelIntensityToQuantum(q))*(QuantumRange- |
| 1701 | fill_color.opacity))/QuantumRange)); |
| 1702 | q->red=fill_color.red; |
| 1703 | q->green=fill_color.green; |
| 1704 | q->blue=fill_color.blue; |
| 1705 | q++; |
| 1706 | } |
| 1707 | sync=SyncCacheViewAuthenticPixels(annotate_view,exception); |
| 1708 | if (sync == MagickFalse) |
| 1709 | break; |
| 1710 | } |
| 1711 | annotate_view=DestroyCacheView(annotate_view); |
| 1712 | (void) CompositeImage(image,OverCompositeOp,annotate_image, |
| 1713 | (long) (offset->x+0.5),(long) (offset->y-(metrics->ascent+ |
| 1714 | metrics->descent)+0.5)); |
| 1715 | } |
| 1716 | annotate_image=DestroyImage(annotate_image); |
| 1717 | return(MagickTrue); |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1722 | % % |
| 1723 | % % |
| 1724 | % % |
| 1725 | + R e n d e r X 1 1 % |
| 1726 | % % |
| 1727 | % % |
| 1728 | % % |
| 1729 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1730 | % |
| 1731 | % RenderX11() renders text on the image with an X11 font. It also returns the |
| 1732 | % bounding box of the text relative to the image. |
| 1733 | % |
| 1734 | % The format of the RenderX11 method is: |
| 1735 | % |
| 1736 | % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info, |
| 1737 | % const PointInfo *offset,TypeMetric *metrics) |
| 1738 | % |
| 1739 | % A description of each parameter follows: |
| 1740 | % |
| 1741 | % o image: the image. |
| 1742 | % |
| 1743 | % o draw_info: the draw info. |
| 1744 | % |
| 1745 | % o offset: (x,y) location of text relative to image. |
| 1746 | % |
| 1747 | % o metrics: bounding box of text. |
| 1748 | % |
| 1749 | */ |
| 1750 | #if defined(MAGICKCORE_X11_DELEGATE) |
| 1751 | static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info, |
| 1752 | const PointInfo *offset,TypeMetric *metrics) |
| 1753 | { |
| 1754 | MagickBooleanType |
| 1755 | status; |
| 1756 | |
| 1757 | static DrawInfo |
| 1758 | cache_info; |
| 1759 | |
| 1760 | static Display |
| 1761 | *display = (Display *) NULL; |
| 1762 | |
| 1763 | static XAnnotateInfo |
| 1764 | annotate_info; |
| 1765 | |
| 1766 | static XFontStruct |
| 1767 | *font_info; |
| 1768 | |
| 1769 | static XPixelInfo |
| 1770 | pixel; |
| 1771 | |
| 1772 | static XResourceInfo |
| 1773 | resource_info; |
| 1774 | |
| 1775 | static XrmDatabase |
| 1776 | resource_database; |
| 1777 | |
| 1778 | static XStandardColormap |
| 1779 | *map_info; |
| 1780 | |
| 1781 | static XVisualInfo |
| 1782 | *visual_info; |
| 1783 | |
| 1784 | unsigned long |
| 1785 | height, |
| 1786 | width; |
| 1787 | |
| 1788 | if (display == (Display *) NULL) |
| 1789 | { |
| 1790 | ImageInfo |
| 1791 | *image_info; |
| 1792 | |
| 1793 | /* |
| 1794 | Open X server connection. |
| 1795 | */ |
| 1796 | display=XOpenDisplay(draw_info->server_name); |
| 1797 | if (display == (Display *) NULL) |
| 1798 | { |
| 1799 | ThrowXWindowException(XServerError,"UnableToOpenXServer", |
| 1800 | draw_info->server_name); |
| 1801 | return(MagickFalse); |
| 1802 | } |
| 1803 | /* |
| 1804 | Get user defaults from X resource database. |
| 1805 | */ |
| 1806 | (void) XSetErrorHandler(XError); |
| 1807 | image_info=AcquireImageInfo(); |
| 1808 | resource_database=XGetResourceDatabase(display,GetClientName()); |
| 1809 | XGetResourceInfo(image_info,resource_database,GetClientName(), |
| 1810 | &resource_info); |
| 1811 | resource_info.close_server=MagickFalse; |
| 1812 | resource_info.colormap=PrivateColormap; |
| 1813 | resource_info.font=AcquireString(draw_info->font); |
| 1814 | resource_info.background_color=AcquireString("#ffffffffffff"); |
| 1815 | resource_info.foreground_color=AcquireString("#000000000000"); |
| 1816 | map_info=XAllocStandardColormap(); |
| 1817 | if (map_info == (XStandardColormap *) NULL) |
| 1818 | { |
| 1819 | ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed", |
| 1820 | image->filename); |
| 1821 | return(MagickFalse); |
| 1822 | } |
| 1823 | /* |
| 1824 | Initialize visual info. |
| 1825 | */ |
| 1826 | visual_info=XBestVisualInfo(display,map_info,&resource_info); |
| 1827 | if (visual_info == (XVisualInfo *) NULL) |
| 1828 | { |
| 1829 | ThrowXWindowException(XServerError,"UnableToGetVisual", |
| 1830 | image->filename); |
| 1831 | return(MagickFalse); |
| 1832 | } |
| 1833 | map_info->colormap=(Colormap) NULL; |
| 1834 | pixel.pixels=(unsigned long *) NULL; |
| 1835 | /* |
| 1836 | Initialize Standard Colormap info. |
| 1837 | */ |
| 1838 | XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen), |
| 1839 | map_info); |
| 1840 | XGetPixelPacket(display,visual_info,map_info,&resource_info, |
| 1841 | (Image *) NULL,&pixel); |
| 1842 | pixel.annotate_context=XDefaultGC(display,visual_info->screen); |
| 1843 | /* |
| 1844 | Initialize font info. |
| 1845 | */ |
| 1846 | font_info=XBestFont(display,&resource_info,MagickFalse); |
| 1847 | if (font_info == (XFontStruct *) NULL) |
| 1848 | { |
| 1849 | ThrowXWindowException(XServerError,"UnableToLoadFont", |
| 1850 | draw_info->font); |
| 1851 | return(MagickFalse); |
| 1852 | } |
| 1853 | if ((map_info == (XStandardColormap *) NULL) || |
| 1854 | (visual_info == (XVisualInfo *) NULL) || |
| 1855 | (font_info == (XFontStruct *) NULL)) |
| 1856 | { |
| 1857 | XFreeResources(display,visual_info,map_info,&pixel,font_info, |
| 1858 | &resource_info,(XWindowInfo *) NULL); |
| 1859 | ThrowXWindowException(XServerError,"UnableToLoadFont", |
| 1860 | image->filename); |
| 1861 | return(MagickFalse); |
| 1862 | } |
| 1863 | cache_info=(*draw_info); |
| 1864 | } |
| 1865 | /* |
| 1866 | Initialize annotate info. |
| 1867 | */ |
| 1868 | XGetAnnotateInfo(&annotate_info); |
| 1869 | annotate_info.stencil=ForegroundStencil; |
| 1870 | if (cache_info.font != draw_info->font) |
| 1871 | { |
| 1872 | /* |
| 1873 | Type name has changed. |
| 1874 | */ |
| 1875 | (void) XFreeFont(display,font_info); |
| 1876 | (void) CloneString(&resource_info.font,draw_info->font); |
| 1877 | font_info=XBestFont(display,&resource_info,MagickFalse); |
| 1878 | if (font_info == (XFontStruct *) NULL) |
| 1879 | { |
| 1880 | ThrowXWindowException(XServerError,"UnableToLoadFont", |
| 1881 | draw_info->font); |
| 1882 | return(MagickFalse); |
| 1883 | } |
| 1884 | } |
| 1885 | if (image->debug != MagickFalse) |
| 1886 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(), |
| 1887 | "Font %s; pointsize %g",draw_info->font != (char *) NULL ? |
| 1888 | draw_info->font : "none",draw_info->pointsize); |
| 1889 | cache_info=(*draw_info); |
| 1890 | annotate_info.font_info=font_info; |
| 1891 | annotate_info.text=(char *) draw_info->text; |
| 1892 | annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text, |
| 1893 | (int) strlen(draw_info->text)); |
| 1894 | annotate_info.height=(unsigned int) font_info->ascent+font_info->descent; |
| 1895 | metrics->pixels_per_em.x=(double) font_info->max_bounds.width; |
| 1896 | metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent; |
| 1897 | metrics->ascent=(double) font_info->ascent+4; |
| 1898 | metrics->descent=(double) (-font_info->descent); |
| 1899 | metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine); |
| 1900 | metrics->height=font_info->ascent+font_info->descent; |
| 1901 | metrics->max_advance=(double) font_info->max_bounds.width; |
| 1902 | metrics->bounds.x1=0.0; |
| 1903 | metrics->bounds.y1=metrics->descent; |
| 1904 | metrics->bounds.x2=metrics->ascent+metrics->descent; |
| 1905 | metrics->bounds.y2=metrics->ascent+metrics->descent; |
| 1906 | metrics->underline_position=(-2.0); |
| 1907 | metrics->underline_thickness=1.0; |
| 1908 | if (draw_info->render == MagickFalse) |
| 1909 | return(MagickTrue); |
| 1910 | if (draw_info->fill.opacity == TransparentOpacity) |
| 1911 | return(MagickTrue); |
| 1912 | /* |
| 1913 | Render fill color. |
| 1914 | */ |
| 1915 | width=annotate_info.width; |
| 1916 | height=annotate_info.height; |
| 1917 | if ((draw_info->affine.rx != 0.0) || (draw_info->affine.ry != 0.0)) |
| 1918 | { |
| 1919 | if (((draw_info->affine.sx-draw_info->affine.sy) == 0.0) && |
| 1920 | ((draw_info->affine.rx+draw_info->affine.ry) == 0.0)) |
| 1921 | annotate_info.degrees=(180.0/MagickPI)* |
| 1922 | atan2(draw_info->affine.rx,draw_info->affine.sx); |
| 1923 | } |
| 1924 | (void) FormatMagickString(annotate_info.geometry,MaxTextExtent, |
| 1925 | "%lux%lu+%ld+%ld",width,height,(long) (offset->x+0.5), |
cristy | 5846039 | 2009-09-09 01:52:06 +0000 | [diff] [blame] | 1926 | (long) (offset->y-metrics->ascent-metrics->descent+ |
| 1927 | draw_info->interline_spacing+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1928 | pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red); |
| 1929 | pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green); |
| 1930 | pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue); |
| 1931 | status=XAnnotateImage(display,&pixel,&annotate_info,image); |
| 1932 | if (status == 0) |
| 1933 | { |
| 1934 | ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed", |
| 1935 | image->filename); |
| 1936 | return(MagickFalse); |
| 1937 | } |
| 1938 | return(MagickTrue); |
| 1939 | } |
| 1940 | #else |
| 1941 | static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info, |
| 1942 | const PointInfo *offset,TypeMetric *metrics) |
| 1943 | { |
| 1944 | (void) draw_info; |
| 1945 | (void) offset; |
| 1946 | (void) metrics; |
| 1947 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1948 | MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (X11)", |
| 1949 | image->filename); |
| 1950 | return(MagickFalse); |
| 1951 | } |
| 1952 | #endif |