summaryrefslogtreecommitdiffstats
path: root/views/controls/textfield/text_style.cc
blob: 18400c6d720db68c191b1732da7ca5a685a9feec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/controls/textfield/text_style.h"

#include "ui/gfx/canvas.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/font.h"

namespace {
// Text color for read only.
const SkColor kReadonlyTextColor = SK_ColorDKGRAY;

// Strike line width.
const int kStrikeWidth = 2;
}

namespace views {

TextStyle::TextStyle()
    : foreground_(SK_ColorBLACK),
      strike_(false),
      underline_(false) {
}

TextStyle::~TextStyle() {
}

void TextStyle::DrawString(gfx::Canvas* canvas,
                           string16& text,
                           gfx::Font& base_font,
                           bool readonly,
                           int x, int y, int width, int height) const {
  SkColor text_color = readonly ? kReadonlyTextColor : foreground_;

  gfx::Font font = underline_ ?
      base_font.DeriveFont(0, base_font.GetStyle() | gfx::Font::UNDERLINED) :
      base_font;
  canvas->DrawStringInt(text, font, text_color, x, y, width, height);
  if (strike_) {
    SkPaint paint;
    paint.setAntiAlias(true);
    paint.setStyle(SkPaint::kFill_Style);
    paint.setColor(text_color);
    paint.setStrokeWidth(kStrikeWidth);
    canvas->AsCanvasSkia()->drawLine(
        SkIntToScalar(x), SkIntToScalar(y + height),
        SkIntToScalar(x + width), SkIntToScalar(y),
        paint);
  }
}

}  // namespace views