Home | History | Annotate | Download | only in ports
      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 "SkScalerContext.h"
     14 #include "SkTypeface.h"
     15 #include "SkTypes.h"
     16 
     17 #include <ft2build.h>
     18 #include FT_FREETYPE_H
     19 
     20 class SkScalerContext_FreeType_Base : public SkScalerContext {
     21 protected:
     22     // See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
     23     // This value was chosen by eyeballing the result in Firefox and trying to match it.
     24     static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
     25 
     26     SkScalerContext_FreeType_Base(SkTypeface* typeface, const SkDescriptor *desc)
     27     : INHERITED(typeface, desc)
     28     {}
     29 
     30     void generateGlyphImage(FT_Face face, const SkGlyph& glyph);
     31     void generateGlyphPath(FT_Face face, SkPath* path);
     32 
     33 private:
     34     typedef SkScalerContext INHERITED;
     35 };
     36 
     37 class SkTypeface_FreeType : public SkTypeface {
     38 public:
     39     /** For SkFontMgrs to make use of our ability to extract
     40      *  name and style from a stream, using FreeType's API.
     41      */
     42     class Scanner : ::SkNoncopyable {
     43     public:
     44         Scanner();
     45         ~Scanner();
     46         bool recognizedFont(SkStream* stream, int* numFonts) const;
     47         bool scanFont(SkStream* stream, int ttcIndex,
     48                       SkString* name, SkFontStyle* style, bool* isFixedPitch) const;
     49     private:
     50         FT_Face openFace(SkStream* stream, int ttcIndex, FT_Stream ftStream) const;
     51         FT_Library fLibrary;
     52         mutable SkMutex fLibraryMutex;
     53     };
     54 
     55 protected:
     56     SkTypeface_FreeType(const SkFontStyle& style, SkFontID uniqueID, bool isFixedPitch)
     57         : INHERITED(style, uniqueID, isFixedPitch)
     58         , fGlyphCount(-1)
     59     {}
     60 
     61     virtual SkScalerContext* onCreateScalerContext(
     62                                         const SkDescriptor*) const override;
     63     void onFilterRec(SkScalerContextRec*) const override;
     64     SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
     65                         PerGlyphInfo, const uint32_t*, uint32_t) const override;
     66     int onGetUPEM() const override;
     67     virtual bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
     68                                        int32_t adjustments[]) const override;
     69     virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[],
     70                                 int glyphCount) const override;
     71     int onCountGlyphs() const override;
     72 
     73     LocalizedStrings* onCreateFamilyNameIterator() const override;
     74 
     75     int onGetTableTags(SkFontTableTag tags[]) const override;
     76     virtual size_t onGetTableData(SkFontTableTag, size_t offset,
     77                                   size_t length, void* data) const override;
     78 
     79 private:
     80     mutable int fGlyphCount;
     81 
     82     typedef SkTypeface INHERITED;
     83 };
     84 
     85 #endif // SKFONTHOST_FREETYPE_COMMON_H_
     86