180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2006 The Android Open Source Project
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkTextBox_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkTextBox_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** \class SkTextBox
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTextBox is a helper class for drawing 1 or more lines of text
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    within a rectangle. The textbox is positioned and clipped by its Frame.
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    The Margin rectangle controls where the text is drawn relative to
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    the Frame. Line-breaks occur inside the Margin rectangle.
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Spacing is a linear equation used to compute the distance between lines
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    of text. Spacing consists of two scalars: mul and add, and the spacing
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    between lines is computed as: spacing = paint.getTextSize() * mul + add
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkTextBox {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTextBox();
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum Mode {
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kOneLine_Mode,
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kLineBreak_Mode,
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kModeCount
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Mode    getMode() const { return (Mode)fMode; }
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    setMode(Mode);
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum SpacingAlign {
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kStart_SpacingAlign,
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kCenter_SpacingAlign,
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kEnd_SpacingAlign,
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kSpacingAlignCount
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SpacingAlign    getSpacingAlign() const { return (SpacingAlign)fSpacingAlign; }
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void            setSpacingAlign(SpacingAlign);
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    getBox(SkRect*) const;
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    setBox(const SkRect&);
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    getSpacing(SkScalar* mul, SkScalar* add) const;
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    setSpacing(SkScalar mul, SkScalar add);
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    draw(SkCanvas*, const char text[], size_t len, const SkPaint&);
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    setText(const char text[], size_t len, const SkPaint&);
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void    draw(SkCanvas*);
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int     countLines() const;
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkScalar getTextHeight() const;
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkRect      fBox;
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkScalar    fSpacingMul, fSpacingAdd;
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint8_t     fMode, fSpacingAlign;
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const char* fText;
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    size_t      fLen;
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkPaint* fPaint;
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkTextLineBreaker {
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static int CountLines(const char text[], size_t len, const SkPaint&, SkScalar width);
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
78