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         *  This is a last resort hinting method applied only if other hinting methods do not apply.
79         *  TODO: where to put auto-normal vs auto-light?
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         *  If the typeface contains explicit bitmaps for hinting, use them.
92         *  If both bytecode and auto hints are also specified, attempt to use the bitmaps first;
93         *  if that fails (e.g. there are no bitmaps), then attempt to bytecode or autohint.
94         */
95        kEmbeddedBitmaps_Flag       = 1 << 2,
96
97        /**
98         *  Use rounded metric values (e.g. advance).
99         *  If either auto or bytecode hinting was used, apply those results to the metrics of the
100         *  glyphs as well. If no hinting was applied, the metrics will just be rounded to the
101         *  nearest integer.
102         *
103         *  This applies to calls that return metrics (e.g. measureText) and to drawing the glyphs
104         *  (see SkCanvas drawText and drawPosText).
105         */
106        kUseNonlinearMetrics_Flag   = 1 << 3,
107
108        kVertical_Flag              = 1 << 4,
109        kGenA8FromLCD_Flag          = 1 << 5,
110        kEmbolden_Flag              = 1 << 6,
111        kDevKern_Flag               = 1 << 7,   // ifdef ANDROID ?
112    };
113
114    enum MaskType {
115        kBW_MaskType,
116        kA8_MaskType,
117        kLCD_MaskType,
118    };
119
120    static SkFont* Create(SkTypeface*, SkScalar size, MaskType, uint32_t flags);
121    static SkFont* Create(SkTypeface*, SkScalar size, SkScalar scaleX, SkScalar skewX,
122                          MaskType, uint32_t flags);
123
124    /**
125     *  Return a font with the same attributes of this font, but with the specified size.
126     *  If size is not supported (e.g. <= 0 or non-finite) NULL will be returned.
127     */
128    SkFont* cloneWithSize(SkScalar size) const;
129
130    SkTypeface* getTypeface() const { return fTypeface; }
131    SkScalar    getSize() const { return fSize; }
132    SkScalar    getScaleX() const { return fScaleX; }
133    SkScalar    getSkewX() const { return fSkewX; }
134    uint32_t    getFlags() const { return fFlags; }
135    MaskType    getMaskType() const { return (MaskType)fMaskType; }
136
137    bool isVertical() const { return SkToBool(fFlags & kVertical_Flag); }
138    bool isEmbolden() const { return SkToBool(fFlags & kEmbolden_Flag); }
139    bool isEnableAutoHints() const { return SkToBool(fFlags & kEnableAutoHints_Flag); }
140    bool isEnableByteCodeHints() const { return SkToBool(fFlags & kEnableByteCodeHints_Flag); }
141    bool isUseNonLinearMetrics() const { return SkToBool(fFlags & kUseNonlinearMetrics_Flag); }
142
143    int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding,
144                     uint16_t glyphs[], int maxGlyphCount) const;
145
146    SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding) const;
147
148    static SkFont* Testing_CreateFromPaint(const SkPaint&);
149
150private:
151    enum {
152        kAllFlags = 0xFF,
153    };
154
155    SkFont(SkTypeface*, SkScalar size, SkScalar scaleX, SkScalar skewX, MaskType, uint32_t flags);
156    virtual ~SkFont();
157
158    SkTypeface* fTypeface;
159    SkScalar    fSize;
160    SkScalar    fScaleX;
161    SkScalar    fSkewX;
162    uint16_t    fFlags;
163    uint8_t     fMaskType;
164//  uint8_t     fPad;
165};
166
167#endif
168