The computation of length on screen depend in particular of the computation of the size of an em. Many places of the code used to rely on the width of the M character, which is not really correct:
http://en.wikipedia.org/wiki/Em_%28typography%29
In digital typography, the best value to use is the point size of the font.
* Implement FontMetrics::em(), which returns the value in pixels of the EM unit.
Convert code to use it.
* Introduce Length::inPixel(MetricsBase const &), which takes the textwidth and em information from the MetricsBase object. Convert code to use it.
* Fix several places where Length::inPixel is used without a proper em value.
* add mathed_font_em() helper function. It should eventually be removed like some other functions in MathSupport.
* Add dummy implementation of FontMetrics to tex2lyx for linking purposes.
// Horizontal scroll offset of the cursor row in pixels
int offset = d->horiz_scroll_offset_;
- int const MARGIN = Length(2, Length::EM).inPixels(workWidth());
+ int const MARGIN = Length(2, Length::EM).inPixels(pi.base);
if (cur_x < offset + MARGIN) {
// scroll right
offset = cur_x - MARGIN;
#include "Length.h"
#include "LyXRC.h"
+#include "MetricsInfo.h"
+
+#include "frontends/FontMetrics.h"
#include "support/docstream.h"
? em_width_base
: 10*(dpi/72.27)*zoom;
// A different estimate for em_width is
- // theFontMetrics(FontInfo(sane_font)).width('M')
+ // theFontMetrics(FontInfo(sane_font)).em()
// but this estimate might not be more accurate as the screen font
// is different then the latex font.
}
+int Length::inPixels(MetricsBase const & base) const
+{
+ return inPixels(base.textwidth, theFontMetrics(base.font).em());
+}
+
+
int Length::inBP() const
{
// return any Length value as a one with
namespace lyx {
+class MetricsBase;
+
// Solaris/x86 version 9 and earlier define these
#undef PC
#undef SP
std::string const asLatexString() const;
/// return string representation for HTML
std::string const asHTMLString() const;
- /// return the on-screen size of this length
+ /** return the on-screen size of this length.
+ *
+ * If the second argument is not provided, then the unit EM will
+ * only be approximated. It is better if possible to use
+ * FontMetrics::em() to get this value.
+ */
int inPixels(int text_width, int em_width = 0) const;
+ /** return the on-screen size of this length
+ *
+ * This version of the function uses the right EM definition.
+ */
+ int inPixels(MetricsBase const &) const;
/// return the value in Big Postscript points.
int inBP() const;
}
if (!par.params().leftIndent().zero())
- l_margin += par.params().leftIndent().inPixels(max_width);
+ l_margin += par.params().leftIndent().inPixels(max_width, labelfont_metrics.em());
LyXAlignment align;
/// return default dimension of the font.
/// \warning \c width is set to zero.
virtual Dimension const defaultDimension() const = 0;
+ /// return the em size
+ virtual int em() const = 0;
+
/// return the width of the char in the font
virtual int width(char_type c) const = 0;
/// return the ascent of the char in the font
}
+int GuiFontMetrics::em() const
+{
+ return QFontInfo(font_).pixelSize();
+}
+
+
int GuiFontMetrics::lbearing(char_type c) const
{
if (!is_utf16(c))
virtual int maxAscent() const;
virtual int maxDescent() const;
virtual Dimension const defaultDimension() const;
+ virtual int em() const;
virtual int width(char_type c) const;
virtual int ascent(char_type c) const;
virtual int descent(char_type c) const;
// back up textwidth.
int textwidth_backup = m.base.textwidth;
if (hasFixedWidth())
- m.base.textwidth = params_.width.inPixels(m.base.textwidth);
+ m.base.textwidth = params_.width.inPixels(m.base);
InsetCollapsable::metrics(m, dim);
// retore textwidth.
m.base.textwidth = textwidth_backup;
int const max_width = mi.base.textwidth;
Length const width(to_ascii(getParam("width")));
- dim.wid = width.inPixels(max_width, fm.width(char_type('M')));
+ dim.wid = width.inPixels(mi.base);
// assure that the line inset is not outside of the window
// check that it doesn't exceed the outer boundary
dim.wid = max(minw, abs(dim.wid));
Length height = Length(to_ascii(getParam("height")));
- height_ = height.inPixels(max_width, fm.width(char_type('M')));
+ height_ = height.inPixels(mi.base);
// get the length of the parameters in pixels
Length offset = Length(to_ascii(getParam("offset")));
- offset_ = offset.inPixels(max_width, fm.width(char_type('M')));
+ offset_ = offset.inPixels(mi.base);
dim.asc = max(fm.maxAscent(), offset_ + height_);
dim.des = max(fm.maxDescent(), - offset_);
frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
dim.asc = fm.maxAscent();
dim.des = fm.maxDescent();
+ int const em = fm.em();
switch (params_.kind) {
case InsetSpaceParams::THIN:
case InsetSpaceParams::NEGTHIN:
- dim.wid = fm.width(char_type('M')) / 6;
+ dim.wid = em / 6;
break;
case InsetSpaceParams::MEDIUM:
case InsetSpaceParams::NEGMEDIUM:
- dim.wid = fm.width(char_type('M')) / 4;
+ dim.wid = em / 4;
break;
case InsetSpaceParams::THICK:
case InsetSpaceParams::NEGTHICK:
- dim.wid = fm.width(char_type('M')) / 2;
+ dim.wid = em / 2;
break;
case InsetSpaceParams::PROTECTED:
case InsetSpaceParams::VISIBLE:
dim.wid = fm.width(char_type(' '));
break;
case InsetSpaceParams::QUAD:
- dim.wid = fm.width(char_type('M'));
+ dim.wid = em;
break;
case InsetSpaceParams::QQUAD:
- dim.wid = 2 * fm.width(char_type('M'));
+ dim.wid = 2 * em;
break;
case InsetSpaceParams::ENSPACE:
case InsetSpaceParams::ENSKIP:
- dim.wid = int(0.5 * fm.width(char_type('M')));
+ dim.wid = int(0.5 * em);
break;
case InsetSpaceParams::CUSTOM:
case InsetSpaceParams::CUSTOM_PROTECTED: {
int const w =
- params_.length.len().inPixels(mi.base.textwidth,
- fm.width(char_type('M')));
+ params_.length.len().inPixels(mi.base);
int const minw = (w < 0) ? 3 * arrow_size : 4;
dim.wid = max(minw, abs(w));
break;
int logoWidth(FontInfo const & font, InsetSpecialChar::Kind kind) {
frontend::FontMetrics const & fm = theFontMetrics(font);
- int const em = fm.width('M');
+ int const em = fm.em();
int width = 0;
// See drawlogo() below to understand what this does.
switch (kind) {
void drawLogo(PainterInfo & pi, InsetSpecialChar::Kind kind, int & x, int & y) {
FontInfo const & font = pi.base.font;
- // FIXME: this definition of em is bogus, but there is a need
- // for a big refactoring of the code around this issue anyway.
- int const em = theFontMetrics(font).width('M');
+ int const em = theFontMetrics(font).em();
switch (kind) {
case InsetSpecialChar::PHRASE_LYX:
/** Reference macro:
MetricsInfo m = mi;
Length const p_width = tabular.getPWidth(cell);
if (!p_width.zero())
- m.base.textwidth = p_width.inPixels(mi.base.textwidth);
+ m.base.textwidth = p_width.inPixels(mi.base);
tabular.cellInset(cell)->metrics(m, dim);
if (!p_width.zero())
dim.wid = m.base.textwidth;
}
int const top_space = tabular.row_info[r].top_space_default ?
default_line_space :
- tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
+ tabular.row_info[r].top_space.inPixels(mi.base);
tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
int const bottom_space = tabular.row_info[r].bottom_space_default ?
default_line_space :
- tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
+ tabular.row_info[r].bottom_space.inPixels(mi.base);
tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
}
dim = fm.dimension(char_);
kerning_ = fm.rbearing(char_) - dim.wid;
}
- int const em = mathed_char_width(mi.base.font, 'M');
+ int const em = mathed_font_em(mi.base.font);
if (isBinaryOp(char_))
dim.wid += static_cast<int>(0.5*em+0.5);
else if (char_ == '\'')
void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
{
//lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
- int const em = mathed_char_width(pi.base.font, 'M');
+ int const em = mathed_font_em(pi.base.font);
if (isBinaryOp(char_))
x += static_cast<int>(0.25*em+0.5);
else if (char_ == '\'')
#include "FuncRequest.h"
#include "frontends/Clipboard.h"
-#include "frontends/FontMetrics.h"
#include "frontends/Painter.h"
#include "support/debug.h"
int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
{
- frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
- return crskip_.inPixels(mi.base.textwidth,
- fm.width(char_type('M')));
+ return crskip_.inPixels(mi.base);
}
{
dim.asc = 0;
dim.des = 0;
- dim.wid = wid_.inPixels(0, mathed_char_width(mi.base.font, 'M'));
+ dim.wid = wid_.inPixels(mi.base);
}
dim.asc = 4;
dim.des = 0;
if (space_info[space_].custom)
- dim.wid = abs(length_.inPixels(
- mi.base.textwidth,
- mathed_char_width(mi.base.font, 'M')));
+ dim.wid = abs(length_.inPixels(mi.base));
else
dim.wid = space_info[space_].width;
}
sym_->extra == "mathalpha" &&
mi.base.fontname == "mathit";
std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
- int const em = mathed_char_width(mi.base.font, 'M');
+ int const em = mathed_font_em(mi.base.font);
FontSetChanger dummy(mi.base, from_ascii(font));
mathed_string_dim(mi.base.font, sym_->draw, dim);
docstring::const_reverse_iterator rit = sym_->draw.rbegin();
sym_->extra == "mathalpha" &&
pi.base.fontname == "mathit";
std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
- int const em = mathed_char_width(pi.base.font, 'M');
+ int const em = mathed_font_em(pi.base.font);
if (isRelOp())
x += static_cast<int>(0.25*em+0.5);
else
} // namespace anon
+int mathed_font_em(FontInfo const & font)
+{
+ return theFontMetrics(font).em();
+}
+
+
int mathed_char_width(FontInfo const & font, char_type c)
{
return theFontMetrics(font).width(c);
class InsetMath;
+int mathed_font_em(FontInfo const &);
+
int mathed_char_width(FontInfo const &, char_type c);
int mathed_char_kerning(FontInfo const &, char_type c);
return string();
}
+//
+// Dummy FontMetrics (needed by Length)
+//
+
+
+class FontMetrics {
+ int em() const { return 0; };
+};
+
+class FontInfo;
+
+FontMetrics const & theFontMetrics(FontInfo const &) {
+ static FontMetrics dummy;
+ return dummy;
+}
+
//
// Keep the linker happy on Windows
//