blob: a9ca42b35fef4d55448a96b3cd8c3911a482c38a [file] [log] [blame]
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001/*
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"
mtklein1b249332015-07-07 12:21:21 -070013#include "SkMutex.h"
george@mozilla.comc59b5da2012-08-23 00:39:08 +000014#include "SkScalerContext.h"
reed@google.com0da48612013-03-19 16:06:52 +000015#include "SkTypeface.h"
bungeman14df8332014-10-28 15:07:23 -070016#include "SkTypes.h"
reed@google.com0da48612013-03-19 16:06:52 +000017
george@mozilla.comc59b5da2012-08-23 00:39:08 +000018#include <ft2build.h>
19#include FT_FREETYPE_H
20
george@mozilla.comc59b5da2012-08-23 00:39:08 +000021class SkScalerContext_FreeType_Base : public SkScalerContext {
reed@google.com0da48612013-03-19 16:06:52 +000022protected:
george@mozilla.comc59b5da2012-08-23 00:39:08 +000023 // See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
24 // This value was chosen by eyeballing the result in Firefox and trying to match it.
25 static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +000026
reed@google.com0da48612013-03-19 16:06:52 +000027 SkScalerContext_FreeType_Base(SkTypeface* typeface, const SkDescriptor *desc)
28 : INHERITED(typeface, desc)
george@mozilla.comc59b5da2012-08-23 00:39:08 +000029 {}
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +000030
bungeman@google.coma76de722012-10-26 19:35:54 +000031 void generateGlyphImage(FT_Face face, const SkGlyph& glyph);
sugoi@google.com66a58ac2013-03-05 20:40:52 +000032 void generateGlyphPath(FT_Face face, SkPath* path);
reed@google.com0da48612013-03-19 16:06:52 +000033
34private:
35 typedef SkScalerContext INHERITED;
36};
37
38class SkTypeface_FreeType : public SkTypeface {
bungeman3a21d612014-07-11 08:52:26 -070039public:
40 /** For SkFontMgrs to make use of our ability to extract
41 * name and style from a stream, using FreeType's API.
42 */
bungeman14df8332014-10-28 15:07:23 -070043 class Scanner : ::SkNoncopyable {
44 public:
45 Scanner();
46 ~Scanner();
bungeman41868fe2015-05-20 09:21:04 -070047 struct AxisDefinition {
48 SkFourByteTag fTag;
49 SkFixed fMinimum;
50 SkFixed fDefault;
51 SkFixed fMaximum;
52 };
53 using AxisDefinitions = SkSTArray<4, AxisDefinition, true>;
bungeman14df8332014-10-28 15:07:23 -070054 bool recognizedFont(SkStream* stream, int* numFonts) const;
55 bool scanFont(SkStream* stream, int ttcIndex,
bungeman41868fe2015-05-20 09:21:04 -070056 SkString* name, SkFontStyle* style, bool* isFixedPitch,
57 AxisDefinitions* axes) const;
bungeman14df8332014-10-28 15:07:23 -070058 private:
59 FT_Face openFace(SkStream* stream, int ttcIndex, FT_Stream ftStream) const;
60 FT_Library fLibrary;
61 mutable SkMutex fLibraryMutex;
62 };
bungeman3a21d612014-07-11 08:52:26 -070063
reed@google.com0da48612013-03-19 16:06:52 +000064protected:
bungemana4c4a2d2014-10-20 13:33:19 -070065 SkTypeface_FreeType(const SkFontStyle& style, SkFontID uniqueID, bool isFixedPitch)
reed@google.comb4162b12013-07-02 16:32:29 +000066 : INHERITED(style, uniqueID, isFixedPitch)
67 , fGlyphCount(-1)
68 {}
reed@google.com0da48612013-03-19 16:06:52 +000069
70 virtual SkScalerContext* onCreateScalerContext(
mtklein36352bf2015-03-25 18:17:31 -070071 const SkDescriptor*) const override;
72 void onFilterRec(SkScalerContextRec*) const override;
reed39a9a502015-05-12 09:50:04 -070073 SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
74 PerGlyphInfo, const uint32_t*, uint32_t) const override;
mtklein36352bf2015-03-25 18:17:31 -070075 int onGetUPEM() const override;
reed@google.com35fe7372013-10-30 15:07:03 +000076 virtual bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
mtklein36352bf2015-03-25 18:17:31 -070077 int32_t adjustments[]) const override;
reed@google.comb4162b12013-07-02 16:32:29 +000078 virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[],
mtklein36352bf2015-03-25 18:17:31 -070079 int glyphCount) const override;
80 int onCountGlyphs() const override;
reed@google.comb4162b12013-07-02 16:32:29 +000081
mtklein36352bf2015-03-25 18:17:31 -070082 LocalizedStrings* onCreateFamilyNameIterator() const override;
bungeman@google.coma9802692013-08-07 02:45:25 +000083
mtklein36352bf2015-03-25 18:17:31 -070084 int onGetTableTags(SkFontTableTag tags[]) const override;
bungeman@google.comddc218e2013-08-01 22:29:43 +000085 virtual size_t onGetTableData(SkFontTableTag, size_t offset,
mtklein36352bf2015-03-25 18:17:31 -070086 size_t length, void* data) const override;
bungeman@google.comddc218e2013-08-01 22:29:43 +000087
reed@google.com0da48612013-03-19 16:06:52 +000088private:
reed@google.comb4162b12013-07-02 16:32:29 +000089 mutable int fGlyphCount;
90
reed@google.com0da48612013-03-19 16:06:52 +000091 typedef SkTypeface INHERITED;
george@mozilla.comc59b5da2012-08-23 00:39:08 +000092};
93
94#endif // SKFONTHOST_FREETYPE_COMMON_H_