diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-06 16:50:53 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-06 16:50:53 +0000 |
commit | 161931972da4ddd4f8928741ac561014585a6df1 (patch) | |
tree | fa36f817bcdd96126ceb0ec28303781c1231af6d /chrome/common/gfx | |
parent | fba17802ccc482b917ee51776a0b54007b6e58f2 (diff) | |
download | chromium_src-161931972da4ddd4f8928741ac561014585a6df1.zip chromium_src-161931972da4ddd4f8928741ac561014585a6df1.tar.gz chromium_src-161931972da4ddd4f8928741ac561014585a6df1.tar.bz2 |
Better Linux ChromeCanvas text drawing, using Pango.
Skia doesn't current support any form of text layout (ellipsizing, etc). We
use Pango / Cairo to draw our text on the surface. Also moved the non-flags
version of DrawStringInt into the share chrome_canvas.cc. Stubbed out the
SizeStringInt(), I haven't seen it used on Linux, and it would be wrong.
Review URL: http://codereview.chromium.org/62053
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13159 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/gfx')
-rw-r--r-- | chrome/common/gfx/chrome_canvas.cc | 9 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas_linux.cc | 107 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas_skia.cc | 65 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas_win.cc | 9 |
4 files changed, 116 insertions, 74 deletions
diff --git a/chrome/common/gfx/chrome_canvas.cc b/chrome/common/gfx/chrome_canvas.cc index f439680..647983e 100644 --- a/chrome/common/gfx/chrome_canvas.cc +++ b/chrome/common/gfx/chrome_canvas.cc @@ -206,6 +206,15 @@ void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, drawRect(dest_rect, p); } +void ChromeCanvas::DrawStringInt(const std::wstring& text, + const ChromeFont& font, + const SkColor& color, + int x, int y, + int w, int h) { + DrawStringInt(text, font, color, x, y, w, h, + l10n_util::DefaultCanvasTextAlignment()); +} + void ChromeCanvas::TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h) { TileImageInt(bitmap, 0, 0, x, y, w, h); diff --git a/chrome/common/gfx/chrome_canvas_linux.cc b/chrome/common/gfx/chrome_canvas_linux.cc new file mode 100644 index 0000000..93a32f7 --- /dev/null +++ b/chrome/common/gfx/chrome_canvas_linux.cc @@ -0,0 +1,107 @@ +// Copyright (c) 2009 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 "chrome/common/gfx/chrome_canvas.h" + +#include <pango/pango.h> + +#include "base/gfx/rect.h" +#include "base/logging.h" +#include "base/string_util.h" +#include "chrome/common/gfx/chrome_font.h" + +namespace { + +// Returns a new pango font, free with pango_font_description_free(). +PangoFontDescription* PangoFontFromChromeFont(const ChromeFont& chrome_font) { + ChromeFont font = chrome_font; // Copy so we can call non-const methods. + PangoFontDescription* pfd = pango_font_description_new(); + pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); + pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); + + switch (font.style()) { + case ChromeFont::NORMAL: + // Nothing to do, should already be PANGO_STYLE_NORMAL. + break; + case ChromeFont::BOLD: + pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); + break; + case ChromeFont::ITALIC: + pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); + break; + case ChromeFont::UNDERLINED: + // TODO(deanm): How to do underlined? Where do we use it? Probably have + // to paint it ourselves, see pango_font_metrics_get_underline_position. + break; + } + + return pfd; +} + +} // namespace + +ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) + : skia::PlatformCanvasLinux(width, height, is_opaque) { +} + +ChromeCanvas::ChromeCanvas() : skia::PlatformCanvasLinux() { +} + +ChromeCanvas::~ChromeCanvas() { +} + +// static +void ChromeCanvas::SizeStringInt(const std::wstring& text, + const ChromeFont& font, + int* width, int* height, int flags) { + NOTIMPLEMENTED(); +} + + +void ChromeCanvas::DrawStringInt(const std::wstring& text, + const ChromeFont& font, + const SkColor& color, int x, int y, int w, + int h, int flags) { + cairo_surface_t* surface = beginPlatformPaint(); + cairo_t* cr = cairo_create(surface); + PangoLayout* layout = pango_cairo_create_layout(cr); + + cairo_set_source_rgb(cr, + SkColorGetR(color) / 255.0, + SkColorGetG(color) / 255.0, + SkColorGetB(color) / 255.0); + + // TODO(deanm): Implement the rest of the ChromeCanvas flags. + if (!(flags & ChromeCanvas::NO_ELLIPSIS)) + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); + + pango_layout_set_width(layout, w * PANGO_SCALE); + pango_layout_set_height(layout, h * PANGO_SCALE); + + std::string utf8 = WideToUTF8(text); + pango_layout_set_text(layout, utf8.data(), utf8.size()); + + PangoFontDescription* desc = PangoFontFromChromeFont(font); + pango_layout_set_font_description(layout, desc); + pango_font_description_free(desc); + + int width, height; + pango_layout_get_size(layout, &width, &height); + + if (flags & ChromeCanvas::TEXT_VALIGN_TOP) { + // Cairo should draw from the top left corner already. + } else if (flags & ChromeCanvas::TEXT_VALIGN_BOTTOM) { + y = y + (h - (height / PANGO_SCALE)); + } else { + // Vertically centered. + y = y + ((h - (height / PANGO_SCALE)) / 2); + } + + cairo_move_to(cr, x, y); + pango_cairo_show_layout(cr, layout); + + g_object_unref(layout); + cairo_destroy(cr); + // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. +} diff --git a/chrome/common/gfx/chrome_canvas_skia.cc b/chrome/common/gfx/chrome_canvas_skia.cc deleted file mode 100644 index b3b64d7..0000000 --- a/chrome/common/gfx/chrome_canvas_skia.cc +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2006-2008 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 "chrome/common/gfx/chrome_canvas.h" - -#include <limits> - -#include "base/gfx/rect.h" -#include "base/sys_string_conversions.h" -#include "skia/include/SkShader.h" -#include "skia/include/SkPaint.h" -#include "chrome/common/gfx/chrome_font.h" -#include "chrome/common/l10n_util.h" - -ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) - : skia::PlatformCanvasLinux(width, height, is_opaque) { -} - -ChromeCanvas::ChromeCanvas() : skia::PlatformCanvasLinux() { -} - -ChromeCanvas::~ChromeCanvas() { -} - -// static -void ChromeCanvas::SizeStringInt(const std::wstring& text, - const ChromeFont& font, - int* width, int* height, int flags) { - // TODO(port): @flags is currently ignored - SkPaint paint; - font.PaintSetup(&paint); - - const std::string utf8(base::SysWideToUTF8(text)); - paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); - SkRect bounds; - paint.measureText(utf8.data(), utf8.size(), &bounds); - - *width = bounds.width(); - *height = bounds.height(); -} - -void ChromeCanvas::DrawStringInt(const std::wstring& text, - const ChromeFont& font, - const SkColor& color, int x, int y, int w, - int h, int flags) { - // TODO(port): @flags, @w and @h are currently ignored - SkPaint paint; - font.PaintSetup(&paint); - - const std::string utf8(base::SysWideToUTF8(text)); - paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); - paint.setColor(color); - - drawText(utf8.data(), utf8.size(), x, y, paint); -} - -void ChromeCanvas::DrawStringInt(const std::wstring& text, - const ChromeFont& font, - const SkColor& color, - int x, int y, - int w, int h) { - DrawStringInt(text, font, color, x, y, w, h, - l10n_util::DefaultCanvasTextAlignment()); -} diff --git a/chrome/common/gfx/chrome_canvas_win.cc b/chrome/common/gfx/chrome_canvas_win.cc index f6db09b..38446ca 100644 --- a/chrome/common/gfx/chrome_canvas_win.cc +++ b/chrome/common/gfx/chrome_canvas_win.cc @@ -182,15 +182,6 @@ void ChromeCanvas::DrawStringInt(const std::wstring& text, HFONT font, void ChromeCanvas::DrawStringInt(const std::wstring& text, const ChromeFont& font, const SkColor& color, - int x, int y, - int w, int h) { - DrawStringInt(text, font, color, x, y, w, h, - l10n_util::DefaultCanvasTextAlignment()); -} - -void ChromeCanvas::DrawStringInt(const std::wstring& text, - const ChromeFont& font, - const SkColor& color, int x, int y, int w, int h, int flags) { DrawStringInt(text, font.hfont(), color, x, y, w, h, flags); } |