blob: d4372d1b6ab8f0d07fdb5991f9fc840774413385 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkTextBox_DEFINED
11#define SkTextBox_DEFINED
12
13#include "SkCanvas.h"
14
15/** \class SkTextBox
16
17 SkTextBox is a helper class for drawing 1 or more lines of text
18 within a rectangle. The textbox is positioned and clipped by its Frame.
19 The Margin rectangle controls where the text is drawn relative to
20 the Frame. Line-breaks occur inside the Margin rectangle.
21
22 Spacing is a linear equation used to compute the distance between lines
23 of text. Spacing consists of two scalars: mul and add, and the spacing
24 between lines is computed as: spacing = paint.getTextSize() * mul + add
25*/
26class SkTextBox {
27public:
28 SkTextBox();
29
30 enum Mode {
31 kOneLine_Mode,
32 kLineBreak_Mode,
33
34 kModeCount
35 };
36 Mode getMode() const { return (Mode)fMode; }
37 void setMode(Mode);
38
39 enum SpacingAlign {
40 kStart_SpacingAlign,
41 kCenter_SpacingAlign,
42 kEnd_SpacingAlign,
43
44 kSpacingAlignCount
45 };
46 SpacingAlign getSpacingAlign() const { return (SpacingAlign)fSpacingAlign; }
47 void setSpacingAlign(SpacingAlign);
48
49 void getBox(SkRect*) const;
50 void setBox(const SkRect&);
51 void setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
52
53 void getSpacing(SkScalar* mul, SkScalar* add) const;
54 void setSpacing(SkScalar mul, SkScalar add);
55
56 void draw(SkCanvas*, const char text[], size_t len, const SkPaint&);
57
reed@android.com033e03c2010-05-18 21:17:43 +000058 void setText(const char text[], size_t len, const SkPaint&);
59 void draw(SkCanvas*);
60 int countLines() const;
61 SkScalar getTextHeight() const;
62
reed1b6ab442014-11-03 19:55:41 -080063 SkTextBlob* snapshotTextBlob(SkScalar* computedBottom) const;
64
65 class Visitor {
66 public:
67 virtual ~Visitor() {}
68 virtual void operator()(const char*, size_t, SkScalar x, SkScalar y, const SkPaint&) = 0;
69 };
70
reed@android.com8a1c16f2008-12-17 15:59:43 +000071private:
72 SkRect fBox;
73 SkScalar fSpacingMul, fSpacingAdd;
74 uint8_t fMode, fSpacingAlign;
reed@android.com033e03c2010-05-18 21:17:43 +000075 const char* fText;
76 size_t fLen;
77 const SkPaint* fPaint;
reed1b6ab442014-11-03 19:55:41 -080078
79 SkScalar visit(Visitor&, const char text[], size_t len, const SkPaint&) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000080};
81
82class SkTextLineBreaker {
83public:
84 static int CountLines(const char text[], size_t len, const SkPaint&, SkScalar width);
85};
86
87#endif