blob: fba833940595c050047352d7ab2bedb310662d8c [file] [log] [blame]
cristybd4fb6e2011-10-26 13:10:01 +00001/*
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"
cristy884cbde2011-10-28 14:42:14 +000043#include "MagickCore/annotate.h"
cristybd4fb6e2011-10-26 13:10:01 +000044#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
cristy884cbde2011-10-28 14:42:14 +000046#include "MagickCore/composite-private.h"
47#include "MagickCore/draw.h"
48#include "MagickCore/draw-private.h"
cristybd4fb6e2011-10-26 13:10:01 +000049#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"
cristy884cbde2011-10-28 14:42:14 +000055#include "MagickCore/module.h"
cristybd4fb6e2011-10-26 13:10:01 +000056#include "MagickCore/memory_.h"
57#include "MagickCore/option.h"
58#include "MagickCore/pixel-accessor.h"
cristy884cbde2011-10-28 14:42:14 +000059#include "MagickCore/property.h"
cristybd4fb6e2011-10-26 13:10:01 +000060#include "MagickCore/quantum-private.h"
61#include "MagickCore/static.h"
62#include "MagickCore/string_.h"
cristy884cbde2011-10-28 14:42:14 +000063#if defined(MAGICKCORE_PANGOFT2_DELEGATE)
cristybd4fb6e2011-10-26 13:10:01 +000064#include <pango/pango.h>
cristy884cbde2011-10-28 14:42:14 +000065#include <pango/pangoft2.h>
cristybd4fb6e2011-10-26 13:10:01 +000066#endif
67
cristy884cbde2011-10-28 14:42:14 +000068#if defined(MAGICKCORE_PANGOFT2_DELEGATE)
cristybd4fb6e2011-10-26 13:10:01 +000069/*
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71% %
72% %
73% %
cristy6c28ec02011-10-28 11:37:57 +000074% R e a d P A N G O I m a g e %
cristybd4fb6e2011-10-26 13:10:01 +000075% %
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*/
94static Image *ReadPANGOImage(const ImageInfo *image_info,
95 ExceptionInfo *exception)
96{
cristy884cbde2011-10-28 14:42:14 +000097 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;
221 CompositePixelOver(image,&fill_color,fill_opacity,q,
222 GetPixelAlpha(image,q),q);
223 p++;
224 q+=GetPixelChannels(image);
225 }
226 }
227 /*
228 Relinquish resources.
229 */
230 draw_info=DestroyDrawInfo(draw_info);
231 canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer);
232 canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas);
233 caption=DestroyString(caption);
234 return(GetFirstImageInList(image));
cristybd4fb6e2011-10-26 13:10:01 +0000235}
236#endif
237
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
243% R e g i s t e r P A N G O I m a g e %
244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
249% RegisterPANGOImage() adds attributes for the Pango Markup Language format to
250% the list of supported formats. The attributes include the image format
251% tag, a method to read and/or write the format, whether the format
252% supports the saving of more than one frame to the same file or blob,
253% whether the format supports native in-memory I/O, and a brief
254% description of the format.
255%
256% The format of the RegisterPANGOImage method is:
257%
258% size_t RegisterPANGOImage(void)
259%
260*/
261ModuleExport size_t RegisterPANGOImage(void)
262{
263 MagickInfo
264 *entry;
265
266 entry=SetMagickInfo("PANGO");
cristy884cbde2011-10-28 14:42:14 +0000267#if defined(MAGICKCORE_PANGOFT2_DELEGATE)
cristybd4fb6e2011-10-26 13:10:01 +0000268 entry->decoder=(DecodeImageHandler *) ReadPANGOImage;
269#endif
270 entry->description=ConstantString("Pango Markup Language");
271 entry->adjoin=MagickFalse;
272 entry->module=ConstantString("PANGO");
273 (void) RegisterMagickInfo(entry);
274 return(MagickImageCoderSignature);
275}
276
277/*
278%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279% %
280% %
281% %
282% U n r e g i s t e r P A N G O I m a g e %
283% %
284% %
285% %
286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287%
288% UnregisterPANGOImage() removes format registrations made by the Pango module
289% from the list of supported formats.
290%
291% The format of the UnregisterPANGOImage method is:
292%
293% UnregisterPANGOImage(void)
294%
295*/
296ModuleExport void UnregisterPANGOImage(void)
297{
298 (void) UnregisterMagickInfo("PANGO");
299}