1 /*
2 * Inkscape::Text::Layout - text layout engine input functions
3 *
4 * Authors:
5 * Richard Hughes <cyreve@users.sf.net>
6 *
7 * Copyright (C) 2005 Richard Hughes
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #define PANGO_ENABLE_ENGINE
14 #include <gtk/gtkversion.h>
15 #include "Layout-TNG.h"
16 #include "style.h"
17 #include "svg/svg-length.h"
18 #include "sp-object.h"
19 #include "FontFactory.h"
21 namespace Inkscape {
22 namespace Text {
24 void Layout::_clearInputObjects()
25 {
26 for(std::vector<InputStreamItem*>::iterator it = _input_stream.begin() ; it != _input_stream.end() ; it++)
27 delete *it;
28 _input_stream.clear();
29 _input_wrap_shapes.clear();
30 }
32 // this function does nothing more than store all its parameters for future reference
33 void Layout::appendText(Glib::ustring const &text, SPStyle *style, void *source_cookie, OptionalTextTagAttrs const *optional_attributes, unsigned optional_attributes_offset, Glib::ustring::const_iterator text_begin, Glib::ustring::const_iterator text_end)
34 {
35 if (style == NULL) return;
37 InputStreamTextSource *new_source = new InputStreamTextSource;
39 new_source->source_cookie = source_cookie;
40 new_source->text = &text;
41 new_source->text_begin = text_begin;
42 new_source->text_end = text_end;
43 new_source->style = style;
44 sp_style_ref(style);
46 new_source->text_length = 0;
47 for ( ; text_begin != text_end && text_begin != text.end() ; text_begin++)
48 new_source->text_length++; // save this because calculating the length of a UTF-8 string is expensive
50 if (optional_attributes) {
51 // we need to fill in x and y even if the text is empty so that empty paragraphs can be positioned correctly
52 _copyInputVector(optional_attributes->x, optional_attributes_offset, &new_source->x, std::max(1, new_source->text_length));
53 _copyInputVector(optional_attributes->y, optional_attributes_offset, &new_source->y, std::max(1, new_source->text_length));
54 _copyInputVector(optional_attributes->dx, optional_attributes_offset, &new_source->dx, new_source->text_length);
55 _copyInputVector(optional_attributes->dy, optional_attributes_offset, &new_source->dy, new_source->text_length);
56 _copyInputVector(optional_attributes->rotate, optional_attributes_offset, &new_source->rotate, new_source->text_length);
57 if (!optional_attributes->rotate.empty() && optional_attributes_offset >= optional_attributes->rotate.size()) {
58 SVGLength last_rotate;
59 last_rotate = 0.f;
60 for (std::vector<SVGLength>::const_iterator it = optional_attributes->rotate.begin() ; it != optional_attributes->rotate.end() ; ++it)
61 if (it->_set)
62 last_rotate = *it;
63 new_source->rotate.resize(1, last_rotate);
64 }
65 }
67 _input_stream.push_back(new_source);
68 }
70 void Layout::_copyInputVector(std::vector<SVGLength> const &input_vector, unsigned input_offset, std::vector<SVGLength> *output_vector, size_t max_length)
71 {
72 output_vector->clear();
73 if (input_offset >= input_vector.size()) return;
74 output_vector->reserve(std::min(max_length, input_vector.size() - input_offset));
75 while (input_offset < input_vector.size() && max_length != 0) {
76 if (!input_vector[input_offset]._set)
77 break;
78 output_vector->push_back(input_vector[input_offset]);
79 input_offset++;
80 max_length--;
81 }
82 }
84 // just save what we've been given, really
85 void Layout::appendControlCode(TextControlCode code, void *source_cookie, double width, double ascent, double descent)
86 {
87 InputStreamControlCode *new_code = new InputStreamControlCode;
89 new_code->source_cookie = source_cookie;
90 new_code->code = code;
91 new_code->width = width;
92 new_code->ascent = ascent;
93 new_code->descent = descent;
95 _input_stream.push_back(new_code);
96 }
98 // more saving of the parameters
99 void Layout::appendWrapShape(Shape const *shape, DisplayAlign display_align)
100 {
101 _input_wrap_shapes.push_back(InputWrapShape());
102 _input_wrap_shapes.back().shape = shape;
103 _input_wrap_shapes.back().display_align = display_align;
104 }
106 int Layout::_enum_converter(int input, EnumConversionItem const *conversion_table, unsigned conversion_table_size)
107 {
108 for (unsigned i = 0 ; i < conversion_table_size ; i++)
109 if (conversion_table[i].input == input)
110 return conversion_table[i].output;
111 return conversion_table[0].output;
112 }
114 // ***** the style format interface
115 // this doesn't include all accesses to SPStyle, only the ones that are non-trivial
117 static const float medium_font_size = 12.0; // more of a default if all else fails than anything else
118 float Layout::InputStreamTextSource::styleComputeFontSize() const
119 {
120 return style->font_size.computed;
122 // in case the computed value's not good enough, here's some manual code held in reserve:
123 SPStyle const *this_style = style;
124 float inherit_multiplier = 1.0;
126 for ( ; ; ) {
127 if (this_style->font_size.set && !this_style->font_size.inherit) {
128 switch (this_style->font_size.type) {
129 case SP_FONT_SIZE_LITERAL: {
130 switch(this_style->font_size.value) { // these multipliers are straight out of the CSS spec
131 case SP_CSS_FONT_SIZE_XX_SMALL: return medium_font_size * inherit_multiplier * (3.0/5.0);
132 case SP_CSS_FONT_SIZE_X_SMALL: return medium_font_size * inherit_multiplier * (3.0/4.0);
133 case SP_CSS_FONT_SIZE_SMALL: return medium_font_size * inherit_multiplier * (8.0/9.0);
134 default:
135 case SP_CSS_FONT_SIZE_MEDIUM: return medium_font_size * inherit_multiplier;
136 case SP_CSS_FONT_SIZE_LARGE: return medium_font_size * inherit_multiplier * (6.0/5.0);
137 case SP_CSS_FONT_SIZE_X_LARGE: return medium_font_size * inherit_multiplier * (3.0/2.0);
138 case SP_CSS_FONT_SIZE_XX_LARGE: return medium_font_size * inherit_multiplier * 2.0;
139 case SP_CSS_FONT_SIZE_SMALLER: inherit_multiplier *= 0.84; break; //not exactly according to spec
140 case SP_CSS_FONT_SIZE_LARGER: inherit_multiplier *= 1.26; break; //not exactly according to spec
141 }
142 break;
143 }
144 case SP_FONT_SIZE_PERCENTAGE: { // 'em' units should be in here, but aren't. Fix in style.cpp.
145 inherit_multiplier *= this_style->font_size.value;
146 break;
147 }
148 case SP_FONT_SIZE_LENGTH: {
149 return this_style->font_size.value * inherit_multiplier;
150 }
151 }
152 }
153 if (this_style->object == NULL || this_style->object->parent == NULL) break;
154 this_style = this_style->object->parent->style;
155 if (this_style == NULL) break;
156 }
157 return medium_font_size * inherit_multiplier;
158 }
160 static const Layout::EnumConversionItem enum_convert_spstyle_block_progression_to_direction[] = {
161 {SP_CSS_BLOCK_PROGRESSION_TB, Layout::TOP_TO_BOTTOM},
162 {SP_CSS_BLOCK_PROGRESSION_LR, Layout::LEFT_TO_RIGHT},
163 {SP_CSS_BLOCK_PROGRESSION_RL, Layout::RIGHT_TO_LEFT}};
165 static const Layout::EnumConversionItem enum_convert_spstyle_writing_mode_to_direction[] = {
166 {SP_CSS_WRITING_MODE_LR_TB, Layout::TOP_TO_BOTTOM},
167 {SP_CSS_WRITING_MODE_RL_TB, Layout::TOP_TO_BOTTOM},
168 {SP_CSS_WRITING_MODE_TB_RL, Layout::RIGHT_TO_LEFT},
169 {SP_CSS_WRITING_MODE_TB_LR, Layout::LEFT_TO_RIGHT}};
171 Layout::Direction Layout::InputStreamTextSource::styleGetBlockProgression() const
172 {
173 // this function shouldn't be necessary, but since style.cpp doesn't support
174 // shorthand properties yet, it is.
175 SPStyle const *this_style = style;
177 for ( ; ; ) {
178 if (this_style->block_progression.set)
179 return (Layout::Direction)_enum_converter(this_style->block_progression.computed, enum_convert_spstyle_block_progression_to_direction, sizeof(enum_convert_spstyle_block_progression_to_direction)/sizeof(enum_convert_spstyle_block_progression_to_direction[0]));
180 if (this_style->writing_mode.set)
181 return (Layout::Direction)_enum_converter(this_style->writing_mode.computed, enum_convert_spstyle_writing_mode_to_direction, sizeof(enum_convert_spstyle_writing_mode_to_direction)/sizeof(enum_convert_spstyle_writing_mode_to_direction[0]));
182 if (this_style->object == NULL || this_style->object->parent == NULL) break;
183 this_style = this_style->object->parent->style;
184 if (this_style == NULL) break;
185 }
186 return TOP_TO_BOTTOM;
188 }
190 static Layout::Alignment text_anchor_to_alignment(unsigned anchor, Layout::Direction /*para_direction*/)
191 {
192 switch (anchor) {
193 default:
194 case SP_CSS_TEXT_ANCHOR_START: return Layout::LEFT;
195 case SP_CSS_TEXT_ANCHOR_MIDDLE: return Layout::CENTER;
196 case SP_CSS_TEXT_ANCHOR_END: return Layout::RIGHT;
197 }
198 }
200 Layout::Alignment Layout::InputStreamTextSource::styleGetAlignment(Layout::Direction para_direction, bool try_text_align) const
201 {
202 if (!try_text_align)
203 return text_anchor_to_alignment(style->text_anchor.computed, para_direction);
205 // there's no way to tell the difference between text-anchor set higher up the cascade to the default and
206 // text-anchor never set anywhere in the cascade, so in order to detect which of text-anchor or text-align
207 // to use we'll have to run up the style tree ourselves.
208 SPStyle const *this_style = style;
210 for ( ; ; ) {
211 // If both text-align and text-anchor are set at the same level, text-align takes
212 // precedence because it is the most expressive.
213 if (this_style->text_align.set) {
214 switch (style->text_align.computed) {
215 default:
216 case SP_CSS_TEXT_ALIGN_START: return para_direction == LEFT_TO_RIGHT ? LEFT : RIGHT;
217 case SP_CSS_TEXT_ALIGN_END: return para_direction == LEFT_TO_RIGHT ? RIGHT : LEFT;
218 case SP_CSS_TEXT_ALIGN_LEFT: return LEFT;
219 case SP_CSS_TEXT_ALIGN_RIGHT: return RIGHT;
220 case SP_CSS_TEXT_ALIGN_CENTER: return CENTER;
221 case SP_CSS_TEXT_ALIGN_JUSTIFY: return FULL;
222 }
223 }
224 if (this_style->text_anchor.set)
225 return text_anchor_to_alignment(this_style->text_anchor.computed, para_direction);
226 if (this_style->object == NULL || this_style->object->parent == NULL) break;
227 this_style = this_style->object->parent->style;
228 if (this_style == NULL) break;
229 }
230 return para_direction == LEFT_TO_RIGHT ? LEFT : RIGHT;
231 }
233 static const Layout::EnumConversionItem enum_convert_spstyle_style_to_pango_style[] = {
234 {SP_CSS_FONT_STYLE_NORMAL, PANGO_STYLE_NORMAL},
235 {SP_CSS_FONT_STYLE_ITALIC, PANGO_STYLE_ITALIC},
236 {SP_CSS_FONT_STYLE_OBLIQUE, PANGO_STYLE_OBLIQUE}};
238 static const Layout::EnumConversionItem enum_convert_spstyle_weight_to_pango_weight[] = {
239 {SP_CSS_FONT_WEIGHT_NORMAL, PANGO_WEIGHT_NORMAL},
240 {SP_CSS_FONT_WEIGHT_100, PANGO_WEIGHT_ULTRALIGHT},
241 {SP_CSS_FONT_WEIGHT_200, PANGO_WEIGHT_ULTRALIGHT},
242 {SP_CSS_FONT_WEIGHT_300, PANGO_WEIGHT_LIGHT},
243 {SP_CSS_FONT_WEIGHT_400, PANGO_WEIGHT_NORMAL},
244 #if GTK_CHECK_VERSION(2,6,0)
245 {SP_CSS_FONT_WEIGHT_500, PANGO_WEIGHT_SEMIBOLD},
246 #else
247 {SP_CSS_FONT_WEIGHT_500, PANGO_WEIGHT_NORMAL},
248 #endif
249 {SP_CSS_FONT_WEIGHT_600, PANGO_WEIGHT_BOLD},
250 {SP_CSS_FONT_WEIGHT_BOLD,PANGO_WEIGHT_BOLD},
251 {SP_CSS_FONT_WEIGHT_700, PANGO_WEIGHT_BOLD},
252 {SP_CSS_FONT_WEIGHT_800, PANGO_WEIGHT_ULTRABOLD},
253 {SP_CSS_FONT_WEIGHT_900, PANGO_WEIGHT_HEAVY}};
255 static const Layout::EnumConversionItem enum_convert_spstyle_stretch_to_pango_stretch[] = {
256 {SP_CSS_FONT_STRETCH_NORMAL, PANGO_STRETCH_NORMAL},
257 {SP_CSS_FONT_STRETCH_ULTRA_CONDENSED, PANGO_STRETCH_ULTRA_CONDENSED},
258 {SP_CSS_FONT_STRETCH_EXTRA_CONDENSED, PANGO_STRETCH_EXTRA_CONDENSED},
259 {SP_CSS_FONT_STRETCH_CONDENSED, PANGO_STRETCH_CONDENSED},
260 {SP_CSS_FONT_STRETCH_SEMI_CONDENSED, PANGO_STRETCH_SEMI_CONDENSED},
261 {SP_CSS_FONT_STRETCH_SEMI_EXPANDED, PANGO_STRETCH_SEMI_EXPANDED},
262 {SP_CSS_FONT_STRETCH_EXPANDED, PANGO_STRETCH_EXPANDED},
263 {SP_CSS_FONT_STRETCH_EXTRA_EXPANDED, PANGO_STRETCH_EXTRA_EXPANDED},
264 {SP_CSS_FONT_STRETCH_ULTRA_EXPANDED, PANGO_STRETCH_ULTRA_EXPANDED}};
266 static const Layout::EnumConversionItem enum_convert_spstyle_variant_to_pango_variant[] = {
267 {SP_CSS_FONT_VARIANT_NORMAL, PANGO_VARIANT_NORMAL},
268 {SP_CSS_FONT_VARIANT_SMALL_CAPS, PANGO_VARIANT_SMALL_CAPS}};
270 font_instance *Layout::InputStreamTextSource::styleGetFontInstance() const
271 {
272 PangoFontDescription *descr = styleGetFontDescription();
273 if (descr == NULL) return NULL;
274 font_instance *res = (font_factory::Default())->Face(descr);
275 pango_font_description_free(descr);
276 return res;
277 }
279 PangoFontDescription *Layout::InputStreamTextSource::styleGetFontDescription() const
280 {
281 if (style->text == NULL) return NULL;
282 PangoFontDescription *descr = pango_font_description_new();
283 // Pango can't cope with spaces before or after the commas - let's remove them.
284 // this code is not exactly unicode-safe, but it's similar to what's done in
285 // pango, so it's not the limiting factor
286 Glib::ustring family;
287 if (style->text->font_family.value == NULL) {
288 family = "Sans";
289 } else {
290 gchar **families = g_strsplit(style->text->font_family.value, ",", -1);
291 if (families) {
292 for (gchar **f = families ; *f ; ++f) {
293 g_strstrip(*f);
294 if (!family.empty()) family += ',';
295 family += *f;
296 }
297 }
298 g_strfreev(families);
299 }
301 pango_font_description_set_family(descr,family.c_str());
302 pango_font_description_set_weight(descr,(PangoWeight)_enum_converter(style->font_weight.computed, enum_convert_spstyle_weight_to_pango_weight, sizeof(enum_convert_spstyle_weight_to_pango_weight)/sizeof(enum_convert_spstyle_weight_to_pango_weight[0])));
303 pango_font_description_set_style(descr,(PangoStyle)_enum_converter(style->font_style.computed, enum_convert_spstyle_style_to_pango_style, sizeof(enum_convert_spstyle_style_to_pango_style)/sizeof(enum_convert_spstyle_style_to_pango_style[0])));
304 pango_font_description_set_variant(descr,(PangoVariant)_enum_converter(style->font_variant.computed, enum_convert_spstyle_variant_to_pango_variant, sizeof(enum_convert_spstyle_variant_to_pango_variant)/sizeof(enum_convert_spstyle_variant_to_pango_variant[0])));
305 #ifdef USE_PANGO_WIN32
306 // damn Pango fudges the size, so we need to unfudge. See source of pango_win32_font_map_init()
307 pango_font_description_set_size(descr, (int) ((font_factory::Default())->fontSize*PANGO_SCALE*72/GetDeviceCaps(pango_win32_get_dc(),LOGPIXELSY))); // mandatory huge size (hinting workaround)
308 // we don't set stretch on Win32, because pango-win32 has no concept of it
309 // (Windows doesn't really provide any useful field it could use).
310 // If we did set stretch, then any text with a font-stretch attribute would
311 // end up falling back to Arial.
312 #else
313 pango_font_description_set_size(descr, (int) ((font_factory::Default())->fontSize*PANGO_SCALE)); // mandatory huge size (hinting workaround)
314 pango_font_description_set_stretch(descr,(PangoStretch)_enum_converter(style->font_stretch.computed, enum_convert_spstyle_stretch_to_pango_stretch, sizeof(enum_convert_spstyle_stretch_to_pango_stretch)/sizeof(enum_convert_spstyle_stretch_to_pango_stretch[0])));
315 #endif
316 return descr;
317 }
319 Layout::InputStreamTextSource::~InputStreamTextSource()
320 {
321 sp_style_unref(style);
322 }
324 }//namespace Text
325 }//namespace Inkscape