+ return main_text_? par_metrics_[pit].rightMargin(*bv_) : 0;
+}
+
+
+void TextMetrics::applyOuterFont(Font & font) const
+{
+ Font lf(font_);
+ lf.fontInfo().reduce(bv_->buffer().params().getFont().fontInfo());
+ lf.fontInfo().realize(font.fontInfo());
+ lf.setLanguage(font.language());
+ font = lf;
+}
+
+
+Font TextMetrics::getDisplayFont(pit_type pit, pos_type pos) const
+{
+ BOOST_ASSERT(pos >= 0);
+
+ ParagraphList const & pars = text_->paragraphs();
+ Paragraph const & par = pars[pit];
+ LayoutPtr const & layout = par.layout();
+ Buffer const & buffer = bv_->buffer();
+ // FIXME: broken?
+ BufferParams const & params = buffer.params();
+ pos_type const body_pos = par.beginOfBody();
+
+ // We specialize the 95% common case:
+ if (!par.getDepth()) {
+ Font f = par.getFontSettings(params, pos);
+ if (!text_->isMainText(buffer))
+ applyOuterFont(f);
+ bool lab = layout->labeltype == LABEL_MANUAL && pos < body_pos;
+
+ FontInfo const & lf = lab ? layout->labelfont : layout->font;
+ FontInfo rlf = lab ? layout->reslabelfont : layout->resfont;
+
+ // In case the default family has been customized
+ if (lf.family() == INHERIT_FAMILY)
+ rlf.setFamily(params.getFont().fontInfo().family());
+ f.fontInfo().realize(rlf);
+ return f;
+ }
+
+ // The uncommon case need not be optimized as much
+ FontInfo const & layoutfont = pos < body_pos ?
+ layout->labelfont : layout->font;
+
+ Font font = par.getFontSettings(params, pos);
+ font.fontInfo().realize(layoutfont);
+
+ if (!text_->isMainText(buffer))
+ applyOuterFont(font);
+
+ // Realize against environment font information
+ // NOTE: the cast to pit_type should be removed when pit_type
+ // changes to a unsigned integer.
+ if (pit < pit_type(pars.size()))
+ font.fontInfo().realize(outerFont(pit, pars).fontInfo());
+
+ // Realize with the fonts of lesser depth.
+ font.fontInfo().realize(params.getFont().fontInfo());
+
+ return font;
+}
+
+
+bool TextMetrics::isRTL(CursorSlice const & sl, bool boundary) const
+{
+ if (!lyxrc.rtl_support || !sl.text())
+ return false;
+
+ int correction = 0;
+ if (boundary && sl.pos() > 0)
+ correction = -1;
+
+ return getDisplayFont(sl.pit(), sl.pos() + correction).isVisibleRightToLeft();
+}
+
+
+bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos) const
+{
+ if (!lyxrc.rtl_support)
+ return false;
+
+ // no RTL boundary at line start
+ if (pos == 0)
+ return false;
+
+ Paragraph const & par = text_->getPar(pit);
+
+ bool left = getDisplayFont(pit, pos - 1).isVisibleRightToLeft();
+ bool right;
+ if (pos == par.size())
+ right = par.isRTL(bv_->buffer().params());
+ else
+ right = getDisplayFont(pit, pos).isVisibleRightToLeft();
+ return left != right;
+}
+
+
+bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos,
+ Font const & font) const
+{
+ if (!lyxrc.rtl_support)
+ return false;
+
+ Paragraph const & par = text_->getPar(pit);
+ bool left = font.isVisibleRightToLeft();
+ bool right;
+ if (pos == par.size())
+ right = par.isRTL(bv_->buffer().params());
+ else
+ right = getDisplayFont(pit, pos).isVisibleRightToLeft();
+ return left != right;