1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTextBox_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTextBox_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkTextBox
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTextBox is a helper class for drawing 1 or more lines of text
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    within a rectangle. The textbox is positioned and clipped by its Frame.
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The Margin rectangle controls where the text is drawn relative to
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    the Frame. Line-breaks occur inside the Margin rectangle.
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Spacing is a linear equation used to compute the distance between lines
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    of text. Spacing consists of two scalars: mul and add, and the spacing
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    between lines is computed as: spacing = paint.getTextSize() * mul + add
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTextBox {
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTextBox();
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Mode {
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kOneLine_Mode,
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kLineBreak_Mode,
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kModeCount
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Mode    getMode() const { return (Mode)fMode; }
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void    setMode(Mode);
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum SpacingAlign {
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kStart_SpacingAlign,
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kCenter_SpacingAlign,
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kEnd_SpacingAlign,
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kSpacingAlignCount
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SpacingAlign    getSpacingAlign() const { return (SpacingAlign)fSpacingAlign; }
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void            setSpacingAlign(SpacingAlign);
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void    getBox(SkRect*) const;
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void    setBox(const SkRect&);
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void    setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void    getSpacing(SkScalar* mul, SkScalar* add) const;
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void    setSpacing(SkScalar mul, SkScalar add);
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void    draw(SkCanvas*, const char text[], size_t len, const SkPaint&);
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
58033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com    void    setText(const char text[], size_t len, const SkPaint&);
59033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com    void    draw(SkCanvas*);
60033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com    int     countLines() const;
61033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com    SkScalar getTextHeight() const;
62033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect      fBox;
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    fSpacingMul, fSpacingAdd;
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint8_t     fMode, fSpacingAlign;
67033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com    const char* fText;
68033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com    size_t      fLen;
69033e03cb191aff56e06e5d6aab917f60740dba63reed@android.com    const SkPaint* fPaint;
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTextLineBreaker {
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static int CountLines(const char text[], size_t len, const SkPaint&, SkScalar width);
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
78