summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authordavemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-20 21:05:28 +0000
committerdavemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-20 21:05:28 +0000
commitf4536d718822a03c571b2c989586d55122c8eaea (patch)
tree2f4f7d419697a3fcaa9f62f9ecbf3e599ecb1c0d /app
parent609b52dc06d5f1c6d35bf743c0281cf629022934 (diff)
downloadchromium_src-f4536d718822a03c571b2c989586d55122c8eaea.zip
chromium_src-f4536d718822a03c571b2c989586d55122c8eaea.tar.gz
chromium_src-f4536d718822a03c571b2c989586d55122c8eaea.tar.bz2
Unified the ways Font and Canvas get the pixel size of strings, using pango
Review URL: http://codereview.chromium.org/115586 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16535 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r--app/gfx/canvas_linux.cc34
-rw-r--r--app/gfx/font_skia.cc19
2 files changed, 38 insertions, 15 deletions
diff --git a/app/gfx/canvas_linux.cc b/app/gfx/canvas_linux.cc
index feb5b7f..3b363f8 100644
--- a/app/gfx/canvas_linux.cc
+++ b/app/gfx/canvas_linux.cc
@@ -57,7 +57,39 @@ Canvas::~Canvas() {
void Canvas::SizeStringInt(const std::wstring& text,
const gfx::Font& font,
int* width, int* height, int flags) {
- NOTIMPLEMENTED();
+ cairo_surface_t* surface =
+ cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
+ cairo_t* cr = cairo_create(surface);
+ PangoLayout* layout = pango_cairo_create_layout(cr);
+
+ if (flags & NO_ELLIPSIS) {
+ pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
+ } else {
+ pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
+ }
+
+ if (flags & TEXT_ALIGN_CENTER) {
+ pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
+ } else if (flags & TEXT_ALIGN_RIGHT) {
+ pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
+ }
+
+ if (flags & MULTI_LINE) {
+ pango_layout_set_wrap(layout,
+ (flags & CHARACTER_BREAK) ? PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD);
+ }
+
+ std::string utf8 = WideToUTF8(text);
+ pango_layout_set_text(layout, utf8.data(), utf8.size());
+ PangoFontDescription* desc = PangoFontFromGfxFont(font);
+ pango_layout_set_font_description(layout, desc);
+
+ pango_layout_get_size(layout, width, height);
+ *width /= PANGO_SCALE;
+ *height /= PANGO_SCALE;
+ g_object_unref(layout);
+ pango_font_description_free(desc);
+ cairo_destroy(cr);
}
void Canvas::ApplySkiaMatrixToCairoContext(cairo_t* cr) {
diff --git a/app/gfx/font_skia.cc b/app/gfx/font_skia.cc
index 0b0b979..0443ace 100644
--- a/app/gfx/font_skia.cc
+++ b/app/gfx/font_skia.cc
@@ -4,6 +4,8 @@
#include "app/gfx/font.h"
+#include "app/gfx/canvas.h"
+
#include "base/logging.h"
#include "base/sys_string_conversions.h"
@@ -126,21 +128,10 @@ void Font::PaintSetup(SkPaint* paint) const {
}
int Font::GetStringWidth(const std::wstring& text) const {
- const std::string utf8(base::SysWideToUTF8(text));
-
- SkPaint paint;
- PaintSetup(&paint);
- paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
- SkScalar width = paint.measureText(utf8.data(), utf8.size());
-
- int breadth = static_cast<int>(ceilf(SkScalarToFloat(width)));
- // Check for overflow. We should probably be returning an unsigned
- // int, but in practice we'll never have a screen massive enough
- // to show that much text anyway.
- if (breadth < 0)
- return INT_MAX;
+ int width = 0, height = 0;
- return breadth;
+ Canvas::SizeStringInt(text, *this, &width, &height, 0);
+ return width;
}
int Font::GetExpectedTextWidth(int length) const {