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_textrenderer.h"
8
9 #include <algorithm>
10
11 #include "core/fpdfapi/font/cpdf_font.h"
12 #include "core/fpdfapi/render/cpdf_charposlist.h"
13 #include "core/fpdfapi/render/cpdf_renderoptions.h"
14 #include "core/fxge/cfx_graphstatedata.h"
15 #include "core/fxge/cfx_pathdata.h"
16 #include "core/fxge/cfx_renderdevice.h"
17 #include "core/fxge/fx_font.h"
18 #include "core/fxge/text_char_pos.h"
19
20 namespace {
21
GetFont(CPDF_Font * pFont,int32_t position)22 CFX_Font* GetFont(CPDF_Font* pFont, int32_t position) {
23 return position == -1 ? pFont->GetFont() : pFont->GetFontFallback(position);
24 }
25
26 } // namespace
27
28 // static
DrawTextPath(CFX_RenderDevice * pDevice,const std::vector<uint32_t> & charCodes,const std::vector<float> & charPos,CPDF_Font * pFont,float font_size,const CFX_Matrix & mtText2User,const CFX_Matrix * pUser2Device,const CFX_GraphStateData * pGraphState,FX_ARGB fill_argb,FX_ARGB stroke_argb,CFX_PathData * pClippingPath,int nFlag)29 bool CPDF_TextRenderer::DrawTextPath(CFX_RenderDevice* pDevice,
30 const std::vector<uint32_t>& charCodes,
31 const std::vector<float>& charPos,
32 CPDF_Font* pFont,
33 float font_size,
34 const CFX_Matrix& mtText2User,
35 const CFX_Matrix* pUser2Device,
36 const CFX_GraphStateData* pGraphState,
37 FX_ARGB fill_argb,
38 FX_ARGB stroke_argb,
39 CFX_PathData* pClippingPath,
40 int nFlag) {
41 const CPDF_CharPosList CharPosList(charCodes, charPos, pFont, font_size);
42 const std::vector<TextCharPos>& pos = CharPosList.Get();
43 if (pos.empty())
44 return true;
45
46 bool bDraw = true;
47 int32_t fontPosition = pos[0].m_FallbackFontPosition;
48 size_t startIndex = 0;
49 for (size_t i = 0; i < pos.size(); ++i) {
50 int32_t curFontPosition = pos[i].m_FallbackFontPosition;
51 if (fontPosition == curFontPosition)
52 continue;
53
54 CFX_Font* font = GetFont(pFont, fontPosition);
55 if (!pDevice->DrawTextPath(i - startIndex, &pos[startIndex], font,
56 font_size, mtText2User, pUser2Device,
57 pGraphState, fill_argb, stroke_argb,
58 pClippingPath, nFlag)) {
59 bDraw = false;
60 }
61 fontPosition = curFontPosition;
62 startIndex = i;
63 }
64 CFX_Font* font = GetFont(pFont, fontPosition);
65 if (!pDevice->DrawTextPath(pos.size() - startIndex, &pos[startIndex], font,
66 font_size, mtText2User, pUser2Device, pGraphState,
67 fill_argb, stroke_argb, pClippingPath, nFlag)) {
68 bDraw = false;
69 }
70 return bDraw;
71 }
72
73 // static
DrawTextString(CFX_RenderDevice * pDevice,float origin_x,float origin_y,CPDF_Font * pFont,float font_size,const CFX_Matrix & matrix,const ByteString & str,FX_ARGB fill_argb,const CPDF_RenderOptions & options)74 void CPDF_TextRenderer::DrawTextString(CFX_RenderDevice* pDevice,
75 float origin_x,
76 float origin_y,
77 CPDF_Font* pFont,
78 float font_size,
79 const CFX_Matrix& matrix,
80 const ByteString& str,
81 FX_ARGB fill_argb,
82 const CPDF_RenderOptions& options) {
83 if (pFont->IsType3Font())
84 return;
85
86 int nChars = pFont->CountChar(str.AsStringView());
87 if (nChars <= 0)
88 return;
89
90 size_t offset = 0;
91 std::vector<uint32_t> codes;
92 std::vector<float> positions;
93 codes.resize(nChars);
94 positions.resize(nChars - 1);
95 float cur_pos = 0;
96 for (int i = 0; i < nChars; i++) {
97 codes[i] = pFont->GetNextChar(str.AsStringView(), &offset);
98 if (i)
99 positions[i - 1] = cur_pos;
100 cur_pos += pFont->GetCharWidthF(codes[i]) * font_size / 1000;
101 }
102 CFX_Matrix new_matrix = matrix;
103 new_matrix.e = origin_x;
104 new_matrix.f = origin_y;
105 DrawNormalText(pDevice, codes, positions, pFont, font_size, new_matrix,
106 fill_argb, options);
107 }
108
109 // static
DrawNormalText(CFX_RenderDevice * pDevice,const std::vector<uint32_t> & charCodes,const std::vector<float> & charPos,CPDF_Font * pFont,float font_size,const CFX_Matrix & mtText2Device,FX_ARGB fill_argb,const CPDF_RenderOptions & options)110 bool CPDF_TextRenderer::DrawNormalText(CFX_RenderDevice* pDevice,
111 const std::vector<uint32_t>& charCodes,
112 const std::vector<float>& charPos,
113 CPDF_Font* pFont,
114 float font_size,
115 const CFX_Matrix& mtText2Device,
116 FX_ARGB fill_argb,
117 const CPDF_RenderOptions& options) {
118 const CPDF_CharPosList CharPosList(charCodes, charPos, pFont, font_size);
119 const std::vector<TextCharPos>& pos = CharPosList.Get();
120 if (pos.empty())
121 return true;
122
123 int fxge_flags = 0;
124 if (options.GetOptions().bClearType) {
125 fxge_flags |= FXTEXT_CLEARTYPE;
126 if (options.GetOptions().bBGRStripe)
127 fxge_flags |= FXTEXT_BGR_STRIPE;
128 }
129 if (options.GetOptions().bNoTextSmooth)
130 fxge_flags |= FXTEXT_NOSMOOTH;
131 if (options.GetOptions().bPrintGraphicText)
132 fxge_flags |= FXTEXT_PRINTGRAPHICTEXT;
133 if (options.GetOptions().bNoNativeText)
134 fxge_flags |= FXTEXT_NO_NATIVETEXT;
135 if (options.GetOptions().bPrintImageText)
136 fxge_flags |= FXTEXT_PRINTIMAGETEXT;
137
138 if (pFont->IsCIDFont())
139 fxge_flags |= FXFONT_CIDFONT;
140
141 bool bDraw = true;
142 int32_t fontPosition = pos[0].m_FallbackFontPosition;
143 size_t startIndex = 0;
144 for (size_t i = 0; i < pos.size(); ++i) {
145 int32_t curFontPosition = pos[i].m_FallbackFontPosition;
146 if (fontPosition == curFontPosition)
147 continue;
148
149 CFX_Font* font = GetFont(pFont, fontPosition);
150 if (!pDevice->DrawNormalText(i - startIndex, &pos[startIndex], font,
151 font_size, mtText2Device, fill_argb,
152 fxge_flags)) {
153 bDraw = false;
154 }
155 fontPosition = curFontPosition;
156 startIndex = i;
157 }
158 CFX_Font* font = GetFont(pFont, fontPosition);
159 if (!pDevice->DrawNormalText(pos.size() - startIndex, &pos[startIndex], font,
160 font_size, mtText2Device, fill_argb,
161 fxge_flags)) {
162 bDraw = false;
163 }
164 return bDraw;
165 }
166