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 |
cristy | 637d950 | 2011-10-03 18:47:13 +0000 | [diff] [blame] | 83 | #if defined(MAGICKCORE_HAVE_FT2BUILD_H) |
| 84 | # include <ft2build.h> |
| 85 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | { |
cristy | 74966db | 2011-09-20 19:37:21 +0000 | [diff] [blame] | 552 | char |
| 553 | *text; |
| 554 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 555 | MagickBooleanType |
| 556 | status; |
| 557 | |
| 558 | register char |
| 559 | *p, |
| 560 | *q, |
| 561 | *s; |
| 562 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 563 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 564 | i; |
| 565 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 566 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 567 | width; |
| 568 | |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 569 | ssize_t |
| 570 | n; |
| 571 | |
cristy | 74966db | 2011-09-20 19:37:21 +0000 | [diff] [blame] | 572 | text=AcquireString(draw_info->text); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 573 | q=draw_info->text; |
| 574 | s=(char *) NULL; |
| 575 | for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p)) |
| 576 | { |
| 577 | if (IsUTFSpace(GetUTFCode(p)) != MagickFalse) |
| 578 | s=p; |
cristy | 2911f2d | 2011-08-20 23:04:43 +0000 | [diff] [blame] | 579 | if (GetUTFCode(p) == '\n') |
cristy | e2c81ad | 2011-08-20 22:54:14 +0000 | [diff] [blame] | 580 | q=draw_info->text; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 581 | for (i=0; i < (ssize_t) GetUTFOctets(p); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 582 | *q++=(*(p+i)); |
| 583 | *q='\0'; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 584 | status=GetTypeMetrics(image,draw_info,metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 585 | if (status == MagickFalse) |
| 586 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 587 | width=(size_t) floor(metrics->width+0.5); |
cristy | 74966db | 2011-09-20 19:37:21 +0000 | [diff] [blame] | 588 | if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0)) |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 589 | continue; |
cristy | 74966db | 2011-09-20 19:37:21 +0000 | [diff] [blame] | 590 | (void) strcpy(text,draw_info->text); |
cristy | 4aa314e | 2011-08-19 13:59:45 +0000 | [diff] [blame] | 591 | if ((s != (char *) NULL) && (GetUTFOctets(s) == 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 592 | { |
| 593 | *s='\n'; |
| 594 | p=s; |
| 595 | } |
| 596 | else |
cristy | 4aa314e | 2011-08-19 13:59:45 +0000 | [diff] [blame] | 597 | if ((s != (char *) NULL) || (split != MagickFalse)) |
cristy | 6b1d05e | 2010-09-22 19:17:27 +0000 | [diff] [blame] | 598 | { |
| 599 | char |
| 600 | *target; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 601 | |
cristy | 6b1d05e | 2010-09-22 19:17:27 +0000 | [diff] [blame] | 602 | /* |
| 603 | No convenient line breaks-- insert newline. |
| 604 | */ |
| 605 | target=AcquireString(*caption); |
| 606 | n=p-(*caption); |
| 607 | CopyMagickString(target,*caption,n+1); |
| 608 | ConcatenateMagickString(target,"\n",strlen(*caption)+1); |
| 609 | ConcatenateMagickString(target,p,strlen(*caption)+2); |
| 610 | (void) DestroyString(*caption); |
| 611 | *caption=target; |
| 612 | p=(*caption)+n; |
| 613 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 614 | q=draw_info->text; |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 615 | s=(char *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 616 | } |
cristy | 74966db | 2011-09-20 19:37:21 +0000 | [diff] [blame] | 617 | text=DestroyString(text); |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 618 | n=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 619 | for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p)) |
| 620 | if (GetUTFCode(p) == '\n') |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 621 | n++; |
| 622 | return(n); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* |
| 626 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 627 | % % |
| 628 | % % |
| 629 | % % |
| 630 | % G e t M u l t i l i n e T y p e M e t r i c s % |
| 631 | % % |
| 632 | % % |
| 633 | % % |
| 634 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 635 | % |
| 636 | % GetMultilineTypeMetrics() returns the following information for the |
| 637 | % specified font and text: |
| 638 | % |
| 639 | % character width |
| 640 | % character height |
| 641 | % ascender |
| 642 | % descender |
| 643 | % text width |
| 644 | % text height |
| 645 | % maximum horizontal advance |
| 646 | % bounds: x1 |
| 647 | % bounds: y1 |
| 648 | % bounds: x2 |
| 649 | % bounds: y2 |
| 650 | % origin: x |
| 651 | % origin: y |
| 652 | % underline position |
| 653 | % underline thickness |
| 654 | % |
| 655 | % This method is like GetTypeMetrics() but it returns the maximum text width |
| 656 | % and height for multiple lines of text. |
| 657 | % |
| 658 | % The format of the GetMultilineTypeMetrics method is: |
| 659 | % |
| 660 | % MagickBooleanType GetMultilineTypeMetrics(Image *image, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 661 | % const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 662 | % |
| 663 | % A description of each parameter follows: |
| 664 | % |
| 665 | % o image: the image. |
| 666 | % |
| 667 | % o draw_info: the draw info. |
| 668 | % |
| 669 | % o metrics: Return the font metrics in this structure. |
| 670 | % |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 671 | % o exception: return any errors or warnings in this structure. |
| 672 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 673 | */ |
| 674 | MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 675 | const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 676 | { |
| 677 | char |
| 678 | **textlist; |
| 679 | |
| 680 | DrawInfo |
| 681 | *annotate_info; |
| 682 | |
| 683 | MagickBooleanType |
| 684 | status; |
| 685 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 686 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 687 | i; |
| 688 | |
| 689 | TypeMetric |
| 690 | extent; |
| 691 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 692 | assert(image != (Image *) NULL); |
| 693 | assert(image->signature == MagickSignature); |
| 694 | if (image->debug != MagickFalse) |
| 695 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 696 | assert(draw_info != (DrawInfo *) NULL); |
| 697 | assert(draw_info->text != (char *) NULL); |
| 698 | assert(draw_info->signature == MagickSignature); |
| 699 | if (*draw_info->text == '\0') |
| 700 | return(MagickFalse); |
| 701 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 702 | annotate_info->text=DestroyString(annotate_info->text); |
| 703 | /* |
| 704 | Convert newlines to multiple lines of text. |
| 705 | */ |
| 706 | textlist=StringToList(draw_info->text); |
| 707 | if (textlist == (char **) NULL) |
| 708 | return(MagickFalse); |
| 709 | annotate_info->render=MagickFalse; |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 710 | annotate_info->direction=UndefinedDirection; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 711 | (void) ResetMagickMemory(metrics,0,sizeof(*metrics)); |
| 712 | (void) ResetMagickMemory(&extent,0,sizeof(extent)); |
| 713 | /* |
| 714 | Find the widest of the text lines. |
| 715 | */ |
| 716 | annotate_info->text=textlist[0]; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 717 | status=GetTypeMetrics(image,annotate_info,&extent,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 718 | *metrics=extent; |
| 719 | for (i=1; textlist[i] != (char *) NULL; i++) |
| 720 | { |
| 721 | annotate_info->text=textlist[i]; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 722 | status=GetTypeMetrics(image,annotate_info,&extent,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | if (extent.width > metrics->width) |
| 724 | *metrics=extent; |
| 725 | } |
cristy | e2c81ad | 2011-08-20 22:54:14 +0000 | [diff] [blame] | 726 | metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+ |
| 727 | (i-1)*draw_info->interline_spacing); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 728 | /* |
| 729 | Relinquish resources. |
| 730 | */ |
| 731 | annotate_info->text=(char *) NULL; |
| 732 | annotate_info=DestroyDrawInfo(annotate_info); |
| 733 | for (i=0; textlist[i] != (char *) NULL; i++) |
| 734 | textlist[i]=DestroyString(textlist[i]); |
| 735 | textlist=(char **) RelinquishMagickMemory(textlist); |
| 736 | return(status); |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 741 | % % |
| 742 | % % |
| 743 | % % |
| 744 | % G e t T y p e M e t r i c s % |
| 745 | % % |
| 746 | % % |
| 747 | % % |
| 748 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 749 | % |
| 750 | % GetTypeMetrics() returns the following information for the specified font |
| 751 | % and text: |
| 752 | % |
| 753 | % character width |
| 754 | % character height |
| 755 | % ascender |
| 756 | % descender |
| 757 | % text width |
| 758 | % text height |
| 759 | % maximum horizontal advance |
| 760 | % bounds: x1 |
| 761 | % bounds: y1 |
| 762 | % bounds: x2 |
| 763 | % bounds: y2 |
| 764 | % origin: x |
| 765 | % origin: y |
| 766 | % underline position |
| 767 | % underline thickness |
| 768 | % |
| 769 | % The format of the GetTypeMetrics method is: |
| 770 | % |
| 771 | % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 772 | % TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 773 | % |
| 774 | % A description of each parameter follows: |
| 775 | % |
| 776 | % o image: the image. |
| 777 | % |
| 778 | % o draw_info: the draw info. |
| 779 | % |
| 780 | % o metrics: Return the font metrics in this structure. |
| 781 | % |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 782 | % o exception: return any errors or warnings in this structure. |
| 783 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 784 | */ |
| 785 | MagickExport MagickBooleanType GetTypeMetrics(Image *image, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 786 | const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 787 | { |
| 788 | DrawInfo |
| 789 | *annotate_info; |
| 790 | |
| 791 | MagickBooleanType |
| 792 | status; |
| 793 | |
| 794 | PointInfo |
| 795 | offset; |
| 796 | |
| 797 | assert(image != (Image *) NULL); |
| 798 | assert(image->signature == MagickSignature); |
| 799 | if (image->debug != MagickFalse) |
| 800 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 801 | assert(draw_info != (DrawInfo *) NULL); |
| 802 | assert(draw_info->text != (char *) NULL); |
| 803 | assert(draw_info->signature == MagickSignature); |
| 804 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 805 | annotate_info->render=MagickFalse; |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 806 | annotate_info->direction=UndefinedDirection; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 807 | (void) ResetMagickMemory(metrics,0,sizeof(*metrics)); |
| 808 | offset.x=0.0; |
| 809 | offset.y=0.0; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 810 | status=RenderType(image,annotate_info,&offset,metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | if (image->debug != MagickFalse) |
| 812 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; " |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 813 | "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; " |
cristy | a8549b1 | 2011-05-18 19:05:08 +0000 | [diff] [blame] | 814 | "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; " |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 815 | "underline position: %g; underline thickness: %g",annotate_info->text, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 816 | metrics->width,metrics->height,metrics->ascent,metrics->descent, |
| 817 | metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 818 | metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y, |
| 819 | metrics->pixels_per_em.x,metrics->pixels_per_em.y, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 820 | metrics->underline_position,metrics->underline_thickness); |
| 821 | annotate_info=DestroyDrawInfo(annotate_info); |
| 822 | return(status); |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 827 | % % |
| 828 | % % |
| 829 | % % |
| 830 | + R e n d e r T y p e % |
| 831 | % % |
| 832 | % % |
| 833 | % % |
| 834 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 835 | % |
| 836 | % RenderType() renders text on the image. It also returns the bounding box of |
| 837 | % the text relative to the image. |
| 838 | % |
| 839 | % The format of the RenderType method is: |
| 840 | % |
| 841 | % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 842 | % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 843 | % |
| 844 | % A description of each parameter follows: |
| 845 | % |
| 846 | % o image: the image. |
| 847 | % |
| 848 | % o draw_info: the draw info. |
| 849 | % |
| 850 | % o offset: (x,y) location of text relative to image. |
| 851 | % |
| 852 | % o metrics: bounding box of text. |
| 853 | % |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 854 | % o exception: return any errors or warnings in this structure. |
| 855 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 856 | */ |
| 857 | static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 858 | const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 859 | { |
| 860 | const TypeInfo |
| 861 | *type_info; |
| 862 | |
| 863 | DrawInfo |
| 864 | *annotate_info; |
| 865 | |
| 866 | MagickBooleanType |
| 867 | status; |
| 868 | |
| 869 | type_info=(const TypeInfo *) NULL; |
| 870 | if (draw_info->font != (char *) NULL) |
| 871 | { |
| 872 | if (*draw_info->font == '@') |
| 873 | { |
| 874 | status=RenderFreetype(image,draw_info,draw_info->encoding,offset, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 875 | metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 876 | return(status); |
| 877 | } |
| 878 | if (*draw_info->font == '-') |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 879 | return(RenderX11(image,draw_info,offset,metrics,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 880 | if (IsPathAccessible(draw_info->font) != MagickFalse) |
| 881 | { |
| 882 | status=RenderFreetype(image,draw_info,draw_info->encoding,offset, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 883 | metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 884 | return(status); |
| 885 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 886 | type_info=GetTypeInfo(draw_info->font,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 887 | if (type_info == (const TypeInfo *) NULL) |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 888 | (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning, |
| 889 | "UnableToReadFont","`%s'",draw_info->font); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 890 | } |
| 891 | if ((type_info == (const TypeInfo *) NULL) && |
| 892 | (draw_info->family != (const char *) NULL)) |
| 893 | { |
| 894 | type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 895 | draw_info->stretch,draw_info->weight,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | if (type_info == (const TypeInfo *) NULL) |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 897 | (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning, |
| 898 | "UnableToReadFont","`%s'",draw_info->family); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 899 | } |
| 900 | if (type_info == (const TypeInfo *) NULL) |
cristy | d984f27 | 2010-03-16 01:46:12 +0000 | [diff] [blame] | 901 | type_info=GetTypeInfoByFamily("Arial",draw_info->style, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 902 | draw_info->stretch,draw_info->weight,exception); |
cristy | d984f27 | 2010-03-16 01:46:12 +0000 | [diff] [blame] | 903 | if (type_info == (const TypeInfo *) NULL) |
cristy | cbb34a4 | 2010-03-16 01:45:18 +0000 | [diff] [blame] | 904 | type_info=GetTypeInfoByFamily("Helvetica",draw_info->style, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 905 | draw_info->stretch,draw_info->weight,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | if (type_info == (const TypeInfo *) NULL) |
cristy | 704acaa | 2010-03-23 13:08:21 +0000 | [diff] [blame] | 907 | type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 908 | draw_info->stretch,draw_info->weight,exception); |
cristy | 704acaa | 2010-03-23 13:08:21 +0000 | [diff] [blame] | 909 | if (type_info == (const TypeInfo *) NULL) |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 910 | type_info=GetTypeInfoByFamily("Sans",draw_info->style, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 911 | draw_info->stretch,draw_info->weight,exception); |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 912 | if (type_info == (const TypeInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 913 | type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 914 | draw_info->stretch,draw_info->weight,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 915 | if (type_info == (const TypeInfo *) NULL) |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 916 | type_info=GetTypeInfo("*",exception); |
cristy | 704acaa | 2010-03-23 13:08:21 +0000 | [diff] [blame] | 917 | if (type_info == (const TypeInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | { |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 919 | status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics, |
| 920 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | return(status); |
| 922 | } |
| 923 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 924 | annotate_info->face=type_info->face; |
| 925 | if (type_info->metrics != (char *) NULL) |
| 926 | (void) CloneString(&annotate_info->metrics,type_info->metrics); |
| 927 | if (type_info->glyphs != (char *) NULL) |
| 928 | (void) CloneString(&annotate_info->font,type_info->glyphs); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 929 | status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics, |
| 930 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 931 | annotate_info=DestroyDrawInfo(annotate_info); |
| 932 | return(status); |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 937 | % % |
| 938 | % % |
| 939 | % % |
| 940 | + R e n d e r F r e e t y p e % |
| 941 | % % |
| 942 | % % |
| 943 | % % |
| 944 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 945 | % |
| 946 | % RenderFreetype() renders text on the image with a Truetype font. It also |
| 947 | % returns the bounding box of the text relative to the image. |
| 948 | % |
| 949 | % The format of the RenderFreetype method is: |
| 950 | % |
| 951 | % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 952 | % const char *encoding,const PointInfo *offset,TypeMetric *metrics, |
| 953 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 954 | % |
| 955 | % A description of each parameter follows: |
| 956 | % |
| 957 | % o image: the image. |
| 958 | % |
| 959 | % o draw_info: the draw info. |
| 960 | % |
| 961 | % o encoding: the font encoding. |
| 962 | % |
| 963 | % o offset: (x,y) location of text relative to image. |
| 964 | % |
| 965 | % o metrics: bounding box of text. |
| 966 | % |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 967 | % o exception: return any errors or warnings in this structure. |
| 968 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 969 | */ |
| 970 | |
| 971 | #if defined(MAGICKCORE_FREETYPE_DELEGATE) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 972 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 973 | static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to, |
| 974 | DrawInfo *draw_info) |
| 975 | { |
| 976 | AffineMatrix |
| 977 | affine; |
| 978 | |
| 979 | char |
| 980 | path[MaxTextExtent]; |
| 981 | |
| 982 | affine=draw_info->affine; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 983 | (void) FormatLocaleString(path,MaxTextExtent, |
cristy | a8549b1 | 2011-05-18 19:05:08 +0000 | [diff] [blame] | 984 | "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] | 985 | p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0, |
| 986 | affine.ty-to->y/64.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 987 | (void) ConcatenateString(&draw_info->primitive,path); |
| 988 | return(0); |
| 989 | } |
| 990 | |
| 991 | static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info) |
| 992 | { |
| 993 | AffineMatrix |
| 994 | affine; |
| 995 | |
| 996 | char |
| 997 | path[MaxTextExtent]; |
| 998 | |
| 999 | affine=draw_info->affine; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1000 | (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+ |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 1001 | to->x/64.0,affine.ty-to->y/64.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | (void) ConcatenateString(&draw_info->primitive,path); |
| 1003 | return(0); |
| 1004 | } |
| 1005 | |
| 1006 | static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info) |
| 1007 | { |
| 1008 | AffineMatrix |
| 1009 | affine; |
| 1010 | |
| 1011 | char |
| 1012 | path[MaxTextExtent]; |
| 1013 | |
| 1014 | affine=draw_info->affine; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1015 | (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+ |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 1016 | to->x/64.0,affine.ty-to->y/64.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1017 | (void) ConcatenateString(&draw_info->primitive,path); |
| 1018 | return(0); |
| 1019 | } |
| 1020 | |
| 1021 | static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to, |
| 1022 | DrawInfo *draw_info) |
| 1023 | { |
| 1024 | AffineMatrix |
| 1025 | affine; |
| 1026 | |
| 1027 | char |
| 1028 | path[MaxTextExtent]; |
| 1029 | |
| 1030 | affine=draw_info->affine; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1031 | (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1032 | affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0, |
| 1033 | affine.ty-to->y/64.0); |
| 1034 | (void) ConcatenateString(&draw_info->primitive,path); |
| 1035 | return(0); |
| 1036 | } |
| 1037 | |
| 1038 | static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1039 | const char *encoding,const PointInfo *offset,TypeMetric *metrics, |
| 1040 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1041 | { |
| 1042 | #if !defined(FT_OPEN_PATHNAME) |
| 1043 | #define FT_OPEN_PATHNAME ft_open_pathname |
| 1044 | #endif |
| 1045 | |
| 1046 | typedef struct _GlyphInfo |
| 1047 | { |
| 1048 | FT_UInt |
| 1049 | id; |
| 1050 | |
| 1051 | FT_Vector |
| 1052 | origin; |
| 1053 | |
| 1054 | FT_Glyph |
| 1055 | image; |
| 1056 | } GlyphInfo; |
| 1057 | |
| 1058 | const char |
| 1059 | *value; |
| 1060 | |
cristy | c9b1295 | 2010-03-28 01:12:28 +0000 | [diff] [blame] | 1061 | double |
| 1062 | direction; |
| 1063 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1064 | DrawInfo |
| 1065 | *annotate_info; |
| 1066 | |
| 1067 | FT_BBox |
| 1068 | bounds; |
| 1069 | |
| 1070 | FT_BitmapGlyph |
| 1071 | bitmap; |
| 1072 | |
| 1073 | FT_Encoding |
| 1074 | encoding_type; |
| 1075 | |
| 1076 | FT_Error |
| 1077 | status; |
| 1078 | |
| 1079 | FT_Face |
| 1080 | face; |
| 1081 | |
| 1082 | FT_Int32 |
| 1083 | flags; |
| 1084 | |
| 1085 | FT_Library |
| 1086 | library; |
| 1087 | |
| 1088 | FT_Matrix |
| 1089 | affine; |
| 1090 | |
| 1091 | FT_Open_Args |
| 1092 | args; |
| 1093 | |
| 1094 | FT_Vector |
| 1095 | origin; |
| 1096 | |
| 1097 | GlyphInfo |
| 1098 | glyph, |
| 1099 | last_glyph; |
| 1100 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1101 | PointInfo |
| 1102 | point, |
| 1103 | resolution; |
| 1104 | |
| 1105 | register char |
| 1106 | *p; |
| 1107 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1108 | ssize_t |
| 1109 | code, |
| 1110 | y; |
| 1111 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1112 | static FT_Outline_Funcs |
| 1113 | OutlineMethods = |
| 1114 | { |
| 1115 | (FT_Outline_MoveTo_Func) TraceMoveTo, |
| 1116 | (FT_Outline_LineTo_Func) TraceLineTo, |
| 1117 | (FT_Outline_ConicTo_Func) TraceQuadraticBezier, |
| 1118 | (FT_Outline_CubicTo_Func) TraceCubicBezier, |
| 1119 | 0, 0 |
| 1120 | }; |
| 1121 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1122 | unsigned char |
| 1123 | *utf8; |
| 1124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1125 | /* |
| 1126 | Initialize Truetype library. |
| 1127 | */ |
| 1128 | status=FT_Init_FreeType(&library); |
| 1129 | if (status != 0) |
| 1130 | ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary", |
| 1131 | image->filename); |
| 1132 | args.flags=FT_OPEN_PATHNAME; |
| 1133 | if (draw_info->font == (char *) NULL) |
| 1134 | args.pathname=ConstantString("helvetica"); |
| 1135 | else |
| 1136 | if (*draw_info->font != '@') |
| 1137 | args.pathname=ConstantString(draw_info->font); |
| 1138 | else |
| 1139 | args.pathname=ConstantString(draw_info->font+1); |
| 1140 | face=(FT_Face) NULL; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 1141 | status=FT_Open_Face(library,&args,(long) draw_info->face,&face); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1142 | args.pathname=DestroyString(args.pathname); |
| 1143 | if (status != 0) |
| 1144 | { |
| 1145 | (void) FT_Done_FreeType(library); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1146 | (void) ThrowMagickException(exception,GetMagickModule(),TypeError, |
| 1147 | "UnableToReadFont","`%s'",draw_info->font); |
| 1148 | return(RenderPostscript(image,draw_info,offset,metrics,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if ((draw_info->metrics != (char *) NULL) && |
| 1151 | (IsPathAccessible(draw_info->metrics) != MagickFalse)) |
| 1152 | (void) FT_Attach_File(face,draw_info->metrics); |
| 1153 | encoding_type=ft_encoding_unicode; |
| 1154 | status=FT_Select_Charmap(face,encoding_type); |
| 1155 | if ((status != 0) && (face->num_charmaps != 0)) |
| 1156 | status=FT_Set_Charmap(face,face->charmaps[0]); |
| 1157 | if (encoding != (const char *) NULL) |
| 1158 | { |
| 1159 | if (LocaleCompare(encoding,"AdobeCustom") == 0) |
| 1160 | encoding_type=ft_encoding_adobe_custom; |
| 1161 | if (LocaleCompare(encoding,"AdobeExpert") == 0) |
| 1162 | encoding_type=ft_encoding_adobe_expert; |
| 1163 | if (LocaleCompare(encoding,"AdobeStandard") == 0) |
| 1164 | encoding_type=ft_encoding_adobe_standard; |
| 1165 | if (LocaleCompare(encoding,"AppleRoman") == 0) |
| 1166 | encoding_type=ft_encoding_apple_roman; |
| 1167 | if (LocaleCompare(encoding,"BIG5") == 0) |
| 1168 | encoding_type=ft_encoding_big5; |
| 1169 | if (LocaleCompare(encoding,"GB2312") == 0) |
| 1170 | encoding_type=ft_encoding_gb2312; |
| 1171 | if (LocaleCompare(encoding,"Johab") == 0) |
| 1172 | encoding_type=ft_encoding_johab; |
| 1173 | #if defined(ft_encoding_latin_1) |
| 1174 | if (LocaleCompare(encoding,"Latin-1") == 0) |
| 1175 | encoding_type=ft_encoding_latin_1; |
| 1176 | #endif |
| 1177 | if (LocaleCompare(encoding,"Latin-2") == 0) |
| 1178 | encoding_type=ft_encoding_latin_2; |
| 1179 | if (LocaleCompare(encoding,"None") == 0) |
| 1180 | encoding_type=ft_encoding_none; |
| 1181 | if (LocaleCompare(encoding,"SJIScode") == 0) |
| 1182 | encoding_type=ft_encoding_sjis; |
| 1183 | if (LocaleCompare(encoding,"Symbol") == 0) |
| 1184 | encoding_type=ft_encoding_symbol; |
| 1185 | if (LocaleCompare(encoding,"Unicode") == 0) |
| 1186 | encoding_type=ft_encoding_unicode; |
| 1187 | if (LocaleCompare(encoding,"Wansung") == 0) |
| 1188 | encoding_type=ft_encoding_wansung; |
| 1189 | status=FT_Select_Charmap(face,encoding_type); |
| 1190 | if (status != 0) |
| 1191 | ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding); |
| 1192 | } |
| 1193 | /* |
| 1194 | Set text size. |
| 1195 | */ |
| 1196 | resolution.x=DefaultResolution; |
| 1197 | resolution.y=DefaultResolution; |
| 1198 | if (draw_info->density != (char *) NULL) |
| 1199 | { |
| 1200 | GeometryInfo |
| 1201 | geometry_info; |
| 1202 | |
| 1203 | MagickStatusType |
| 1204 | flags; |
| 1205 | |
| 1206 | flags=ParseGeometry(draw_info->density,&geometry_info); |
| 1207 | resolution.x=geometry_info.rho; |
| 1208 | resolution.y=geometry_info.sigma; |
| 1209 | if ((flags & SigmaValue) == 0) |
| 1210 | resolution.y=resolution.x; |
| 1211 | } |
| 1212 | status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize), |
| 1213 | (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x, |
| 1214 | (FT_UInt) resolution.y); |
| 1215 | metrics->pixels_per_em.x=face->size->metrics.x_ppem; |
| 1216 | metrics->pixels_per_em.y=face->size->metrics.y_ppem; |
| 1217 | metrics->ascent=(double) face->size->metrics.ascender/64.0; |
| 1218 | metrics->descent=(double) face->size->metrics.descender/64.0; |
| 1219 | metrics->width=0; |
| 1220 | metrics->origin.x=0; |
| 1221 | metrics->origin.y=0; |
| 1222 | metrics->height=(double) face->size->metrics.height/64.0; |
| 1223 | metrics->max_advance=0.0; |
| 1224 | if (face->size->metrics.max_advance > MagickEpsilon) |
| 1225 | metrics->max_advance=(double) face->size->metrics.max_advance/64.0; |
| 1226 | metrics->bounds.x1=0.0; |
| 1227 | metrics->bounds.y1=metrics->descent; |
| 1228 | metrics->bounds.x2=metrics->ascent+metrics->descent; |
| 1229 | metrics->bounds.y2=metrics->ascent+metrics->descent; |
| 1230 | metrics->underline_position=face->underline_position/64.0; |
| 1231 | metrics->underline_thickness=face->underline_thickness/64.0; |
cristy | 71709bf | 2011-09-25 13:10:29 +0000 | [diff] [blame] | 1232 | if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0')) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1233 | { |
| 1234 | (void) FT_Done_Face(face); |
| 1235 | (void) FT_Done_FreeType(library); |
| 1236 | return(MagickTrue); |
| 1237 | } |
| 1238 | /* |
| 1239 | Compute bounding box. |
| 1240 | */ |
| 1241 | if (image->debug != MagickFalse) |
| 1242 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; " |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 1243 | "font-encoding %s; text-encoding %s; pointsize %g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1244 | draw_info->font != (char *) NULL ? draw_info->font : "none", |
| 1245 | encoding != (char *) NULL ? encoding : "none", |
| 1246 | draw_info->encoding != (char *) NULL ? draw_info->encoding : "none", |
| 1247 | draw_info->pointsize); |
| 1248 | flags=FT_LOAD_NO_BITMAP; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1249 | value=GetImageProperty(image,"type:hinting",exception); |
cristy | 3320424 | 2010-10-07 23:17:12 +0000 | [diff] [blame] | 1250 | if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1251 | flags|=FT_LOAD_NO_HINTING; |
| 1252 | glyph.id=0; |
| 1253 | glyph.image=NULL; |
| 1254 | last_glyph.id=0; |
| 1255 | last_glyph.image=NULL; |
| 1256 | origin.x=0; |
| 1257 | origin.y=0; |
| 1258 | affine.xx=65536L; |
| 1259 | affine.yx=0L; |
| 1260 | affine.xy=0L; |
| 1261 | affine.yy=65536L; |
| 1262 | if (draw_info->render != MagickFalse) |
| 1263 | { |
| 1264 | affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5); |
| 1265 | affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5); |
| 1266 | affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5); |
| 1267 | affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5); |
| 1268 | } |
| 1269 | annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info); |
| 1270 | (void) CloneString(&annotate_info->primitive,"path '"); |
| 1271 | if (draw_info->render != MagickFalse) |
| 1272 | { |
| 1273 | if (image->storage_class != DirectClass) |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1274 | (void) SetImageStorageClass(image,DirectClass,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1275 | if (image->matte == MagickFalse) |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1276 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1277 | } |
cristy | c9b1295 | 2010-03-28 01:12:28 +0000 | [diff] [blame] | 1278 | direction=1.0; |
cristy | cf5467b | 2010-03-28 16:22:06 +0000 | [diff] [blame] | 1279 | if (draw_info->direction == RightToLeftDirection) |
cristy | c9b1295 | 2010-03-28 01:12:28 +0000 | [diff] [blame] | 1280 | direction=(-1.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1281 | point.x=0.0; |
| 1282 | point.y=0.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1283 | for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p)) |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1284 | if (GetUTFCode(p) < 0) |
| 1285 | break; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1286 | utf8=(unsigned char *) NULL; |
cristy | d913ea1 | 2010-02-13 02:32:57 +0000 | [diff] [blame] | 1287 | if (GetUTFCode(p) == 0) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1288 | p=draw_info->text; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1289 | else |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1290 | { |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1291 | utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text); |
| 1292 | if (utf8 != (unsigned char *) NULL) |
cristy | c9b1295 | 2010-03-28 01:12:28 +0000 | [diff] [blame] | 1293 | p=(char *) utf8; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1294 | } |
| 1295 | for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p)) |
| 1296 | { |
| 1297 | /* |
| 1298 | Render UTF-8 sequence. |
| 1299 | */ |
| 1300 | glyph.id=FT_Get_Char_Index(face,GetUTFCode(p)); |
| 1301 | if (glyph.id == 0) |
| 1302 | glyph.id=FT_Get_Char_Index(face,'?'); |
| 1303 | if ((glyph.id != 0) && (last_glyph.id != 0)) |
| 1304 | { |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1305 | if (fabs(draw_info->kerning) >= MagickEpsilon) |
cristy | 94b1183 | 2011-09-08 19:46:03 +0000 | [diff] [blame] | 1306 | origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1307 | else |
| 1308 | if (FT_HAS_KERNING(face)) |
| 1309 | { |
| 1310 | FT_Vector |
| 1311 | kerning; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1312 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1313 | status=FT_Get_Kerning(face,last_glyph.id,glyph.id, |
| 1314 | ft_kerning_default,&kerning); |
| 1315 | if (status == 0) |
cristy | 94b1183 | 2011-09-08 19:46:03 +0000 | [diff] [blame] | 1316 | origin.x+=(FT_Pos) (direction*kerning.x); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | glyph.origin=origin; |
| 1320 | status=FT_Load_Glyph(face,glyph.id,flags); |
| 1321 | if (status != 0) |
| 1322 | continue; |
| 1323 | status=FT_Get_Glyph(face->glyph,&glyph.image); |
| 1324 | if (status != 0) |
| 1325 | continue; |
| 1326 | status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline, |
| 1327 | &bounds); |
| 1328 | if (status != 0) |
| 1329 | continue; |
| 1330 | if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1)) |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1331 | metrics->bounds.x1=(double) bounds.xMin; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1332 | if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1)) |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1333 | metrics->bounds.y1=(double) bounds.yMin; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1334 | if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2)) |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1335 | metrics->bounds.x2=(double) bounds.xMax; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1336 | if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2)) |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1337 | metrics->bounds.y2=(double) bounds.yMax; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1338 | if (draw_info->render != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1339 | if ((draw_info->stroke.alpha != TransparentAlpha) || |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1340 | (draw_info->stroke_pattern != (Image *) NULL)) |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1341 | { |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1342 | /* |
| 1343 | Trace the glyph. |
| 1344 | */ |
| 1345 | annotate_info->affine.tx=glyph.origin.x/64.0; |
| 1346 | annotate_info->affine.ty=glyph.origin.y/64.0; |
| 1347 | (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)-> |
| 1348 | outline,&OutlineMethods,annotate_info); |
| 1349 | } |
| 1350 | FT_Vector_Transform(&glyph.origin,&affine); |
| 1351 | (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin); |
| 1352 | status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal, |
| 1353 | (FT_Vector *) NULL,MagickTrue); |
| 1354 | if (status != 0) |
| 1355 | continue; |
| 1356 | bitmap=(FT_BitmapGlyph) glyph.image; |
| 1357 | point.x=offset->x+bitmap->left; |
cristy | 2f5b5c7 | 2011-09-16 16:52:00 +0000 | [diff] [blame] | 1358 | if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono) |
| 1359 | point.x=offset->x+(origin.x >> 6); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1360 | point.y=offset->y-bitmap->top; |
| 1361 | if (draw_info->render != MagickFalse) |
| 1362 | { |
| 1363 | CacheView |
| 1364 | *image_view; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1365 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1366 | MagickBooleanType |
| 1367 | status; |
| 1368 | |
cristy | 2f5b5c7 | 2011-09-16 16:52:00 +0000 | [diff] [blame] | 1369 | register unsigned char |
| 1370 | *p; |
| 1371 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1372 | /* |
| 1373 | Rasterize the glyph. |
| 1374 | */ |
| 1375 | status=MagickTrue; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1376 | image_view=AcquireCacheView(image); |
cristy | 2f5b5c7 | 2011-09-16 16:52:00 +0000 | [diff] [blame] | 1377 | p=bitmap->bitmap.buffer; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1378 | for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1379 | { |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1380 | MagickBooleanType |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1381 | active, |
| 1382 | sync; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1383 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1384 | MagickRealType |
| 1385 | fill_opacity; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1386 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1387 | PixelInfo |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1388 | fill_color; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1389 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1390 | register Quantum |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1391 | *restrict q; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1392 | |
cristy | 8815806 | 2011-06-09 00:31:43 +0000 | [diff] [blame] | 1393 | register ssize_t |
| 1394 | x; |
| 1395 | |
cristy | 73ce31b | 2010-06-13 23:17:39 +0000 | [diff] [blame] | 1396 | ssize_t |
cristy | 2f5b5c7 | 2011-09-16 16:52:00 +0000 | [diff] [blame] | 1397 | n, |
cristy | 73ce31b | 2010-06-13 23:17:39 +0000 | [diff] [blame] | 1398 | x_offset, |
| 1399 | y_offset; |
| 1400 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1401 | if (status == MagickFalse) |
| 1402 | continue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1403 | x_offset=(ssize_t) ceil(point.x-0.5); |
| 1404 | y_offset=(ssize_t) ceil(point.y+y-0.5); |
| 1405 | if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows)) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1406 | continue; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1407 | q=(Quantum *) NULL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1408 | if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns)) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1409 | active=MagickFalse; |
| 1410 | else |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1411 | { |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1412 | q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset, |
| 1413 | bitmap->bitmap.width,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1414 | active=q != (Quantum *) NULL ? MagickTrue : MagickFalse; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1415 | } |
cristy | 854209a | 2011-09-19 17:02:57 +0000 | [diff] [blame] | 1416 | n=y*bitmap->bitmap.pitch-1; |
| 1417 | for (x=0; x < (ssize_t) bitmap->bitmap.width; x++) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1418 | { |
cristy | 854209a | 2011-09-19 17:02:57 +0000 | [diff] [blame] | 1419 | n++; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1420 | x_offset++; |
cristy | 2f5b5c7 | 2011-09-16 16:52:00 +0000 | [diff] [blame] | 1421 | if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns)) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1422 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1423 | q+=GetPixelChannels(image); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1424 | continue; |
| 1425 | } |
cristy | 2f5b5c7 | 2011-09-16 16:52:00 +0000 | [diff] [blame] | 1426 | if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono) |
| 1427 | fill_opacity=(MagickRealType) (p[n])/(bitmap->bitmap.num_grays-1); |
| 1428 | else |
| 1429 | fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] & |
| 1430 | (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1431 | if (draw_info->text_antialias == MagickFalse) |
| 1432 | fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0; |
| 1433 | if (active == MagickFalse) |
| 1434 | q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1, |
| 1435 | exception); |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 1436 | if (q == (Quantum *) NULL) |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1437 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1438 | q+=GetPixelChannels(image); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1439 | continue; |
| 1440 | } |
cristy | ec061ec | 2011-10-21 17:41:17 +0000 | [diff] [blame] | 1441 | GetPixelInfo(image,&fill_color); |
cristy | 2ed42f6 | 2011-10-02 19:49:57 +0000 | [diff] [blame] | 1442 | (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color, |
| 1443 | exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1444 | fill_opacity=fill_opacity*fill_color.alpha; |
| 1445 | CompositePixelOver(image,&fill_color,fill_opacity,q, |
| 1446 | GetPixelAlpha(image,q),q); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1447 | if (active == MagickFalse) |
| 1448 | { |
| 1449 | sync=SyncCacheViewAuthenticPixels(image_view,exception); |
| 1450 | if (sync == MagickFalse) |
| 1451 | status=MagickFalse; |
| 1452 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1453 | q+=GetPixelChannels(image); |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1454 | } |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1455 | sync=SyncCacheViewAuthenticPixels(image_view,exception); |
| 1456 | if (sync == MagickFalse) |
| 1457 | status=MagickFalse; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 1458 | } |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1459 | image_view=DestroyCacheView(image_view); |
| 1460 | } |
| 1461 | if ((bitmap->left+bitmap->bitmap.width) > metrics->width) |
| 1462 | metrics->width=bitmap->left+bitmap->bitmap.width; |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1463 | if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) && |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1464 | (IsUTFSpace(GetUTFCode(p)) != MagickFalse) && |
| 1465 | (IsUTFSpace(code) == MagickFalse)) |
cristy | 94b1183 | 2011-09-08 19:46:03 +0000 | [diff] [blame] | 1466 | origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1467 | else |
cristy | 94b1183 | 2011-09-08 19:46:03 +0000 | [diff] [blame] | 1468 | origin.x+=(FT_Pos) (direction*face->glyph->advance.x); |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1469 | metrics->origin.x=(double) origin.x; |
| 1470 | metrics->origin.y=(double) origin.y; |
cristy | bd5a96c | 2011-08-21 00:04:26 +0000 | [diff] [blame] | 1471 | if (last_glyph.id != 0) |
| 1472 | FT_Done_Glyph(last_glyph.image); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 1473 | last_glyph=glyph; |
| 1474 | code=GetUTFCode(p); |
| 1475 | } |
| 1476 | if (utf8 != (unsigned char *) NULL) |
| 1477 | utf8=(unsigned char *) RelinquishMagickMemory(utf8); |
cristy | bd5a96c | 2011-08-21 00:04:26 +0000 | [diff] [blame] | 1478 | if (last_glyph.id != 0) |
| 1479 | FT_Done_Glyph(last_glyph.image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1480 | if ((draw_info->stroke.alpha != TransparentAlpha) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1481 | (draw_info->stroke_pattern != (Image *) NULL)) |
| 1482 | { |
| 1483 | if (draw_info->render != MagickFalse) |
| 1484 | { |
| 1485 | /* |
| 1486 | Draw text stroke. |
| 1487 | */ |
| 1488 | annotate_info->linejoin=RoundJoin; |
| 1489 | annotate_info->affine.tx=offset->x; |
| 1490 | annotate_info->affine.ty=offset->y; |
| 1491 | (void) ConcatenateString(&annotate_info->primitive,"'"); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1492 | (void) DrawImage(image,annotate_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | /* |
| 1496 | Determine font metrics. |
| 1497 | */ |
| 1498 | glyph.id=FT_Get_Char_Index(face,'_'); |
| 1499 | glyph.origin=origin; |
| 1500 | status=FT_Load_Glyph(face,glyph.id,flags); |
| 1501 | if (status == 0) |
| 1502 | { |
| 1503 | status=FT_Get_Glyph(face->glyph,&glyph.image); |
| 1504 | if (status == 0) |
| 1505 | { |
| 1506 | status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)-> |
| 1507 | outline,&bounds); |
| 1508 | if (status == 0) |
| 1509 | { |
| 1510 | FT_Vector_Transform(&glyph.origin,&affine); |
| 1511 | (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin); |
| 1512 | status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal, |
| 1513 | (FT_Vector *) NULL,MagickTrue); |
| 1514 | bitmap=(FT_BitmapGlyph) glyph.image; |
| 1515 | if (bitmap->left > metrics->width) |
| 1516 | metrics->width=bitmap->left; |
| 1517 | } |
| 1518 | } |
cristy | e2c81ad | 2011-08-20 22:54:14 +0000 | [diff] [blame] | 1519 | FT_Done_Glyph(glyph.image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1520 | } |
| 1521 | metrics->width-=metrics->bounds.x1/64.0; |
| 1522 | metrics->bounds.x1/=64.0; |
| 1523 | metrics->bounds.y1/=64.0; |
| 1524 | metrics->bounds.x2/=64.0; |
| 1525 | metrics->bounds.y2/=64.0; |
| 1526 | metrics->origin.x/=64.0; |
| 1527 | metrics->origin.y/=64.0; |
| 1528 | /* |
| 1529 | Relinquish resources. |
| 1530 | */ |
| 1531 | annotate_info=DestroyDrawInfo(annotate_info); |
| 1532 | (void) FT_Done_Face(face); |
| 1533 | (void) FT_Done_FreeType(library); |
| 1534 | return(MagickTrue); |
| 1535 | } |
| 1536 | #else |
| 1537 | static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info, |
| 1538 | const char *magick_unused(encoding),const PointInfo *offset, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1539 | TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1540 | { |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1541 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1542 | MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)", |
cristy | ee08182 | 2011-02-02 19:07:30 +0000 | [diff] [blame] | 1543 | draw_info->font != (char *) NULL ? draw_info->font : "none"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1544 | return(RenderPostscript(image,draw_info,offset,metrics)); |
| 1545 | } |
| 1546 | #endif |
| 1547 | |
| 1548 | /* |
| 1549 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1550 | % % |
| 1551 | % % |
| 1552 | % % |
| 1553 | + R e n d e r P o s t s c r i p t % |
| 1554 | % % |
| 1555 | % % |
| 1556 | % % |
| 1557 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1558 | % |
| 1559 | % RenderPostscript() renders text on the image with a Postscript font. It |
| 1560 | % also returns the bounding box of the text relative to the image. |
| 1561 | % |
| 1562 | % The format of the RenderPostscript method is: |
| 1563 | % |
| 1564 | % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1565 | % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1566 | % |
| 1567 | % A description of each parameter follows: |
| 1568 | % |
| 1569 | % o image: the image. |
| 1570 | % |
| 1571 | % o draw_info: the draw info. |
| 1572 | % |
| 1573 | % o offset: (x,y) location of text relative to image. |
| 1574 | % |
| 1575 | % o metrics: bounding box of text. |
| 1576 | % |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1577 | % o exception: return any errors or warnings in this structure. |
| 1578 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1579 | */ |
| 1580 | |
| 1581 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 1582 | { |
| 1583 | if (x < y) |
| 1584 | return(x); |
| 1585 | return(y); |
| 1586 | } |
| 1587 | |
| 1588 | static char *EscapeParenthesis(const char *text) |
| 1589 | { |
| 1590 | char |
| 1591 | *buffer; |
| 1592 | |
| 1593 | register char |
| 1594 | *p; |
| 1595 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1596 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1597 | i; |
| 1598 | |
| 1599 | size_t |
| 1600 | escapes; |
| 1601 | |
| 1602 | escapes=0; |
| 1603 | buffer=AcquireString(text); |
| 1604 | p=buffer; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1605 | for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1606 | { |
| 1607 | if ((text[i] == '(') || (text[i] == ')')) |
| 1608 | { |
| 1609 | *p++='\\'; |
| 1610 | escapes++; |
| 1611 | } |
| 1612 | *p++=text[i]; |
| 1613 | } |
| 1614 | *p='\0'; |
| 1615 | return(buffer); |
| 1616 | } |
| 1617 | |
| 1618 | static MagickBooleanType RenderPostscript(Image *image, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1619 | const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics, |
| 1620 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1621 | { |
| 1622 | char |
| 1623 | filename[MaxTextExtent], |
| 1624 | geometry[MaxTextExtent], |
| 1625 | *text; |
| 1626 | |
| 1627 | FILE |
| 1628 | *file; |
| 1629 | |
| 1630 | Image |
| 1631 | *annotate_image; |
| 1632 | |
| 1633 | ImageInfo |
| 1634 | *annotate_info; |
| 1635 | |
| 1636 | int |
| 1637 | unique_file; |
| 1638 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1639 | MagickBooleanType |
| 1640 | identity; |
| 1641 | |
| 1642 | PointInfo |
| 1643 | extent, |
| 1644 | point, |
| 1645 | resolution; |
| 1646 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1647 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1648 | i; |
| 1649 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1650 | ssize_t |
| 1651 | y; |
| 1652 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1653 | /* |
| 1654 | Render label with a Postscript font. |
| 1655 | */ |
| 1656 | if (image->debug != MagickFalse) |
| 1657 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 1658 | "Font %s; pointsize %g",draw_info->font != (char *) NULL ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1659 | draw_info->font : "none",draw_info->pointsize); |
| 1660 | file=(FILE *) NULL; |
| 1661 | unique_file=AcquireUniqueFileResource(filename); |
| 1662 | if (unique_file != -1) |
| 1663 | file=fdopen(unique_file,"wb"); |
| 1664 | if ((unique_file == -1) || (file == (FILE *) NULL)) |
| 1665 | { |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1666 | ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1667 | return(MagickFalse); |
| 1668 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1669 | (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n"); |
| 1670 | (void) FormatLocaleFile(file,"/ReencodeType\n"); |
| 1671 | (void) FormatLocaleFile(file,"{\n"); |
| 1672 | (void) FormatLocaleFile(file," findfont dup length\n"); |
| 1673 | (void) FormatLocaleFile(file, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1674 | " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n"); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1675 | (void) FormatLocaleFile(file, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1676 | " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n"); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1677 | (void) FormatLocaleFile(file,"} bind def\n"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1678 | /* |
| 1679 | Sample to compute bounding box. |
| 1680 | */ |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 1681 | identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) && |
| 1682 | (fabs(draw_info->affine.rx) < MagickEpsilon) && |
| 1683 | (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1684 | extent.x=0.0; |
| 1685 | extent.y=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1686 | for (i=0; i <= (ssize_t) (strlen(draw_info->text)+2); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1687 | { |
| 1688 | point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+ |
| 1689 | draw_info->affine.ry*2.0*draw_info->pointsize); |
| 1690 | point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+ |
| 1691 | draw_info->affine.sy*2.0*draw_info->pointsize); |
| 1692 | if (point.x > extent.x) |
| 1693 | extent.x=point.x; |
| 1694 | if (point.y > extent.y) |
| 1695 | extent.y=point.y; |
| 1696 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1697 | (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 : |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1698 | extent.x/2.0,extent.y/2.0); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1699 | (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1700 | draw_info->pointsize); |
| 1701 | if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') || |
| 1702 | (strchr(draw_info->font,'/') != (char *) NULL)) |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1703 | (void) FormatLocaleFile(file, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1704 | "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n"); |
| 1705 | else |
cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 1706 | (void) FormatLocaleFile(file, |
| 1707 | "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font, |
| 1708 | draw_info->font); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1709 | (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n", |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 1710 | draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry, |
| 1711 | draw_info->affine.sy); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1712 | text=EscapeParenthesis(draw_info->text); |
| 1713 | if (identity == MagickFalse) |
cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 1714 | (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n", |
| 1715 | text); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1716 | (void) FormatLocaleFile(file,"(%s) show\n",text); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1717 | text=DestroyString(text); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1718 | (void) FormatLocaleFile(file,"showpage\n"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1719 | (void) fclose(file); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1720 | (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1721 | floor(extent.x+0.5),floor(extent.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1722 | annotate_info=AcquireImageInfo(); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1723 | (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1724 | filename); |
| 1725 | (void) CloneString(&annotate_info->page,geometry); |
| 1726 | if (draw_info->density != (char *) NULL) |
| 1727 | (void) CloneString(&annotate_info->density,draw_info->density); |
| 1728 | annotate_info->antialias=draw_info->text_antialias; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1729 | annotate_image=ReadImage(annotate_info,exception); |
| 1730 | CatchException(exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1731 | annotate_info=DestroyImageInfo(annotate_info); |
| 1732 | (void) RelinquishUniqueFileResource(filename); |
| 1733 | if (annotate_image == (Image *) NULL) |
| 1734 | return(MagickFalse); |
| 1735 | resolution.x=DefaultResolution; |
| 1736 | resolution.y=DefaultResolution; |
| 1737 | if (draw_info->density != (char *) NULL) |
| 1738 | { |
| 1739 | GeometryInfo |
| 1740 | geometry_info; |
| 1741 | |
| 1742 | MagickStatusType |
| 1743 | flags; |
| 1744 | |
| 1745 | flags=ParseGeometry(draw_info->density,&geometry_info); |
| 1746 | resolution.x=geometry_info.rho; |
| 1747 | resolution.y=geometry_info.sigma; |
| 1748 | if ((flags & SigmaValue) == 0) |
| 1749 | resolution.y=resolution.x; |
| 1750 | } |
| 1751 | if (identity == MagickFalse) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 1752 | (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1753 | else |
| 1754 | { |
| 1755 | RectangleInfo |
| 1756 | crop_info; |
| 1757 | |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1758 | crop_info=GetImageBoundingBox(annotate_image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1759 | crop_info.height=(size_t) ((resolution.y/DefaultResolution)* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1760 | ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1761 | crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0- |
cristy | 06609ee | 2010-03-17 20:21:27 +0000 | [diff] [blame] | 1762 | 0.5); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1763 | (void) FormatLocaleString(geometry,MaxTextExtent, |
cristy | 6d8abba | 2010-06-03 01:10:47 +0000 | [diff] [blame] | 1764 | "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double) |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1765 | crop_info.height,(double) crop_info.x,(double) crop_info.y); |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 1766 | (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1767 | } |
| 1768 | metrics->pixels_per_em.x=(resolution.y/DefaultResolution)* |
| 1769 | ExpandAffine(&draw_info->affine)*draw_info->pointsize; |
| 1770 | metrics->pixels_per_em.y=metrics->pixels_per_em.x; |
| 1771 | metrics->ascent=metrics->pixels_per_em.x; |
| 1772 | metrics->descent=metrics->pixels_per_em.y/-5.0; |
| 1773 | metrics->width=(double) annotate_image->columns/ |
| 1774 | ExpandAffine(&draw_info->affine); |
| 1775 | metrics->height=1.152*metrics->pixels_per_em.x; |
| 1776 | metrics->max_advance=metrics->pixels_per_em.x; |
| 1777 | metrics->bounds.x1=0.0; |
| 1778 | metrics->bounds.y1=metrics->descent; |
| 1779 | metrics->bounds.x2=metrics->ascent+metrics->descent; |
| 1780 | metrics->bounds.y2=metrics->ascent+metrics->descent; |
| 1781 | metrics->underline_position=(-2.0); |
| 1782 | metrics->underline_thickness=1.0; |
| 1783 | if (draw_info->render == MagickFalse) |
| 1784 | { |
| 1785 | annotate_image=DestroyImage(annotate_image); |
| 1786 | return(MagickTrue); |
| 1787 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1788 | if (draw_info->fill.alpha != TransparentAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1789 | { |
cristy | bd65948 | 2011-10-09 23:19:32 +0000 | [diff] [blame] | 1790 | CacheView |
| 1791 | *annotate_view; |
| 1792 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1793 | MagickBooleanType |
| 1794 | sync; |
| 1795 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1796 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1797 | fill_color; |
| 1798 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1799 | /* |
| 1800 | Render fill color. |
| 1801 | */ |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 1802 | if (image->matte == MagickFalse) |
| 1803 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception); |
| 1804 | if (annotate_image->matte == MagickFalse) |
| 1805 | (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel, |
| 1806 | exception); |
| 1807 | fill_color=draw_info->fill; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1808 | annotate_view=AcquireCacheView(annotate_image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1809 | for (y=0; y < (ssize_t) annotate_image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1810 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1811 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1812 | x; |
| 1813 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1814 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1815 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1816 | |
| 1817 | q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns, |
| 1818 | 1,exception); |
cristy | 6193b86 | 2011-08-06 16:33:59 +0000 | [diff] [blame] | 1819 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1820 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1821 | for (x=0; x < (ssize_t) annotate_image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1822 | { |
cristy | 2ed42f6 | 2011-10-02 19:49:57 +0000 | [diff] [blame] | 1823 | (void) GetFillColor(draw_info,x,y,&fill_color,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1824 | SetPixelAlpha(annotate_image,ClampToQuantum((((MagickRealType) |
| 1825 | GetPixelIntensity(annotate_image,q)*fill_color.alpha)/ |
| 1826 | QuantumRange)),q); |
| 1827 | SetPixelRed(annotate_image,fill_color.red,q); |
| 1828 | SetPixelGreen(annotate_image,fill_color.green,q); |
| 1829 | SetPixelBlue(annotate_image,fill_color.blue,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1830 | q+=GetPixelChannels(annotate_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1831 | } |
| 1832 | sync=SyncCacheViewAuthenticPixels(annotate_view,exception); |
| 1833 | if (sync == MagickFalse) |
| 1834 | break; |
| 1835 | } |
| 1836 | annotate_view=DestroyCacheView(annotate_view); |
| 1837 | (void) CompositeImage(image,OverCompositeOp,annotate_image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1838 | (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+ |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 1839 | metrics->descent)-0.5),exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1840 | } |
| 1841 | annotate_image=DestroyImage(annotate_image); |
| 1842 | return(MagickTrue); |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1847 | % % |
| 1848 | % % |
| 1849 | % % |
| 1850 | + R e n d e r X 1 1 % |
| 1851 | % % |
| 1852 | % % |
| 1853 | % % |
| 1854 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1855 | % |
| 1856 | % RenderX11() renders text on the image with an X11 font. It also returns the |
| 1857 | % bounding box of the text relative to the image. |
| 1858 | % |
| 1859 | % The format of the RenderX11 method is: |
| 1860 | % |
| 1861 | % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1862 | % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1863 | % |
| 1864 | % A description of each parameter follows: |
| 1865 | % |
| 1866 | % o image: the image. |
| 1867 | % |
| 1868 | % o draw_info: the draw info. |
| 1869 | % |
| 1870 | % o offset: (x,y) location of text relative to image. |
| 1871 | % |
| 1872 | % o metrics: bounding box of text. |
| 1873 | % |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1874 | % o exception: return any errors or warnings in this structure. |
| 1875 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1876 | */ |
| 1877 | #if defined(MAGICKCORE_X11_DELEGATE) |
| 1878 | static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 1879 | const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1880 | { |
| 1881 | MagickBooleanType |
| 1882 | status; |
| 1883 | |
| 1884 | static DrawInfo |
| 1885 | cache_info; |
| 1886 | |
| 1887 | static Display |
| 1888 | *display = (Display *) NULL; |
| 1889 | |
| 1890 | static XAnnotateInfo |
| 1891 | annotate_info; |
| 1892 | |
| 1893 | static XFontStruct |
| 1894 | *font_info; |
| 1895 | |
| 1896 | static XPixelInfo |
| 1897 | pixel; |
| 1898 | |
| 1899 | static XResourceInfo |
| 1900 | resource_info; |
| 1901 | |
| 1902 | static XrmDatabase |
| 1903 | resource_database; |
| 1904 | |
| 1905 | static XStandardColormap |
| 1906 | *map_info; |
| 1907 | |
| 1908 | static XVisualInfo |
| 1909 | *visual_info; |
| 1910 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1911 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1912 | height, |
| 1913 | width; |
| 1914 | |
cristy | d7ecaca | 2011-01-24 14:14:53 +0000 | [diff] [blame] | 1915 | if (annotate_semaphore == (SemaphoreInfo *) NULL) |
| 1916 | AcquireSemaphoreInfo(&annotate_semaphore); |
| 1917 | LockSemaphoreInfo(annotate_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1918 | if (display == (Display *) NULL) |
| 1919 | { |
cristy | 104cea8 | 2009-10-25 02:26:51 +0000 | [diff] [blame] | 1920 | const char |
| 1921 | *client_name; |
| 1922 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1923 | ImageInfo |
| 1924 | *image_info; |
| 1925 | |
| 1926 | /* |
| 1927 | Open X server connection. |
| 1928 | */ |
| 1929 | display=XOpenDisplay(draw_info->server_name); |
| 1930 | if (display == (Display *) NULL) |
| 1931 | { |
| 1932 | ThrowXWindowException(XServerError,"UnableToOpenXServer", |
| 1933 | draw_info->server_name); |
| 1934 | return(MagickFalse); |
| 1935 | } |
| 1936 | /* |
| 1937 | Get user defaults from X resource database. |
| 1938 | */ |
| 1939 | (void) XSetErrorHandler(XError); |
| 1940 | image_info=AcquireImageInfo(); |
cristy | 104cea8 | 2009-10-25 02:26:51 +0000 | [diff] [blame] | 1941 | client_name=GetClientName(); |
cristy | 309c861 | 2009-10-25 13:16:09 +0000 | [diff] [blame] | 1942 | resource_database=XGetResourceDatabase(display,client_name); |
cristy | 104cea8 | 2009-10-25 02:26:51 +0000 | [diff] [blame] | 1943 | XGetResourceInfo(image_info,resource_database,client_name,&resource_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1944 | resource_info.close_server=MagickFalse; |
| 1945 | resource_info.colormap=PrivateColormap; |
| 1946 | resource_info.font=AcquireString(draw_info->font); |
| 1947 | resource_info.background_color=AcquireString("#ffffffffffff"); |
| 1948 | resource_info.foreground_color=AcquireString("#000000000000"); |
| 1949 | map_info=XAllocStandardColormap(); |
| 1950 | if (map_info == (XStandardColormap *) NULL) |
| 1951 | { |
| 1952 | ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed", |
| 1953 | image->filename); |
| 1954 | return(MagickFalse); |
| 1955 | } |
| 1956 | /* |
| 1957 | Initialize visual info. |
| 1958 | */ |
| 1959 | visual_info=XBestVisualInfo(display,map_info,&resource_info); |
| 1960 | if (visual_info == (XVisualInfo *) NULL) |
| 1961 | { |
| 1962 | ThrowXWindowException(XServerError,"UnableToGetVisual", |
| 1963 | image->filename); |
| 1964 | return(MagickFalse); |
| 1965 | } |
| 1966 | map_info->colormap=(Colormap) NULL; |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 1967 | pixel.pixels=(unsigned long *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1968 | /* |
| 1969 | Initialize Standard Colormap info. |
| 1970 | */ |
| 1971 | XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen), |
| 1972 | map_info); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1973 | XGetPixelInfo(display,visual_info,map_info,&resource_info,(Image *) NULL, |
| 1974 | &pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1975 | pixel.annotate_context=XDefaultGC(display,visual_info->screen); |
| 1976 | /* |
| 1977 | Initialize font info. |
| 1978 | */ |
| 1979 | font_info=XBestFont(display,&resource_info,MagickFalse); |
| 1980 | if (font_info == (XFontStruct *) NULL) |
| 1981 | { |
| 1982 | ThrowXWindowException(XServerError,"UnableToLoadFont", |
| 1983 | draw_info->font); |
| 1984 | return(MagickFalse); |
| 1985 | } |
| 1986 | if ((map_info == (XStandardColormap *) NULL) || |
| 1987 | (visual_info == (XVisualInfo *) NULL) || |
| 1988 | (font_info == (XFontStruct *) NULL)) |
| 1989 | { |
| 1990 | XFreeResources(display,visual_info,map_info,&pixel,font_info, |
| 1991 | &resource_info,(XWindowInfo *) NULL); |
| 1992 | ThrowXWindowException(XServerError,"UnableToLoadFont", |
| 1993 | image->filename); |
| 1994 | return(MagickFalse); |
| 1995 | } |
| 1996 | cache_info=(*draw_info); |
| 1997 | } |
cristy | d7ecaca | 2011-01-24 14:14:53 +0000 | [diff] [blame] | 1998 | UnlockSemaphoreInfo(annotate_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1999 | /* |
| 2000 | Initialize annotate info. |
| 2001 | */ |
| 2002 | XGetAnnotateInfo(&annotate_info); |
| 2003 | annotate_info.stencil=ForegroundStencil; |
| 2004 | if (cache_info.font != draw_info->font) |
| 2005 | { |
| 2006 | /* |
| 2007 | Type name has changed. |
| 2008 | */ |
| 2009 | (void) XFreeFont(display,font_info); |
| 2010 | (void) CloneString(&resource_info.font,draw_info->font); |
| 2011 | font_info=XBestFont(display,&resource_info,MagickFalse); |
| 2012 | if (font_info == (XFontStruct *) NULL) |
| 2013 | { |
| 2014 | ThrowXWindowException(XServerError,"UnableToLoadFont", |
| 2015 | draw_info->font); |
| 2016 | return(MagickFalse); |
| 2017 | } |
| 2018 | } |
| 2019 | if (image->debug != MagickFalse) |
| 2020 | (void) LogMagickEvent(AnnotateEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 2021 | "Font %s; pointsize %g",draw_info->font != (char *) NULL ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2022 | draw_info->font : "none",draw_info->pointsize); |
| 2023 | cache_info=(*draw_info); |
| 2024 | annotate_info.font_info=font_info; |
| 2025 | annotate_info.text=(char *) draw_info->text; |
| 2026 | annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text, |
| 2027 | (int) strlen(draw_info->text)); |
| 2028 | annotate_info.height=(unsigned int) font_info->ascent+font_info->descent; |
| 2029 | metrics->pixels_per_em.x=(double) font_info->max_bounds.width; |
| 2030 | metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent; |
| 2031 | metrics->ascent=(double) font_info->ascent+4; |
| 2032 | metrics->descent=(double) (-font_info->descent); |
| 2033 | metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine); |
| 2034 | metrics->height=font_info->ascent+font_info->descent; |
| 2035 | metrics->max_advance=(double) font_info->max_bounds.width; |
| 2036 | metrics->bounds.x1=0.0; |
| 2037 | metrics->bounds.y1=metrics->descent; |
| 2038 | metrics->bounds.x2=metrics->ascent+metrics->descent; |
| 2039 | metrics->bounds.y2=metrics->ascent+metrics->descent; |
| 2040 | metrics->underline_position=(-2.0); |
| 2041 | metrics->underline_thickness=1.0; |
| 2042 | if (draw_info->render == MagickFalse) |
| 2043 | return(MagickTrue); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2044 | if (draw_info->fill.alpha == TransparentAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2045 | return(MagickTrue); |
| 2046 | /* |
| 2047 | Render fill color. |
| 2048 | */ |
| 2049 | width=annotate_info.width; |
| 2050 | height=annotate_info.height; |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 2051 | if ((fabs(draw_info->affine.rx) >= MagickEpsilon) || |
| 2052 | (fabs(draw_info->affine.ry) >= MagickEpsilon)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2053 | { |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 2054 | if ((fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) && |
| 2055 | (fabs(draw_info->affine.rx+draw_info->affine.ry) < MagickEpsilon)) |
| 2056 | annotate_info.degrees=(double) (180.0/MagickPI)* |
cristy | 854209a | 2011-09-19 17:02:57 +0000 | [diff] [blame] | 2057 | atan2(draw_info->affine.rx,draw_info->affine.sx); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2058 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2059 | (void) FormatLocaleString(annotate_info.geometry,MaxTextExtent, |
cristy | 6d8abba | 2010-06-03 01:10:47 +0000 | [diff] [blame] | 2060 | "%.20gx%.20g%+.20g%+.20g",(double) width,(double) height, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2061 | ceil(offset->x-0.5),ceil(offset->y-metrics->ascent-metrics->descent+ |
cristy | 06609ee | 2010-03-17 20:21:27 +0000 | [diff] [blame] | 2062 | draw_info->interline_spacing-0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2063 | pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red); |
| 2064 | pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green); |
| 2065 | pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue); |
cristy | 7c3af95 | 2011-10-20 16:04:16 +0000 | [diff] [blame] | 2066 | status=XAnnotateImage(display,&pixel,&annotate_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2067 | if (status == 0) |
| 2068 | { |
| 2069 | ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed", |
| 2070 | image->filename); |
| 2071 | return(MagickFalse); |
| 2072 | } |
| 2073 | return(MagickTrue); |
| 2074 | } |
| 2075 | #else |
| 2076 | static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info, |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 2077 | const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2078 | { |
| 2079 | (void) draw_info; |
| 2080 | (void) offset; |
| 2081 | (void) metrics; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 2082 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2083 | MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (X11)", |
| 2084 | image->filename); |
| 2085 | return(MagickFalse); |
| 2086 | } |
| 2087 | #endif |