1 /*
2  * Copyright 2006-2012 The Android Open Source Project
3  * Copyright 2012 Mozilla Foundation
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #ifndef SKFONTHOST_FREETYPE_COMMON_H_
10 #define SKFONTHOST_FREETYPE_COMMON_H_
11 
12 #include "SkGlyph.h"
13 #include "SkMutex.h"
14 #include "SkScalerContext.h"
15 #include "SkTypeface.h"
16 #include "SkTypes.h"
17 
18 #include "SkFontMgr.h"
19 
20 #include <ft2build.h>
21 #include FT_FREETYPE_H
22 
23 class SkScalerContext_FreeType_Base : public SkScalerContext {
24 protected:
25     // See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
26     // This value was chosen by eyeballing the result in Firefox and trying to match it.
27     static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
28 
SkScalerContext_FreeType_Base(SkTypeface * typeface,const SkDescriptor * desc)29     SkScalerContext_FreeType_Base(SkTypeface* typeface, const SkDescriptor *desc)
30     : INHERITED(typeface, desc)
31     {}
32 
33     void generateGlyphImage(FT_Face face, const SkGlyph& glyph);
34     void generateGlyphPath(FT_Face face, SkPath* path);
35 
36 private:
37     typedef SkScalerContext INHERITED;
38 };
39 
40 class SkTypeface_FreeType : public SkTypeface {
41 public:
42     /** For SkFontMgrs to make use of our ability to extract
43      *  name and style from a stream, using FreeType's API.
44      */
45     class Scanner : ::SkNoncopyable {
46     public:
47         Scanner();
48         ~Scanner();
49         struct AxisDefinition {
50             SkFourByteTag fTag;
51             SkFixed fMinimum;
52             SkFixed fDefault;
53             SkFixed fMaximum;
54         };
55         using AxisDefinitions = SkSTArray<4, AxisDefinition, true>;
56         bool recognizedFont(SkStream* stream, int* numFonts) const;
57         bool scanFont(SkStream* stream, int ttcIndex,
58                       SkString* name, SkFontStyle* style, bool* isFixedPitch,
59                       AxisDefinitions* axes) const;
60         static void computeAxisValues(
61             AxisDefinitions axisDefinitions,
62             const SkFontMgr::FontParameters::Axis* requestedAxis, int requestedAxisCount,
63             SkFixed* axisValues,
64             const SkString& name);
65 
66     private:
67         FT_Face openFace(SkStream* stream, int ttcIndex, FT_Stream ftStream) const;
68         FT_Library fLibrary;
69         mutable SkMutex fLibraryMutex;
70     };
71 
72 protected:
SkTypeface_FreeType(const SkFontStyle & style,SkFontID uniqueID,bool isFixedPitch)73     SkTypeface_FreeType(const SkFontStyle& style, SkFontID uniqueID, bool isFixedPitch)
74         : INHERITED(style, uniqueID, isFixedPitch)
75         , fGlyphCount(-1)
76     {}
77 
78     virtual SkScalerContext* onCreateScalerContext(
79                                         const SkDescriptor*) const override;
80     void onFilterRec(SkScalerContextRec*) const override;
81     SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
82                         PerGlyphInfo, const uint32_t*, uint32_t) const override;
83     int onGetUPEM() const override;
84     virtual bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
85                                        int32_t adjustments[]) const override;
86     virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[],
87                                 int glyphCount) const override;
88     int onCountGlyphs() const override;
89 
90     LocalizedStrings* onCreateFamilyNameIterator() const override;
91 
92     int onGetTableTags(SkFontTableTag tags[]) const override;
93     virtual size_t onGetTableData(SkFontTableTag, size_t offset,
94                                   size_t length, void* data) const override;
95 
96 private:
97     mutable int fGlyphCount;
98 
99     typedef SkTypeface INHERITED;
100 };
101 
102 #endif // SKFONTHOST_FREETYPE_COMMON_H_
103