Code

- try to use more forward declarations for less dependencies on display/curve.h
[inkscape.git] / src / libnrtype / Layout-TNG-Output.cpp
1 /*
2  * Inkscape::Text::Layout - text layout engine output 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  */
11 #include <glib/gmem.h>
12 #include "Layout-TNG.h"
13 #include "display/nr-arena-glyphs.h"
14 #include "style.h"
15 #include "print.h"
16 #include "extension/print.h"
17 #include "livarot/Path.h"
18 #include "libnr/nr-matrix-fns.h"
19 #include "libnr/nr-scale-matrix-ops.h"
20 #include "font-instance.h"
21 #include "svg/svg-length.h"
22 #include "extension/internal/cairo-render-context.h"
23 #include "display/curve.h"
25 namespace Inkscape {
26     namespace Extension {
27         namespace Internal {
28             class CairoRenderContext;
29             class CairoGlyphInfo;
30         }
31     }
32 }
34 using Inkscape::Extension::Internal::CairoRenderContext;
35 using Inkscape::Extension::Internal::CairoGlyphInfo;
37 namespace Inkscape {
38 namespace Text {
40 void Layout::_clearOutputObjects()
41 {
42     _paragraphs.clear();
43     _lines.clear();
44     _chunks.clear();
45     for (std::vector<Span>::iterator it_span = _spans.begin() ; it_span != _spans.end() ; it_span++)
46         if (it_span->font) it_span->font->Unref();
47     _spans.clear();
48     _characters.clear();
49     _glyphs.clear();
50     _path_fitted = NULL;
51 }
53 void Layout::LineHeight::max(LineHeight const &other)
54 {
55     if (other.ascent > ascent)  ascent  = other.ascent;
56     if (other.descent > descent) descent = other.descent;
57     if (other.leading > leading) leading = other.leading;
58 }
60 void Layout::_getGlyphTransformMatrix(int glyph_index, NR::Matrix *matrix) const
61 {
62     Span const &span = _glyphs[glyph_index].span(this);
63     double sin_rotation = sin(_glyphs[glyph_index].rotation);
64     double cos_rotation = cos(_glyphs[glyph_index].rotation);
65     (*matrix)[0] = span.font_size * cos_rotation;
66     (*matrix)[1] = span.font_size * sin_rotation;
67     (*matrix)[2] = span.font_size * sin_rotation;
68     (*matrix)[3] = -span.font_size * cos_rotation;
69     if (span.block_progression == LEFT_TO_RIGHT || span.block_progression == RIGHT_TO_LEFT) {
70         (*matrix)[4] = _lines[_chunks[span.in_chunk].in_line].baseline_y + _glyphs[glyph_index].y;
71         (*matrix)[5] = _chunks[span.in_chunk].left_x + _glyphs[glyph_index].x;
72     } else {
73         (*matrix)[4] = _chunks[span.in_chunk].left_x + _glyphs[glyph_index].x;
74         (*matrix)[5] = _lines[_chunks[span.in_chunk].in_line].baseline_y + _glyphs[glyph_index].y;
75     }
76 }
78 void Layout::show(NRArenaGroup *in_arena, NRRect const *paintbox) const
79 {
80     int glyph_index = 0;
81     for (unsigned span_index = 0 ; span_index < _spans.size() ; span_index++) {
82         if (_input_stream[_spans[span_index].in_input_stream_item]->Type() != TEXT_SOURCE) continue;
83         InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_input_stream[_spans[span_index].in_input_stream_item]);
84         NRArenaGlyphsGroup *nr_group = NRArenaGlyphsGroup::create(in_arena->arena);
85         nr_arena_item_add_child(in_arena, nr_group, NULL);
86         nr_arena_item_unref(nr_group);
88         nr_arena_glyphs_group_set_style(nr_group, text_source->style);
89         while (glyph_index < (int)_glyphs.size() && _characters[_glyphs[glyph_index].in_character].in_span == span_index) {
90             if (_characters[_glyphs[glyph_index].in_character].in_glyph != -1) {
91                 NR::Matrix glyph_matrix;
92                 _getGlyphTransformMatrix(glyph_index, &glyph_matrix);
93                 nr_arena_glyphs_group_add_component(nr_group, _spans[span_index].font, _glyphs[glyph_index].glyph, &glyph_matrix);
94             }
95             glyph_index++;
96         }
97         nr_arena_glyphs_group_set_paintbox(NR_ARENA_GLYPHS_GROUP(nr_group), paintbox);
98     }
99     nr_arena_item_request_update(NR_ARENA_ITEM(in_arena), NR_ARENA_ITEM_STATE_ALL, FALSE);
102 void Layout::getBoundingBox(NRRect *bounding_box, NR::Matrix const &transform, int start, int length) const
104     for (unsigned glyph_index = 0 ; glyph_index < _glyphs.size() ; glyph_index++) {
105         if (_characters[_glyphs[glyph_index].in_character].in_glyph == -1) continue;
106         if (start != -1 && (int) _glyphs[glyph_index].in_character < start) continue;
107         if (length != -1) {
108             if (start == -1)
109                 start = 0;
110             if ((int) _glyphs[glyph_index].in_character > start + length) continue;
111         }
112         // this could be faster
113         NR::Matrix glyph_matrix;
114         _getGlyphTransformMatrix(glyph_index, &glyph_matrix);
115         NR::Matrix total_transform = glyph_matrix;
116         total_transform *= transform;
117         NR::Maybe<NR::Rect> glyph_rect = _glyphs[glyph_index].span(this).font->BBox(_glyphs[glyph_index].glyph);
118         if (glyph_rect) {
119             NR::Point bmi = glyph_rect->min(), bma = glyph_rect->max();
120             NR::Point tlp(bmi[0],bmi[1]), trp(bma[0],bmi[1]), blp(bmi[0],bma[1]), brp(bma[0],bma[1]);
121             tlp *= total_transform;
122             trp *= total_transform;
123             blp *= total_transform;
124             brp *= total_transform;
125             *glyph_rect = NR::Rect(tlp,trp);
126             glyph_rect->expandTo(blp);
127             glyph_rect->expandTo(brp);
128             if ( (glyph_rect->min())[0] < bounding_box->x0 ) bounding_box->x0=(glyph_rect->min())[0];
129             if ( (glyph_rect->max())[0] > bounding_box->x1 ) bounding_box->x1=(glyph_rect->max())[0];
130             if ( (glyph_rect->min())[1] < bounding_box->y0 ) bounding_box->y0=(glyph_rect->min())[1];
131             if ( (glyph_rect->max())[1] > bounding_box->y1 ) bounding_box->y1=(glyph_rect->max())[1];
132         }
133     }
136 void Layout::print(SPPrintContext *ctx,
137                    NRRect const *pbox, NRRect const *dbox, NRRect const *bbox,
138                    NR::Matrix const &ctm) const
140     if (_input_stream.empty()) return;
142     Direction block_progression = _blockProgression();
143     bool text_to_path = ctx->module->textToPath();
144     for (unsigned glyph_index = 0 ; glyph_index < _glyphs.size() ; ) {
145         if (_characters[_glyphs[glyph_index].in_character].in_glyph == -1) {
146             // invisible glyphs
147             unsigned same_character = _glyphs[glyph_index].in_character;
148             while (_glyphs[glyph_index].in_character == same_character)
149                 glyph_index++;
150             continue;
151         }
152         NR::Matrix glyph_matrix;
153         Span const &span = _spans[_characters[_glyphs[glyph_index].in_character].in_span];
154         InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_input_stream[span.in_input_stream_item]);
155         if (text_to_path || _path_fitted) {
156             NRBPath bpath;
157             bpath.path = (NArtBpath*)span.font->ArtBPath(_glyphs[glyph_index].glyph);
158             if (bpath.path) {
159                 NRBPath abp;
160                 _getGlyphTransformMatrix(glyph_index, &glyph_matrix);
161                 abp.path = nr_artpath_affine(bpath.path, glyph_matrix);
162                 if (!text_source->style->fill.isNone())
163                     sp_print_fill(ctx, &abp, &ctm, text_source->style, pbox, dbox, bbox);
164                 if (!text_source->style->stroke.isNone())
165                     sp_print_stroke(ctx, &abp, &ctm, text_source->style, pbox, dbox, bbox);
166                 g_free(abp.path);
167             }
168             glyph_index++;
169         } else {
170             NR::Point g_pos(0,0);    // all strings are output at (0,0) because we do the translation using the matrix
171             glyph_matrix = NR::Matrix(NR::scale(1.0, -1.0) * NR::Matrix(NR::rotate(_glyphs[glyph_index].rotation)));
172             if (block_progression == LEFT_TO_RIGHT || block_progression == RIGHT_TO_LEFT) {
173                 glyph_matrix[4] = span.line(this).baseline_y + span.baseline_shift;
174                 // since we're outputting character codes, not glyphs, we want the character x
175                 glyph_matrix[5] = span.chunk(this).left_x + span.x_start + _characters[_glyphs[glyph_index].in_character].x;
176             } else {
177                 glyph_matrix[4] = span.chunk(this).left_x + span.x_start + _characters[_glyphs[glyph_index].in_character].x;
178                 glyph_matrix[5] = span.line(this).baseline_y + span.baseline_shift;
179             }
180             Glib::ustring::const_iterator span_iter = span.input_stream_first_character;
181             unsigned char_index = _glyphs[glyph_index].in_character;
182             unsigned original_span = _characters[char_index].in_span;
183             while (char_index && _characters[char_index - 1].in_span == original_span) {
184                 char_index--;
185                 span_iter++;
186             }
188             // try to output as many characters as possible in one go by detecting kerning and stopping when we encounter it
189             Glib::ustring span_string;
190             double char_x = _characters[_glyphs[glyph_index].in_character].x;
191             unsigned this_span_index = _characters[_glyphs[glyph_index].in_character].in_span;
192             do {
193                 span_string += *span_iter;
194                 span_iter++;
196                 unsigned same_character = _glyphs[glyph_index].in_character;
197                 while (glyph_index < _glyphs.size() && _glyphs[glyph_index].in_character == same_character) {
198                     char_x += _glyphs[glyph_index].width;
199                     glyph_index++;
200                 }
201             } while (glyph_index < _glyphs.size()
202                      && _path_fitted == NULL
203                      && _characters[_glyphs[glyph_index].in_character].in_span == this_span_index
204                      && fabs(char_x - _characters[_glyphs[glyph_index].in_character].x) < 1e-5);
205             sp_print_bind(ctx, glyph_matrix, 1.0);
206             sp_print_text(ctx, span_string.c_str(), g_pos, text_source->style);
207             sp_print_release(ctx);
208         }
209     }
212 #ifdef HAVE_CAIRO_PDF
213 void Layout::showGlyphs(CairoRenderContext *ctx) const
215     if (_input_stream.empty()) return;
216     
217     bool clip_mode = false;//(ctx->getRenderMode() == CairoRenderContext::RENDER_MODE_CLIP);
218     std::vector<CairoGlyphInfo> glyphtext;
220     for (unsigned glyph_index = 0 ; glyph_index < _glyphs.size() ; ) {
221         if (_characters[_glyphs[glyph_index].in_character].in_glyph == -1) {
222             // invisible glyphs
223             unsigned same_character = _glyphs[glyph_index].in_character;
224             while (_glyphs[glyph_index].in_character == same_character)
225                 glyph_index++;
226             continue;
227         }
228         Span const &span = _spans[_characters[_glyphs[glyph_index].in_character].in_span];
229         InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_input_stream[span.in_input_stream_item]);
231         NR::Matrix glyph_matrix;
232         _getGlyphTransformMatrix(glyph_index, &glyph_matrix);
233         if (clip_mode) {
234             NArtBpath *bpath = (NArtBpath*)span.font->ArtBPath(_glyphs[glyph_index].glyph);
235             if (bpath) {
236                 NArtBpath *abp = nr_artpath_affine(bpath, glyph_matrix);
237                 NRBPath bpath;
238                 bpath.path = abp;
239                 SPStyle const *style = text_source->style;
240                 ctx->renderPath(&bpath, style, NULL);
241                 g_free(abp);
242             }
243             glyph_index++;
244             continue;
245         }
247         NR::Matrix font_matrix;
248         if (_path_fitted == NULL) {
249             font_matrix = glyph_matrix;
250             font_matrix[4] = 0;
251             font_matrix[5] = 0;
252         } else {
253             font_matrix.set_identity();
254         }
256         Glib::ustring::const_iterator span_iter = span.input_stream_first_character;
257         unsigned char_index = _glyphs[glyph_index].in_character;
258         unsigned original_span = _characters[char_index].in_span;
259         while (char_index && _characters[char_index - 1].in_span == original_span) {
260             char_index--;
261             span_iter++;
262         }
264         // try to output as many characters as possible in one go
265         Glib::ustring span_string;
266         unsigned this_span_index = _characters[_glyphs[glyph_index].in_character].in_span;
267         unsigned int first_index = glyph_index;
268         glyphtext.clear();
269         do {
270             span_string += *span_iter;
271             span_iter++;
273             unsigned same_character = _glyphs[glyph_index].in_character;
274             while (glyph_index < _glyphs.size() && _glyphs[glyph_index].in_character == same_character) {
275                 if (glyph_index != first_index)
276                     _getGlyphTransformMatrix(glyph_index, &glyph_matrix);
278                 CairoGlyphInfo info;
279                 info.index = _glyphs[glyph_index].glyph;
280                 if (_path_fitted == NULL) {
281                     info.x = glyph_matrix[4];
282                     info.y = glyph_matrix[5];
283                 } else {
284                     info.x = 0;
285                     info.y = 0;                    
286                 }
287                 glyphtext.push_back(info);
289                 glyph_index++;
290             }
291         } while (glyph_index < _glyphs.size()
292                  && _path_fitted == NULL
293                  && NR::transform_equalp(font_matrix, glyph_matrix, NR_EPSILON)
294                  && _characters[_glyphs[glyph_index].in_character].in_span == this_span_index);
295          
296         // remove vertical flip
297         font_matrix[3] *= -1.0;
299         SPStyle const *style = text_source->style;
300         float opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
301         
302         if (_path_fitted) {
303             ctx->pushState();
304             ctx->transform(&glyph_matrix);
305         } else if (opacity != 1.0) {
306             ctx->pushState();
307             ctx->setStateForStyle(style);
308             ctx->pushLayer();
309         }
310         if (glyph_index - first_index > 0)
311             ctx->renderGlyphtext(span.font->pFont, &font_matrix, glyphtext, style);
312         if (_path_fitted)
313             ctx->popState();
314         else if (opacity != 1.0) {
315             ctx->popLayer();
316             ctx->popState();
317         }
318     }
320 #endif
322 // these functions are for dumpAsText() only. No need to translate
323 static char const *direction_to_text(Layout::Direction d)
325     switch (d) {
326         case Layout::LEFT_TO_RIGHT: return "ltr";
327         case Layout::RIGHT_TO_LEFT: return "rtl";
328         case Layout::TOP_TO_BOTTOM: return "ttb";
329         case Layout::BOTTOM_TO_TOP: return "btt";
330     }
331     return "???";
334 static char const *style_to_text(PangoStyle s)
336     switch (s) {
337         case PANGO_STYLE_NORMAL: return "upright";
338         case PANGO_STYLE_ITALIC: return "italic";
339         case PANGO_STYLE_OBLIQUE: return "oblique";
340     }
341     return "???";
344 static char const *weight_to_text(PangoWeight w)
346     switch (w) {
347         case PANGO_WEIGHT_ULTRALIGHT: return "ultralight";
348         case PANGO_WEIGHT_LIGHT     : return "light";
349         case PANGO_WEIGHT_SEMIBOLD  : return "semibold";
350         case PANGO_WEIGHT_NORMAL    : return "normalweight";
351         case PANGO_WEIGHT_BOLD      : return "bold";
352         case PANGO_WEIGHT_ULTRABOLD : return "ultrabold";
353         case PANGO_WEIGHT_HEAVY     : return "heavy";
354     }
355     return "???";
358 Glib::ustring Layout::dumpAsText() const
360     Glib::ustring result;
362     for (unsigned span_index = 0 ; span_index < _spans.size() ; span_index++) {
363         char line[256];
364         snprintf(line, sizeof(line), "==== span %d\n", span_index);
365         result += line;
366         snprintf(line, sizeof(line), "  in para %d (direction=%s)\n", _lines[_chunks[_spans[span_index].in_chunk].in_line].in_paragraph,
367                  direction_to_text(_paragraphs[_lines[_chunks[_spans[span_index].in_chunk].in_line].in_paragraph].base_direction));
368         result += line;
369         snprintf(line, sizeof(line), "  in source %d (type=%d, cookie=%p)\n", _spans[span_index].in_input_stream_item,
370                  _input_stream[_spans[span_index].in_input_stream_item]->Type(),
371                  _input_stream[_spans[span_index].in_input_stream_item]->source_cookie);
372         result += line;
373         snprintf(line, sizeof(line), "  in line %d (baseline=%f, shape=%d)\n", _chunks[_spans[span_index].in_chunk].in_line,
374                  _lines[_chunks[_spans[span_index].in_chunk].in_line].baseline_y,
375                  _lines[_chunks[_spans[span_index].in_chunk].in_line].in_shape);
376         result += line;
377         snprintf(line, sizeof(line), "  in chunk %d (x=%f, baselineshift=%f)\n", _spans[span_index].in_chunk, _chunks[_spans[span_index].in_chunk].left_x, _spans[span_index].baseline_shift);
378         result += line;
379         if (_spans[span_index].font) {
380             snprintf(line, sizeof(line), "    font '%s' %f %s %s\n", pango_font_description_get_family(_spans[span_index].font->descr), _spans[span_index].font_size, style_to_text(pango_font_description_get_style(_spans[span_index].font->descr)), weight_to_text(pango_font_description_get_weight(_spans[span_index].font->descr)));
381             result += line;
382         }
383         snprintf(line, sizeof(line), "    x_start = %f, x_end = %f\n", _spans[span_index].x_start, _spans[span_index].x_end);
384         result += line;
385         snprintf(line, sizeof(line), "    line height: ascent %f, descent %f leading %f\n", _spans[span_index].line_height.ascent, _spans[span_index].line_height.descent, _spans[span_index].line_height.leading);
386         result += line;
387         snprintf(line, sizeof(line), "    direction %s, block-progression %s\n", direction_to_text(_spans[span_index].direction), direction_to_text(_spans[span_index].block_progression));
388         result += line;
389         result += "    ** characters:\n";
390         Glib::ustring::const_iterator iter_char = _spans[span_index].input_stream_first_character;
391         // very inefficent code. what the hell, it's only debug stuff.
392         for (unsigned char_index = 0 ; char_index < _characters.size() ; char_index++) {
393             if (_characters[char_index].in_span != span_index) continue;
394             if (_input_stream[_spans[span_index].in_input_stream_item]->Type() != TEXT_SOURCE) {
395                 snprintf(line, sizeof(line), "      %d: control x=%f flags=%03x glyph=%d\n", char_index, _characters[char_index].x, *(unsigned*)&_characters[char_index].char_attributes, _characters[char_index].in_glyph);
396             } else {
397                 snprintf(line, sizeof(line), "      %d: '%c' x=%f flags=%03x glyph=%d\n", char_index, *iter_char, _characters[char_index].x, *(unsigned*)&_characters[char_index].char_attributes, _characters[char_index].in_glyph);
398                 iter_char++;
399             }
400             result += line;
401         }
402         result += "    ** glyphs:\n";
403         for (unsigned glyph_index = 0 ; glyph_index < _glyphs.size() ; glyph_index++) {
404             if (_characters[_glyphs[glyph_index].in_character].in_span != span_index) continue;
405             snprintf(line, sizeof(line), "      %d: %d (%f,%f) rot=%f cx=%f char=%d\n", glyph_index, _glyphs[glyph_index].glyph, _glyphs[glyph_index].x, _glyphs[glyph_index].y, _glyphs[glyph_index].rotation, _glyphs[glyph_index].width, _glyphs[glyph_index].in_character);
406             result += line;
407         }
408         result += "\n";
409     }
410     result += "EOT\n";
411     return result;
414 void Layout::fitToPathAlign(SVGLength const &startOffset, Path const &path)
416     double offset = 0.0;
418     if (startOffset._set) {
419         if (startOffset.unit == SVGLength::PERCENT)
420             offset = startOffset.computed * const_cast<Path&>(path).Length();
421         else
422             offset = startOffset.computed;
423     }
425     switch (_paragraphs.front().alignment) {
426         case CENTER:
427             offset -= _getChunkWidth(0) * 0.5;
428             break;
429         case RIGHT:
430             offset -= _getChunkWidth(0);
431             break;
432         default:
433             break;
434     }
436     if (_characters.empty()) {
437         int unused = 0;
438         Path::cut_position *point_otp = const_cast<Path&>(path).CurvilignToPosition(1, &offset, unused);
439         if (offset >= 0.0 && point_otp != NULL && point_otp[0].piece >= 0) {
440             NR::Point point;
441             NR::Point tangent;
442             const_cast<Path&>(path).PointAndTangentAt(point_otp[0].piece, point_otp[0].t, point, tangent);
443             _empty_cursor_shape.position = point;
444             _empty_cursor_shape.rotation = atan2(tangent[NR::Y], tangent[NR::X]);
445         }
446     }
448     for (unsigned char_index = 0 ; char_index < _characters.size() ; ) {
449         int next_cluster_glyph_index;
450         unsigned next_cluster_char_index;
451         double character_advance;
452         Span const &span = _characters[char_index].span(this);
454         for (next_cluster_char_index = char_index + 1 ;
455              next_cluster_char_index < _characters.size() && !_characters[next_cluster_char_index].char_attributes.is_cursor_position;
456              next_cluster_char_index++);
458         if (next_cluster_char_index == _characters.size()) {
459             next_cluster_glyph_index = _glyphs.size();
460             character_advance = 0.0;   // arbitrary because we're not going to advance
461         } else {
462             next_cluster_glyph_index = _characters[next_cluster_char_index].in_glyph;
463             character_advance =   (_glyphs[next_cluster_glyph_index].x + _glyphs[next_cluster_glyph_index].chunk(this).left_x)
464                 - (_glyphs[_characters[char_index].in_glyph].x + span.chunk(this).left_x);
465         }
467         double start_offset = offset + span.x_start + _characters[char_index].x;
468         double cluster_width = 0.0;
469         for (int glyph_index = _characters[char_index].in_glyph ; glyph_index < next_cluster_glyph_index ; glyph_index++)
470             cluster_width += _glyphs[glyph_index].width;
471         if (span.direction == RIGHT_TO_LEFT)
472             start_offset -= cluster_width;
473         double end_offset = start_offset + cluster_width;
475         int unused = 0;
476         double midpoint_offset = (start_offset + end_offset) * 0.5;
477         // as far as I know these functions are const, they're just not marked as such
478         Path::cut_position *midpoint_otp = const_cast<Path&>(path).CurvilignToPosition(1, &midpoint_offset, unused);
479         if (midpoint_offset >= 0.0 && midpoint_otp != NULL && midpoint_otp[0].piece >= 0) {
480             NR::Point midpoint;
481             NR::Point tangent;
483             const_cast<Path&>(path).PointAndTangentAt(midpoint_otp[0].piece, midpoint_otp[0].t, midpoint, tangent);
485             if (start_offset >= 0.0 && end_offset >= 0.0) {
486                 Path::cut_position *start_otp = const_cast<Path&>(path).CurvilignToPosition(1, &start_offset, unused);
487                 if (start_otp != NULL && start_otp[0].piece >= 0) {
488                     Path::cut_position *end_otp = const_cast<Path&>(path).CurvilignToPosition(1, &end_offset, unused);
489                     if (end_otp != NULL && end_otp[0].piece >= 0) {
490                         bool on_same_subpath = true;
491                         for (size_t i = 0 ; i < path.pts.size() ; i++) {
492                             if (path.pts[i].piece <= start_otp[0].piece) continue;
493                             if (path.pts[i].piece >= end_otp[0].piece) break;
494                             if (path.pts[i].isMoveTo == polyline_moveto) {
495                                 on_same_subpath = false;
496                                 break;
497                             }
498                         }
499                         if (on_same_subpath) {
500                             // both points were on the same subpath (without this test the angle is very weird)
501                             NR::Point startpoint, endpoint;
502                             const_cast<Path&>(path).PointAt(start_otp[0].piece, start_otp[0].t, startpoint);
503                             const_cast<Path&>(path).PointAt(end_otp[0].piece, end_otp[0].t, endpoint);
504                             if (endpoint != startpoint) {
505                                 tangent = endpoint - startpoint;
506                                 tangent.normalize();
507                             } else {
508                                 tangent = NR::Point (0,0);
509                             }
510                         }
511                         g_free(end_otp);
512                     }
513                     g_free(start_otp);
514                 }
515             }
517             double rotation = atan2(tangent[1], tangent[0]);
518             for (int glyph_index = _characters[char_index].in_glyph ; glyph_index < next_cluster_glyph_index ; glyph_index++) {
519                 double tangent_shift = -cluster_width * 0.5 + _glyphs[glyph_index].x - (_characters[char_index].x + span.x_start);
520                 double normal_shift = _glyphs[glyph_index].y;
521                 if (span.direction == RIGHT_TO_LEFT)
522                     tangent_shift += cluster_width;
523                 _glyphs[glyph_index].x = midpoint[0] - span.chunk(this).left_x + tangent[0] * tangent_shift - tangent[1] * normal_shift;
524                 _glyphs[glyph_index].y = midpoint[1] - _lines.front().baseline_y + tangent[1] * tangent_shift + tangent[0] * normal_shift;
525                 _glyphs[glyph_index].rotation += rotation;
526             }
527         } else {  // outside the bounds of the path: hide the glyphs
528             _characters[char_index].in_glyph = -1;
529         }
530         g_free(midpoint_otp);
532         char_index = next_cluster_char_index;
533     }
535     for (unsigned span_index = 0 ; span_index < _spans.size() ; span_index++) {
536         _spans[span_index].x_start += offset;
537         _spans[span_index].x_end += offset;
538     }
540     _path_fitted = &path;
543 SPCurve *Layout::convertToCurves(iterator const &from_glyph, iterator const &to_glyph) const
545     GSList *cc = NULL;
547     for (int glyph_index = from_glyph._glyph_index ; glyph_index < to_glyph._glyph_index ; glyph_index++) {
548         NR::Matrix glyph_matrix;
549         Span const &span = _glyphs[glyph_index].span(this);
550         _getGlyphTransformMatrix(glyph_index, &glyph_matrix);
552         NRBPath bpath;
553         bpath.path = (NArtBpath*)span.font->ArtBPath(_glyphs[glyph_index].glyph);
554         if (bpath.path) {
555             NArtBpath *abp = nr_artpath_affine(bpath.path, glyph_matrix);
556             SPCurve *c = SPCurve::new_from_bpath(abp);
557             if (c) cc = g_slist_prepend(cc, c);
558         }
559     }
560     cc = g_slist_reverse(cc);
562     SPCurve *curve;
563     if ( cc ) {
564         curve = SPCurve::concat(cc);
565     } else {
566         curve = new SPCurve();
567     }
569     while (cc) {
570         /* fixme: This is dangerous, as we are mixing art_alloc and g_new */
571         reinterpret_cast<SPCurve *>(cc->data)->unref();
572         cc = g_slist_remove(cc, cc->data);
573     }
575     return curve;
578 void Layout::transform(NR::Matrix const &transform)
580     // this is all massively oversimplified
581     // I can't actually think of anybody who'll want to use it at the moment, so it'll stay simple
582     for (unsigned glyph_index = 0 ; glyph_index < _glyphs.size() ; glyph_index++) {
583         NR::Point point(_glyphs[glyph_index].x, _glyphs[glyph_index].y);
584         point *= transform;
585         _glyphs[glyph_index].x = point[0];
586         _glyphs[glyph_index].y = point[1];
587     }
590 }//namespace Text
591 }//namespace Inkscape
594 /*
595   Local Variables:
596   mode:c++
597   c-file-style:"stroustrup"
598   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
599   indent-tabs-mode:nil
600   fill-column:99
601   End:
602 */
603 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :