OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/fpdfapi/fpdf_font/cpdf_simplefont.h" |
| 8 |
| 9 #include "core/fpdfapi/fpdf_font/font_int.h" |
| 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
| 12 #include "core/include/fxge/fx_freetype.h" |
| 13 |
| 14 CPDF_SimpleFont::CPDF_SimpleFont() |
| 15 : m_pCharNames(nullptr), m_BaseEncoding(PDFFONT_ENCODING_BUILTIN) { |
| 16 FXSYS_memset(m_CharWidth, 0xff, sizeof m_CharWidth); |
| 17 FXSYS_memset(m_GlyphIndex, 0xff, sizeof m_GlyphIndex); |
| 18 FXSYS_memset(m_ExtGID, 0xff, sizeof m_ExtGID); |
| 19 } |
| 20 |
| 21 CPDF_SimpleFont::~CPDF_SimpleFont() { |
| 22 delete[] m_pCharNames; |
| 23 } |
| 24 |
| 25 int CPDF_SimpleFont::GlyphFromCharCode(FX_DWORD charcode, FX_BOOL* pVertGlyph) { |
| 26 if (pVertGlyph) { |
| 27 *pVertGlyph = FALSE; |
| 28 } |
| 29 if (charcode > 0xff) { |
| 30 return -1; |
| 31 } |
| 32 int index = m_GlyphIndex[(uint8_t)charcode]; |
| 33 if (index == 0xffff) { |
| 34 return -1; |
| 35 } |
| 36 return index; |
| 37 } |
| 38 |
| 39 void CPDF_SimpleFont::LoadCharMetrics(int charcode) { |
| 40 if (!m_Font.GetFace()) |
| 41 return; |
| 42 |
| 43 if (charcode < 0 || charcode > 0xff) { |
| 44 return; |
| 45 } |
| 46 int glyph_index = m_GlyphIndex[charcode]; |
| 47 if (glyph_index == 0xffff) { |
| 48 if (!m_pFontFile && charcode != 32) { |
| 49 LoadCharMetrics(32); |
| 50 m_CharBBox[charcode] = m_CharBBox[32]; |
| 51 if (m_bUseFontWidth) { |
| 52 m_CharWidth[charcode] = m_CharWidth[32]; |
| 53 } |
| 54 } |
| 55 return; |
| 56 } |
| 57 FXFT_Face face = m_Font.GetFace(); |
| 58 int err = FXFT_Load_Glyph( |
| 59 face, glyph_index, |
| 60 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); |
| 61 if (err) { |
| 62 return; |
| 63 } |
| 64 m_CharBBox[charcode] = FX_SMALL_RECT( |
| 65 TT2PDF(FXFT_Get_Glyph_HoriBearingX(face), face), |
| 66 TT2PDF(FXFT_Get_Glyph_HoriBearingY(face), face), |
| 67 TT2PDF(FXFT_Get_Glyph_HoriBearingX(face) + FXFT_Get_Glyph_Width(face), |
| 68 face), |
| 69 TT2PDF(FXFT_Get_Glyph_HoriBearingY(face) - FXFT_Get_Glyph_Height(face), |
| 70 face)); |
| 71 |
| 72 if (m_bUseFontWidth) { |
| 73 int TT_Width = TT2PDF(FXFT_Get_Glyph_HoriAdvance(face), face); |
| 74 if (m_CharWidth[charcode] == 0xffff) { |
| 75 m_CharWidth[charcode] = TT_Width; |
| 76 } else if (TT_Width && !IsEmbedded()) { |
| 77 m_CharBBox[charcode].right = |
| 78 m_CharBBox[charcode].right * m_CharWidth[charcode] / TT_Width; |
| 79 m_CharBBox[charcode].left = |
| 80 m_CharBBox[charcode].left * m_CharWidth[charcode] / TT_Width; |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 int CPDF_SimpleFont::GetCharWidthF(FX_DWORD charcode, int level) { |
| 86 if (charcode > 0xff) { |
| 87 charcode = 0; |
| 88 } |
| 89 if (m_CharWidth[charcode] == 0xffff) { |
| 90 LoadCharMetrics(charcode); |
| 91 if (m_CharWidth[charcode] == 0xffff) { |
| 92 m_CharWidth[charcode] = 0; |
| 93 } |
| 94 } |
| 95 return (int16_t)m_CharWidth[charcode]; |
| 96 } |
| 97 |
| 98 FX_RECT CPDF_SimpleFont::GetCharBBox(FX_DWORD charcode, int level) { |
| 99 if (charcode > 0xff) |
| 100 charcode = 0; |
| 101 |
| 102 if (m_CharBBox[charcode].left == FX_SMALL_RECT::kInvalid) |
| 103 LoadCharMetrics(charcode); |
| 104 |
| 105 return FX_RECT(m_CharBBox[charcode]); |
| 106 } |
| 107 |
| 108 FX_BOOL CPDF_SimpleFont::LoadCommon() { |
| 109 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictBy("FontDescriptor"); |
| 110 if (pFontDesc) { |
| 111 LoadFontDescriptor(pFontDesc); |
| 112 } |
| 113 CPDF_Array* pWidthArray = m_pFontDict->GetArrayBy("Widths"); |
| 114 int width_start = 0, width_end = -1; |
| 115 m_bUseFontWidth = TRUE; |
| 116 if (pWidthArray) { |
| 117 m_bUseFontWidth = FALSE; |
| 118 if (pFontDesc && pFontDesc->KeyExist("MissingWidth")) { |
| 119 int MissingWidth = pFontDesc->GetIntegerBy("MissingWidth"); |
| 120 for (int i = 0; i < 256; i++) { |
| 121 m_CharWidth[i] = MissingWidth; |
| 122 } |
| 123 } |
| 124 width_start = m_pFontDict->GetIntegerBy("FirstChar", 0); |
| 125 width_end = m_pFontDict->GetIntegerBy("LastChar", 0); |
| 126 if (width_start >= 0 && width_start <= 255) { |
| 127 if (width_end <= 0 || |
| 128 width_end >= width_start + (int)pWidthArray->GetCount()) { |
| 129 width_end = width_start + pWidthArray->GetCount() - 1; |
| 130 } |
| 131 if (width_end > 255) { |
| 132 width_end = 255; |
| 133 } |
| 134 for (int i = width_start; i <= width_end; i++) { |
| 135 m_CharWidth[i] = pWidthArray->GetIntegerAt(i - width_start); |
| 136 } |
| 137 } |
| 138 } |
| 139 if (m_pFontFile) { |
| 140 if (m_BaseFont.GetLength() > 8 && m_BaseFont[7] == '+') { |
| 141 m_BaseFont = m_BaseFont.Mid(8); |
| 142 } |
| 143 } else { |
| 144 LoadSubstFont(); |
| 145 } |
| 146 if (!(m_Flags & PDFFONT_SYMBOLIC)) { |
| 147 m_BaseEncoding = PDFFONT_ENCODING_STANDARD; |
| 148 } |
| 149 CPDF_Object* pEncoding = m_pFontDict->GetElementValue("Encoding"); |
| 150 LoadPDFEncoding(pEncoding, m_BaseEncoding, m_pCharNames, m_pFontFile != NULL, |
| 151 m_Font.IsTTFont()); |
| 152 LoadGlyphMap(); |
| 153 delete[] m_pCharNames; |
| 154 m_pCharNames = NULL; |
| 155 if (!m_Font.GetFace()) |
| 156 return TRUE; |
| 157 |
| 158 if (m_Flags & PDFFONT_ALLCAP) { |
| 159 unsigned char lowercases[] = {'a', 'z', 0xe0, 0xf6, 0xf8, 0xfd}; |
| 160 for (size_t range = 0; range < sizeof lowercases / 2; range++) { |
| 161 for (int i = lowercases[range * 2]; i <= lowercases[range * 2 + 1]; i++) { |
| 162 if (m_GlyphIndex[i] != 0xffff && m_pFontFile) { |
| 163 continue; |
| 164 } |
| 165 m_GlyphIndex[i] = m_GlyphIndex[i - 32]; |
| 166 if (m_CharWidth[i - 32]) { |
| 167 m_CharWidth[i] = m_CharWidth[i - 32]; |
| 168 m_CharBBox[i] = m_CharBBox[i - 32]; |
| 169 } |
| 170 } |
| 171 } |
| 172 } |
| 173 CheckFontMetrics(); |
| 174 return TRUE; |
| 175 } |
| 176 |
| 177 void CPDF_SimpleFont::LoadSubstFont() { |
| 178 if (!m_bUseFontWidth && !(m_Flags & PDFFONT_FIXEDPITCH)) { |
| 179 int width = 0, i; |
| 180 for (i = 0; i < 256; i++) { |
| 181 if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff) { |
| 182 continue; |
| 183 } |
| 184 if (width == 0) { |
| 185 width = m_CharWidth[i]; |
| 186 } else if (width != m_CharWidth[i]) { |
| 187 break; |
| 188 } |
| 189 } |
| 190 if (i == 256 && width) { |
| 191 m_Flags |= PDFFONT_FIXEDPITCH; |
| 192 } |
| 193 } |
| 194 int weight = m_StemV < 140 ? m_StemV * 5 : (m_StemV * 4 + 140); |
| 195 m_Font.LoadSubst(m_BaseFont, IsTrueTypeFont(), m_Flags, weight, m_ItalicAngle, |
| 196 0); |
| 197 if (m_Font.GetSubstFont()->m_SubstFlags & FXFONT_SUBST_NONSYMBOL) { |
| 198 } |
| 199 } |
| 200 |
| 201 FX_BOOL CPDF_SimpleFont::IsUnicodeCompatible() const { |
| 202 return m_BaseEncoding != PDFFONT_ENCODING_BUILTIN && |
| 203 m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL && |
| 204 m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS; |
| 205 } |
| 206 |
| 207 CFX_WideString CPDF_SimpleFont::UnicodeFromCharCode(FX_DWORD charcode) const { |
| 208 CFX_WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode); |
| 209 if (!unicode.IsEmpty()) |
| 210 return unicode; |
| 211 FX_WCHAR ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode); |
| 212 if (ret == 0) |
| 213 return CFX_WideString(); |
| 214 return ret; |
| 215 } |
| 216 |
| 217 FX_DWORD CPDF_SimpleFont::CharCodeFromUnicode(FX_WCHAR unicode) const { |
| 218 FX_DWORD ret = CPDF_Font::CharCodeFromUnicode(unicode); |
| 219 if (ret) |
| 220 return ret; |
| 221 return m_Encoding.CharCodeFromUnicode(unicode); |
| 222 } |
OLD | NEW |