OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "fx_fpf.h" | 7 #include "fx_fpf.h" |
8 #if _FX_OS_ == _FX_ANDROID_ | 8 #if _FX_OS_ == _FX_ANDROID_ |
9 #include "fpf_skiafont.h" | 9 #include "fpf_skiafont.h" |
10 #include "fpf_skiafontmgr.h" | 10 #include "fpf_skiafontmgr.h" |
11 #define FPF_EM_ADJUST(em, a) (em == 0 ? (a) : (a) * 1000 / em) | 11 #define FPF_EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em) |
12 CFPF_SkiaFont::CFPF_SkiaFont() | 12 CFPF_SkiaFont::CFPF_SkiaFont() |
13 : m_pFontMgr(NULL) | 13 : m_pFontMgr(NULL), |
14 , m_pFontDes(NULL) | 14 m_pFontDes(NULL), |
15 , m_Face(NULL) | 15 m_Face(NULL), |
16 , m_dwStyle(0) | 16 m_dwStyle(0), |
17 , m_uCharset(0) | 17 m_uCharset(0), |
18 , m_dwRefCount(0) | 18 m_dwRefCount(0) { |
19 { | 19 } |
20 } | 20 CFPF_SkiaFont::~CFPF_SkiaFont() { |
21 CFPF_SkiaFont::~CFPF_SkiaFont() | 21 if (m_Face) { |
22 { | 22 FXFT_Done_Face(m_Face); |
23 if (m_Face) { | 23 } |
24 FXFT_Done_Face(m_Face); | 24 } |
| 25 void CFPF_SkiaFont::Release() { |
| 26 if (--m_dwRefCount == 0) { |
| 27 delete this; |
| 28 } |
| 29 } |
| 30 IFPF_Font* CFPF_SkiaFont::Retain() { |
| 31 m_dwRefCount++; |
| 32 return (IFPF_Font*)this; |
| 33 } |
| 34 FPF_HFONT CFPF_SkiaFont::GetHandle() { |
| 35 return NULL; |
| 36 } |
| 37 CFX_ByteString CFPF_SkiaFont::GetFamilyName() { |
| 38 if (!m_Face) { |
| 39 return CFX_ByteString(); |
| 40 } |
| 41 return CFX_ByteString(FXFT_Get_Face_Family_Name(m_Face)); |
| 42 } |
| 43 CFX_WideString CFPF_SkiaFont::GetPsName() { |
| 44 if (!m_Face) { |
| 45 return CFX_WideString(); |
| 46 } |
| 47 return CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(m_Face)); |
| 48 } |
| 49 FX_INT32 CFPF_SkiaFont::GetGlyphIndex(FX_WCHAR wUnicode) { |
| 50 if (!m_Face) { |
| 51 return wUnicode; |
| 52 } |
| 53 if (FXFT_Select_Charmap(m_Face, FXFT_ENCODING_UNICODE)) { |
| 54 return 0; |
| 55 } |
| 56 return FXFT_Get_Char_Index(m_Face, wUnicode); |
| 57 } |
| 58 FX_INT32 CFPF_SkiaFont::GetGlyphWidth(FX_INT32 iGlyphIndex) { |
| 59 if (!m_Face) { |
| 60 return 0; |
| 61 } |
| 62 if (FXFT_Load_Glyph( |
| 63 m_Face, |
| 64 iGlyphIndex, |
| 65 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH)) { |
| 66 return 0; |
| 67 } |
| 68 return FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
| 69 FXFT_Get_Glyph_HoriAdvance(m_Face)); |
| 70 } |
| 71 FX_INT32 CFPF_SkiaFont::GetAscent() const { |
| 72 if (!m_Face) { |
| 73 return 0; |
| 74 } |
| 75 return FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
| 76 FXFT_Get_Face_Ascender(m_Face)); |
| 77 } |
| 78 FX_INT32 CFPF_SkiaFont::GetDescent() const { |
| 79 if (!m_Face) { |
| 80 return 0; |
| 81 } |
| 82 return FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
| 83 FXFT_Get_Face_Descender(m_Face)); |
| 84 } |
| 85 FX_BOOL CFPF_SkiaFont::GetGlyphBBox(FX_INT32 iGlyphIndex, FX_RECT& rtBBox) { |
| 86 if (!m_Face) { |
| 87 return FALSE; |
| 88 } |
| 89 if (FXFT_Is_Face_Tricky(m_Face)) { |
| 90 if (FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72)) { |
| 91 return FALSE; |
25 } | 92 } |
26 } | 93 if (FXFT_Load_Glyph( |
27 void CFPF_SkiaFont::Release() | 94 m_Face, iGlyphIndex, FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH)) { |
28 { | 95 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); |
29 if (--m_dwRefCount == 0) { | 96 return FALSE; |
30 delete this; | |
31 } | 97 } |
32 } | 98 FXFT_Glyph glyph; |
33 IFPF_Font* CFPF_SkiaFont::Retain() | 99 if (FXFT_Get_Glyph(m_Face->glyph, &glyph)) { |
34 { | 100 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); |
35 m_dwRefCount++; | 101 return FALSE; |
36 return (IFPF_Font*)this; | |
37 } | |
38 FPF_HFONT CFPF_SkiaFont::GetHandle() | |
39 { | |
40 return NULL; | |
41 } | |
42 CFX_ByteString CFPF_SkiaFont::GetFamilyName() | |
43 { | |
44 if (!m_Face) { | |
45 return CFX_ByteString(); | |
46 } | 102 } |
47 return CFX_ByteString(FXFT_Get_Face_Family_Name(m_Face)); | 103 FXFT_BBox cbox; |
48 } | 104 FXFT_Glyph_Get_CBox(glyph, FXFT_GLYPH_BBOX_PIXELS, &cbox); |
49 CFX_WideString CFPF_SkiaFont::GetPsName() | 105 FX_INT32 x_ppem = m_Face->size->metrics.x_ppem; |
50 { | 106 FX_INT32 y_ppem = m_Face->size->metrics.y_ppem; |
51 if (!m_Face) { | 107 rtBBox.left = FPF_EM_ADJUST(x_ppem, cbox.xMin); |
52 return CFX_WideString(); | 108 rtBBox.right = FPF_EM_ADJUST(x_ppem, cbox.xMax); |
53 } | 109 rtBBox.top = FPF_EM_ADJUST(y_ppem, cbox.yMax); |
54 return CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(m_Face)); | 110 rtBBox.bottom = FPF_EM_ADJUST(y_ppem, cbox.yMin); |
55 } | 111 rtBBox.top = FX_MIN(rtBBox.top, GetAscent()); |
56 FX_INT32 CFPF_SkiaFont::GetGlyphIndex(FX_WCHAR wUnicode) | 112 rtBBox.bottom = FX_MAX(rtBBox.bottom, GetDescent()); |
57 { | 113 FXFT_Done_Glyph(glyph); |
58 if (!m_Face) { | 114 return FXFT_Set_Pixel_Sizes(m_Face, 0, 64) == 0; |
59 return wUnicode; | 115 } |
60 } | 116 if (FXFT_Load_Glyph( |
61 if (FXFT_Select_Charmap(m_Face, FXFT_ENCODING_UNICODE)) { | 117 m_Face, |
62 return 0; | 118 iGlyphIndex, |
63 } | 119 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH)) { |
64 return FXFT_Get_Char_Index(m_Face, wUnicode); | 120 return FALSE; |
65 } | 121 } |
66 FX_INT32 CFPF_SkiaFont::GetGlyphWidth(FX_INT32 iGlyphIndex) | 122 rtBBox.left = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
67 { | 123 FXFT_Get_Glyph_HoriBearingX(m_Face)); |
68 if (!m_Face) { | 124 rtBBox.bottom = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
69 return 0; | 125 FXFT_Get_Glyph_HoriBearingY(m_Face)); |
70 } | 126 rtBBox.right = FPF_EM_ADJUST( |
71 if (FXFT_Load_Glyph(m_Face, iGlyphIndex, FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNO
RE_GLOBAL_ADVANCE_WIDTH)) { | 127 FXFT_Get_Face_UnitsPerEM(m_Face), |
72 return 0; | 128 FXFT_Get_Glyph_HoriBearingX(m_Face) + FXFT_Get_Glyph_Width(m_Face)); |
73 } | 129 rtBBox.top = FPF_EM_ADJUST( |
74 return FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Glyph_HoriAd
vance(m_Face)); | 130 FXFT_Get_Face_UnitsPerEM(m_Face), |
75 } | 131 FXFT_Get_Glyph_HoriBearingY(m_Face) - FXFT_Get_Glyph_Height(m_Face)); |
76 FX_INT32 CFPF_SkiaFont::GetAscent() const | 132 return TRUE; |
77 { | 133 } |
78 if (!m_Face) { | 134 FX_BOOL CFPF_SkiaFont::GetBBox(FX_RECT& rtBBox) { |
79 return 0; | 135 if (!m_Face) { |
80 } | 136 return FALSE; |
81 return FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_Ascende
r(m_Face)); | 137 } |
82 } | 138 rtBBox.left = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
83 FX_INT32 CFPF_SkiaFont::GetDescent() const | 139 FXFT_Get_Face_xMin(m_Face)); |
84 { | 140 rtBBox.top = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
85 if (!m_Face) { | 141 FXFT_Get_Face_yMin(m_Face)); |
86 return 0; | 142 rtBBox.right = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
87 } | 143 FXFT_Get_Face_xMax(m_Face)); |
88 return FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_Descend
er(m_Face)); | 144 rtBBox.bottom = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
89 } | 145 FXFT_Get_Face_yMax(m_Face)); |
90 FX_BOOL CFPF_SkiaFont::GetGlyphBBox(FX_INT32 iGlyphIndex, FX_RECT &rtBBox) | 146 return TRUE; |
91 { | 147 } |
92 if (!m_Face) { | 148 FX_INT32 CFPF_SkiaFont::GetHeight() const { |
93 return FALSE; | 149 if (!m_Face) { |
94 } | 150 return 0; |
95 if (FXFT_Is_Face_Tricky(m_Face)) { | 151 } |
96 if (FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72)) { | 152 return FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
97 return FALSE; | 153 FXFT_Get_Face_Height(m_Face)); |
98 } | 154 } |
99 if (FXFT_Load_Glyph(m_Face, iGlyphIndex, FXFT_LOAD_IGNORE_GLOBAL_ADVANCE
_WIDTH)) { | 155 FX_INT32 CFPF_SkiaFont::GetItalicAngle() const { |
100 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); | 156 if (!m_Face) { |
101 return FALSE; | 157 return 0; |
102 } | 158 } |
103 FXFT_Glyph glyph; | 159 TT_Postscript* ttInfo = |
104 if (FXFT_Get_Glyph(m_Face->glyph, &glyph)) { | 160 (TT_Postscript*)FT_Get_Sfnt_Table(m_Face, ft_sfnt_post); |
105 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); | 161 if (ttInfo) { |
106 return FALSE; | 162 return ttInfo->italicAngle; |
107 } | 163 } |
108 FXFT_BBox cbox; | 164 return 0; |
109 FXFT_Glyph_Get_CBox(glyph, FXFT_GLYPH_BBOX_PIXELS, &cbox); | 165 } |
110 FX_INT32 x_ppem = m_Face->size->metrics.x_ppem; | 166 FX_DWORD CFPF_SkiaFont::GetFontData(FX_DWORD dwTable, |
111 FX_INT32 y_ppem = m_Face->size->metrics.y_ppem; | 167 FX_LPBYTE pBuffer, |
112 rtBBox.left = FPF_EM_ADJUST(x_ppem, cbox.xMin); | 168 FX_DWORD dwSize) { |
113 rtBBox.right = FPF_EM_ADJUST(x_ppem, cbox.xMax); | 169 if (!m_Face) { |
114 rtBBox.top = FPF_EM_ADJUST(y_ppem, cbox.yMax); | 170 return FALSE; |
115 rtBBox.bottom = FPF_EM_ADJUST(y_ppem, cbox.yMin); | 171 } |
116 rtBBox.top = FX_MIN(rtBBox.top, GetAscent()); | 172 if (FXFT_Load_Sfnt_Table( |
117 rtBBox.bottom = FX_MAX(rtBBox.bottom, GetDescent()); | 173 m_Face, dwTable, 0, pBuffer, (unsigned long*)&dwSize)) { |
118 FXFT_Done_Glyph(glyph); | 174 return 0; |
119 return FXFT_Set_Pixel_Sizes(m_Face, 0, 64) == 0; | 175 } |
120 } | 176 return dwSize; |
121 if (FXFT_Load_Glyph(m_Face, iGlyphIndex, FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNO
RE_GLOBAL_ADVANCE_WIDTH)) { | 177 } |
122 return FALSE; | 178 FX_BOOL CFPF_SkiaFont::InitFont(CFPF_SkiaFontMgr* pFontMgr, |
123 } | 179 CFPF_SkiaFontDescriptor* pFontDes, |
124 rtBBox.left = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Glyph
_HoriBearingX(m_Face)); | 180 FX_BSTR bsFamily, |
125 rtBBox.bottom = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Gly
ph_HoriBearingY(m_Face)); | 181 FX_DWORD dwStyle, |
126 rtBBox.right = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Glyp
h_HoriBearingX(m_Face) + FXFT_Get_Glyph_Width(m_Face)); | 182 FX_BYTE uCharset) { |
127 rtBBox.top = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Glyph_
HoriBearingY(m_Face) - FXFT_Get_Glyph_Height(m_Face)); | 183 if (!pFontMgr || !pFontDes) { |
128 return TRUE; | 184 return FALSE; |
129 } | 185 } |
130 FX_BOOL CFPF_SkiaFont::GetBBox(FX_RECT &rtBBox) | 186 switch (pFontDes->GetType()) { |
131 { | 187 case FPF_SKIAFONTTYPE_Path: { |
132 if (!m_Face) { | 188 CFPF_SkiaPathFont* pFont = (CFPF_SkiaPathFont*)pFontDes; |
133 return FALSE; | 189 m_Face = pFontMgr->GetFontFace(pFont->m_pPath, pFont->m_iFaceIndex); |
134 } | 190 } break; |
135 rtBBox.left = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_
xMin(m_Face)); | 191 case FPF_SKIAFONTTYPE_File: { |
136 rtBBox.top = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_y
Min(m_Face)); | 192 CFPF_SkiaFileFont* pFont = (CFPF_SkiaFileFont*)pFontDes; |
137 rtBBox.right = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face
_xMax(m_Face)); | 193 m_Face = pFontMgr->GetFontFace(pFont->m_pFile, pFont->m_iFaceIndex); |
138 rtBBox.bottom = FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Fac
e_yMax(m_Face)); | 194 } break; |
139 return TRUE; | 195 case FPF_SKIAFONTTYPE_Buffer: { |
140 } | 196 CFPF_SkiaBufferFont* pFont = (CFPF_SkiaBufferFont*)pFontDes; |
141 FX_INT32 CFPF_SkiaFont::GetHeight() const | 197 m_Face = pFontMgr->GetFontFace( |
142 { | 198 (FX_LPCBYTE)pFont->m_pBuffer, pFont->m_szBuffer, pFont->m_iFaceIndex); |
143 if (!m_Face) { | 199 } break; |
144 return 0; | 200 default: |
145 } | 201 return FALSE; |
146 return» FPF_EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_He
ight(m_Face)); | 202 } |
147 } | 203 if (!m_Face) { |
148 FX_INT32 CFPF_SkiaFont::GetItalicAngle() const | 204 return FALSE; |
149 { | 205 } |
150 if (!m_Face) { | 206 m_dwStyle = dwStyle; |
151 return 0; | 207 m_uCharset = uCharset; |
152 } | 208 m_pFontMgr = pFontMgr; |
153 TT_Postscript *ttInfo = (TT_Postscript*)FT_Get_Sfnt_Table(m_Face, ft_sfnt_po
st); | 209 m_pFontDes = pFontDes; |
154 if (ttInfo) { | 210 m_dwRefCount = 1; |
155 return ttInfo->italicAngle; | 211 return TRUE; |
156 } | |
157 return 0; | |
158 } | |
159 FX_DWORD CFPF_SkiaFont::GetFontData(FX_DWORD dwTable, FX_LPBYTE pBuffer, FX_DWOR
D dwSize) | |
160 { | |
161 if (!m_Face) { | |
162 return FALSE; | |
163 } | |
164 if (FXFT_Load_Sfnt_Table(m_Face, dwTable, 0, pBuffer, (unsigned long*)&dwSiz
e)) { | |
165 return 0; | |
166 } | |
167 return dwSize; | |
168 } | |
169 FX_BOOL CFPF_SkiaFont::InitFont(CFPF_SkiaFontMgr *pFontMgr, CFPF_SkiaFontDescrip
tor *pFontDes, FX_BSTR bsFamily, FX_DWORD dwStyle, FX_BYTE uCharset) | |
170 { | |
171 if (!pFontMgr || !pFontDes) { | |
172 return FALSE; | |
173 } | |
174 switch (pFontDes->GetType()) { | |
175 case FPF_SKIAFONTTYPE_Path: { | |
176 CFPF_SkiaPathFont *pFont = (CFPF_SkiaPathFont*)pFontDes; | |
177 m_Face = pFontMgr->GetFontFace(pFont->m_pPath, pFont->m_iFaceInd
ex); | |
178 } | |
179 break; | |
180 case FPF_SKIAFONTTYPE_File: { | |
181 CFPF_SkiaFileFont *pFont = (CFPF_SkiaFileFont*)pFontDes; | |
182 m_Face = pFontMgr->GetFontFace(pFont->m_pFile, pFont->m_iFaceInd
ex); | |
183 } | |
184 break; | |
185 case FPF_SKIAFONTTYPE_Buffer: { | |
186 CFPF_SkiaBufferFont *pFont = (CFPF_SkiaBufferFont*)pFontDes; | |
187 m_Face = pFontMgr->GetFontFace((FX_LPCBYTE)pFont->m_pBuffer, pFo
nt->m_szBuffer, pFont->m_iFaceIndex); | |
188 } | |
189 break; | |
190 default: | |
191 return FALSE; | |
192 } | |
193 if (!m_Face) { | |
194 return FALSE; | |
195 } | |
196 m_dwStyle = dwStyle; | |
197 m_uCharset = uCharset; | |
198 m_pFontMgr = pFontMgr; | |
199 m_pFontDes = pFontDes; | |
200 m_dwRefCount = 1; | |
201 return TRUE; | |
202 } | 212 } |
203 #endif | 213 #endif |
OLD | NEW |