• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SkTextBox_DEFINED
18 #define SkTextBox_DEFINED
19 
20 #include "SkCanvas.h"
21 
22 /** \class SkTextBox
23 
24     SkTextBox is a helper class for drawing 1 or more lines of text
25     within a rectangle. The textbox is positioned and clipped by its Frame.
26     The Margin rectangle controls where the text is drawn relative to
27     the Frame. Line-breaks occur inside the Margin rectangle.
28 
29     Spacing is a linear equation used to compute the distance between lines
30     of text. Spacing consists of two scalars: mul and add, and the spacing
31     between lines is computed as: spacing = paint.getTextSize() * mul + add
32 */
33 class SkTextBox {
34 public:
35     SkTextBox();
36 
37     enum Mode {
38         kOneLine_Mode,
39         kLineBreak_Mode,
40 
41         kModeCount
42     };
getMode()43     Mode    getMode() const { return (Mode)fMode; }
44     void    setMode(Mode);
45 
46     enum SpacingAlign {
47         kStart_SpacingAlign,
48         kCenter_SpacingAlign,
49         kEnd_SpacingAlign,
50 
51         kSpacingAlignCount
52     };
getSpacingAlign()53     SpacingAlign    getSpacingAlign() const { return (SpacingAlign)fSpacingAlign; }
54     void            setSpacingAlign(SpacingAlign);
55 
56     void    getBox(SkRect*) const;
57     void    setBox(const SkRect&);
58     void    setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
59 
60     void    getSpacing(SkScalar* mul, SkScalar* add) const;
61     void    setSpacing(SkScalar mul, SkScalar add);
62 
63     void    draw(SkCanvas*, const char text[], size_t len, const SkPaint&);
64 
65 private:
66     SkRect      fBox;
67     SkScalar    fSpacingMul, fSpacingAdd;
68     uint8_t     fMode, fSpacingAlign;
69 };
70 
71 class SkTextLineBreaker {
72 public:
73     static int CountLines(const char text[], size_t len, const SkPaint&, SkScalar width);
74 };
75 
76 #endif
77 
78