diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-06 00:39:23 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-06 00:39:23 +0000 |
commit | a5da0aa93ab70006854396439e47c795716664c2 (patch) | |
tree | 406ec34ecae757f3e43f813af40a21e8e9aeb9de /chrome/common/gfx | |
parent | acc9a6def1c4c7d8b54149667e0c476d0c85a8f4 (diff) | |
download | chromium_src-a5da0aa93ab70006854396439e47c795716664c2.zip chromium_src-a5da0aa93ab70006854396439e47c795716664c2.tar.gz chromium_src-a5da0aa93ab70006854396439e47c795716664c2.tar.bz2 |
Move chrome_canvas to app/gfx
http://crbug.com/11387
Review URL: http://codereview.chromium.org/113011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15361 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/gfx')
-rw-r--r-- | chrome/common/gfx/chrome_canvas.cc | 265 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas.h | 217 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas_linux.cc | 123 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas_win.cc | 264 |
4 files changed, 0 insertions, 869 deletions
diff --git a/chrome/common/gfx/chrome_canvas.cc b/chrome/common/gfx/chrome_canvas.cc deleted file mode 100644 index 328bf65..0000000 --- a/chrome/common/gfx/chrome_canvas.cc +++ /dev/null @@ -1,265 +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 "app/l10n_util.h" -#include "base/gfx/rect.h" -#include "base/logging.h" -#include "skia/include/SkShader.h" -#include "chrome/common/gfx/chrome_font.h" - -bool ChromeCanvas::GetClipRect(gfx::Rect* r) { - SkRect clip; - if (!getClipBounds(&clip)) { - if (r) - r->SetRect(0, 0, 0, 0); - return false; - } - r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop), - SkScalarRound(clip.fRight - clip.fLeft), - SkScalarRound(clip.fBottom - clip.fTop)); - return true; -} - -bool ChromeCanvas::ClipRectInt(int x, int y, int w, int h) { - SkRect new_clip; - new_clip.set(SkIntToScalar(x), SkIntToScalar(y), - SkIntToScalar(x + w), SkIntToScalar(y + h)); - return clipRect(new_clip); -} - -bool ChromeCanvas::IntersectsClipRectInt(int x, int y, int w, int h) { - SkRect clip; - return getClipBounds(&clip) && - clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), - SkIntToScalar(y + h)); -} - -void ChromeCanvas::TranslateInt(int x, int y) { - translate(SkIntToScalar(x), SkIntToScalar(y)); -} - -void ChromeCanvas::ScaleInt(int x, int y) { - scale(SkIntToScalar(x), SkIntToScalar(y)); -} - -void ChromeCanvas::FillRectInt(const SkColor& color, - int x, int y, int w, int h) { - SkPaint paint; - paint.setColor(color); - paint.setStyle(SkPaint::kFill_Style); - paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode); - FillRectInt(x, y, w, h, paint); -} - -void ChromeCanvas::FillRectInt(int x, int y, int w, int h, - const SkPaint& paint) { - SkIRect rc = {x, y, x + w, y + h}; - drawIRect(rc, paint); -} - -void ChromeCanvas::DrawRectInt(const SkColor& color, - int x, int y, int w, int h) { - DrawRectInt(color, x, y, w, h, SkPorterDuff::kSrcOver_Mode); -} - -void ChromeCanvas::DrawRectInt(const SkColor& color, - int x, int y, int w, int h, - SkPorterDuff::Mode mode) { - SkPaint paint; - paint.setColor(color); - paint.setStyle(SkPaint::kStroke_Style); - // Set a stroke width of 0, which will put us down the stroke rect path. If - // we set a stroke width of 1, for example, this will internally create a - // path and fill it, which causes problems near the edge of the canvas. - paint.setStrokeWidth(SkIntToScalar(0)); - paint.setPorterDuffXfermode(mode); - - SkIRect rc = {x, y, x + w, y + h}; - drawIRect(rc, paint); -} - -void ChromeCanvas::DrawLineInt(const SkColor& color, - int x1, int y1, int x2, int y2) { - SkPaint paint; - paint.setColor(color); - paint.setStrokeWidth(SkIntToScalar(1)); - drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), - SkIntToScalar(y2), paint); -} - -void ChromeCanvas::DrawFocusRect(int x, int y, int width, int height) { - // Create a 2D bitmap containing alternating on/off pixels - we do this - // so that you never get two pixels of the same color around the edges - // of the focus rect (this may mean that opposing edges of the rect may - // have a dot pattern out of phase to each other). - static SkBitmap* dots = NULL; - if (!dots) { - int col_pixels = 32; - int row_pixels = 32; - - dots = new SkBitmap; - dots->setConfig(SkBitmap::kARGB_8888_Config, col_pixels, row_pixels); - dots->allocPixels(); - dots->eraseARGB(0, 0, 0, 0); - - uint32_t* dot = dots->getAddr32(0, 0); - for (int i = 0; i < row_pixels; i++) { - for (int u = 0; u < col_pixels; u++) { - if ((u % 2 + i % 2) % 2 != 0) { - dot[i * row_pixels + u] = SK_ColorGRAY; - } - } - } - } - - // First the horizontal lines. - - // Make a shader for the bitmap with an origin of the box we'll draw. This - // shader is refcounted and will have an initial refcount of 1. - SkShader* shader = SkShader::CreateBitmapShader( - *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); - // Assign the shader to the paint & release our reference. The paint will - // now own the shader and the shader will be destroyed when the paint goes - // out of scope. - SkPaint paint; - paint.setShader(shader); - shader->unref(); - - SkRect rect; - rect.set(SkIntToScalar(x), SkIntToScalar(y), - SkIntToScalar(x + width), SkIntToScalar(y + 1)); - drawRect(rect, paint); - rect.set(SkIntToScalar(x), SkIntToScalar(y + height - 1), - SkIntToScalar(x + width), SkIntToScalar(y + height)); - drawRect(rect, paint); - - rect.set(SkIntToScalar(x), SkIntToScalar(y), - SkIntToScalar(x + 1), SkIntToScalar(y + height)); - drawRect(rect, paint); - rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y), - SkIntToScalar(x + width), SkIntToScalar(y + height)); - drawRect(rect, paint); -} - -void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { - drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); -} - -void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y, - const SkPaint& paint) { - drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); -} - -void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, - int src_w, int src_h, int dest_x, int dest_y, - int dest_w, int dest_h, - bool filter) { - SkPaint p; - DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, - dest_w, dest_h, filter, p); -} - -void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, - int src_w, int src_h, int dest_x, int dest_y, - int dest_w, int dest_h, - bool filter, const SkPaint& paint) { - DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && - src_y + src_h < std::numeric_limits<int16_t>::max()); - if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) { - NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!"; - return; - } - - if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) - return; - - SkRect dest_rect = { SkIntToScalar(dest_x), - SkIntToScalar(dest_y), - SkIntToScalar(dest_x + dest_w), - SkIntToScalar(dest_y + dest_h) }; - - if (src_w == dest_w && src_h == dest_h) { - // Workaround for apparent bug in Skia that causes image to occasionally - // shift. - SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; - drawBitmapRect(bitmap, &src_rect, dest_rect, &paint); - return; - } - - // Make a bitmap shader that contains the bitmap we want to draw. This is - // basically what SkCanvas.drawBitmap does internally, but it gives us - // more control over quality and will use the mipmap in the source image if - // it has one, whereas drawBitmap won't. - SkShader* shader = SkShader::CreateBitmapShader(bitmap, - SkShader::kRepeat_TileMode, - SkShader::kRepeat_TileMode); - SkMatrix shader_scale; - shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w), - SkFloatToScalar(static_cast<float>(dest_h) / src_h)); - shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); - shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); - shader->setLocalMatrix(shader_scale); - - // Set up our paint to use the shader & release our reference (now just owned - // by the paint). - SkPaint p(paint); - p.setFilterBitmap(filter); - p.setShader(shader); - shader->unref(); - - // The rect will be filled by the bitmap. - 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); -} - -void ChromeCanvas::TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, - int dest_x, int dest_y, int w, int h) { - if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) - return; - - SkPaint paint; - - SkShader* shader = SkShader::CreateBitmapShader(bitmap, - SkShader::kRepeat_TileMode, - SkShader::kRepeat_TileMode); - paint.setShader(shader); - paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode); - - // CreateBitmapShader returns a Shader with a reference count of one, we - // need to unref after paint takes ownership of the shader. - shader->unref(); - save(); - translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); - ClipRectInt(src_x, src_y, w, h); - drawPaint(paint); - restore(); -} - -SkBitmap ChromeCanvas::ExtractBitmap() { - const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); - - // Make a bitmap to return, and a canvas to draw into it. We don't just want - // to call extractSubset or the copy constuctor, since we want an actual copy - // of the bitmap. - SkBitmap result; - device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); - return result; -} diff --git a/chrome/common/gfx/chrome_canvas.h b/chrome/common/gfx/chrome_canvas.h deleted file mode 100644 index 72db8a5..0000000 --- a/chrome/common/gfx/chrome_canvas.h +++ /dev/null @@ -1,217 +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. - -#ifndef CHROME_COMMON_GFX_CHROME_CANVAS_H_ -#define CHROME_COMMON_GFX_CHROME_CANVAS_H_ - -#if defined(OS_WIN) -#include <windows.h> -#endif - -#include <string> - -#include "base/basictypes.h" -#include "skia/ext/platform_canvas.h" - -class ChromeFont; -namespace gfx { -class Rect; -} - -#if defined(OS_LINUX) -typedef struct _cairo cairo_t; -#endif - -// ChromeCanvas is the SkCanvas used by Views for all painting. It -// provides a handful of methods for the common operations used throughout -// Views. With few exceptions, you should NOT create a ChromeCanvas directly, -// rather one will be passed to you via the various paint methods in view. -// -// All methods that take integer arguments (as is used throughout views) -// end with Int. If you need to use methods provided by the superclass -// you'll need to do a conversion. In particular you'll need to use -// macro SkIntToScalar(xxx), or if converting from a scalar to an integer -// SkScalarRound. -// -// A handful of methods in this class are overloaded providing an additional -// argument of type SkPorterDuff::Mode. SkPorterDuff::Mode specifies how the -// source and destination colors are combined. Unless otherwise specified, -// the variant that does not take a SkPorterDuff::Mode uses a transfer mode -// of kSrcOver_Mode. -class ChromeCanvas : public skia::PlatformCanvas { - public: - // Specifies the alignment for text rendered with the DrawStringInt method. - enum { - TEXT_ALIGN_LEFT = 1, - TEXT_ALIGN_CENTER = 2, - TEXT_ALIGN_RIGHT = 4, - TEXT_VALIGN_TOP = 8, - TEXT_VALIGN_MIDDLE = 16, - TEXT_VALIGN_BOTTOM = 32, - - // Specifies the text consists of multiple lines. - MULTI_LINE = 64, - - // By default DrawStringInt does not process the prefix ('&') character - // specially. That is, the string "&foo" is rendered as "&foo". When - // rendering text from a resource that uses the prefix character for - // mnemonics, the prefix should be processed and can be rendered as an - // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). - SHOW_PREFIX = 128, - HIDE_PREFIX = 256, - - // Prevent ellipsizing - NO_ELLIPSIS = 512, - - // Specifies if words can be split by new lines. - // This only works with MULTI_LINE. - CHARACTER_BREAK = 1024, - }; - - // Creates an empty ChromeCanvas. Callers must use initialize before using - // the canvas. - ChromeCanvas(); - - ChromeCanvas(int width, int height, bool is_opaque); - - virtual ~ChromeCanvas(); - - // Retrieves the clip rectangle and sets it in the specified rectangle if any. - // Returns true if the clip rect is non-empty. - bool GetClipRect(gfx::Rect* clip_rect); - - // Wrapper function that takes integer arguments. - // Returns true if the clip is non-empty. - // See clipRect for specifics. - bool ClipRectInt(int x, int y, int w, int h); - - // Test whether the provided rectangle intersects the current clip rect. - bool IntersectsClipRectInt(int x, int y, int w, int h); - - // Wrapper function that takes integer arguments. - // See translate() for specifics. - void TranslateInt(int x, int y); - - // Wrapper function that takes integer arguments. - // See scale() for specifics. - void ScaleInt(int x, int y); - - // Fills the given rectangle with the given paint's parameters. - void FillRectInt(int x, int y, int w, int h, const SkPaint& paint); - - // Fills the specified region with the specified color using a transfer - // mode of SkPorterDuff::kSrcOver_Mode. - void FillRectInt(const SkColor& color, int x, int y, int w, int h); - - // Draws a single pixel rect in the specified region with the specified - // color, using a transfer mode of SkPorterDuff::kSrcOver_Mode. - // - // NOTE: if you need a single pixel line, use DraLineInt. - void DrawRectInt(const SkColor& color, int x, int y, int w, int h); - - // Draws a single pixel rect in the specified region with the specified - // color and transfer mode. - // - // NOTE: if you need a single pixel line, use DraLineInt. - void DrawRectInt(const SkColor& color, int x, int y, int w, int h, - SkPorterDuff::Mode mode); - - // Draws a single pixel line with the specified color. - void DrawLineInt(const SkColor& color, int x1, int y1, int x2, int y2); - - // Draws a bitmap with the origin at the specified location. The upper left - // corner of the bitmap is rendered at the specified location. - void DrawBitmapInt(const SkBitmap& bitmap, int x, int y); - - // Draws a bitmap with the origin at the specified location, using the - // specified paint. The upper left corner of the bitmap is rendered at the - // specified location. - void DrawBitmapInt(const SkBitmap& bitmap, int x, int y, - const SkPaint& paint); - - // Draws a portion of a bitmap in the specified location. The src parameters - // correspond to the region of the bitmap to draw in the region defined - // by the dest coordinates. - // - // If the width or height of the source differs from that of the destination, - // the bitmap will be scaled. When scaling down, it is highly recommended - // that you call buildMipMap(false) on your bitmap to ensure that it has - // a mipmap, which will result in much higher-quality output. Set |filter| - // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm - // is used for resampling. - // - // An optional custom SkPaint can be provided. - void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, int src_w, - int src_h, int dest_x, int dest_y, int dest_w, int dest_h, - bool filter); - void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, int src_w, - int src_h, int dest_x, int dest_y, int dest_w, int dest_h, - bool filter, const SkPaint& paint); - - // Draws text with the specified color, font and location. The text is - // aligned to the left, vertically centered, clipped to the region. If the - // text is too big, it is truncated and '...' is added to the end. - void DrawStringInt(const std::wstring& text, const ChromeFont& font, - const SkColor& color, int x, int y, int w, int h); - - // Draws text with the specified color, font and location. The last argument - // specifies flags for how the text should be rendered. It can be one of - // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. - void DrawStringInt(const std::wstring& text, const ChromeFont& font, - const SkColor& color, int x, int y, int w, int h, - int flags); - -#ifdef OS_WIN // Only implemented on Windows for now. - // Draws text with a 1-pixel halo around it of the given color. It allows - // ClearType to be drawn to an otherwise transparenct bitmap for drag images. - // Drag images have only 1-bit of transparency, so we don't do any fancy - // blurring. - void DrawStringWithHalo(const std::wstring& text, const ChromeFont& font, - const SkColor& text_color, const SkColor& halo_color, - int x, int y, int w, int h, int flags); -#endif - - // Draws a dotted gray rectangle used for focus purposes. - void DrawFocusRect(int x, int y, int width, int height); - - // Tiles the image in the specified region. - void TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h); - void TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, - int dest_x, int dest_y, int w, int h); - - // Extracts a bitmap from the contents of this canvas. - SkBitmap ExtractBitmap(); - -#if defined(OS_LINUX) - // Applies current matrix on the canvas to the cairo context. This should be - // invoked anytime you plan on drawing directly to the cairo context. Be - // sure and set the matrix back to the identity when done. - void ApplySkiaMatrixToCairoContext(cairo_t* cr); -#endif - - // Compute the size required to draw some text with the provided font. - // Attempts to fit the text with the provided width and height. Increases - // height and then width as needed to make the text fit. This method - // supports multiple lines. - static void SizeStringInt(const std::wstring& test, const ChromeFont& font, - int *width, int* height, int flags); - - private: -#if defined(OS_WIN) - // Draws text with the specified color, font and location. The text is - // aligned to the left, vertically centered, clipped to the region. If the - // text is too big, it is truncated and '...' is added to the end. - void DrawStringInt(const std::wstring& text, HFONT font, - const SkColor& color, int x, int y, int w, int h, - int flags); -#endif - - DISALLOW_EVIL_CONSTRUCTORS(ChromeCanvas); -}; - -#if defined(OS_WIN) || defined(OS_LINUX) -typedef skia::CanvasPaintT<ChromeCanvas> ChromeCanvasPaint; -#endif - -#endif // CHROME_COMMON_GFX_CHROME_CANVAS_H_ diff --git a/chrome/common/gfx/chrome_canvas_linux.cc b/chrome/common/gfx/chrome_canvas_linux.cc deleted file mode 100644 index 5f8b430..0000000 --- a/chrome/common/gfx/chrome_canvas_linux.cc +++ /dev/null @@ -1,123 +0,0 @@ -// 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::ApplySkiaMatrixToCairoContext(cairo_t* cr) { - const SkMatrix& skia_matrix = getTotalMatrix(); - cairo_matrix_t cairo_matrix; - cairo_matrix_init(&cairo_matrix, - SkScalarToFloat(skia_matrix.getScaleX()), - SkScalarToFloat(skia_matrix.getSkewY()), - SkScalarToFloat(skia_matrix.getSkewX()), - SkScalarToFloat(skia_matrix.getScaleY()), - SkScalarToFloat(skia_matrix.getTranslateX()), - SkScalarToFloat(skia_matrix.getTranslateY())); - cairo_set_matrix(cr, &cairo_matrix); -} - -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); - // We're going to draw onto the surface directly. This circumvents the matrix - // installed by Skia. Apply the matrix from skia to cairo so they align and - // we draw at the right place. - ApplySkiaMatrixToCairoContext(cr); - 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_win.cc b/chrome/common/gfx/chrome_canvas_win.cc deleted file mode 100644 index aefb640..0000000 --- a/chrome/common/gfx/chrome_canvas_win.cc +++ /dev/null @@ -1,264 +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 "app/l10n_util.h" -#include "base/gfx/rect.h" -#include "skia/include/SkShader.h" -#include "chrome/common/gfx/chrome_font.h" - -namespace { - -// We make sure that LTR text we draw in an RTL context is modified -// appropriately to make sure it maintains it LTR orientation. -void DoDrawText(HDC hdc, const std::wstring& text, - RECT* text_bounds, int flags) { - std::wstring localized_text; - const wchar_t* string_ptr = text.c_str(); - int string_size = static_cast<int>(text.length()); - // Only adjust string directionality if both of the following are true: - // 1. The current locale is RTL. - // 2. The string itself has RTL directionality. - if (flags & DT_RTLREADING) { - if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) { - string_ptr = localized_text.c_str(); - string_size = static_cast<int>(localized_text.length()); - } - } - - DrawText(hdc, string_ptr, string_size, text_bounds, flags); -} - -// Compute the windows flags necessary to implement the provided text -// ChromeCanvas flags. -int ComputeFormatFlags(int flags, const std::wstring& text) { - int f = 0; - - // Setting the text alignment explicitly in case it hasn't already been set. - // This will make sure that we don't align text to the left on RTL locales - // just because no alignment flag was passed to DrawStringInt(). - if (!(flags & (ChromeCanvas::TEXT_ALIGN_CENTER | - ChromeCanvas::TEXT_ALIGN_RIGHT | - ChromeCanvas::TEXT_ALIGN_LEFT))) { - flags |= l10n_util::DefaultCanvasTextAlignment(); - } - - if (flags & ChromeCanvas::HIDE_PREFIX) - f |= DT_HIDEPREFIX; - else if ((flags & ChromeCanvas::SHOW_PREFIX) == 0) - f |= DT_NOPREFIX; - - if (flags & ChromeCanvas::MULTI_LINE) { - f |= DT_WORDBREAK; - if (flags & ChromeCanvas::CHARACTER_BREAK) - f |= DT_EDITCONTROL; - } else { - f |= DT_SINGLELINE | DT_VCENTER; - if (!(flags & ChromeCanvas::NO_ELLIPSIS)) - f |= DT_END_ELLIPSIS; - } - - // vertical alignment - if (flags & ChromeCanvas::TEXT_VALIGN_TOP) - f |= DT_TOP; - else if (flags & ChromeCanvas::TEXT_VALIGN_BOTTOM) - f |= DT_BOTTOM; - else - f |= DT_VCENTER; - - // horizontal alignment - if (flags & ChromeCanvas::TEXT_ALIGN_CENTER) - f |= DT_CENTER; - else if (flags & ChromeCanvas::TEXT_ALIGN_RIGHT) - f |= DT_RIGHT; - else - f |= DT_LEFT; - - // In order to make sure RTL/BiDi strings are rendered correctly, we must - // pass the flag DT_RTLREADING to DrawText (when the locale's language is - // a right-to-left language) so that Windows does the right thing. - // - // In addition to correctly displaying text containing both RTL and LTR - // elements (for example, a string containing a telephone number within a - // sentence in Hebrew, or a sentence in Hebrew that contains a word in - // English) this flag also makes sure that if there is not enough space to - // display the entire string, the ellipsis is displayed on the left hand side - // of the truncated string and not on the right hand side. - // - // We make a distinction between Chrome UI strings and text coming from a web - // page. - // - // For text coming from a web page we determine the alignment based on the - // first character with strong directionality. If the directionality of the - // first character with strong directionality in the text is LTR, the - // alignment is set to DT_LEFT, and the directionality should not be set as - // DT_RTLREADING. - // - // This heuristic doesn't work for Chrome UI strings since even in RTL - // locales, some of those might start with English text but we know they're - // localized so we always want them to be right aligned, and their - // directionality should be set as DT_RTLREADING. - // - // Caveat: If the string is purely LTR, don't set DTL_RTLREADING since when - // the flag is set, LRE-PDF don't have the desired effect of rendering - // multiline English-only text as LTR. - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT && - (f & DT_RIGHT)) { - if (l10n_util::StringContainsStrongRTLChars(text)) { - f |= DT_RTLREADING; - } - } - return f; -} - -} // anonymous namespace - -ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) - : skia::PlatformCanvasWin(width, height, is_opaque) { -} - -ChromeCanvas::ChromeCanvas() : skia::PlatformCanvasWin() { -} - -ChromeCanvas::~ChromeCanvas() { -} - -// static -void ChromeCanvas::SizeStringInt(const std::wstring& text, - const ChromeFont& font, - int *width, int *height, int flags) { - HDC dc = GetDC(NULL); - HFONT old_font = static_cast<HFONT>(SelectObject(dc, font.hfont())); - RECT b; - b.left = 0; - b.top = 0; - b.right = *width; - if (b.right == 0 && !text.empty()) { - // Width needs to be at least 1 or else DoDrawText will not resize it. - b.right = 1; - } - b.bottom = *height; - DoDrawText(dc, text, &b, ComputeFormatFlags(flags, text) | DT_CALCRECT); - - // Restore the old font. This way we don't have to worry if the caller - // deletes the font and the DC lives longer. - SelectObject(dc, old_font); - *width = b.right; - *height = b.bottom; - - ReleaseDC(NULL, dc); -} - -void ChromeCanvas::DrawStringInt(const std::wstring& text, HFONT font, - const SkColor& color, int x, int y, int w, - int h, int flags) { - if (!IntersectsClipRectInt(x, y, w, h)) - return; - - RECT text_bounds = { x, y, x + w, y + h }; - HDC dc = beginPlatformPaint(); - SetBkMode(dc, TRANSPARENT); - HFONT old_font = (HFONT)SelectObject(dc, font); - COLORREF brush_color = RGB(SkColorGetR(color), SkColorGetG(color), - SkColorGetB(color)); - SetTextColor(dc, brush_color); - - int f = ComputeFormatFlags(flags, text); - DoDrawText(dc, text, &text_bounds, f); - endPlatformPaint(); - - // Restore the old font. This way we don't have to worry if the caller - // deletes the font and the DC lives longer. - SelectObject(dc, old_font); - - // Windows will have cleared the alpha channel of the text we drew. Assume - // we're drawing to an opaque surface, or at least the text rect area is - // opaque. - getTopPlatformDevice().makeOpaque(x, y, w, h); -} - -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); -} - -// Checks each pixel immediately adjacent to the given pixel in the bitmap. If -// any of them are not the halo color, returns true. This defines the halo of -// pixels that will appear around the text. Note that we have to check each -// pixel against both the halo color and transparent since DrawStringWithHalo -// will modify the bitmap as it goes, and clears pixels shouldn't count as -// changed. -static bool pixelShouldGetHalo(const SkBitmap& bitmap, int x, int y, - SkColor halo_color) { - if (x > 0 && - *bitmap.getAddr32(x - 1, y) != halo_color && - *bitmap.getAddr32(x - 1, y) != 0) - return true; // Touched pixel to the left. - if (x < bitmap.width() - 1 && - *bitmap.getAddr32(x + 1, y) != halo_color && - *bitmap.getAddr32(x + 1, y) != 0) - return true; // Touched pixel to the right. - if (y > 0 && - *bitmap.getAddr32(x, y - 1) != halo_color && - *bitmap.getAddr32(x, y - 1) != 0) - return true; // Touched pixel above. - if (y < bitmap.height() - 1 && - *bitmap.getAddr32(x, y + 1) != halo_color && - *bitmap.getAddr32(x, y + 1) != 0) - return true; // Touched pixel below. - return false; -} - -void ChromeCanvas::DrawStringWithHalo(const std::wstring& text, - const ChromeFont& font, - const SkColor& text_color, - const SkColor& halo_color_in, - int x, int y, int w, int h, - int flags) { - // Some callers will have semitransparent halo colors, which we don't handle - // (since the resulting image can have 1-bit transparency only). - SkColor halo_color = halo_color_in | 0xFF000000; - - // Create a temporary buffer filled with the halo color. It must leave room - // for the 1-pixel border around the text. - ChromeCanvas text_canvas(w + 2, h + 2, true); - SkPaint bkgnd_paint; - bkgnd_paint.setColor(halo_color); - text_canvas.FillRectInt(0, 0, w + 2, h + 2, bkgnd_paint); - - // Draw the text into the temporary buffer. This will have correct - // ClearType since the background color is the same as the halo color. - text_canvas.DrawStringInt(text, font, text_color, 1, 1, w, h, flags); - - // Windows will have cleared the alpha channel for the pixels it drew. Make it - // opaque. We have to do this first since pixelShouldGetHalo will check for - // 0 to see if a pixel has been modified to transparent, and black text that - // Windows draw will look transparent to it! - text_canvas.getTopPlatformDevice().makeOpaque(0, 0, w + 2, h + 2); - - uint32_t halo_premul = SkPreMultiplyColor(halo_color); - SkBitmap& text_bitmap = const_cast<SkBitmap&>( - text_canvas.getTopPlatformDevice().accessBitmap(true)); - for (int cur_y = 0; cur_y < h + 2; cur_y++) { - uint32_t* text_row = text_bitmap.getAddr32(0, cur_y); - for (int cur_x = 0; cur_x < w + 2; cur_x++) { - if (text_row[cur_x] == halo_premul) { - // This pixel was not touched by the text routines. See if it borders - // a touched pixel in any of the 4 directions (not diagonally). - if (!pixelShouldGetHalo(text_bitmap, cur_x, cur_y, halo_premul)) - text_row[cur_x] = 0; // Make transparent. - } else { - text_row[cur_x] |= 0xff << SK_A32_SHIFT; // Make opaque. - } - } - } - - // Draw the halo bitmap with blur. - drawBitmap(text_bitmap, SkIntToScalar(x - 1), SkIntToScalar(y - 1)); -} |