diff options
author | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-09 00:35:05 +0000 |
---|---|---|
committer | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-09 00:35:05 +0000 |
commit | 67b98156a64fcb8b70ad67006e4d8f53ec5df668 (patch) | |
tree | 3d2ac316c8215c6df36cd541b01b7f2ac94e1a6c /ui/gfx/render_text.cc | |
parent | de09a216e6dcb5b5cd84f422ed1287d6b4cb026f (diff) | |
download | chromium_src-67b98156a64fcb8b70ad67006e4d8f53ec5df668.zip chromium_src-67b98156a64fcb8b70ad67006e4d8f53ec5df668.tar.gz chromium_src-67b98156a64fcb8b70ad67006e4d8f53ec5df668.tar.bz2 |
Draw text via Skia in RenderTextLinux.
Refactors some common Skia drawing code into RenderText internal
class SkiaTextRenderer. Rewrite RenderTextLinux::DrawVisualText()
to use SkiaTextRenderer and refactor RenderTextWin::DrawVisualText()
to also use SkiaTextRenderer, to re-use code.
BUG=103648
TEST=Run Linux views_examples_exe --use-pure-views. In the textfield
example tab, the text field should draw its text correctly.
Review URL: http://codereview.chromium.org/8725002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113706 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/render_text.cc')
-rw-r--r-- | ui/gfx/render_text.cc | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc index 27ff71e..10e6590 100644 --- a/ui/gfx/render_text.cc +++ b/ui/gfx/render_text.cc @@ -9,6 +9,7 @@ #include "base/i18n/break_iterator.h" #include "base/logging.h" #include "base/stl_util.h" +#include "third_party/skia/include/core/SkTypeface.h" #include "ui/gfx/canvas.h" #include "ui/gfx/canvas_skia.h" #include "unicode/uchar.h" @@ -79,6 +80,79 @@ void ApplyStyleRangeImpl(gfx::StyleRanges* style_ranges, namespace gfx { +namespace internal { + +SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) + : canvas_skia_(canvas->GetSkCanvas()) { + DCHECK(canvas_skia_); + paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + paint_.setStyle(SkPaint::kFill_Style); + paint_.setAntiAlias(true); + paint_.setSubpixelText(true); + paint_.setLCDRenderText(true); +} + +SkiaTextRenderer::~SkiaTextRenderer() { +} + +void SkiaTextRenderer::SetFont(const gfx::Font& font) { + SkAutoTUnref<SkTypeface> typeface( + SkTypeface::CreateFromName(font.GetFontName().c_str(), + SkTypeface::kNormal)); + if (typeface.get()) { + // |paint_| adds its own ref. So don't |release()| it from the ref ptr here. + paint_.setTypeface(typeface.get()); + } + paint_.setTextSize(font.GetFontSize()); +} + +void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { + paint_.setColor(foreground); +} + +void SkiaTextRenderer::DrawPosText(const SkPoint* pos, + const uint16* glyphs, + size_t glyph_count) { + size_t byte_length = glyph_count * sizeof(glyphs[0]); + canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); +} + +// Draw underline and strike through text decorations. +// Based on |SkCanvas::DrawTextDecorations()| and constants from: +// third_party/skia/src/core/SkTextFormatParams.h +void SkiaTextRenderer::DrawDecorations(int x, int y, int width, + bool underline, bool strike) { + // Fraction of the text size to lower a strike through below the baseline. + const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); + // Fraction of the text size to lower an underline below the baseline. + const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); + // Fraction of the text size to use for a strike through or under-line. + const SkScalar kLineThickness = (SK_Scalar1 / 18); + + SkScalar text_size = paint_.getTextSize(); + SkScalar height = SkScalarMul(text_size, kLineThickness); + SkRect r; + + r.fLeft = x; + r.fRight = x + width; + + if (underline) { + SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y); + r.fTop = offset; + r.fBottom = offset + height; + canvas_skia_->drawRect(r, paint_); + } + if (strike) { + SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); + r.fTop = offset; + r.fBottom = offset + height; + canvas_skia_->drawRect(r, paint_); + } +} + +} // namespace internal + + StyleRange::StyleRange() : font(), foreground(SK_ColorBLACK), @@ -555,6 +629,18 @@ Point RenderText::ToViewPoint(const Point& point) { return p; } +Point RenderText::GetOriginForSkiaDrawing() { + Point origin(ToViewPoint(Point())); + // TODO(msw): Establish a vertical baseline for strings of mixed font heights. + const Font& font = default_style().font; + size_t height = font.GetHeight(); + // Center the text vertically in the display area. + origin.Offset(0, (display_rect().height() - height) / 2); + // Offset by the font size to account for Skia expecting y to be the bottom. + origin.Offset(0, font.GetFontSize()); + return origin; +} + void RenderText::MoveCursorTo(size_t position, bool select) { size_t cursor = std::min(position, text().length()); size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |