Index: ui/gfx/render_text.cc |
=================================================================== |
--- ui/gfx/render_text.cc (revision 112492) |
+++ ui/gfx/render_text.cc (working copy) |
@@ -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" |
@@ -78,6 +79,83 @@ |
namespace gfx { |
+namespace internal { |
+ |
+SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
+ : canvas_skia_(canvas->GetSkCanvas()) { |
+ DCHECK(canvas_skia_); |
+} |
+ |
+SkiaTextRenderer::~SkiaTextRenderer() { |
+} |
+ |
+void SkiaTextRenderer::Init() { |
+ paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
+ paint_.setStyle(SkPaint::kFill_Style); |
+ paint_.setAntiAlias(true); |
+ paint_.setSubpixelText(true); |
+ paint_.setLCDRenderText(true); |
+} |
+ |
+void SkiaTextRenderer::SetFont(const gfx::Font& font) { |
+ SkTypeface* typeface = |
+ SkTypeface::CreateFromName(font.GetFontName().c_str(), |
+ SkTypeface::kNormal); |
+ if (typeface) { |
+ paint_.setTypeface(typeface); |
+ // |paint_| adds its own ref. Release the ref from |CreateFromName()|. |
+ typeface->unref(); |
+ } |
+ 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), |