Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: core/src/fxge/ge/fx_ge_font.cpp

Issue 1293973002: Cleanup CFX_Font a bit. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: rebase Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "../../../include/fxge/fx_ge.h" 7 #include "../../../include/fxge/fx_ge.h"
8 #include "../../../include/fxge/fx_freetype.h" 8 #include "../../../include/fxge/fx_freetype.h"
9 #include "text_int.h" 9 #include "text_int.h"
10 #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em) 10 #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em)
11 extern void _FPDFAPI_GetInternalFontData(int id1, 11 extern void _FPDFAPI_GetInternalFontData(int id1,
12 const uint8_t*& data, 12 const uint8_t*& data,
13 FX_DWORD& size); 13 FX_DWORD& size);
14 CFX_Font::CFX_Font() { 14 CFX_Font::CFX_Font() {
15 m_pSubstFont = NULL; 15 m_pSubstFont = NULL;
16 m_Face = NULL; 16 m_Face = NULL;
17 m_bEmbedded = FALSE; 17 m_bEmbedded = FALSE;
18 m_bVertical = FALSE; 18 m_bVertical = FALSE;
19 m_pFontData = NULL; 19 m_pFontData = NULL;
20 m_pFontDataAllocation = NULL; 20 m_pFontDataAllocation = NULL;
21 m_dwSize = 0; 21 m_dwSize = 0;
22 m_pOwnedStream = NULL;
23 m_pGsubData = NULL; 22 m_pGsubData = NULL;
24 m_pPlatformFont = NULL; 23 m_pPlatformFont = NULL;
25 m_pPlatformFontCollection = NULL; 24 m_pPlatformFontCollection = NULL;
26 m_pDwFont = NULL; 25 m_pDwFont = NULL;
27 m_hHandle = NULL; 26 m_hHandle = NULL;
28 m_bDwLoaded = FALSE; 27 m_bDwLoaded = FALSE;
29 } 28 }
30 CFX_Font::~CFX_Font() { 29 CFX_Font::~CFX_Font() {
31 delete m_pSubstFont; 30 delete m_pSubstFont;
32 m_pSubstFont = NULL; 31 m_pSubstFont = NULL;
33 FX_Free(m_pFontDataAllocation); 32 FX_Free(m_pFontDataAllocation);
34 m_pFontDataAllocation = NULL; 33 m_pFontDataAllocation = NULL;
35 if (m_Face) { 34 if (m_Face) {
36 if (FXFT_Get_Face_External_Stream(m_Face)) { 35 if (FXFT_Get_Face_External_Stream(m_Face)) {
37 FXFT_Clear_Face_External_Stream(m_Face); 36 FXFT_Clear_Face_External_Stream(m_Face);
38 } 37 }
39 if (m_bEmbedded) { 38 if (m_bEmbedded) {
40 DeleteFace(); 39 DeleteFace();
41 } else { 40 } else {
42 CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face); 41 CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face);
43 } 42 }
44 } 43 }
45 FX_Free(m_pOwnedStream);
46 m_pOwnedStream = NULL;
47 FX_Free(m_pGsubData); 44 FX_Free(m_pGsubData);
48 m_pGsubData = NULL; 45 m_pGsubData = NULL;
49 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ 46 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
50 ReleasePlatformResource(); 47 ReleasePlatformResource();
51 #endif 48 #endif
52 } 49 }
53 void CFX_Font::DeleteFace() { 50 void CFX_Font::DeleteFace() {
54 FXFT_Done_Face(m_Face); 51 FXFT_Done_Face(m_Face);
55 m_Face = NULL; 52 m_Face = NULL;
56 } 53 }
(...skipping 15 matching lines...) Expand all
72 m_pPlatformFont = m_pSubstFont->m_ExtHandle; 69 m_pPlatformFont = m_pSubstFont->m_ExtHandle;
73 m_pSubstFont->m_ExtHandle = NULL; 70 m_pSubstFont->m_ExtHandle = NULL;
74 } 71 }
75 #endif 72 #endif
76 if (m_Face) { 73 if (m_Face) {
77 m_pFontData = FXFT_Get_Face_Stream_Base(m_Face); 74 m_pFontData = FXFT_Get_Face_Stream_Base(m_Face);
78 m_dwSize = FXFT_Get_Face_Stream_Size(m_Face); 75 m_dwSize = FXFT_Get_Face_Stream_Size(m_Face);
79 } 76 }
80 return TRUE; 77 return TRUE;
81 } 78 }
82 extern "C" { 79
83 unsigned long _FTStreamRead(FXFT_Stream stream,
84 unsigned long offset,
85 unsigned char* buffer,
86 unsigned long count) {
87 if (count == 0) {
88 return 0;
89 }
90 IFX_FileRead* pFile = (IFX_FileRead*)stream->descriptor.pointer;
91 int res = pFile->ReadBlock(buffer, offset, count);
92 if (res) {
93 return count;
94 }
95 return 0;
96 }
97 void _FTStreamClose(FXFT_Stream stream) {}
98 };
99 FX_BOOL _LoadFile(FXFT_Library library,
100 FXFT_Face* Face,
101 IFX_FileRead* pFile,
102 FXFT_Stream* stream) {
103 FXFT_Stream stream1 = (FXFT_Stream)FX_Alloc(uint8_t, sizeof(FXFT_StreamRec));
104 stream1->base = NULL;
105 stream1->size = (unsigned long)pFile->GetSize();
106 stream1->pos = 0;
107 stream1->descriptor.pointer = pFile;
108 stream1->close = _FTStreamClose;
109 stream1->read = _FTStreamRead;
110 FXFT_Open_Args args;
111 args.flags = FT_OPEN_STREAM;
112 args.stream = stream1;
113 if (FXFT_Open_Face(library, &args, 0, Face)) {
114 FX_Free(stream1);
115 return FALSE;
116 }
117 if (stream) {
118 *stream = stream1;
119 }
120 return TRUE;
121 }
122 FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile) {
123 m_bEmbedded = FALSE;
124 FXFT_Library library;
125 if (CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary == NULL) {
126 FXFT_Init_FreeType(&CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary);
127 }
128 library = CFX_GEModule::Get()->GetFontMgr()->m_FTLibrary;
129 FXFT_Stream stream = NULL;
130 if (!_LoadFile(library, &m_Face, pFile, &stream)) {
131 return FALSE;
132 }
133 m_pOwnedStream = stream;
134 FXFT_Set_Pixel_Sizes(m_Face, 0, 64);
135 return TRUE;
136 }
137 int CFX_Font::GetGlyphWidth(FX_DWORD glyph_index) { 80 int CFX_Font::GetGlyphWidth(FX_DWORD glyph_index) {
138 if (!m_Face) { 81 if (!m_Face) {
139 return 0; 82 return 0;
140 } 83 }
141 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { 84 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) {
142 AdjustMMParams(glyph_index, 0, 0); 85 AdjustMMParams(glyph_index, 0, 0);
143 } 86 }
144 int err = FXFT_Load_Glyph( 87 int err = FXFT_Load_Glyph(
145 m_Face, glyph_index, 88 m_Face, glyph_index,
146 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); 89 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
(...skipping 23 matching lines...) Expand all
170 } 113 }
171 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, FX_DWORD size) { 114 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, FX_DWORD size) {
172 m_pFontDataAllocation = FX_Alloc(uint8_t, size); 115 m_pFontDataAllocation = FX_Alloc(uint8_t, size);
173 FXSYS_memcpy(m_pFontDataAllocation, data, size); 116 FXSYS_memcpy(m_pFontDataAllocation, data, size);
174 m_Face = FT_LoadFont((uint8_t*)m_pFontDataAllocation, size); 117 m_Face = FT_LoadFont((uint8_t*)m_pFontDataAllocation, size);
175 m_pFontData = (uint8_t*)m_pFontDataAllocation; 118 m_pFontData = (uint8_t*)m_pFontDataAllocation;
176 m_bEmbedded = TRUE; 119 m_bEmbedded = TRUE;
177 m_dwSize = size; 120 m_dwSize = size;
178 return m_Face != NULL; 121 return m_Face != NULL;
179 } 122 }
180 FX_BOOL CFX_Font::IsTTFont() { 123
181 if (m_Face == NULL) { 124 FX_BOOL CFX_Font::IsTTFont() const {
125 if (!m_Face)
182 return FALSE; 126 return FALSE;
183 }
184 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; 127 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT;
185 } 128 }
129
186 int CFX_Font::GetAscent() const { 130 int CFX_Font::GetAscent() const {
187 if (m_Face == NULL) { 131 if (!m_Face)
188 return 0; 132 return 0;
189 } 133 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face),
190 int ascent = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), 134 FXFT_Get_Face_Ascender(m_Face));
191 FXFT_Get_Face_Ascender(m_Face));
192 return ascent;
193 } 135 }
136
194 int CFX_Font::GetDescent() const { 137 int CFX_Font::GetDescent() const {
195 if (m_Face == NULL) { 138 if (!m_Face)
196 return 0; 139 return 0;
197 } 140 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face),
198 int descent = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), 141 FXFT_Get_Face_Descender(m_Face));
199 FXFT_Get_Face_Descender(m_Face));
200 return descent;
201 } 142 }
143
202 FX_BOOL CFX_Font::GetGlyphBBox(FX_DWORD glyph_index, FX_RECT& bbox) { 144 FX_BOOL CFX_Font::GetGlyphBBox(FX_DWORD glyph_index, FX_RECT& bbox) {
203 if (m_Face == NULL) { 145 if (!m_Face)
204 return FALSE; 146 return FALSE;
205 } 147
206 if (FXFT_Is_Face_Tricky(m_Face)) { 148 if (FXFT_Is_Face_Tricky(m_Face)) {
207 int error = FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72); 149 int error = FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72);
208 if (error) { 150 if (error) {
209 return FALSE; 151 return FALSE;
210 } 152 }
211 error = FXFT_Load_Glyph(m_Face, glyph_index, 153 error = FXFT_Load_Glyph(m_Face, glyph_index,
212 FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); 154 FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
213 if (error) { 155 if (error) {
214 return FALSE; 156 return FALSE;
215 } 157 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 bbox.top = 200 bbox.top =
259 (FXFT_Get_Glyph_HoriBearingY(m_Face) - FXFT_Get_Glyph_Height(m_Face)) * 201 (FXFT_Get_Glyph_HoriBearingY(m_Face) - FXFT_Get_Glyph_Height(m_Face)) *
260 1000 / em; 202 1000 / em;
261 bbox.right = 203 bbox.right =
262 (FXFT_Get_Glyph_HoriBearingX(m_Face) + FXFT_Get_Glyph_Width(m_Face)) * 204 (FXFT_Get_Glyph_HoriBearingX(m_Face) + FXFT_Get_Glyph_Width(m_Face)) *
263 1000 / em; 205 1000 / em;
264 bbox.bottom = (FXFT_Get_Glyph_HoriBearingY(m_Face)) * 1000 / em; 206 bbox.bottom = (FXFT_Get_Glyph_HoriBearingY(m_Face)) * 1000 / em;
265 } 207 }
266 return TRUE; 208 return TRUE;
267 } 209 }
268 FX_BOOL CFX_Font::IsItalic() { 210
269 if (m_Face == NULL) { 211 FX_BOOL CFX_Font::IsItalic() const {
212 if (!m_Face)
270 return FALSE; 213 return FALSE;
271 } 214
272 FX_BOOL ret = FXFT_Is_Face_Italic(m_Face) == FXFT_STYLE_FLAG_ITALIC; 215 FX_BOOL ret = FXFT_Is_Face_Italic(m_Face) == FXFT_STYLE_FLAG_ITALIC;
273 if (!ret) { 216 if (!ret) {
274 CFX_ByteString str(FXFT_Get_Face_Style_Name(m_Face)); 217 CFX_ByteString str(FXFT_Get_Face_Style_Name(m_Face));
275 str.MakeLower(); 218 str.MakeLower();
276 if (str.Find("italic") != -1) { 219 if (str.Find("italic") != -1) {
277 ret = TRUE; 220 ret = TRUE;
278 } 221 }
279 } 222 }
280 return ret; 223 return ret;
281 } 224 }
282 FX_BOOL CFX_Font::IsBold() { 225
283 if (m_Face == NULL) { 226 FX_BOOL CFX_Font::IsBold() const {
227 if (!m_Face)
284 return FALSE; 228 return FALSE;
285 }
286 return FXFT_Is_Face_Bold(m_Face) == FXFT_STYLE_FLAG_BOLD; 229 return FXFT_Is_Face_Bold(m_Face) == FXFT_STYLE_FLAG_BOLD;
287 } 230 }
288 FX_BOOL CFX_Font::IsFixedWidth() { 231
289 if (m_Face == NULL) { 232 FX_BOOL CFX_Font::IsFixedWidth() const {
233 if (!m_Face)
290 return FALSE; 234 return FALSE;
291 }
292 return FXFT_Is_Face_fixedwidth(m_Face); 235 return FXFT_Is_Face_fixedwidth(m_Face);
293 } 236 }
237
294 CFX_WideString CFX_Font::GetPsName() const { 238 CFX_WideString CFX_Font::GetPsName() const {
295 if (m_Face == NULL) { 239 if (m_Face == NULL) {
296 return CFX_WideString(); 240 return CFX_WideString();
297 } 241 }
298 CFX_WideString psName = 242 CFX_WideString psName =
299 CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(m_Face)); 243 CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(m_Face));
300 if (psName.IsEmpty()) { 244 if (psName.IsEmpty()) {
301 psName = CFX_WideString::FromLocal("Untitled"); 245 psName = CFX_WideString::FromLocal("Untitled");
302 } 246 }
303 return psName; 247 return psName;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 bbox.top = FXFT_Get_Face_yMin(m_Face); 284 bbox.top = FXFT_Get_Face_yMin(m_Face);
341 bbox.right = FXFT_Get_Face_xMax(m_Face); 285 bbox.right = FXFT_Get_Face_xMax(m_Face);
342 } else { 286 } else {
343 bbox.left = FXFT_Get_Face_xMin(m_Face) * 1000 / em; 287 bbox.left = FXFT_Get_Face_xMin(m_Face) * 1000 / em;
344 bbox.top = FXFT_Get_Face_yMin(m_Face) * 1000 / em; 288 bbox.top = FXFT_Get_Face_yMin(m_Face) * 1000 / em;
345 bbox.right = FXFT_Get_Face_xMax(m_Face) * 1000 / em; 289 bbox.right = FXFT_Get_Face_xMax(m_Face) * 1000 / em;
346 bbox.bottom = FXFT_Get_Face_yMax(m_Face) * 1000 / em; 290 bbox.bottom = FXFT_Get_Face_yMax(m_Face) * 1000 / em;
347 } 291 }
348 return TRUE; 292 return TRUE;
349 } 293 }
350 int CFX_Font::GetHeight() { 294
351 if (m_Face == NULL) { 295 int CFX_Font::GetHeight() const {
296 if (!m_Face)
352 return 0; 297 return 0;
353 } 298
354 int height = 299 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face),
355 EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), FXFT_Get_Face_Height(m_Face)); 300 FXFT_Get_Face_Height(m_Face));
356 return height;
357 } 301 }
358 int CFX_Font::GetMaxAdvanceWidth() { 302
359 if (m_Face == NULL) { 303 int CFX_Font::GetMaxAdvanceWidth() const {
304 if (!m_Face)
360 return 0; 305 return 0;
361 } 306
362 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), 307 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face),
363 FXFT_Get_Face_MaxAdvanceWidth(m_Face)); 308 FXFT_Get_Face_MaxAdvanceWidth(m_Face));
364 return width;
365 } 309 }
366 int CFX_Font::GetULPos() { 310
367 if (m_Face == NULL) { 311 int CFX_Font::GetULPos() const {
312 if (!m_Face)
368 return 0; 313 return 0;
369 } 314
370 int pos = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), 315 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face),
371 FXFT_Get_Face_UnderLinePosition(m_Face)); 316 FXFT_Get_Face_UnderLinePosition(m_Face));
372 return pos;
373 } 317 }
374 int CFX_Font::GetULthickness() { 318
375 if (m_Face == NULL) { 319 int CFX_Font::GetULthickness() const {
320 if (!m_Face)
376 return 0; 321 return 0;
377 } 322
378 int thickness = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), 323 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face),
379 FXFT_Get_Face_UnderLineThickness(m_Face)); 324 FXFT_Get_Face_UnderLineThickness(m_Face));
380 return thickness;
381 } 325 }
382 326
383 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) : m_pFont(pFont) { 327 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) : m_pFont(pFont) {
384 } 328 }
385 329
386 CFX_UnicodeEncoding::~CFX_UnicodeEncoding() { 330 CFX_UnicodeEncoding::~CFX_UnicodeEncoding() {
387 } 331 }
388 332
389 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode) { 333 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode) {
390 FXFT_Face face = m_pFont->GetFace(); 334 FXFT_Face face = m_pFont->GetFace();
391 if (!face) 335 if (!face)
392 return charcode; 336 return charcode;
393 337
394 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0) 338 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0)
395 return FXFT_Get_Char_Index(face, charcode); 339 return FXFT_Get_Char_Index(face, charcode);
396 340
397 if (m_pFont->m_pSubstFont && m_pFont->m_pSubstFont->m_Charset == 2) { 341 if (m_pFont->GetSubstFont() && m_pFont->GetSubstFont()->m_Charset == 2) {
398 FX_DWORD index = 0; 342 FX_DWORD index = 0;
399 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0) 343 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0)
400 index = FXFT_Get_Char_Index(face, charcode); 344 index = FXFT_Get_Char_Index(face, charcode);
401 if (!index && !FXFT_Select_Charmap(face, FXFT_ENCODING_APPLE_ROMAN)) 345 if (!index && !FXFT_Select_Charmap(face, FXFT_ENCODING_APPLE_ROMAN))
402 return FXFT_Get_Char_Index(face, charcode); 346 return FXFT_Get_Char_Index(face, charcode);
403 } 347 }
404 return charcode; 348 return charcode;
405 } 349 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698