diff options
Diffstat (limited to 'chrome/common/gfx/chrome_canvas.cc')
-rw-r--r-- | chrome/common/gfx/chrome_canvas.cc | 156 |
1 files changed, 82 insertions, 74 deletions
diff --git a/chrome/common/gfx/chrome_canvas.cc b/chrome/common/gfx/chrome_canvas.cc index 3114b14..7c19b874 100644 --- a/chrome/common/gfx/chrome_canvas.cc +++ b/chrome/common/gfx/chrome_canvas.cc @@ -12,6 +12,84 @@ #include "chrome/common/gfx/chrome_font.h" #include "chrome/common/l10n_util.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()); + 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) { + 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; + } 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. + if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) + f |= DT_RTLREADING; + + return f; +} + +} // namespace + ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) : gfx::PlatformCanvasWin(width, height, is_opaque) { } @@ -217,66 +295,11 @@ void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, drawRect(dest_rect, p); } -int ChromeCanvas::ComputeFormatFlags(int flags) { - 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 & (TEXT_ALIGN_CENTER | TEXT_ALIGN_RIGHT | TEXT_ALIGN_LEFT))) { - flags |= l10n_util::DefaultCanvasTextAlignment(); - } - - if (flags & HIDE_PREFIX) - f |= DT_HIDEPREFIX; - else if ((flags & SHOW_PREFIX) == 0) - f |= DT_NOPREFIX; - - if (flags & MULTI_LINE) { - f |= DT_WORDBREAK; - } else { - f |= DT_SINGLELINE | DT_VCENTER; - if (!(flags & NO_ELLIPSIS)) - f |= DT_END_ELLIPSIS; - } - - // vertical alignment - if (flags & TEXT_VALIGN_TOP) - f |= DT_TOP; - else if (flags & TEXT_VALIGN_BOTTOM) - f |= DT_BOTTOM; - else - f |= DT_VCENTER; - - // horizontal alignment - if (flags & TEXT_ALIGN_CENTER) - f |= DT_CENTER; - else if (flags & 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. - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) { - f |= DT_RTLREADING; - } - - return f; -} - +// static void ChromeCanvas::SizeStringInt(const std::wstring& text, const ChromeFont& font, int *width, int *height, int flags) { - HDC dc = beginPlatformPaint(); + HDC dc = GetDC(NULL); HFONT old_font = static_cast<HFONT>(SelectObject(dc, font.hfont())); RECT b; b.left = 0; @@ -288,13 +311,14 @@ void ChromeCanvas::SizeStringInt(const std::wstring& text, } b.bottom = *height; DoDrawText(dc, text, &b, ComputeFormatFlags(flags) | DT_CALCRECT); - 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); *width = b.right; *height = b.bottom; + + ReleaseDC(NULL, dc); } void ChromeCanvas::DrawStringInt(const std::wstring& text, HFONT font, @@ -331,22 +355,6 @@ void ChromeCanvas::DrawStringInt(const std::wstring& text, l10n_util::DefaultCanvasTextAlignment()); } -// We make sure that LTR text we draw in an RTL context is modified -// appropriately to make sure it maintains it LTR orientation. -void ChromeCanvas::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()); - 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); -} - - void ChromeCanvas::DrawStringInt(const std::wstring& text, const ChromeFont& font, const SkColor& color, |