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