| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "app/gfx/chrome_canvas.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 | |
| 9 #include "app/gfx/chrome_font.h" | |
| 10 #include "app/l10n_util.h" | |
| 11 #include "base/gfx/rect.h" | |
| 12 #include "third_party/skia/include/core/SkShader.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // We make sure that LTR text we draw in an RTL context is modified | |
| 17 // appropriately to make sure it maintains it LTR orientation. | |
| 18 void DoDrawText(HDC hdc, const std::wstring& text, | |
| 19 RECT* text_bounds, int flags) { | |
| 20 std::wstring localized_text; | |
| 21 const wchar_t* string_ptr = text.c_str(); | |
| 22 int string_size = static_cast<int>(text.length()); | |
| 23 // Only adjust string directionality if both of the following are true: | |
| 24 // 1. The current locale is RTL. | |
| 25 // 2. The string itself has RTL directionality. | |
| 26 if (flags & DT_RTLREADING) { | |
| 27 if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) { | |
| 28 string_ptr = localized_text.c_str(); | |
| 29 string_size = static_cast<int>(localized_text.length()); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 DrawText(hdc, string_ptr, string_size, text_bounds, flags); | |
| 34 } | |
| 35 | |
| 36 // Compute the windows flags necessary to implement the provided text | |
| 37 // ChromeCanvas flags. | |
| 38 int ComputeFormatFlags(int flags, const std::wstring& text) { | |
| 39 int f = 0; | |
| 40 | |
| 41 // Setting the text alignment explicitly in case it hasn't already been set. | |
| 42 // This will make sure that we don't align text to the left on RTL locales | |
| 43 // just because no alignment flag was passed to DrawStringInt(). | |
| 44 if (!(flags & (ChromeCanvas::TEXT_ALIGN_CENTER | | |
| 45 ChromeCanvas::TEXT_ALIGN_RIGHT | | |
| 46 ChromeCanvas::TEXT_ALIGN_LEFT))) { | |
| 47 flags |= l10n_util::DefaultCanvasTextAlignment(); | |
| 48 } | |
| 49 | |
| 50 if (flags & ChromeCanvas::HIDE_PREFIX) | |
| 51 f |= DT_HIDEPREFIX; | |
| 52 else if ((flags & ChromeCanvas::SHOW_PREFIX) == 0) | |
| 53 f |= DT_NOPREFIX; | |
| 54 | |
| 55 if (flags & ChromeCanvas::MULTI_LINE) { | |
| 56 f |= DT_WORDBREAK; | |
| 57 if (flags & ChromeCanvas::CHARACTER_BREAK) | |
| 58 f |= DT_EDITCONTROL; | |
| 59 } else { | |
| 60 f |= DT_SINGLELINE | DT_VCENTER; | |
| 61 if (!(flags & ChromeCanvas::NO_ELLIPSIS)) | |
| 62 f |= DT_END_ELLIPSIS; | |
| 63 } | |
| 64 | |
| 65 // vertical alignment | |
| 66 if (flags & ChromeCanvas::TEXT_VALIGN_TOP) | |
| 67 f |= DT_TOP; | |
| 68 else if (flags & ChromeCanvas::TEXT_VALIGN_BOTTOM) | |
| 69 f |= DT_BOTTOM; | |
| 70 else | |
| 71 f |= DT_VCENTER; | |
| 72 | |
| 73 // horizontal alignment | |
| 74 if (flags & ChromeCanvas::TEXT_ALIGN_CENTER) | |
| 75 f |= DT_CENTER; | |
| 76 else if (flags & ChromeCanvas::TEXT_ALIGN_RIGHT) | |
| 77 f |= DT_RIGHT; | |
| 78 else | |
| 79 f |= DT_LEFT; | |
| 80 | |
| 81 // In order to make sure RTL/BiDi strings are rendered correctly, we must | |
| 82 // pass the flag DT_RTLREADING to DrawText (when the locale's language is | |
| 83 // a right-to-left language) so that Windows does the right thing. | |
| 84 // | |
| 85 // In addition to correctly displaying text containing both RTL and LTR | |
| 86 // elements (for example, a string containing a telephone number within a | |
| 87 // sentence in Hebrew, or a sentence in Hebrew that contains a word in | |
| 88 // English) this flag also makes sure that if there is not enough space to | |
| 89 // display the entire string, the ellipsis is displayed on the left hand side | |
| 90 // of the truncated string and not on the right hand side. | |
| 91 // | |
| 92 // We make a distinction between Chrome UI strings and text coming from a web | |
| 93 // page. | |
| 94 // | |
| 95 // For text coming from a web page we determine the alignment based on the | |
| 96 // first character with strong directionality. If the directionality of the | |
| 97 // first character with strong directionality in the text is LTR, the | |
| 98 // alignment is set to DT_LEFT, and the directionality should not be set as | |
| 99 // DT_RTLREADING. | |
| 100 // | |
| 101 // This heuristic doesn't work for Chrome UI strings since even in RTL | |
| 102 // locales, some of those might start with English text but we know they're | |
| 103 // localized so we always want them to be right aligned, and their | |
| 104 // directionality should be set as DT_RTLREADING. | |
| 105 // | |
| 106 // Caveat: If the string is purely LTR, don't set DTL_RTLREADING since when | |
| 107 // the flag is set, LRE-PDF don't have the desired effect of rendering | |
| 108 // multiline English-only text as LTR. | |
| 109 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT && | |
| 110 (f & DT_RIGHT)) { | |
| 111 if (l10n_util::StringContainsStrongRTLChars(text)) { | |
| 112 f |= DT_RTLREADING; | |
| 113 } | |
| 114 } | |
| 115 return f; | |
| 116 } | |
| 117 | |
| 118 } // anonymous namespace | |
| 119 | |
| 120 ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) | |
| 121 : skia::PlatformCanvasWin(width, height, is_opaque) { | |
| 122 } | |
| 123 | |
| 124 ChromeCanvas::ChromeCanvas() : skia::PlatformCanvasWin() { | |
| 125 } | |
| 126 | |
| 127 ChromeCanvas::~ChromeCanvas() { | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 void ChromeCanvas::SizeStringInt(const std::wstring& text, | |
| 132 const gfx::Font& font, | |
| 133 int *width, int *height, int flags) { | |
| 134 HDC dc = GetDC(NULL); | |
| 135 HFONT old_font = static_cast<HFONT>(SelectObject(dc, font.hfont())); | |
| 136 RECT b; | |
| 137 b.left = 0; | |
| 138 b.top = 0; | |
| 139 b.right = *width; | |
| 140 if (b.right == 0 && !text.empty()) { | |
| 141 // Width needs to be at least 1 or else DoDrawText will not resize it. | |
| 142 b.right = 1; | |
| 143 } | |
| 144 b.bottom = *height; | |
| 145 DoDrawText(dc, text, &b, ComputeFormatFlags(flags, text) | DT_CALCRECT); | |
| 146 | |
| 147 // Restore the old font. This way we don't have to worry if the caller | |
| 148 // deletes the font and the DC lives longer. | |
| 149 SelectObject(dc, old_font); | |
| 150 *width = b.right; | |
| 151 *height = b.bottom; | |
| 152 | |
| 153 ReleaseDC(NULL, dc); | |
| 154 } | |
| 155 | |
| 156 void ChromeCanvas::DrawStringInt(const std::wstring& text, HFONT font, | |
| 157 const SkColor& color, int x, int y, int w, | |
| 158 int h, int flags) { | |
| 159 if (!IntersectsClipRectInt(x, y, w, h)) | |
| 160 return; | |
| 161 | |
| 162 RECT text_bounds = { x, y, x + w, y + h }; | |
| 163 HDC dc = beginPlatformPaint(); | |
| 164 SetBkMode(dc, TRANSPARENT); | |
| 165 HFONT old_font = (HFONT)SelectObject(dc, font); | |
| 166 COLORREF brush_color = RGB(SkColorGetR(color), SkColorGetG(color), | |
| 167 SkColorGetB(color)); | |
| 168 SetTextColor(dc, brush_color); | |
| 169 | |
| 170 int f = ComputeFormatFlags(flags, text); | |
| 171 DoDrawText(dc, text, &text_bounds, f); | |
| 172 endPlatformPaint(); | |
| 173 | |
| 174 // Restore the old font. This way we don't have to worry if the caller | |
| 175 // deletes the font and the DC lives longer. | |
| 176 SelectObject(dc, old_font); | |
| 177 | |
| 178 // Windows will have cleared the alpha channel of the text we drew. Assume | |
| 179 // we're drawing to an opaque surface, or at least the text rect area is | |
| 180 // opaque. | |
| 181 getTopPlatformDevice().makeOpaque(x, y, w, h); | |
| 182 } | |
| 183 | |
| 184 void ChromeCanvas::DrawStringInt(const std::wstring& text, | |
| 185 const gfx::Font& font, | |
| 186 const SkColor& color, | |
| 187 int x, int y, int w, int h, int flags) { | |
| 188 DrawStringInt(text, font.hfont(), color, x, y, w, h, flags); | |
| 189 } | |
| 190 | |
| 191 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If | |
| 192 // any of them are not the halo color, returns true. This defines the halo of | |
| 193 // pixels that will appear around the text. Note that we have to check each | |
| 194 // pixel against both the halo color and transparent since DrawStringWithHalo | |
| 195 // will modify the bitmap as it goes, and clears pixels shouldn't count as | |
| 196 // changed. | |
| 197 static bool pixelShouldGetHalo(const SkBitmap& bitmap, int x, int y, | |
| 198 SkColor halo_color) { | |
| 199 if (x > 0 && | |
| 200 *bitmap.getAddr32(x - 1, y) != halo_color && | |
| 201 *bitmap.getAddr32(x - 1, y) != 0) | |
| 202 return true; // Touched pixel to the left. | |
| 203 if (x < bitmap.width() - 1 && | |
| 204 *bitmap.getAddr32(x + 1, y) != halo_color && | |
| 205 *bitmap.getAddr32(x + 1, y) != 0) | |
| 206 return true; // Touched pixel to the right. | |
| 207 if (y > 0 && | |
| 208 *bitmap.getAddr32(x, y - 1) != halo_color && | |
| 209 *bitmap.getAddr32(x, y - 1) != 0) | |
| 210 return true; // Touched pixel above. | |
| 211 if (y < bitmap.height() - 1 && | |
| 212 *bitmap.getAddr32(x, y + 1) != halo_color && | |
| 213 *bitmap.getAddr32(x, y + 1) != 0) | |
| 214 return true; // Touched pixel below. | |
| 215 return false; | |
| 216 } | |
| 217 | |
| 218 void ChromeCanvas::DrawStringWithHalo(const std::wstring& text, | |
| 219 const gfx::Font& font, | |
| 220 const SkColor& text_color, | |
| 221 const SkColor& halo_color_in, | |
| 222 int x, int y, int w, int h, | |
| 223 int flags) { | |
| 224 // Some callers will have semitransparent halo colors, which we don't handle | |
| 225 // (since the resulting image can have 1-bit transparency only). | |
| 226 SkColor halo_color = halo_color_in | 0xFF000000; | |
| 227 | |
| 228 // Create a temporary buffer filled with the halo color. It must leave room | |
| 229 // for the 1-pixel border around the text. | |
| 230 ChromeCanvas text_canvas(w + 2, h + 2, true); | |
| 231 SkPaint bkgnd_paint; | |
| 232 bkgnd_paint.setColor(halo_color); | |
| 233 text_canvas.FillRectInt(0, 0, w + 2, h + 2, bkgnd_paint); | |
| 234 | |
| 235 // Draw the text into the temporary buffer. This will have correct | |
| 236 // ClearType since the background color is the same as the halo color. | |
| 237 text_canvas.DrawStringInt(text, font, text_color, 1, 1, w, h, flags); | |
| 238 | |
| 239 // Windows will have cleared the alpha channel for the pixels it drew. Make it | |
| 240 // opaque. We have to do this first since pixelShouldGetHalo will check for | |
| 241 // 0 to see if a pixel has been modified to transparent, and black text that | |
| 242 // Windows draw will look transparent to it! | |
| 243 text_canvas.getTopPlatformDevice().makeOpaque(0, 0, w + 2, h + 2); | |
| 244 | |
| 245 uint32_t halo_premul = SkPreMultiplyColor(halo_color); | |
| 246 SkBitmap& text_bitmap = const_cast<SkBitmap&>( | |
| 247 text_canvas.getTopPlatformDevice().accessBitmap(true)); | |
| 248 for (int cur_y = 0; cur_y < h + 2; cur_y++) { | |
| 249 uint32_t* text_row = text_bitmap.getAddr32(0, cur_y); | |
| 250 for (int cur_x = 0; cur_x < w + 2; cur_x++) { | |
| 251 if (text_row[cur_x] == halo_premul) { | |
| 252 // This pixel was not touched by the text routines. See if it borders | |
| 253 // a touched pixel in any of the 4 directions (not diagonally). | |
| 254 if (!pixelShouldGetHalo(text_bitmap, cur_x, cur_y, halo_premul)) | |
| 255 text_row[cur_x] = 0; // Make transparent. | |
| 256 } else { | |
| 257 text_row[cur_x] |= 0xff << SK_A32_SHIFT; // Make opaque. | |
| 258 } | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 // Draw the halo bitmap with blur. | |
| 263 drawBitmap(text_bitmap, SkIntToScalar(x - 1), SkIntToScalar(y - 1)); | |
| 264 } | |
| OLD | NEW |