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