Code

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