Code

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