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