+ostream & operator<<(ostream & os, Row::Element const & e)
+{
+ if (e.isRTL())
+ os << e.endpos << "<<" << e.pos << " ";
+ else
+ os << e.pos << ">>" << e.endpos << " ";
+
+ switch (e.type) {
+ case Row::STRING:
+ os << "STRING: `" << to_utf8(e.str) << "' ("
+ << e.countSeparators() << " sep.), ";
+ break;
+ case Row::VIRTUAL:
+ os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
+ break;
+ case Row::INSET:
+ os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
+ break;
+ case Row::SPACE:
+ os << "SPACE: ";
+ break;
+ }
+ os << "width=" << e.full_width();
+ return os;
+}
+
+
+ostream & operator<<(ostream & os, Row const & row)
+{
+ os << " pos: " << row.pos_ << " end: " << row.end_
+ << " left_margin: " << row.left_margin
+ << " width: " << row.dim_.wid
+ << " right_margin: " << row.right_margin
+ << " ascent: " << row.dim_.asc
+ << " descent: " << row.dim_.des
+ << " separator: " << row.separator
+ << " label_hfill: " << row.label_hfill
+ << " row_boundary: " << row.right_boundary() << "\n";
+ double x = row.left_margin;
+ Row::Elements::const_iterator it = row.elements_.begin();
+ for ( ; it != row.elements_.end() ; ++it) {
+ os << "x=" << x << " => " << *it << endl;
+ x += it->full_width();
+ }
+ return os;
+}
+
+
+int Row::left_x() const
+{
+ double x = left_margin;
+ const_iterator const end = elements_.end();
+ const_iterator cit = elements_.begin();
+ while (cit != end && cit->isVirtual()) {
+ x += cit->full_width();
+ ++cit;
+ }
+ return int(x + 0.5);
+}
+
+
+int Row::right_x() const
+{
+ double x = dim_.wid;
+ const_iterator const begin = elements_.begin();
+ const_iterator cit = elements_.end();
+ while (cit != begin) {
+ --cit;
+ if (cit->isVirtual())
+ x -= cit->full_width();
+ else
+ break;
+ }
+ return int(x + 0.5);
+}
+
+
+int Row::countSeparators() const
+{
+ int n = 0;
+ const_iterator const end = elements_.end();
+ for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
+ n += cit->countSeparators();
+ return n;
+}
+
+
+void Row::setSeparatorExtraWidth(double w)
+{
+ separator = w;
+ iterator const end = elements_.end();
+ for (iterator it = elements_.begin() ; it != end ; ++it)
+ if (it->type == Row::STRING)
+ it->extra = w;
+}
+
+
+bool Row::sameString(Font const & f, Change const & ch) const
+{
+ if (elements_.empty())
+ return false;
+ Element const & elt = elements_.back();
+ return elt.type == STRING && !elt.final
+ && elt.font == f && elt.change == ch;
+}
+
+
+void Row::finalizeLast()
+{
+ if (elements_.empty())
+ return;
+ Element & elt = elements_.back();
+ if (elt.final)
+ return;
+ elt.final = true;
+
+ if (elt.type == STRING) {
+ dim_.wid -= elt.dim.wid;
+ elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
+ dim_.wid += elt.dim.wid;
+ }
+}
+
+
+void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
+ Font const & f, Change const & ch)
+{
+ finalizeLast();
+ Element e(INSET, pos, f, ch);
+ e.inset = ins;
+ e.dim = dim;
+ elements_.push_back(e);
+ dim_.wid += dim.wid;
+}
+
+
+void Row::add(pos_type const pos, char_type const c,
+ Font const & f, Change const & ch)
+{
+ if (!sameString(f, ch)) {
+ finalizeLast();
+ Element e(STRING, pos, f, ch);
+ elements_.push_back(e);
+ }
+ if (back().str.length() % 30 == 0) {
+ dim_.wid -= back().dim.wid;
+ back().str += c;
+ back().endpos = pos + 1;
+ back().dim.wid = theFontMetrics(back().font).width(back().str);
+ dim_.wid += back().dim.wid;
+ } else {
+ back().str += c;
+ back().endpos = pos + 1;
+ }
+}
+
+
+void Row::addVirtual(pos_type const pos, docstring const & s,
+ Font const & f, Change const & ch)
+{
+ finalizeLast();
+ Element e(VIRTUAL, pos, f, ch);
+ e.str = s;
+ e.dim.wid = theFontMetrics(f).width(s);
+ dim_.wid += e.dim.wid;
+ e.endpos = pos;
+ elements_.push_back(e);
+ finalizeLast();
+}
+
+
+void Row::addSpace(pos_type const pos, int const width,
+ Font const & f, Change const & ch)
+{
+ finalizeLast();
+ Element e(SPACE, pos, f, ch);
+ e.dim.wid = width;
+ elements_.push_back(e);
+ dim_.wid += e.dim.wid;
+}
+
+
+void Row::pop_back()