summaryrefslogtreecommitdiffstats
path: root/views/view_text_utils.h
blob: 6261f58bcb0ab17aa9dc85fa6d1489ffba6b00c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2010 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.
//
// This file defines utility functions for working with text in views.

#ifndef VIEWS_VIEW_TEXT_UTILS_H_
#define VIEWS_VIEW_TEXT_UTILS_H_
#pragma once

#include <string>

#include "gfx/rect.h"
#include "gfx/font.h"

namespace gfx {
class Canvas;
class Size;
}

namespace views {
class Label;
class Link;
}

namespace view_text_utils {

// Draws a string onto the canvas (wrapping if needed) while also keeping
// track of where it ends so we can position a URL after the text. The
// parameter |bounds| represents the boundary we have to work with, |position|
// specifies where to draw the string (relative to the top left corner of the
// |bounds| rectangle and |font| specifies the font to use when drawing. When
// the function returns, the parameter |rect| contains where to draw the URL
// (to the right of where we just drew the text) and |position| is updated to
// reflect where to draw the next string after the URL.  |label| is a dummy
// label with the correct width and origin for the text to be written; it's
// used so that the x position can be correctly mirrored in RTL languages.
// |text_direction_is_rtl| is true if an RTL language is being used.
// NOTE: The reason why we need this function is because while Skia knows how
// to wrap text appropriately, it doesn't tell us where it drew the last
// character, which we need to position the URLs within the text.
void DrawTextAndPositionUrl(gfx::Canvas* canvas,
                            views::Label* label,
                            const std::wstring& text,
                            views::Link* link,
                            gfx::Rect* rect,
                            gfx::Size* position,
                            bool text_direction_is_rtl,
                            const gfx::Rect& bounds,
                            const gfx::Font& font);

// A helper function for DrawTextAndPositionUrl, which simply draws the text
// from a certain starting point |position| and wraps within bounds.
// |word_for_word| specifies whether to draw the text word for word or whether
// to treat the text as one blurb (similar to the way URL's are treated inside
// RTL text. For details on the other parameters, see DrawTextAndPositionUrl.
void DrawTextStartingFrom(gfx::Canvas* canvas,
                          views::Label* label,
                          const std::wstring& text,
                          gfx::Size* position,
                          const gfx::Rect& bounds,
                          const gfx::Font& font,
                          bool text_direction_is_rtl,
                          bool word_for_word);

// A simply utility function that calculates whether a word of width
// |word_width| fits at position |position| within the |bounds| rectangle. If
// not, |position| is updated to wrap to the beginning of the next line.
void WrapIfWordDoesntFit(int word_width,
                         int font_height,
                         gfx::Size* position,
                         const gfx::Rect& bounds);

}  // namespace view_text_utils

#endif  // CHROME_BROWSER_VIEWS_VIEW_TEXT_UTILS_H_