blob: 3471f5b6bf9e7e9d2f53a7f546b2657b24ed6adc [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
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*/
33class SkTextBox {
34public:
35 SkTextBox();
36
37 enum Mode {
38 kOneLine_Mode,
39 kLineBreak_Mode,
40
41 kModeCount
42 };
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 };
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
reed@android.com033e03c2010-05-18 21:17:43 +000065 void setText(const char text[], size_t len, const SkPaint&);
66 void draw(SkCanvas*);
67 int countLines() const;
68 SkScalar getTextHeight() const;
69
reed@android.com8a1c16f2008-12-17 15:59:43 +000070private:
71 SkRect fBox;
72 SkScalar fSpacingMul, fSpacingAdd;
73 uint8_t fMode, fSpacingAlign;
reed@android.com033e03c2010-05-18 21:17:43 +000074 const char* fText;
75 size_t fLen;
76 const SkPaint* fPaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077};
78
79class SkTextLineBreaker {
80public:
81 static int CountLines(const char text[], size_t len, const SkPaint&, SkScalar width);
82};
83
84#endif
85