blob: 2b8f39e6d21f6c386ccba5f9e4fe88ca5b4822a8 [file] [log] [blame]
[email protected]7c1e647e2012-01-14 01:07:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]6bf1a812009-07-11 01:57:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d7c919662012-01-12 20:15:585#import <Cocoa/Cocoa.h>
6
[email protected]94a0d2582012-03-09 00:30:597#include "ui/gfx/canvas.h"
[email protected]6bf1a812009-07-11 01:57:288
[email protected]7c1e647e2012-01-14 01:07:579#include "base/logging.h"
[email protected]d7c919662012-01-12 20:15:5810#include "base/sys_string_conversions.h"
[email protected]7c1e647e2012-01-14 01:07:5711#include "third_party/skia/include/core/SkTypeface.h"
[email protected]08397d52011-02-05 01:53:3812#include "ui/gfx/font.h"
[email protected]0834e1b12012-04-11 02:23:5613#include "ui/gfx/rect.h"
[email protected]7c1e647e2012-01-14 01:07:5714
15// Note: This is a temporary Skia-based implementation of the ui/gfx text
16// rendering routines for views/aura. It replaces the stale Cocoa-based
[email protected]62424a52012-03-18 03:09:5017// implementation. A future |canvas_skia.cc| implementation will supersede
[email protected]7c1e647e2012-01-14 01:07:5718// this and the other platform-specific implmenentations.
19// Most drawing options, such as alignment and multi-line, are not implemented
20// here.
21
22namespace {
23
24SkTypeface::Style FontTypefaceStyle(const gfx::Font& font) {
25 int style = 0;
26 if (font.GetStyle() & gfx::Font::BOLD)
27 style |= SkTypeface::kBold;
28 if (font.GetStyle() & gfx::Font::ITALIC)
29 style |= SkTypeface::kItalic;
30
31 return static_cast<SkTypeface::Style>(style);
32}
33
34} // namespace
[email protected]6bf1a812009-07-11 01:57:2835
36namespace gfx {
37
[email protected]6bf1a812009-07-11 01:57:2838// static
[email protected]3127f6632012-03-17 00:14:0639void Canvas::SizeStringInt(const string16& text,
40 const gfx::Font& font,
41 int* width,
42 int* height,
43 int flags) {
[email protected]d7c919662012-01-12 20:15:5844 NSFont* native_font = font.GetNativeFont();
45 NSString* ns_string = base::SysUTF16ToNSString(text);
46 NSDictionary* attributes =
47 [NSDictionary dictionaryWithObject:native_font
48 forKey:NSFontAttributeName];
49 NSSize string_size = [ns_string sizeWithAttributes:attributes];
50 *width = string_size.width;
51 *height = font.GetHeight();
[email protected]6bf1a812009-07-11 01:57:2852}
53
[email protected]0834e1b12012-04-11 02:23:5654void Canvas::DrawStringWithShadows(const string16& text,
55 const gfx::Font& font,
56 SkColor color,
57 const gfx::Rect& text_bounds,
58 int flags,
[email protected]25650682012-05-29 23:09:1959 const ShadowValues& shadows) {
[email protected]0834e1b12012-04-11 02:23:5660 DLOG_IF(WARNING, !shadows.empty()) << "Text shadow not implemented.";
61
[email protected]7c1e647e2012-01-14 01:07:5762 SkTypeface* typeface = SkTypeface::CreateFromName(font.GetFontName().c_str(),
63 FontTypefaceStyle(font));
64 SkPaint paint;
65 paint.setTypeface(typeface);
66 typeface->unref();
67 paint.setColor(color);
68 canvas_->drawText(text.c_str(),
69 text.size() * sizeof(string16::value_type),
[email protected]0834e1b12012-04-11 02:23:5670 text_bounds.x(),
71 text_bounds.bottom(),
[email protected]7c1e647e2012-01-14 01:07:5772 paint);
73}
[email protected]6bf1a812009-07-11 01:57:2874
[email protected]3127f6632012-03-17 00:14:0675void Canvas::DrawStringWithHalo(const string16& text,
76 const gfx::Font& font,
[email protected]1b81ff22012-03-24 22:04:4277 SkColor text_color,
78 SkColor halo_color,
[email protected]3127f6632012-03-17 00:14:0679 int x, int y, int w, int h,
80 int flags) {
[email protected]6bf1a812009-07-11 01:57:2881}
82
[email protected]6bf1a812009-07-11 01:57:2883} // namespace gfx