• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // These are forward declared to avoid pimpl but also hide the FreeType implementation.
21 typedef struct FT_LibraryRec_* FT_Library;
22 typedef struct FT_FaceRec_* FT_Face;
23 typedef struct FT_StreamRec_* FT_Stream;
24 typedef signed long FT_Pos;
25 
26 class SkScalerContext_FreeType_Base : public SkScalerContext {
27 protected:
28     // See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
29     // This value was chosen by eyeballing the result in Firefox and trying to match it.
30     static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
31 
SkScalerContext_FreeType_Base(sk_sp<SkTypeface> typeface,const SkScalerContextEffects & effects,const SkDescriptor * desc)32     SkScalerContext_FreeType_Base(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
33                                   const SkDescriptor *desc)
34         : INHERITED(std::move(typeface), effects, desc)
35     {}
36 
37     void generateGlyphImage(FT_Face face, const SkGlyph& glyph, const SkMatrix& bitmapTransform);
38     void generateGlyphPath(FT_Face face, SkPath* path);
39 private:
40     typedef SkScalerContext INHERITED;
41 };
42 
43 class SkTypeface_FreeType : public SkTypeface {
44 public:
45     /** For SkFontMgrs to make use of our ability to extract
46      *  name and style from a stream, using FreeType's API.
47      */
48     class Scanner : ::SkNoncopyable {
49     public:
50         Scanner();
51         ~Scanner();
52         struct AxisDefinition {
53             SkFourByteTag fTag;
54             SkFixed fMinimum;
55             SkFixed fDefault;
56             SkFixed fMaximum;
57         };
58         using AxisDefinitions = SkSTArray<4, AxisDefinition, true>;
59         bool recognizedFont(SkStreamAsset* stream, int* numFonts) const;
60         bool scanFont(SkStreamAsset* stream, int ttcIndex,
61                       SkString* name, SkFontStyle* style, bool* isFixedPitch,
62                       AxisDefinitions* axes) const;
63         static void computeAxisValues(
64             AxisDefinitions axisDefinitions,
65             const SkFontArguments::VariationPosition position,
66             SkFixed* axisValues,
67             const SkString& name);
68 
69     private:
70         FT_Face openFace(SkStreamAsset* stream, int ttcIndex, FT_Stream ftStream) const;
71         FT_Library fLibrary;
72         mutable SkMutex fLibraryMutex;
73     };
74 
75 protected:
SkTypeface_FreeType(const SkFontStyle & style,bool isFixedPitch)76     SkTypeface_FreeType(const SkFontStyle& style, bool isFixedPitch)
77         : INHERITED(style, isFixedPitch)
78     {}
79 
80     virtual SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
81                                                    const SkDescriptor*) const override;
82     void onFilterRec(SkScalerContextRec*) const override;
83     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
84     int onGetUPEM() const override;
85     bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
86                                      int32_t adjustments[]) const override;
87     int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[],
88                         int glyphCount) const override;
89     int onCountGlyphs() const override;
90 
91     LocalizedStrings* onCreateFamilyNameIterator() const override;
92 
93     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
94                                      int coordinateCount) const override;
95     int onGetTableTags(SkFontTableTag tags[]) const override;
96     size_t onGetTableData(SkFontTableTag, size_t offset,
97                           size_t length, void* data) const override;
98 
99 private:
100     typedef SkTypeface INHERITED;
101 };
102 
103 #endif // SKFONTHOST_FREETYPE_COMMON_H_
104