Layout-TNG-Compute.cpp revision 8eb2b6772a9db58582162899b9d525211efbc11c
/*
* Inkscape::Text::Layout::Calculator - text layout engine meaty bits
*
* Authors:
* Richard Hughes <cyreve@users.sf.net>
*
* Copyright (C) 2005 Richard Hughes
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "Layout-TNG.h"
#include "style.h"
#include "font-instance.h"
#include "svg/svg-length.h"
#include "sp-object.h"
#include "Layout-TNG-Scanline-Maker.h"
namespace Inkscape {
namespace Text {
//#define IFTRACE(_code) _code
// ******* enum conversion tables
/** \brief private to Layout. Does the real work of text flowing.
This class does a standard greedy paragraph wrapping algorithm.
Very high-level overview:
<pre>
foreach(paragraph) {
call pango_itemize() (_buildPangoItemizationForPara())
break into spans, without dealing with wrapping (_buildSpansForPara())
foreach(line in flow shape) {
foreach(chunk in flow shape) { (in _buildChunksInScanRun())
// this inner loop in _measureUnbrokenSpan()
if the line height changed discard the line and start again
keep adding characters until we run out of space in the chunk, then back up to the last word boundary
(do sensible things if there is no previous word break)
}
push all the glyphs, chars, spans, chunks and line to output (not completely trivial because we must draw rtl in character order) (in _outputLine())
}
push the paragraph (in calculate())
}
</pre>
...and all of that needs to work vertically too, and with all the little details that make life annoying
*/
class Layout::Calculator
{
class SpanPosition;
friend class SpanPosition;
unsigned _current_shape_index; /// index into Layout::_input_wrap_shapes
/** for y= attributes in tspan elements et al, we do the adjustment by moving each
glyph individually by this number. The spec means that this is maintained across
paragraphs. */
double _y_offset;
/** to stop pango from hinting its output, the font factory creates all fonts very large.
All numbers returned from pango have to be divided by this number \em and divided by
PANGO_SCALE. See font_factory::font_factory(). */
double _font_factory_size_multiplier;
/** Temporary storage associated with each item in Layout::_input_stream. */
struct InputItemInfo {
bool in_sub_flow;
void free();
};
/** Temporary storage associated with each item returned by the call to
pango_itemize(). */
struct PangoItemInfo {
void free();
};
/** These spans have approximately the same definition as that used for
Layout::Span (constant font, direction, etc), except that they are from
before we have located the line breaks, so bear no relation to chunks.
They are guaranteed to be in at most one PangoItem (spans with no text in
them will not have an associated PangoItem), exactly one input source and
will only have one change of x, y, dx, dy or rotate attribute, which will
be at the beginning. An UnbrokenSpan can cross a chunk boundary, c.f.
BrokenSpan. */
struct UnbrokenSpan {
int pango_item_index; /// index into _para.pango_items, or -1 if this is style only
unsigned input_index; /// index into Layout::_input_stream
double font_size;
double line_height_multiplier; /// calculated from the font-height css property
unsigned text_bytes;
unsigned char_index_in_para; /// the index of the first character in this span in the paragraph, for looking up char_attributes
SVGLength x, y, dx, dy, rotate; // these are reoriented copies of the <tspan> attributes. We change span when we encounter one.
};
/** a useful little iterator for moving char-by-char across spans. */
struct UnbrokenSpanPosition {
unsigned char_byte;
unsigned char_index;
void increment(); ///< Step forward by one character.
inline bool operator== (UnbrokenSpanPosition const &other) const
inline bool operator!= (UnbrokenSpanPosition const &other) const
};
/** The line breaking algorithm will convert each UnbrokenSpan into one
or more of these. A BrokenSpan will never cross a chunk boundary, c.f.
UnbrokenSpan. */
struct BrokenSpan {
unsigned start_glyph_index;
unsigned end_glyph_index;
double width;
unsigned whitespace_count;
bool ends_with_whitespace;
double each_whitespace_width;
void setZero();
};
/** The definition of a chunk used here is the same as that used in Layout. */
struct ChunkInfo {
double scanrun_width;
double text_width; ///< Total width used by the text (excluding justification).
double x;
int whitespace_count;
};
/** Used to provide storage for anything that applies to the current
paragraph only. Since we're only processing one paragraph at a time,
there's only one instantiation of this struct, on the stack of
calculate(). */
struct ParagraphInfo {
unsigned first_input_index; ///< Index into Layout::_input_stream.
template<typename T> static void free_sequence(T &seq);
void free();
};
/* *********************************************************************************************************/
// Initialisation of ParagraphInfo structure
#if 0 /* unused */
#endif
double *line_height_multiplier);
/* *********************************************************************************************************/
// Per-line functions
bool _goToNextWrapShape();
UnbrokenSpanPosition const &span_pos)
{
}
UnbrokenSpanPosition const &start_span_pos,
LineHeight *line_height) const;
/** computes the width of a single UnbrokenSpan (pointed to by span->start.iter_span)
and outputs its vital statistics into the other fields of \a span.
Measuring will stop if maximum_width is reached and in that case the
function will return false. In other cases where a line break must be
done immediately the function will also return false. On return
\a last_break_span will contain the vital statistics for the span only
up to the last line breaking change. If there are no line breaking
characters in the span then \a last_break_span will not be altered.
Similarly, \a last_emergency_break_span will contain the vital
statistics for the span up to the last inter-character boundary,
or will be unaltered if there is none. */
bool _measureUnbrokenSpan(ParagraphInfo const ¶, BrokenSpan *span, BrokenSpan *last_break_span, BrokenSpan *last_emergency_break_span, double maximum_width) const
{
// if this is a style-only span there's no text in it
// so we don't need to do very much at all
return true;
}
InputStreamControlCode const *control_code = static_cast<InputStreamControlCode const *>(_flow._input_stream[span->start.iter_span->input_index]);
return false;
}
return false;
}
return true;
}
return true; // never happens
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_flow._input_stream[span->start.iter_span->input_index]);
// TODO: block-progression altered in the middle
// Measure the precomputed flow from para.input_items
return true;
}
// a normal span going with a normal block-progression
double font_size_multiplier = span->start.iter_span->font_size / (PANGO_SCALE * _font_factory_size_multiplier);
double soft_hyphen_glyph_width = 0.0;
bool soft_hyphen_in_word = false;
bool is_soft_hyphen = false;
IFTRACE(int char_count = 0);
// if we're not at the start of the span we need to pre-init glyph_index
span->start_glyph_index = 0;
&& span->start.iter_span->glyph_string->log_clusters[span->start_glyph_index] < (int)span->start.char_byte)
// go char-by-char summing the width, while keeping track of the previous break point
do {
TRACE(("span %d end of para; width = %f chars = %d\n", span->start.iter_span - para.unbroken_spans.begin(), span->width, char_count));
return false;
}
if (char_attributes.is_line_break) {
// a suitable position to break at, record where we are
if (soft_hyphen_in_word) {
// if there was a previous soft hyphen we're not going to need it any more so we can remove it
if (!is_soft_hyphen)
soft_hyphen_in_word = false;
}
} else if (char_attributes.is_char_break) {
}
// todo: break between chars if necessary (ie no word breaks present) when doing rectangular flowing
// sum the glyph widths, letter spacing and word spacing to get the character width
double char_width = 0.0;
&& span->end.iter_span->glyph_string->log_clusters[span->end_glyph_index] <= (int)span->end.char_byte) {
char_width += span->start.iter_span->font_size * para.pango_items[span->end.iter_span->pango_item_index].font->Advance(span->end.iter_span->glyph_string->glyphs[span->end_glyph_index].glyph, true);
else
char_width += font_size_multiplier * span->end.iter_span->glyph_string->glyphs[span->end_glyph_index].geometry.width;
span->end_glyph_index++;
}
if (char_attributes.is_white)
IFTRACE(char_count++);
if (char_attributes.is_white) {
span->whitespace_count++;
}
is_soft_hyphen = (UNICODE_SOFT_HYPHEN == *Glib::ustring::const_iterator(span->end.iter_span->input_stream_first_character.base() + span->end.char_byte));
if (is_soft_hyphen)
if (span->width > maximum_width && !char_attributes.is_white) { // whitespaces don't matter, we can put as many as we want at eol
TRACE(("span %d exceeded scanrun; width = %f chars = %d\n", span->start.iter_span - para.unbroken_spans.begin(), span->width, char_count));
return false;
}
TRACE(("fitted span %d width = %f chars = %d\n", span->start.iter_span - para.unbroken_spans.begin(), span->width, char_count));
return true;
}
/* *********************************************************************************************************/
// Per-line functions (output)
/** Uses the paragraph alignment and the chunk information to work out
where the actual left of the final chunk must be. Also sets
\a add_to_each_whitespace to be the amount of x to add at each
whitespace character to make full justification work. */
double _getChunkLeftWithAlignment(ParagraphInfo const ¶, std::vector<ChunkInfo>::const_iterator it_chunk, double *add_to_each_whitespace) const
{
*add_to_each_whitespace = 0.0;
case FULL:
case LEFT:
default:
return it_chunk->x;
case RIGHT:
case CENTER:
}
}
case FULL:
&& it_chunk->broken_spans.back().end.iter_span != para.unbroken_spans.end()) { // don't justify the last chunk in the para
if (it_chunk->whitespace_count)
*add_to_each_whitespace = (it_chunk->scanrun_width - it_chunk->text_width) / it_chunk->whitespace_count;
//else
//add_to_each_charspace = something
}
return it_chunk->x;
case LEFT:
default:
return it_chunk->x;
case RIGHT:
case CENTER:
}
}
/** Once we've got here we have finished making changes to the line and
are ready to output the final result to #_flow. This method takes its
input parameters and does that.
*/
void _outputLine(ParagraphInfo const ¶, LineHeight const &line_height, std::vector<ChunkInfo> const &chunk_info)
{
if (chunk_info.empty()) {
TRACE(("line too short to fit anything on it, go to next\n"));
return;
}
// we've finished fiddling about with ascents and descents: create the output
TRACE(("found line fit; creating output\n"));
for (std::vector<ChunkInfo>::const_iterator it_chunk = chunk_info.begin() ; it_chunk != chunk_info.end() ; it_chunk++) {
double add_to_each_whitespace;
// add the chunk to the list
// we may also have y move orders to deal with here (dx, dy and rotate are done per span)
// if this is the start of a line, we should change the baseline rather than each glyph individually
if (_flow._characters.empty() || _flow._characters.back().chunk(&_flow).in_line != _flow._lines.size() - 1) {
_y_offset = 0.0;
} else
}
double x;
double direction_sign;
double counter_directional_width_remaining = 0.0;
float glyph_rotate = 0.0;
direction_sign = +1.0;
x = 0.0;
} else {
direction_sign = -1.0;
x = it_chunk->scanrun_width;
else
x = it_chunk->text_width;
}
for (std::vector<BrokenSpan>::const_iterator it_span = it_chunk->broken_spans.begin() ; it_span != it_chunk->broken_spans.end() ; it_span++) {
// begin adding spans to the list
// start of an unbroken span, we might have dx, dy or rotate still to process (x and y are done per chunk)
}
// style only, nothing to output
continue;
}
double x_in_span = 0.0;
if ((_flow._input_stream[unbroken_span.input_index]->Type() == TEXT_SOURCE) && (new_span.font = para.pango_items[unbroken_span.pango_item_index].font))
{
new_span.direction = para.pango_items[unbroken_span.pango_item_index].item->analysis.level & 1 ? RIGHT_TO_LEFT : LEFT_TO_RIGHT;
new_span.input_stream_first_character = Glib::ustring::const_iterator(unbroken_span.input_stream_first_character.base() + it_span->start.char_byte);
} else { // a control code
}
// measure width of spans we need to switch round
for (it_following_span = it_span ; it_following_span != it_chunk->broken_spans.end() ; it_following_span++) {
Layout::Direction following_span_progression = static_cast<InputStreamTextSource const *>(_flow._input_stream[it_following_span->start.iter_span->input_index])->styleGetBlockProgression();
if (it_following_span->start.iter_span->pango_item_index == -1) { // when the span came from a control code
} else
if (new_span.direction != (para.pango_items[it_following_span->start.iter_span->pango_item_index].item->analysis.level & 1 ? RIGHT_TO_LEFT : LEFT_TO_RIGHT)) break;
}
counter_directional_width_remaining += direction_sign * (it_following_span->width + it_following_span->whitespace_count * add_to_each_whitespace);
}
}
// the span is set up, push the glyphs and chars
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_flow._input_stream[unbroken_span.input_index]);
Glib::ustring::const_iterator iter_source_text = Glib::ustring::const_iterator(unbroken_span.input_stream_first_character.base() + it_span->start.char_byte) ;
for (unsigned glyph_index = it_span->start_glyph_index ; glyph_index < it_span->end_glyph_index ; glyph_index++) {
// if we're looking at a soft hyphen and it's not the last glyph in the
// chunk we don't draw the glyph but we still need to add to _characters
new_character.char_attributes = para.char_attributes[unbroken_span.char_index_in_para + char_index_in_unbroken_span];
glyph_index++;
glyph_index--;
continue;
}
// create the Layout::Glyph
/* put something like this back in when we do glyph-rotation-horizontal/vertical
if (new_span.block_progression == LEFT_TO_RIGHT || new_span.block_progression == RIGHT_TO_LEFT) {
new_glyph.x += new_span.line_height.ascent;
new_glyph.y -= unbroken_span.glyph_string->glyphs[glyph_index].geometry.width * font_size_multiplier * 0.5;
new_glyph.width = new_span.line_height.ascent + new_span.line_height.descent;
} else */
new_glyph.x = x + unbroken_span.glyph_string->glyphs[glyph_index].geometry.x_offset * font_size_multiplier + new_span.line_height.ascent;
new_glyph.y = _y_offset + (unbroken_span.glyph_string->glyphs[glyph_index].geometry.y_offset - unbroken_span.glyph_string->glyphs[glyph_index].geometry.width * 0.5) * font_size_multiplier;
new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span.glyph_string->glyphs[glyph_index].glyph, true);
} else {
new_glyph.x = x + unbroken_span.glyph_string->glyphs[glyph_index].geometry.x_offset * font_size_multiplier;
new_glyph.y = _y_offset + unbroken_span.glyph_string->glyphs[glyph_index].geometry.y_offset * font_size_multiplier;
new_glyph.width = unbroken_span.glyph_string->glyphs[glyph_index].geometry.width * font_size_multiplier;
new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span.glyph_string->glyphs[glyph_index].glyph, false);
// for some reason pango returns zero width for invalid glyph characters (those empty boxes), so go to freetype for the info
}
// pango wanted to give us glyphs in visual order but we refused, so we need to work
// out where the cluster start is ourselves
double cluster_width = 0.0;
if (unbroken_span.glyph_string->glyphs[rtl_index].attr.is_cluster_start && rtl_index != glyph_index)
break;
cluster_width += new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span.glyph_string->glyphs[rtl_index].glyph, true);
else
cluster_width += font_size_multiplier * unbroken_span.glyph_string->glyphs[rtl_index].geometry.width;
}
new_glyph.x -= cluster_width;
}
// create the Layout::Character(s)
unsigned end_byte;
else {
// output chars for the whole cluster that is commenced by this glyph
else
} else
}
new_character.x = x_in_span;
new_character.char_attributes = para.char_attributes[unbroken_span.char_index_in_para + char_index_in_unbroken_span];
advance_width += text_source->style->word_spacing.computed + add_to_each_whitespace; // justification
glyph_rotate = 0.0;
}
x -= advance_width;
} else {
x += advance_width;
}
}
x += static_cast<InputStreamControlCode const *>(_flow._input_stream[unbroken_span.input_index])->width;
}
}
// end adding spans to the list, on to the next chunk...
}
TRACE(("output done\n"));
}
/* *********************************************************************************************************/
// Setup and top-level functions
/** initialises the ScanlineMaker for the first shape in the flow, or
the infinite version if we're not doing wrapping. */
void _createFirstScanlineMaker()
{
_current_shape_index = 0;
// create the special no-wrapping infinite scanline maker
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_flow._input_stream.front());
if (!text_source->x.empty())
if (!text_source->y.empty())
TRACE((" wrapping disabled\n"));
}
else {
_scanline_maker = new ShapeScanlineMaker(_flow._input_wrap_shapes[_current_shape_index].shape, _block_progression);
TRACE((" begin wrap shape 0\n"));
}
}
public:
bool calculate();
};
/* fixme: I don't like the fact that InputItemInfo etc. use the default copy constructor and
* operator= (and thus don't involve incrementing reference counts), yet they provide a free method
* that does delete or Unref.
*
* I suggest using the garbage collector to manage deletion.
*/
{
if (sub_flow) {
delete sub_flow;
}
}
{
if (item) {
}
if (font) {
}
}
{
char_index++;
iter_span++;
char_index = char_byte = 0;
}
}
{
width = 0.0;
whitespace_count = 0;
end_glyph_index = start_glyph_index = 0;
ends_with_whitespace = false;
each_whitespace_width = 0.0;
}
{
}
}
{
}
///**
// * For sections of text with a block-progression different to the rest
// * of the flow, the best thing to do is to detect them in advance and
// * create child TextFlow objects with just the rotated text. In the
// * parent we then effectively use ARBITRARY_GAP fields during the
// * flowing (because we don't allow wrapping when the block-progression
// * changes) and copy the actual text in during the output phase.
// *
// * NB: this code not enabled yet.
// */
//void Layout::Calculator::_initialiseInputItems(ParagraphInfo *para) const
//{
// Direction prev_block_progression = _block_progression;
// int run_start_input_index = para->first_input_index;
//
// para->free_sequence(para->input_items);
// for(int input_index = para->first_input_index ; input_index < (int)_flow._input_stream.size() ; input_index++) {
// InputItemInfo input_item;
//
// input_item.in_sub_flow = false;
// input_item.sub_flow = NULL;
// if (_flow._input_stream[input_index]->Type() == CONTROL_CODE) {
// Layout::InputStreamControlCode const *control_code = static_cast<Layout::InputStreamControlCode const *>(_flow._input_stream[input_index]);
// if ( control_code->code == SHAPE_BREAK
// || control_code->code == PARAGRAPH_BREAK)
// break; // stop at the end of the paragraph
// // all other control codes we'll pick up later
//
// } else if (_flow._input_stream[input_index]->Type() == TEXT_SOURCE) {
// Layout::InputStreamTextSource *text_source = static_cast<Layout::InputStreamTextSource *>(_flow._input_stream[input_index]);
// Direction this_block_progression = text_source->styleGetBlockProgression();
// if (this_block_progression != prev_block_progression) {
// if (prev_block_progression != _block_progression) {
// // need to back up so that control codes belong outside the block-progression change
// int run_end_input_index = input_index - 1;
// while (run_end_input_index > run_start_input_index
// && _flow._input_stream[run_end_input_index]->Type() != TEXT_SOURCE)
// run_end_input_index--;
// // now create the sub-flow
// input_item.sub_flow = new Layout;
// for (int sub_input_index = run_start_input_index ; sub_input_index <= run_end_input_index ; sub_input_index++) {
// input_item.in_sub_flow = true;
// if (_flow._input_stream[sub_input_index]->Type() == CONTROL_CODE) {
// Layout::InputStreamControlCode const *control_code = static_cast<Layout::InputStreamControlCode const *>(_flow._input_stream[sub_input_index]);
// input_item.sub_flow->appendControlCode(control_code->code, control_code->source_cookie, control_code->width, control_code->ascent, control_code->descent);
// } else if (_flow._input_stream[sub_input_index]->Type() == TEXT_SOURCE) {
// Layout::InputStreamTextSource *text_source = static_cast<Layout::InputStreamTextSource *>(_flow._input_stream[sub_input_index]);
// input_item.sub_flow->appendText(*text_source->text, text_source->style, text_source->source_cookie, NULL, 0, text_source->text_begin, text_source->text_end);
// Layout::InputStreamTextSource *sub_flow_text_source = static_cast<Layout::InputStreamTextSource *>(input_item.sub_flow->_input_stream.back());
// sub_flow_text_source->x = text_source->x; // this is easier than going via optionalattrs for the appendText() call
// sub_flow_text_source->y = text_source->y; // should these actually be allowed anyway? You'll almost never get the results you expect
// sub_flow_text_source->dx = text_source->dx; // (not that it's very clear what you should expect, anyway)
// sub_flow_text_source->dy = text_source->dy;
// sub_flow_text_source->rotate = text_source->rotate;
// }
// }
// input_item.sub_flow->calculateFlow();
// }
// run_start_input_index = input_index;
// }
// prev_block_progression = this_block_progression;
// }
// para->input_items.push_back(input_item);
// }
//}
/**
* Take all the text from \a _para.first_input_index to the end of the
* paragraph and stitch it together so that pango_itemize() can be called on
* the whole thing.
*
* Input: para.first_input_index.
* Output: para.direction, para.pango_items, para.char_attributes.
*/
{
unsigned input_index;
for(input_index = para->first_input_index ; input_index < _flow._input_stream.size() ; input_index++) {
Layout::InputStreamControlCode const *control_code = static_cast<Layout::InputStreamControlCode const *>(_flow._input_stream[input_index]);
break; // stop at the end of the paragraph
// all other control codes we'll pick up later
Layout::InputStreamTextSource *text_source = static_cast<Layout::InputStreamTextSource *>(_flow._input_stream[input_index]);
// create the font_instance
continue; // bad news: we'll have to ignore all this text because we know of no font to render it
para_text.append(&*text_source->text_begin.base(), text_source->text_length); // build the combined text
// ownership of attribute is assumed by the list
}
}
// do the pango_itemize()
Layout::InputStreamTextSource const *text_source = static_cast<Layout::InputStreamTextSource *>(_flow._input_stream[para->first_input_index]);
PangoDirection pango_direction = (PangoDirection)_enum_converter(text_source->style->direction.computed, enum_convert_spstyle_direction_to_pango_direction, sizeof(enum_convert_spstyle_direction_to_pango_direction)/sizeof(enum_convert_spstyle_direction_to_pango_direction[0]));
pango_items_glist = pango_itemize_with_base_dir(_pango_context, pango_direction, para_text.data(), 0, para_text.bytes(), attributes_list, NULL);
para->direction = (Layout::Direction)_enum_converter(text_source->style->direction.computed, enum_convert_spstyle_direction_to_my_direction, sizeof(enum_convert_spstyle_direction_to_my_direction)/sizeof(enum_convert_spstyle_direction_to_my_direction[0]));
}
}
pango_items_glist = pango_itemize(_pango_context, para_text.data(), 0, para_text.bytes(), attributes_list, NULL);
// I think according to the css spec this is wrong and we're never allowed to guess the directionality
// of a paragraph. Need to talk to an rtl speaker.
else para->direction = (((PangoItem*)pango_items_glist->data)->analysis.level & 1) ? RIGHT_TO_LEFT : LEFT_TO_RIGHT;
}
// convert the GList to our vector<> and make the font_instance for each PangoItem at the same time
for (GList *current_pango_item = pango_items_glist ; current_pango_item != NULL ; current_pango_item = current_pango_item->next) {
}
// and get the character attributes on everything
pango_get_log_attrs(para_text.data(), para_text.bytes(), -1, NULL, &*para->char_attributes.begin(), para->char_attributes.size());
}
/**
* Gets the ascent, descent and leading for a font and the alteration that has to be performed
* according to the value specified by the line-height css property. The result of multiplying
* \a line_height by \a line_height_multiplier is the inline box height as specified in css2
* section 10.8.
*/
double *line_height_multiplier)
{
line_height->setZero();
*line_height_multiplier = 1.0;
}
else
*line_height *= font_size;
// yet another borked SPStyle member that we're going to have to fix ourselves
for ( ; ; ) {
break;
case SP_CSS_UNIT_NONE:
return;
case SP_CSS_UNIT_EX:
// 0.5 is an approximation of the x-height. Fixme.
return;
case SP_CSS_UNIT_EM:
case SP_CSS_UNIT_PERCENT:
return;
default: // absolute values
return;
}
break;
}
}
}
/**
* Split the paragraph into spans. Also call pango_shape() on them.
*
* Input: para->first_input_index, para->pango_items
* Output: para->spans
* Returns: the index of the beginning of the following paragraph in _flow._input_stream
*/
{
unsigned pango_item_index = 0;
unsigned char_index_in_para = 0;
unsigned byte_index_in_para = 0;
unsigned input_index;
TRACE(("build spans\n"));
for(input_index = para->first_input_index ; input_index < _flow._input_stream.size() ; input_index++) {
Layout::InputStreamControlCode const *control_code = static_cast<Layout::InputStreamControlCode const *>(_flow._input_stream[input_index]);
break; // stop at the end of the paragraph
new_span.text_bytes = 0;
}
} else if (_flow._input_stream[input_index]->Type() == TEXT_SOURCE && pango_item_index < para->pango_items.size()) {
Layout::InputStreamTextSource const *text_source = static_cast<Layout::InputStreamTextSource const *>(_flow._input_stream[input_index]);
unsigned char_index_in_source = 0;
unsigned span_start_byte_in_source = 0;
// we'll need to make several spans from each text source, based on the rules described about the UnbrokenSpan definition
for ( ; ; ) {
/* we need to change spans at every change of PangoItem, source stream change,
? 0
- byte_index_in_para ) );
new_span.input_stream_first_character = Glib::ustring::const_iterator(text_source->text_begin.base() + span_start_byte_in_source);
// cut at <tspan> attribute changes as well
if (text_source->x.size() > char_index_in_source) new_span.x = text_source->x[char_index_in_source];
if (text_source->y.size() > char_index_in_source) new_span.y = text_source->y[char_index_in_source];
if (text_source->dx.size() > char_index_in_source) new_span.dx = text_source->dx[char_index_in_source];
if (text_source->dy.size() > char_index_in_source) new_span.dy = text_source->dy[char_index_in_source];
} else {
if (text_source->x.size() > char_index_in_source) new_span.y = text_source->x[char_index_in_source];
if (text_source->y.size() > char_index_in_source) new_span.x = text_source->y[char_index_in_source];
if (text_source->dx.size() > char_index_in_source) new_span.dy = text_source->dx[char_index_in_source];
if (text_source->dy.size() > char_index_in_source) new_span.dx = text_source->dy[char_index_in_source];
}
if (text_source->rotate.size() > char_index_in_source) new_span.rotate = text_source->rotate[char_index_in_source];
if (input_index == 0 && para->unbroken_spans.empty() && !new_span.y._set && _flow._input_wrap_shapes.empty()) {
// if we don't set an explicit y some of the automatic wrapping code takes over and moves the text vertically
// so that the top of the letters is at zero, not the baseline
new_span.y = 0.0;
}
iter_text++;
if (iter_text.base() - new_span.input_stream_first_character.base() >= (int)new_span.text_bytes) break;
break;
}
}
// now we know the length, do some final calculations and add the UnbrokenSpan to the list
if (new_span.text_bytes) {
/* Some assertions intended to help diagnose bug #1277746. */
g_assert( memchr(text_source->text->data() + span_start_byte_in_source, '\0', static_cast<size_t>(new_span.text_bytes))
== NULL );
// pango_shape() will reorder glyphs in rtl sections into visual order which messes
// us up because the svg spec requires us to draw glyphs in logical order
// let's reverse the glyphstring on a cluster-by-cluster basis
unsigned i, cluster_start = 0;
for (i = 0 ; i < nglyphs ; ++i) {
if (i != cluster_start) {
std::copy(&new_span.glyph_string->glyphs[cluster_start], &new_span.glyph_string->glyphs[i], infos.end() - i);
std::copy(&new_span.glyph_string->log_clusters[cluster_start], &new_span.glyph_string->log_clusters[i], clusters.end() - i);
}
cluster_start = i;
}
}
if (i != cluster_start) {
std::copy(&new_span.glyph_string->glyphs[cluster_start], &new_span.glyph_string->glyphs[i], infos.end() - i);
std::copy(&new_span.glyph_string->log_clusters[cluster_start], &new_span.glyph_string->log_clusters[i], clusters.end() - i);
}
}
_computeFontLineHeight(para->pango_items[pango_item_index].font, new_span.font_size, text_source->style, &new_span.line_height, &new_span.line_height_multiplier);
// TODO: metrics for vertical text
TRACE(("add text span %d \"%s\"\n", para->unbroken_spans.size(), text_source->text->raw().substr(span_start_byte_in_source, new_span.text_bytes).c_str()));
} else {
// if there's no text we still need to initialise the styles
if (font) {
_computeFontLineHeight(font, new_span.font_size, text_source->style, &new_span.line_height, &new_span.line_height_multiplier);
} else {
}
}
// calculations for moving to the next UnbrokenSpan
char_index_in_source += g_utf8_strlen(&*new_span.input_stream_first_character.base(), new_span.text_bytes);
}
break; // end of source
// else <tspan> attribute changed
}
}
}
TRACE(("end build spans\n"));
return input_index;
}
/**
* Reinitialises the variables required on completion of one shape and
* moving on to the next. Returns false if there are no more shapes to wrap
* in to.
*/
{
delete _scanline_maker;
_scanline_maker = new ShapeScanlineMaker(_flow._input_wrap_shapes[_current_shape_index].shape, _block_progression);
return true;
}
/**
* Given \a para filled in and \a start_span_pos set, keeps trying to
* find somewhere it can fit the next line of text. The process of finding
* the text that fits will involve creating one or more entries in
* \a chunk_info describing the bounds of the fitted text and several
* bits of information that will prove useful when we come to output the
* line to #_flow. Returns with \a start_span_pos set to the end of the
* text that was fitted, \a chunk_info completely filled out and
* \a line_height set to the largest line box on the line. The return
* value is false only if we've run out of shapes to wrap inside (and
* hence couldn't create any chunks).
*/
{
// init the initial line_height
// empty first para: create a font for the sole purpose of measuring it
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_flow._input_stream.front());
if (font) {
double multiplier;
*line_height *= multiplier;
}
}
// else empty subsequent para: keep the old line height
} else {
// if we're not wrapping set the line_height big and negative so we can use negative line height
}
else
line_height->setZero();
}
for( ; ; ) {
if (!_goToNextWrapShape()) return false; // no more shapes to wrap in to
}
chunk_info->clear();
unsigned scan_run_index;
break;
}
}
return true;
}
/**
* Given a scan run and a first character, append one or more chunks to
* the \a chunk_info vector that describe all the spans and other detail
* necessary to output the greatest amount of text that will fit on this scan
* line (greedy line breaking algorithm). Each chunk contains one or more
* BrokenSpan structures that link back to UnbrokenSpan structures that link
* to the text itself. Normally there will be either one or zero (if the
* scanrun is too short to fit any text) chunk added to \a chunk_info by
* each call to this method, but we will add more than one if an x or y
* attribute has been set on a tspan. \a line_height must be set on input,
* and if it needs to be made larger and the #_scanline_maker can't do
* an in-situ resize then it will be set to the required value and the
* method will return false.
*/
UnbrokenSpanPosition const &start_span_pos,
LineHeight *line_height) const
{
// we haven't done anything yet so the last valid break position is the beginning
while (new_span.end.iter_span != para.unbroken_spans.end()) { // this loops once for each UnbrokenSpan
// force a chunk change at x or y attribute change
if ((new_span.start.iter_span->x._set || new_span.start.iter_span->y._set) && new_span.start.char_byte == 0) {
// y doesn't need to be done until output time
}
// see if this span is too tall to fit on the current line
/* floating point 80-bit/64-bit rounding problems require epsilon. See
discussion http://inkscape.gristle.org/2005-03-16.txt around 22:00 */
return false;
}
bool span_fitted = _measureUnbrokenSpan(para, &new_span, &last_span_at_break, &last_span_at_emergency_break, new_chunk.scanrun_width - new_chunk.text_width);
if (!span_fitted) break;
break;
}
}
TRACE(("chunk complete, used %f width (%d whitespaces, %d brokenspans)\n", new_chunk.text_width, new_chunk.whitespace_count, new_chunk.broken_spans.size()));
/* **non-SVG spec bit**: See bug #1191102
If the user types a very long line with no spaces, the way the spec
is written at the moment means that when the length of the text
exceeds the available width of all remaining areas, the text is
completely hidden. This condition alters that behaviour so that if
the length of the line is greater than four times the line-height
and there are no spaces, it'll be emergency-wrapped at the last
character. One could read the SVG Tiny 1.2 draft as permitting this
sort of behaviour, but it's still a bit dodgy. The hard-coding of
4x is not nice, either. */
}
if (!chunk_info->back().broken_spans.empty() && last_span_at_break.end != chunk_info->back().broken_spans.back().end) {
// need to back out spans until we come to the one with the last break in it
while (!chunk_info->empty() && last_span_at_break.start.iter_span != chunk_info->back().broken_spans.back().start.iter_span) {
chunk_info->pop_back();
}
if (!chunk_info->empty()) {
chunk_info->pop_back();
} else {
}
TRACE(("correction: fitted span %d width = %f\n", last_span_at_break.start.iter_span - para.unbroken_spans.begin(), last_span_at_break.width));
}
}
if (!chunk_info->empty() && !chunk_info->back().broken_spans.empty() && chunk_info->back().broken_spans.back().ends_with_whitespace) {
// for justification we need to discard space occupied by the single whitespace at the end of the chunk
chunk_info->back().broken_spans.back().width -= chunk_info->back().broken_spans.back().each_whitespace_width;
}
return true;
}
/** The management function to start the whole thing off. */
{
return false;
return false;
TRACE(("begin calculateFlow()\n"));
_y_offset = 0.0;
LineHeight line_height; // needs to be maintained across paragraphs to be able to deal with blank paras (this is wrong)
// jump to the next wrap shape if this is a SHAPE_BREAK control code
InputStreamControlCode const *control_code = static_cast<InputStreamControlCode const *>(_flow._input_stream[para.first_input_index]);
TRACE(("shape break control code\n"));
if (!_goToNextWrapShape()) break;
continue;
}
}
if (_scanline_maker == NULL)
break; // we're trying to flow past the last wrap shape
para.alignment = static_cast<InputStreamTextSource*>(_flow._input_stream[para.first_input_index])->styleGetAlignment(para.direction, !_flow._input_wrap_shapes.empty());
else
// start scanning lines
span_pos.char_index = 0;
do { // for each line in the paragraph
TRACE(("begin line\n"));
break; // out of shapes to wrap in to
if (_scanline_maker != NULL) {
bool is_empty_para = _flow._characters.empty() || _flow._characters.back().line(&_flow).in_paragraph != _flow._paragraphs.size() - 1;
// we need a span just for the para if it's either an empty last para or a break in the middle
} else {
}
else
}
// we've got to add an invisible character between paragraphs so that we can position iterators
// (and hence cursors) both before and after the paragraph break
}
}
}
if (_scanline_maker)
delete _scanline_maker;
return true;
}
void Layout::_calculateCursorShapeForEmpty()
{
return;
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_input_stream.front());
if (font) {
line_height *= font_size;
} else {
}
if (_input_wrap_shapes.empty()) {
_empty_cursor_shape.position = NR::Point(text_source->x.empty() || !text_source->x.front()._set ? 0.0 : text_source->x.front().computed,
} else {
_empty_cursor_shape.position = NR::Point(scan_runs.front().y + font_size, scan_runs.front().x_start);
else
_empty_cursor_shape.position = NR::Point(scan_runs.front().x_start, scan_runs.front().y + font_size);
}
}
}
bool Layout::calculateFlow()
{
if (_characters.empty())
return result;
}
}//namespace Text
}//namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :