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/render/cpdf_charposlist.h"
8
9 #include "core/fpdfapi/font/cpdf_cidfont.h"
10 #include "core/fpdfapi/font/cpdf_font.h"
11 #include "third_party/base/stl_util.h"
12
CPDF_CharPosList()13 CPDF_CharPosList::CPDF_CharPosList() {
14 m_pCharPos = nullptr;
15 m_nChars = 0;
16 }
17
~CPDF_CharPosList()18 CPDF_CharPosList::~CPDF_CharPosList() {
19 FX_Free(m_pCharPos);
20 }
21
Load(const std::vector<uint32_t> & charCodes,const std::vector<FX_FLOAT> & charPos,CPDF_Font * pFont,FX_FLOAT FontSize)22 void CPDF_CharPosList::Load(const std::vector<uint32_t>& charCodes,
23 const std::vector<FX_FLOAT>& charPos,
24 CPDF_Font* pFont,
25 FX_FLOAT FontSize) {
26 int nChars = pdfium::CollectionSize<int>(charCodes);
27 m_pCharPos = FX_Alloc(FXTEXT_CHARPOS, nChars);
28 m_nChars = 0;
29 CPDF_CIDFont* pCIDFont = pFont->AsCIDFont();
30 bool bVertWriting = pCIDFont && pCIDFont->IsVertWriting();
31 for (int iChar = 0; iChar < nChars; iChar++) {
32 uint32_t CharCode = charCodes[iChar];
33 if (CharCode == static_cast<uint32_t>(-1))
34 continue;
35
36 bool bVert = false;
37 FXTEXT_CHARPOS& charpos = m_pCharPos[m_nChars++];
38 if (pCIDFont)
39 charpos.m_bFontStyle = true;
40
41 charpos.m_GlyphIndex = pFont->GlyphFromCharCode(CharCode, &bVert);
42 if (charpos.m_GlyphIndex != static_cast<uint32_t>(-1)) {
43 charpos.m_FallbackFontPosition = -1;
44 } else {
45 charpos.m_FallbackFontPosition =
46 pFont->FallbackFontFromCharcode(CharCode);
47 charpos.m_GlyphIndex = pFont->FallbackGlyphFromCharcode(
48 charpos.m_FallbackFontPosition, CharCode);
49 }
50
51 // TODO(npm): Figure out how this affects m_ExtGID
52 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
53 charpos.m_ExtGID = pFont->GlyphFromCharCodeExt(CharCode);
54 #endif
55 if (!pFont->IsEmbedded() && !pFont->IsCIDFont())
56 charpos.m_FontCharWidth = pFont->GetCharWidthF(CharCode);
57 else
58 charpos.m_FontCharWidth = 0;
59
60 charpos.m_Origin = CFX_PointF(iChar ? charPos[iChar - 1] : 0, 0);
61 charpos.m_bGlyphAdjust = false;
62 if (!pCIDFont)
63 continue;
64
65 uint16_t CID = pCIDFont->CIDFromCharCode(CharCode);
66 if (bVertWriting) {
67 charpos.m_Origin = CFX_PointF(0, charpos.m_Origin.x);
68
69 short vx;
70 short vy;
71 pCIDFont->GetVertOrigin(CID, vx, vy);
72 charpos.m_Origin.x -= FontSize * vx / 1000;
73 charpos.m_Origin.y -= FontSize * vy / 1000;
74 }
75
76 const uint8_t* pTransform = pCIDFont->GetCIDTransform(CID);
77 if (pTransform && !bVert) {
78 charpos.m_AdjustMatrix[0] = pCIDFont->CIDTransformToFloat(pTransform[0]);
79 charpos.m_AdjustMatrix[2] = pCIDFont->CIDTransformToFloat(pTransform[2]);
80 charpos.m_AdjustMatrix[1] = pCIDFont->CIDTransformToFloat(pTransform[1]);
81 charpos.m_AdjustMatrix[3] = pCIDFont->CIDTransformToFloat(pTransform[3]);
82 charpos.m_Origin.x +=
83 pCIDFont->CIDTransformToFloat(pTransform[4]) * FontSize;
84 charpos.m_Origin.y +=
85 pCIDFont->CIDTransformToFloat(pTransform[5]) * FontSize;
86 charpos.m_bGlyphAdjust = true;
87 }
88 }
89 }
90