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