cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP AAA N N GGGG OOO % |
| 7 | % P P A A NN N G O O % |
| 8 | % PPPP AAAAA N N N G GGG O O % |
| 9 | % P M A A N NN G G O O % |
| 10 | % P A A N N GGGG OOO % |
| 11 | % % |
| 12 | % % |
| 13 | % Read Pango Markup Language Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % March 2012 % |
| 18 | % % |
| 19 | % % |
| 20 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
| 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
| 42 | #include "MagickCore/studio.h" |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 43 | #include "MagickCore/annotate.h" |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 46 | #include "MagickCore/composite-private.h" |
| 47 | #include "MagickCore/draw.h" |
| 48 | #include "MagickCore/draw-private.h" |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 49 | #include "MagickCore/exception.h" |
| 50 | #include "MagickCore/exception-private.h" |
| 51 | #include "MagickCore/image.h" |
| 52 | #include "MagickCore/image-private.h" |
| 53 | #include "MagickCore/list.h" |
| 54 | #include "MagickCore/magick.h" |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 55 | #include "MagickCore/module.h" |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 56 | #include "MagickCore/memory_.h" |
| 57 | #include "MagickCore/option.h" |
| 58 | #include "MagickCore/pixel-accessor.h" |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 59 | #include "MagickCore/property.h" |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 60 | #include "MagickCore/quantum-private.h" |
| 61 | #include "MagickCore/static.h" |
| 62 | #include "MagickCore/string_.h" |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 63 | #if defined(MAGICKCORE_PANGOFT2_DELEGATE) |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 64 | #include <pango/pango.h> |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 65 | #include <pango/pangoft2.h> |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 68 | #if defined(MAGICKCORE_PANGOFT2_DELEGATE) |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 69 | /* |
| 70 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 71 | % % |
| 72 | % % |
| 73 | % % |
cristy | 6c28ec0 | 2011-10-28 11:37:57 +0000 | [diff] [blame] | 74 | % R e a d P A N G O I m a g e % |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 75 | % % |
| 76 | % % |
| 77 | % % |
| 78 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 79 | % |
| 80 | % ReadPANGOImage() reads an image in the Pango Markup Language Format. |
| 81 | % |
| 82 | % The format of the ReadPANGOImage method is: |
| 83 | % |
| 84 | % Image *ReadPANGOImage(const ImageInfo *image_info, |
| 85 | % ExceptionInfo *exception) |
| 86 | % |
| 87 | % A description of each parameter follows: |
| 88 | % |
| 89 | % o image_info: the image info. |
| 90 | % |
| 91 | % o exception: return any errors or warnings in this structure. |
| 92 | % |
| 93 | */ |
| 94 | static Image *ReadPANGOImage(const ImageInfo *image_info, |
| 95 | ExceptionInfo *exception) |
| 96 | { |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 97 | char |
| 98 | *caption, |
| 99 | *property; |
| 100 | |
| 101 | DrawInfo |
| 102 | *draw_info; |
| 103 | |
| 104 | FT_Bitmap |
| 105 | *canvas; |
| 106 | |
| 107 | Image |
| 108 | *image; |
| 109 | |
| 110 | MagickBooleanType |
| 111 | status; |
| 112 | |
| 113 | PangoContext |
| 114 | *context; |
| 115 | |
| 116 | PangoFontDescription |
| 117 | *description; |
| 118 | |
| 119 | PangoFontMap |
| 120 | *fontmap; |
| 121 | |
| 122 | PangoLayout |
| 123 | *layout; |
| 124 | |
| 125 | PixelInfo |
| 126 | fill_color; |
| 127 | |
| 128 | register Quantum |
| 129 | *q; |
| 130 | |
| 131 | register unsigned char |
| 132 | *p; |
| 133 | |
| 134 | ssize_t |
| 135 | y; |
| 136 | |
| 137 | /* |
| 138 | Initialize Image structure. |
| 139 | */ |
| 140 | assert(image_info != (const ImageInfo *) NULL); |
| 141 | assert(image_info->signature == MagickSignature); |
| 142 | if (image_info->debug != MagickFalse) |
| 143 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 144 | image_info->filename); |
| 145 | assert(exception != (ExceptionInfo *) NULL); |
| 146 | assert(exception->signature == MagickSignature); |
| 147 | image=AcquireImage(image_info,exception); |
| 148 | if ((image->columns == 0) || (image->rows == 0)) |
| 149 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
| 150 | (void) ResetImagePage(image,"0x0+0+0"); |
| 151 | if (SetImageBackgroundColor(image,exception) == MagickFalse) |
| 152 | { |
| 153 | image=DestroyImageList(image); |
| 154 | return((Image *) NULL); |
| 155 | } |
| 156 | /* |
| 157 | Get context. |
| 158 | */ |
| 159 | fontmap=(PangoFontMap *) pango_ft2_font_map_new(); |
| 160 | pango_ft2_font_map_set_resolution((PangoFT2FontMap *) fontmap, |
| 161 | image->x_resolution,image->y_resolution); |
| 162 | pango_ft2_font_map_set_default_substitute((PangoFT2FontMap *) fontmap,NULL, |
| 163 | NULL,NULL); |
| 164 | context=pango_font_map_create_context(fontmap); |
| 165 | /* |
| 166 | Create canvas. |
| 167 | */ |
| 168 | canvas=(FT_Bitmap *) AcquireMagickMemory(sizeof(*canvas)); |
| 169 | if (canvas == (FT_Bitmap *) NULL) |
| 170 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 171 | canvas->width=image->columns; |
| 172 | canvas->pitch=(canvas->width+3) & ~3; |
| 173 | canvas->rows=image->rows; |
| 174 | canvas->buffer=(unsigned char *) AcquireQuantumMemory(canvas->pitch, |
| 175 | canvas->rows*sizeof(*canvas->buffer)); |
| 176 | if (canvas->buffer == (unsigned char *) NULL) |
| 177 | { |
| 178 | canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); |
| 179 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 180 | } |
| 181 | canvas->num_grays=256; |
| 182 | canvas->pixel_mode=ft_pixel_mode_grays; |
| 183 | ResetMagickMemory(canvas->buffer,0x00,canvas->pitch*canvas->rows); |
| 184 | /* |
| 185 | Render caption. |
| 186 | */ |
| 187 | layout=pango_layout_new(context); |
| 188 | description=pango_font_description_from_string("Arial,20"); |
| 189 | pango_layout_set_font_description(layout,description); |
| 190 | pango_font_description_free(description); |
| 191 | property=InterpretImageProperties(image_info,image,image_info->filename, |
| 192 | exception); |
| 193 | (void) SetImageProperty(image,"caption",property,exception); |
| 194 | property=DestroyString(property); |
| 195 | caption=ConstantString(GetImageProperty(image,"caption",exception)); |
| 196 | pango_layout_set_text(layout,caption,-1); |
| 197 | pango_layout_context_changed(layout); |
| 198 | /* wrapping: pango_layout_set_width(layout,72*image->columns); */ |
| 199 | pango_ft2_render_layout(canvas,layout,0,0); |
| 200 | /* |
| 201 | Convert caption to image. |
| 202 | */ |
| 203 | draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); |
| 204 | GetPixelInfo(image,&fill_color); |
| 205 | p=canvas->buffer; |
| 206 | for (y=0; y < (ssize_t) image->rows; y++) |
| 207 | { |
| 208 | register ssize_t |
| 209 | x; |
| 210 | |
| 211 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 212 | if (q == (Quantum *) NULL) |
| 213 | break; |
| 214 | for (x=0; x < (ssize_t) image->columns; x++) |
| 215 | { |
| 216 | MagickRealType |
| 217 | fill_opacity; |
| 218 | |
| 219 | (void) GetFillColor(draw_info,x,y,&fill_color,exception); |
| 220 | fill_opacity=(*p)*fill_color.alpha/canvas->num_grays; |
cristy | 7949782 | 2011-10-28 14:51:23 +0000 | [diff] [blame^] | 221 | if (draw_info->text_antialias == MagickFalse) |
| 222 | fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0; |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 223 | CompositePixelOver(image,&fill_color,fill_opacity,q, |
| 224 | GetPixelAlpha(image,q),q); |
| 225 | p++; |
| 226 | q+=GetPixelChannels(image); |
| 227 | } |
| 228 | } |
| 229 | /* |
| 230 | Relinquish resources. |
| 231 | */ |
| 232 | draw_info=DestroyDrawInfo(draw_info); |
| 233 | canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer); |
| 234 | canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); |
| 235 | caption=DestroyString(caption); |
| 236 | return(GetFirstImageInList(image)); |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 237 | } |
| 238 | #endif |
| 239 | |
| 240 | /* |
| 241 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 242 | % % |
| 243 | % % |
| 244 | % % |
| 245 | % R e g i s t e r P A N G O I m a g e % |
| 246 | % % |
| 247 | % % |
| 248 | % % |
| 249 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 250 | % |
| 251 | % RegisterPANGOImage() adds attributes for the Pango Markup Language format to |
| 252 | % the list of supported formats. The attributes include the image format |
| 253 | % tag, a method to read and/or write the format, whether the format |
| 254 | % supports the saving of more than one frame to the same file or blob, |
| 255 | % whether the format supports native in-memory I/O, and a brief |
| 256 | % description of the format. |
| 257 | % |
| 258 | % The format of the RegisterPANGOImage method is: |
| 259 | % |
| 260 | % size_t RegisterPANGOImage(void) |
| 261 | % |
| 262 | */ |
| 263 | ModuleExport size_t RegisterPANGOImage(void) |
| 264 | { |
| 265 | MagickInfo |
| 266 | *entry; |
| 267 | |
| 268 | entry=SetMagickInfo("PANGO"); |
cristy | 884cbde | 2011-10-28 14:42:14 +0000 | [diff] [blame] | 269 | #if defined(MAGICKCORE_PANGOFT2_DELEGATE) |
cristy | bd4fb6e | 2011-10-26 13:10:01 +0000 | [diff] [blame] | 270 | entry->decoder=(DecodeImageHandler *) ReadPANGOImage; |
| 271 | #endif |
| 272 | entry->description=ConstantString("Pango Markup Language"); |
| 273 | entry->adjoin=MagickFalse; |
| 274 | entry->module=ConstantString("PANGO"); |
| 275 | (void) RegisterMagickInfo(entry); |
| 276 | return(MagickImageCoderSignature); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 281 | % % |
| 282 | % % |
| 283 | % % |
| 284 | % U n r e g i s t e r P A N G O I m a g e % |
| 285 | % % |
| 286 | % % |
| 287 | % % |
| 288 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 289 | % |
| 290 | % UnregisterPANGOImage() removes format registrations made by the Pango module |
| 291 | % from the list of supported formats. |
| 292 | % |
| 293 | % The format of the UnregisterPANGOImage method is: |
| 294 | % |
| 295 | % UnregisterPANGOImage(void) |
| 296 | % |
| 297 | */ |
| 298 | ModuleExport void UnregisterPANGOImage(void) |
| 299 | { |
| 300 | (void) UnregisterMagickInfo("PANGO"); |
| 301 | } |