1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkFont_DEFINED
9#define SkFont_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkScalar.h"
13
14class SkPaint;
15class SkTypeface;
16
17enum SkTextEncoding {
18    kUTF8_SkTextEncoding,
19    kUTF16_SkTextEncoding,
20    kUTF32_SkTextEncoding,
21    kGlyphID_SkTextEncoding,
22};
23
24/*
25 1. The Hinting enum in SkPaint is gone entirely, absorbed into SkFont's flags.
26
27 2. SkPaint Flags look like this today
28
29 enum Flags {
30     kAntiAlias_Flag       = 0x01,   //!< mask to enable antialiasing
31     kDither_Flag          = 0x04,   //!< mask to enable dithering
32     kUnderlineText_Flag   = 0x08,   //!< mask to enable underline text
33     kStrikeThruText_Flag  = 0x10,   //!< mask to enable strike-thru text
34     kFakeBoldText_Flag    = 0x20,   //!< mask to enable fake-bold text
35     kLinearText_Flag      = 0x40,   //!< mask to enable linear-text
36     kSubpixelText_Flag    = 0x80,   //!< mask to enable subpixel text positioning
37     kDevKernText_Flag     = 0x100,  //!< mask to enable device kerning text
38     kLCDRenderText_Flag   = 0x200,  //!< mask to enable subpixel glyph renderering
39     kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
40     kAutoHinting_Flag     = 0x800,  //!< mask to force Freetype's autohinter
41     kVerticalText_Flag    = 0x1000,
42     kGenA8FromLCD_Flag    = 0x2000, // hack for GDI -- do not use if you can help it
43 };
44
45 SkFont would absorb these:
46
47     kFakeBoldText_Flag    = 0x20,   //!< mask to enable fake-bold text
48     kLinearText_Flag      = 0x40,   //!< mask to enable linear-text
49     kSubpixelText_Flag    = 0x80,   //!< mask to enable subpixel text positioning
50     kDevKernText_Flag     = 0x100,  //!< mask to enable device kerning text
51     kLCDRenderText_Flag   = 0x200,  //!< mask to enable subpixel glyph renderering
52     kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
53     kAutoHinting_Flag     = 0x800,  //!< mask to force Freetype's autohinter
54     kVerticalText_Flag    = 0x1000,
55     kGenA8FromLCD_Flag    = 0x2000, // hack for GDI -- do not use if you can help it
56
57 leaving these still in paint
58
59     kAntiAlias_Flag       = 0x01,   //!< mask to enable antialiasing
60     kDither_Flag          = 0x04,   //!< mask to enable dithering
61     kUnderlineText_Flag   = 0x08,   //!< mask to enable underline text
62     kStrikeThruText_Flag  = 0x10,   //!< mask to enable strike-thru text
63
64 3. Antialiasing
65
66    SkFont has a mask-type: BW, AA, LCD
67    SkPaint has antialias boolean
68
69    What to do if the font's mask-type disagrees with the paint?
70
71 */
72
73class SkFont : public SkRefCnt {
74public:
75    enum Flags {
76        /**
77         *  Use the system's automatic hinting mechanism to hint the typeface.
78         *  If both bytecode and auto hints are specified, attempt to use the bytecodes first.
79         *  If that fails (e.g. there are no codes), then attempt to autohint.
80         */
81        kEnableAutoHints_Flag       = 1 << 0,
82
83        /**
84         *  If the typeface contains explicit bytecodes for hinting, use them.
85         *  If both bytecode and auto hints are specified, attempt to use the bytecodes first;
86         *  if that fails (e.g. there are no codes), then attempt to autohint.
87         */
88        kEnableByteCodeHints_Flag   = 1 << 1,
89
90        /**
91         *  Use rounded metric values (e.g. advance).
92         *  If either auto or bytecode hinting was used, apply those results to the metrics of the
93         *  glyphs as well. If no hinting was applied, the metrics will just be rounded to the
94         *  nearest integer.
95         *
96         *  This applies to calls that return metrics (e.g. measureText) and to drawing the glyphs
97         *  (see SkCanvas drawText and drawPosText).
98         */
99        kUseNonlinearMetrics_Flag   = 1 << 2,
100
101        kVertical_Flag              = 1 << 3,
102        kEmbeddedBitmaps_Flag       = 1 << 4,
103        kGenA8FromLCD_Flag          = 1 << 5,
104        kEmbolden_Flag              = 1 << 6,
105        kDevKern_Flag               = 1 << 7,   // ifdef ANDROID ?
106    };
107
108    enum MaskType {
109        kBW_MaskType,
110        kA8_MaskType,
111        kLCD_MaskType,
112    };
113
114    static SkFont* Create(SkTypeface*, SkScalar size, MaskType, uint32_t flags);
115    static SkFont* Create(SkTypeface*, SkScalar size, SkScalar scaleX, SkScalar skewX,
116                          MaskType, uint32_t flags);
117
118    /**
119     *  Return a font with the same attributes of this font, but with the specified size.
120     *  If size is not supported (e.g. <= 0 or non-finite) NULL will be returned.
121     */
122    SkFont* cloneWithSize(SkScalar size) const;
123
124    SkTypeface* getTypeface() const { return fTypeface; }
125    SkScalar    getSize() const { return fSize; }
126    SkScalar    getScaleX() const { return fScaleX; }
127    SkScalar    getSkewX() const { return fSkewX; }
128    uint32_t    getFlags() const { return fFlags; }
129    MaskType    getMaskType() const { return (MaskType)fMaskType; }
130
131    bool isVertical() const { return SkToBool(fFlags & kVertical_Flag); }
132    bool isEmbolden() const { return SkToBool(fFlags & kEmbolden_Flag); }
133    bool isEnableAutoHints() const { return SkToBool(fFlags & kEnableAutoHints_Flag); }
134    bool isEnableByteCodeHints() const { return SkToBool(fFlags & kEnableByteCodeHints_Flag); }
135    bool isUseNonLinearMetrics() const { return SkToBool(fFlags & kUseNonlinearMetrics_Flag); }
136
137    int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding,
138                     uint16_t glyphs[], int maxGlyphCount) const;
139
140    SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding) const;
141
142    static SkFont* Testing_CreateFromPaint(const SkPaint&);
143
144private:
145    enum {
146        kAllFlags = 0xFF,
147    };
148
149    SkFont(SkTypeface*, SkScalar size, SkScalar scaleX, SkScalar skewX, MaskType, uint32_t flags);
150    virtual ~SkFont();
151
152    SkTypeface* fTypeface;
153    SkScalar    fSize;
154    SkScalar    fScaleX;
155    SkScalar    fSkewX;
156    uint16_t    fFlags;
157    uint8_t     fMaskType;
158//  uint8_t     fPad;
159};
160
161#endif
162