Fix severe security issues
[external/pango1.0.git] / pango / pango-engine.c
1 /* Pango
2  * pango-engine.c: Engines for script and language specific processing
3  *
4  * Copyright (C) 2003 Red Hat Software
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include "pango-engine.h"
25 #include "pango-engine-private.h"
26 #include "pango-impl-utils.h"
27
28
29 G_DEFINE_ABSTRACT_TYPE (PangoEngine, pango_engine, G_TYPE_OBJECT);
30
31 static void
32 pango_engine_init (PangoEngine *self)
33 {
34 }
35
36 static void
37 pango_engine_class_init (PangoEngineClass *klass)
38 {
39 }
40
41
42 G_DEFINE_ABSTRACT_TYPE (PangoEngineLang, pango_engine_lang, PANGO_TYPE_ENGINE);
43
44 static void
45 pango_engine_lang_init (PangoEngineLang *self)
46 {
47 }
48
49 static void
50 pango_engine_lang_class_init (PangoEngineLangClass *klass)
51 {
52 }
53
54
55 static PangoCoverageLevel
56 pango_engine_shape_real_covers (PangoEngineShape *engine G_GNUC_UNUSED,
57                                 PangoFont        *font,
58                                 PangoLanguage    *language,
59                                 gunichar          wc)
60 {
61
62   PangoCoverage *coverage = pango_font_get_coverage (font, language);
63   PangoCoverageLevel result = pango_coverage_get (coverage, wc);
64
65   pango_coverage_unref (coverage);
66
67   return result;
68 }
69
70
71 G_DEFINE_ABSTRACT_TYPE (PangoEngineShape, pango_engine_shape, PANGO_TYPE_ENGINE);
72
73 static void
74 pango_engine_shape_init (PangoEngineShape *klass)
75 {
76 }
77
78 static void
79 pango_engine_shape_class_init (PangoEngineShapeClass *class)
80 {
81   class->covers = pango_engine_shape_real_covers;
82 }
83
84 void
85 _pango_engine_shape_shape (PangoEngineShape *engine,
86                            PangoFont        *font,
87                            const char       *text,
88                            int               length,
89                            const PangoAnalysis *analysis,
90                            PangoGlyphString *glyphs)
91 {
92   glyphs->num_glyphs = 0;
93
94   PANGO_ENGINE_SHAPE_GET_CLASS (engine)->script_shape (engine,
95                                                        font,
96                                                        text, length,
97                                                        analysis,
98                                                        glyphs);
99 }
100
101 PangoCoverageLevel
102 _pango_engine_shape_covers (PangoEngineShape *engine,
103                             PangoFont        *font,
104                             PangoLanguage    *language,
105                             gunichar          wc)
106 {
107   g_return_val_if_fail (PANGO_IS_ENGINE_SHAPE (engine), PANGO_COVERAGE_NONE);
108   g_return_val_if_fail (PANGO_IS_FONT (font), PANGO_COVERAGE_NONE);
109
110   return PANGO_ENGINE_SHAPE_GET_CLASS (engine)->covers (engine,
111                                                         font,
112                                                         language,
113                                                         wc);
114 }
115
116 /* No extra fields needed */
117 typedef PangoEngineShape PangoFallbackEngine;
118 typedef PangoEngineShapeClass PangoFallbackEngineClass;
119
120 static void
121 fallback_engine_shape (PangoEngineShape *engine G_GNUC_UNUSED,
122                        PangoFont        *font G_GNUC_UNUSED,
123                        const char       *text,
124                        gint              length,
125                        const PangoAnalysis *analysis,
126                        PangoGlyphString *glyphs)
127 {
128   int n_chars;
129   const char *p;
130   int cluster = 0;
131   int i;
132
133   n_chars = text ? pango_utf8_strlen (text, length) : 0;
134
135   pango_glyph_string_set_size (glyphs, n_chars);
136
137   p = text;
138   for (i = 0; i < n_chars; i++)
139     {
140       gunichar wc;
141       PangoGlyph glyph;
142       PangoRectangle logical_rect;
143
144       wc = g_utf8_get_char (p);
145
146       if (g_unichar_type (wc) != G_UNICODE_NON_SPACING_MARK)
147         cluster = p - text;
148
149       if (pango_is_zero_width (wc))
150         glyph = PANGO_GLYPH_EMPTY;
151       else
152         glyph = PANGO_GET_UNKNOWN_GLYPH (wc);
153
154       pango_font_get_glyph_extents (analysis->font, glyph, NULL, &logical_rect);
155
156       glyphs->glyphs[i].glyph = glyph;
157
158       glyphs->glyphs[i].geometry.x_offset = 0;
159       glyphs->glyphs[i].geometry.y_offset = 0;
160       glyphs->glyphs[i].geometry.width = logical_rect.width;
161
162       glyphs->log_clusters[i] = cluster;
163
164       p = g_utf8_next_char (p);
165     }
166
167   if (analysis->level & 1)
168     pango_glyph_string_reverse_range (glyphs, 0, glyphs->num_glyphs);
169 }
170
171 static PangoCoverageLevel
172 fallback_engine_covers (PangoEngineShape *engine G_GNUC_UNUSED,
173                         PangoFont        *font G_GNUC_UNUSED,
174                         PangoLanguage    *lang G_GNUC_UNUSED,
175                         gunichar          wc G_GNUC_UNUSED)
176 {
177   return PANGO_COVERAGE_NONE;
178 }
179
180
181 static GType pango_fallback_engine_get_type (void);
182
183 G_DEFINE_TYPE (PangoFallbackEngine, pango_fallback_engine, PANGO_TYPE_ENGINE_SHAPE);
184
185 static void
186 pango_fallback_engine_init (PangoFallbackEngine *self)
187 {
188 }
189
190 static void
191 pango_fallback_engine_class_init (PangoFallbackEngineClass *class)
192 {
193   class->covers = fallback_engine_covers;
194   class->script_shape = fallback_engine_shape;
195 }
196
197 PangoEngineShape *
198 _pango_get_fallback_shaper (void)
199 {
200   static PangoEngineShape *fallback_shaper = NULL;
201   if (!fallback_shaper)
202     fallback_shaper = g_object_new (pango_fallback_engine_get_type (), NULL);
203
204   return fallback_shaper;
205 }
206