#include "language.h"
+#include "support/unicode.h"
+
+using lyx::char_type;
+using lyx::docstring;
using std::string;
-namespace font_metrics {
+namespace {
+
+int smallcapswidth(unsigned short const * s, size_t ls, LyXFont const & f)
+{
+ if (!lyx_gui::use_gui)
+ return 1;
+ // handle small caps ourselves ...
+
+ LyXFont smallfont = f;
+ smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
+
+ QFontMetrics const & qm = theApp->fontLoader().metrics(f);
+ QFontMetrics const & qsmallm = theApp->fontLoader().metrics(smallfont);
+
+ int w = 0;
+
+ for (size_t i = 0; i < ls; ++i) {
+ QChar const c = s[i];
+ QChar const uc = c.toUpper();
+ if (c != uc)
+ w += qsmallm.width(uc);
+ else
+ w += qm.width(c);
+ }
+ return w;
+}
+
+
+} // anon namespace
-int maxAscent(LyXFont const & f)
+
+int font_metrics::maxAscent(LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
}
-int maxDescent(LyXFont const & f)
+int font_metrics::maxDescent(LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
}
-int ascent(char c, LyXFont const & f)
+int font_metrics::ascent(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
- QRect const & r = theApp->fontLoader().metrics(f).boundingRect(c);
+ QRect const & r = theApp->fontLoader().metrics(f).boundingRect(ucs4_to_ucs2(c));
// Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
// value by the height: (x, -y-height, width, height).
// Other versions return: (x, -y, width, height)
}
-int descent(char c, LyXFont const & f)
+int font_metrics::descent(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
- QRect const & r = theApp->fontLoader().metrics(f).boundingRect(c);
+ QRect const & r = theApp->fontLoader().metrics(f).boundingRect(ucs4_to_ucs2(c));
// Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
// value by the height: (x, -y-height, width, height).
// Other versions return: (x, -y, width, height)
}
-int lbearing(char c, LyXFont const & f)
+int font_metrics::lbearing(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
- return theApp->fontLoader().metrics(f).leftBearing(c);
+ return theApp->fontLoader().metrics(f).leftBearing(ucs4_to_ucs2(c));
}
-int rbearing(char c, LyXFont const & f)
+int font_metrics::rbearing(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
QFontMetrics const & m = theApp->fontLoader().metrics(f);
// Qt rbearing is from the right edge of the char's width().
- return m.width(c) - m.rightBearing(c);
-}
-
-
-Encoding const * fontencoding(LyXFont const & f)
-{
- Encoding const * encoding = f.language()->encoding();
- if (f.isSymbolFont())
- encoding = encodings.symbol_encoding();
- return encoding;
-}
-
-
-int smallcapswidth(char const * s, size_t ls, LyXFont const & f)
-{
- if (!lyx_gui::use_gui)
- return 1;
- // handle small caps ourselves ...
-
- LyXFont smallfont = f;
- smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
-
- QFontMetrics const & qm = theApp->fontLoader().metrics(f);
- QFontMetrics const & qsmallm = theApp->fontLoader().metrics(smallfont);
-
- Encoding const * encoding = fontencoding(f);
-
- int w = 0;
-
- for (size_t i = 0; i < ls; ++i) {
- QChar const c = QChar(encoding->ucs(s[i]));
- QChar const uc = c.upper();
- if (c != uc)
- w += qsmallm.width(uc);
- else
- w += qm.width(c);
- }
- return w;
+ unsigned short sc = ucs4_to_ucs2(c);
+ return m.width(sc) - m.rightBearing(sc);
}
-int width(char const * s, size_t ls, LyXFont const & f)
+int font_metrics::width(char_type const * s, size_t ls, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return ls;
+ std::vector<unsigned short> ucs2 = ucs4_to_ucs2(s, ls);
+ ucs2.push_back(0);
+
if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
- return smallcapswidth(s, ls, f);
+ return smallcapswidth(&ucs2[0], ls, f);
- Encoding const * encoding = fontencoding(f);
QLFontInfo & fi = theApp->fontLoader().fontinfo(f);
if (ls == 1)
- return fi.width(encoding->ucs(s[0]));
+ return fi.width(ucs2[0]);
int w = 0;
for (size_t i = 0; i < ls; ++i)
- w += fi.width(encoding->ucs(s[i]));
+ w += fi.width(ucs2[i]);
return w;
}
-int signedWidth(string const & s, LyXFont const & f)
+int font_metrics::signedWidth(docstring const & s, LyXFont const & f)
{
if (s[0] == '-')
return -width(s.substr(1, s.length() - 1), f);
}
-void rectText(string const & str, LyXFont const & f,
+void font_metrics::rectText(docstring const & str, LyXFont const & f,
int & w, int & ascent, int & descent)
{
QFontMetrics const & m = theApp->fontLoader().metrics(f);
-void buttonText(string const & str, LyXFont const & f,
+void font_metrics::buttonText(docstring const & str, LyXFont const & f,
int & w, int & ascent, int & descent)
{
QFontMetrics const & m = theApp->fontLoader().metrics(f);
ascent = m.ascent() + d;
descent = m.descent() + d;
}
-
-} // namespace font_metrics