Git init
[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 PANGO_DEFINE_TYPE_ABSTRACT (PangoEngine, pango_engine,
29                             NULL, NULL,
30                             G_TYPE_OBJECT)
31
32 PANGO_DEFINE_TYPE_ABSTRACT (PangoEngineLang, pango_engine_lang,
33                             NULL, NULL,
34                             PANGO_TYPE_ENGINE)
35
36 static PangoCoverageLevel
37 pango_engine_shape_real_covers (PangoEngineShape *engine G_GNUC_UNUSED,
38                                 PangoFont        *font,
39                                 PangoLanguage    *language,
40                                 gunichar          wc)
41 {
42
43   PangoCoverage *coverage = pango_font_get_coverage (font, language);
44   PangoCoverageLevel result = pango_coverage_get (coverage, wc);
45
46   pango_coverage_unref (coverage);
47
48   return result;
49 }
50
51 static void
52 pango_engine_shape_class_init (PangoEngineShapeClass *class)
53 {
54   class->covers = pango_engine_shape_real_covers;
55 }
56
57 PANGO_DEFINE_TYPE_ABSTRACT (PangoEngineShape, pango_engine_shape,
58                             pango_engine_shape_class_init, NULL,
59                             PANGO_TYPE_ENGINE)
60
61 void
62 _pango_engine_shape_shape (PangoEngineShape *engine,
63                            PangoFont        *font,
64                            const char       *text,
65                            int               length,
66                            const PangoAnalysis *analysis,
67                            PangoGlyphString *glyphs)
68 {
69   glyphs->num_glyphs = 0;
70
71   PANGO_ENGINE_SHAPE_GET_CLASS (engine)->script_shape (engine,
72                                                        font,
73                                                        text, length,
74                                                        analysis,
75                                                        glyphs);
76 }
77
78 PangoCoverageLevel
79 _pango_engine_shape_covers (PangoEngineShape *engine,
80                             PangoFont        *font,
81                             PangoLanguage    *language,
82                             gunichar          wc)
83 {
84   g_return_val_if_fail (PANGO_IS_ENGINE_SHAPE (engine), PANGO_COVERAGE_NONE);
85   g_return_val_if_fail (PANGO_IS_FONT (font), PANGO_COVERAGE_NONE);
86
87   return PANGO_ENGINE_SHAPE_GET_CLASS (engine)->covers (engine,
88                                                         font,
89                                                         language,
90                                                         wc);
91 }
92
93 /* No extra fields needed */
94 typedef PangoEngineShape PangoFallbackEngine;
95 typedef PangoEngineShapeClass PangoFallbackEngineClass;
96
97 static void
98 fallback_engine_shape (PangoEngineShape *engine G_GNUC_UNUSED,
99                        PangoFont        *font G_GNUC_UNUSED,
100                        const char       *text,
101                        gint              length,
102                        const PangoAnalysis *analysis,
103                        PangoGlyphString *glyphs)
104 {
105   int n_chars;
106   const char *p;
107   int cluster = 0;
108   int i;
109
110   n_chars = text ? pango_utf8_strlen (text, length) : 0;
111
112   pango_glyph_string_set_size (glyphs, n_chars);
113
114   p = text;
115   for (i = 0; i < n_chars; i++)
116     {
117       gunichar wc;
118       PangoGlyph glyph;
119       PangoRectangle logical_rect;
120
121       wc = g_utf8_get_char (p);
122
123       if (g_unichar_type (wc) != G_UNICODE_NON_SPACING_MARK)
124         cluster = p - text;
125
126       if (pango_is_zero_width (wc))
127         glyph = PANGO_GLYPH_EMPTY;
128       else
129         glyph = PANGO_GET_UNKNOWN_GLYPH (wc);
130
131       pango_font_get_glyph_extents (analysis->font, glyph, NULL, &logical_rect);
132
133       glyphs->glyphs[i].glyph = glyph;
134
135       glyphs->glyphs[i].geometry.x_offset = 0;
136       glyphs->glyphs[i].geometry.y_offset = 0;
137       glyphs->glyphs[i].geometry.width = logical_rect.width;
138
139       glyphs->log_clusters[i] = cluster;
140
141       p = g_utf8_next_char (p);
142     }
143
144   if (analysis->level & 1)
145     pango_glyph_string_reverse_range (glyphs, 0, glyphs->num_glyphs);
146 }
147
148 static PangoCoverageLevel
149 fallback_engine_covers (PangoEngineShape *engine G_GNUC_UNUSED,
150                         PangoFont        *font G_GNUC_UNUSED,
151                         PangoLanguage    *lang G_GNUC_UNUSED,
152                         gunichar          wc G_GNUC_UNUSED)
153 {
154   return PANGO_COVERAGE_NONE;
155 }
156
157 static void
158 fallback_engine_class_init (PangoEngineShapeClass *class)
159 {
160   class->covers = fallback_engine_covers;
161   class->script_shape = fallback_engine_shape;
162 }
163
164 static PANGO_DEFINE_TYPE (PangoFallbackEngine, pango_fallback_engine,
165                           fallback_engine_class_init, NULL,
166                           PANGO_TYPE_ENGINE_SHAPE)
167
168 PangoEngineShape *
169 _pango_get_fallback_shaper (void)
170 {
171   static PangoEngineShape *fallback_shaper = NULL;
172   if (!fallback_shaper)
173     fallback_shaper = g_object_new (pango_fallback_engine_get_type (), NULL);
174
175   return fallback_shaper;
176 }
177