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