From 4add25262877ab4da0e6e56b06e7872aeddc03c0 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Tue, 10 Oct 2006 13:24:08 +0000 Subject: [PATCH] * frontends/Painter: - text(): now returns drawn text width() * rowpainter: - paintChars(): use the returned width from Painter::text() instead of recalculating it. All other files: implement the API change. qt3 and gtk not 100% guaranted to compile nor work. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15294 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/Painter.h | 13 ++++++++++--- src/frontends/gtk/GPainter.C | 26 +++++++++++++++----------- src/frontends/gtk/GPainter.h | 6 +++--- src/frontends/nullpainter.h | 8 ++++---- src/frontends/qt3/QLPainter.C | 29 ++++++++++++++++++----------- src/frontends/qt3/QLPainter.h | 8 ++++---- src/frontends/qt4/QLPainter.C | 7 ++++--- src/frontends/qt4/QLPainter.h | 6 +++--- src/rowpainter.C | 7 +++---- 9 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/frontends/Painter.h b/src/frontends/Painter.h index 9dd8731c44..783c899a5c 100644 --- a/src/frontends/Painter.h +++ b/src/frontends/Painter.h @@ -132,19 +132,26 @@ public: lyx::graphics::Image const & image) = 0; /// draw a string at position x, y (y is the baseline) - virtual void text(int x, int y, + /** + * \return the width of the drawn text. + */ + virtual int text(int x, int y, lyx::docstring const & str, LyXFont const & f) = 0; /** * Draw a string at position x, y (y is the baseline) * This is just for fast drawing + * \return the width of the drawn text. */ - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::char_type const * str, size_t l, LyXFont const & f) = 0; /// draw a char at position x, y (y is the baseline) - virtual void text(int x, int y, + /** + * \return the width of the drawn text. + */ + virtual int text(int x, int y, lyx::char_type c, LyXFont const & f) = 0; /** diff --git a/src/frontends/gtk/GPainter.C b/src/frontends/gtk/GPainter.C index 96d5c5734c..5da8aff2a9 100644 --- a/src/frontends/gtk/GPainter.C +++ b/src/frontends/gtk/GPainter.C @@ -196,7 +196,7 @@ inline XftFont * getXftFont(LyXFont const & f) } // anon namespace -void GPainter::text(int x, int y, +int GPainter::text(int x, int y, char_type const * s, size_t ls, LyXFont const & f) { @@ -204,6 +204,8 @@ void GPainter::text(int x, int y, XftColor * xftClr = owner_.getColorHandler(). getXftColor(f.realColor()); XftDraw * draw = owner_.getXftDraw(); + int textwidth = 0; + if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) { XftDrawString32(draw, xftClr, @@ -211,11 +213,11 @@ void GPainter::text(int x, int y, x, y, reinterpret_cast(s), ls); + textwidth = font_metrics::width(s, ls, f); } else { LyXFont smallfont(f); smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE); XftFont * fontS = getXftFont(smallfont); - int tmpx = x; for (unsigned int i = 0; i < ls; ++i) { // Ok, this looks quite ugly... char_type c = gdk_keyval_to_unicode(gdk_keyval_to_upper(gdk_unicode_to_keyval(s[i]))); @@ -223,35 +225,37 @@ void GPainter::text(int x, int y, XftDrawString32(draw, xftClr, fontS, - tmpx, y, + x + textwidth, y, reinterpret_cast(&c), 1); - tmpx += font_metrics::width(c, smallfont); + textwidth += font_metrics::width(c, smallfont); } else { XftDrawString32(draw, xftClr, font, - tmpx, y, + x + textwidth, y, reinterpret_cast(&c), 1); - tmpx += font_metrics::width(c, f); + textwidth += font_metrics::width(c, f); } } } if (f.underbar() == LyXFont::ON) - underline(f, x, y, font_metrics::width(s, ls, f)); + underline(f, x, y, textwidth); + + return textwidth; } -void GPainter::text(int x, int y, docstring const & s, LyXFont const & f) +int GPainter::text(int x, int y, docstring const & s, LyXFont const & f) { - text (x, y, reinterpret_cast(s.data()), s.size(), f); + return text (x, y, reinterpret_cast(s.data()), s.size(), f); } -void GPainter::text(int x, int y, char_type c, LyXFont const & f) +int GPainter::text(int x, int y, char_type c, LyXFont const & f) { - text (x, y, &c, 1, f); + return text (x, y, &c, 1, f); } diff --git a/src/frontends/gtk/GPainter.h b/src/frontends/gtk/GPainter.h index 6201db57c8..1671156951 100644 --- a/src/frontends/gtk/GPainter.h +++ b/src/frontends/gtk/GPainter.h @@ -97,16 +97,16 @@ public: graphics::Image const & image); /// draw a string at position x, y (y is the baseline) - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::docstring const & str, LyXFont const & f); /// draw a string at position x, y (y is the baseline) - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::char_type const * str, size_t l, LyXFont const & f); /// draw a char at position x, y (y is the baseline) - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::char_type c, LyXFont const & f); void start(); diff --git a/src/frontends/nullpainter.h b/src/frontends/nullpainter.h index bb77976bca..931e2bbc1e 100644 --- a/src/frontends/nullpainter.h +++ b/src/frontends/nullpainter.h @@ -57,13 +57,13 @@ public: /// void image(int, int, int, int, lyx::graphics::Image const &) {} /// - void text(int, int, lyx::docstring const &, LyXFont const &) {} + int text(int, int, lyx::docstring const &, LyXFont const &) { return 0; } // /// -// void text(int, int, char const *, size_t, LyXFont const &) {} +// int text(int, int, char const *, size_t, LyXFont const &) { return 0; } /// - void text(int, int, lyx::char_type const *, size_t, LyXFont const &) {} + int text(int, int, lyx::char_type const *, size_t, LyXFont const &) { return 0; } /// - void text(int, int, lyx::char_type, LyXFont const &) {} + int text(int, int, lyx::char_type, LyXFont const &) { return 0; } /// void rectText(int, int, lyx::docstring const &, LyXFont const &, LColor_color, LColor_color) {} diff --git a/src/frontends/qt3/QLPainter.C b/src/frontends/qt3/QLPainter.C index 7c788193b6..8bf7c274bc 100644 --- a/src/frontends/qt3/QLPainter.C +++ b/src/frontends/qt3/QLPainter.C @@ -159,21 +159,21 @@ void QLPainter::image(int x, int y, int w, int h, } -void QLPainter::text(int x, int y, docstring const & s, LyXFont const & f) +int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f) { lyxerr << "Drawing string" << endl; return text(x, y, reinterpret_cast(s.data()), s.length(), f); } -void QLPainter::text(int x, int y, lyx::char_type c, LyXFont const & f) +int QLPainter::text(int x, int y, lyx::char_type c, LyXFont const & f) { char_type s[2] = { c, L'\0' }; return text(x, y, s, 1, f); } -void QLPainter::smallCapsText(int x, int y, +int QLPainter::smallCapsText(int x, int y, QString const & s, LyXFont const & f) { LyXFont smallfont(f); @@ -184,25 +184,27 @@ void QLPainter::smallCapsText(int x, int y, QFontMetrics const & qfontm = QFontMetrics(qfont); QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont); - int tmpx = x; size_t ls = s.length(); + int textwidth = 0; for (size_t i = 0; i < ls; ++i) { // Brain-dead MSVC wants at(i) rather than operator[] QChar const c = s.at(i).upper(); if (c != s.at(i)) { qp_->setFont(qsmallfont); - qp_->drawText(tmpx, y, c); - tmpx += qsmallfontm.width(c); + qp_->drawText(x + textwidth, y, c); + textwidth += qsmallfontm.width(c); } else { qp_->setFont(qfont); - qp_->drawText(tmpx, y, c); - tmpx += qfontm.width(c); + qp_->drawText(x + textwidth, y, c); + textwidth += qfontm.width(c); } } + + return textwidth; } -void QLPainter::text(int x, int y, lyx::char_type const * s, size_t ls, +int QLPainter::text(int x, int y, lyx::char_type const * s, size_t ls, LyXFont const & f) { lyxerr << "Drawing lyx::char_type const * s" << endl; @@ -235,18 +237,23 @@ void QLPainter::text(int x, int y, lyx::char_type const * s, size_t ls, str = ' ' + str; #endif + int textwidth; + if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) { qp_->setFont(fontloader.get(f)); // We need to draw the text as LTR as we use our own bidi // code. qp_->drawText(x, y, str, -1, QPainter::LTR); + textwidth = qp_->fontMetrics().width(str); } else { - smallCapsText(x, y, str, f); + textwidth = smallCapsText(x, y, str, f); } if (f.underbar() == LyXFont::ON) { - underline(f, x, y, qp_->fontMetrics().width(str)); + underline(f, x, y, textwidth); } + + return textwidth; } } // namespace frontend diff --git a/src/frontends/qt3/QLPainter.h b/src/frontends/qt3/QLPainter.h index ade005a79c..1c6a91f92b 100644 --- a/src/frontends/qt3/QLPainter.h +++ b/src/frontends/qt3/QLPainter.h @@ -98,22 +98,22 @@ public: lyx::graphics::Image const & image); /// draw a string at position x, y (y is the baseline) - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::docstring const & str, LyXFont const & f); /** Draw a string at position x, y (y is the baseline) * This is just for fast drawing */ - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::char_type const * str, size_t l, LyXFont const & f); /// draw a char at position x, y (y is the baseline) - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::char_type c, LyXFont const & f); private: /// draw small caps text - void smallCapsText(int x, int y, + int smallCapsText(int x, int y, QString const & str, LyXFont const & f); /// set pen parameters diff --git a/src/frontends/qt4/QLPainter.C b/src/frontends/qt4/QLPainter.C index 92744f0d2a..9bd17c44b4 100644 --- a/src/frontends/qt4/QLPainter.C +++ b/src/frontends/qt4/QLPainter.C @@ -183,13 +183,13 @@ void QLPainter::image(int x, int y, int w, int h, } -void QLPainter::text(int x, int y, docstring const & s, LyXFont const & f) +int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f) { return text(x, y, reinterpret_cast(s.data()), s.length(), f); } -void QLPainter::text(int x, int y, char_type c, LyXFont const & f) +int QLPainter::text(int x, int y, char_type c, LyXFont const & f) { char_type s[2] = { c, char_type('\0') }; return text(x, y, s, 1, f); @@ -222,7 +222,7 @@ int QLPainter::smallCapsText(int x, int y, } -void QLPainter::text(int x, int y, char_type const * s, size_t ls, +int QLPainter::text(int x, int y, char_type const * s, size_t ls, LyXFont const & f) { #if 0 @@ -260,6 +260,7 @@ void QLPainter::text(int x, int y, char_type const * s, size_t ls, underline(f, x, y, textwidth); } + return textwidth; } diff --git a/src/frontends/qt4/QLPainter.h b/src/frontends/qt4/QLPainter.h index f53b0e67c9..145e974c56 100644 --- a/src/frontends/qt4/QLPainter.h +++ b/src/frontends/qt4/QLPainter.h @@ -103,18 +103,18 @@ public: lyx::graphics::Image const & image); /// draw a string at position x, y (y is the baseline) - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::docstring const & str, LyXFont const & f); /** Draw a string at position x, y (y is the baseline) * This is just for fast drawing */ - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::char_type const * str, size_t l, LyXFont const & f); /// draw a char at position x, y (y is the baseline) - virtual void text(int x, int y, + virtual int text(int x, int y, lyx::char_type c, LyXFont const & f); /// draw a pixmap from the image cache diff --git a/src/rowpainter.C b/src/rowpainter.C index bb22359928..10ecef35b9 100644 --- a/src/rowpainter.C +++ b/src/rowpainter.C @@ -316,12 +316,11 @@ void RowPainter::paintChars(pos_type & vpos, LyXFont font, // Draw text and set the new x position //lyxerr << "paint row: yo_ " << yo_ << "\n"; #if 0 - pain_.text(int(x_), yo_, str, font); - x_ += theApp->fontLoader().metrics(font).width(str); + int width = pain_.text(int(x_), yo_, str, font); #else - pain_.text(int(x_), yo_, &str[0], str.size(), font); - x_ += theApp->fontLoader().metrics(font).width(&str[0], str.size()); + int width = pain_.text(int(x_), yo_, &str[0], str.size(), font); #endif + x_ += width; } -- 2.39.2