[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | // This file defines utility functions for working with text in views. |
| 6 | |
| 7 | #ifndef VIEWS_VIEW_TEXT_UTILS_H_ |
| 8 | #define VIEWS_VIEW_TEXT_UTILS_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 9 | #pragma once |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 10 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include "gfx/rect.h" |
| 14 | #include "gfx/font.h" |
| 15 | |
| 16 | namespace gfx { |
| 17 | class Canvas; |
| 18 | class Size; |
| 19 | } |
| 20 | |
| 21 | namespace views { |
| 22 | class Label; |
| 23 | class Link; |
| 24 | } |
| 25 | |
| 26 | namespace view_text_utils { |
| 27 | |
| 28 | // Draws a string onto the canvas (wrapping if needed) while also keeping |
| 29 | // track of where it ends so we can position a URL after the text. The |
| 30 | // parameter |bounds| represents the boundary we have to work with, |position| |
| 31 | // specifies where to draw the string (relative to the top left corner of the |
| 32 | // |bounds| rectangle and |font| specifies the font to use when drawing. When |
| 33 | // the function returns, the parameter |rect| contains where to draw the URL |
| 34 | // (to the right of where we just drew the text) and |position| is updated to |
| 35 | // reflect where to draw the next string after the URL. |label| is a dummy |
| 36 | // label with the correct width and origin for the text to be written; it's |
| 37 | // used so that the x position can be correctly mirrored in RTL languages. |
| 38 | // |text_direction_is_rtl| is true if an RTL language is being used. |
| 39 | // NOTE: The reason why we need this function is because while Skia knows how |
| 40 | // to wrap text appropriately, it doesn't tell us where it drew the last |
| 41 | // character, which we need to position the URLs within the text. |
| 42 | void DrawTextAndPositionUrl(gfx::Canvas* canvas, |
| 43 | views::Label* label, |
| 44 | const std::wstring& text, |
| 45 | views::Link* link, |
| 46 | gfx::Rect* rect, |
| 47 | gfx::Size* position, |
| 48 | bool text_direction_is_rtl, |
| 49 | const gfx::Rect& bounds, |
| 50 | const gfx::Font& font); |
| 51 | |
| 52 | // A helper function for DrawTextAndPositionUrl, which simply draws the text |
| 53 | // from a certain starting point |position| and wraps within bounds. |
| 54 | // |word_for_word| specifies whether to draw the text word for word or whether |
| 55 | // to treat the text as one blurb (similar to the way URL's are treated inside |
| 56 | // RTL text. For details on the other parameters, see DrawTextAndPositionUrl. |
| 57 | void DrawTextStartingFrom(gfx::Canvas* canvas, |
| 58 | views::Label* label, |
| 59 | const std::wstring& text, |
| 60 | gfx::Size* position, |
| 61 | const gfx::Rect& bounds, |
| 62 | const gfx::Font& font, |
| 63 | bool text_direction_is_rtl, |
| 64 | bool word_for_word); |
| 65 | |
| 66 | // A simply utility function that calculates whether a word of width |
| 67 | // |word_width| fits at position |position| within the |bounds| rectangle. If |
| 68 | // not, |position| is updated to wrap to the beginning of the next line. |
| 69 | void WrapIfWordDoesntFit(int word_width, |
| 70 | int font_height, |
| 71 | gfx::Size* position, |
| 72 | const gfx::Rect& bounds); |
| 73 | |
| 74 | } // namespace view_text_utils |
| 75 | |
| 76 | #endif // CHROME_BROWSER_VIEWS_VIEW_TEXT_UTILS_H_ |