7#include "core/fpdfapi/page/cpdf_textobject.h"
11#include "core/fpdfapi/font/cpdf_cidfont.h"
12#include "core/fpdfapi/font/cpdf_font.h"
13#include "core/fxcrt/fx_coordinates.h"
14#include "third_party/base/check.h"
15#include "third_party/base/containers/span.h"
17#define ISLATINWORD(u) (u != 0x20
&& u <= 0x28FF
)
21bool IsVertWritingCIDFont(
const CPDF_CIDFont* font) {
27CPDF_TextObject::
Item::
Item() =
default;
31CPDF_TextObject::
Item::~
Item() =
default;
41 return m_CharCodes.size();
45 DCHECK(index < m_CharCodes.size());
48 info.m_CharCode = m_CharCodes[index];
49 info.m_Origin = CFX_PointF(index > 0 ? m_CharPos[index - 1] : 0, 0);
54 const CPDF_CIDFont* pCIDFont = pFont->AsCIDFont();
55 if (!IsVertWritingCIDFont(pCIDFont))
59 info.m_Origin = CFX_PointF(0, info.m_Origin.x);
61 CFX_Point16 vertical_origin = pCIDFont->GetVertOrigin(cid);
63 info.m_Origin.x -= fontsize * vertical_origin.x / 1000;
64 info.m_Origin.y -= fontsize * vertical_origin.y / 1000;
70 for (uint32_t charcode : m_CharCodes) {
71 if (charcode != CPDF_Font::kInvalidCharCode)
79 for (uint32_t code : m_CharCodes) {
80 if (code == CPDF_Font::kInvalidCharCode)
91 for (size_t i = 0; i < m_CharCodes.size(); ++i) {
92 uint32_t charcode = m_CharCodes[i];
96 return GetItemInfo(i);
103 bool bInLatinWord =
false;
105 for (size_t i = 0, sz = CountChars(); i < sz; ++i) {
106 uint32_t charcode = GetCharCode(i);
108 WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
109 uint16_t unicode = 0;
110 if (swUnicode.GetLength() > 0)
111 unicode = swUnicode[0];
114 if (bIsLatin && bInLatinWord)
117 bInLatinWord = bIsLatin;
129 bool bInLatinWord =
false;
130 for (size_t i = 0, sz = CountChars(); i < sz; ++i) {
131 uint32_t charcode = GetCharCode(i);
133 WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
134 uint16_t unicode = 0;
135 if (swUnicode.GetLength() > 0)
136 unicode = swUnicode[0];
139 if (!bIsLatin || !bInLatinWord) {
140 bInLatinWord = bIsLatin;
144 if (nWords - 1 == nWordIndex)
151 auto obj =
std::make_unique<CPDF_TextObject>();
153 obj->m_CharCodes = m_CharCodes;
154 obj->m_CharPos = m_CharPos;
172CPDF_TextObject* CPDF_TextObject::
AsText() {
176const CPDF_TextObject* CPDF_TextObject::
AsText()
const {
181 pdfium::span<
const float> pTextMatrix = text_state().GetMatrix();
182 return CFX_Matrix(pTextMatrix[0], pTextMatrix[2], pTextMatrix[1],
183 pTextMatrix[3], m_Pos.x, m_Pos.y);
187 pdfium::span<
float> pTextMatrix = mutable_text_state().GetMutableMatrix();
188 pTextMatrix[0] = matrix
.a;
189 pTextMatrix[1] = matrix
.c;
190 pTextMatrix[2] = matrix
.b;
191 pTextMatrix[3] = matrix
.d;
192 m_Pos = CFX_PointF(matrix.e, matrix.f);
193 CalcPositionDataInternal(
GetFont());
197 const std::vector<
float>& kernings,
203 size_t nChars = nSegs - 1;
204 for (size_t i = 0; i < nSegs; ++i)
205 nChars += pFont->CountChar(pStrs[i].AsStringView());
208 m_CharCodes.resize(nChars);
209 m_CharPos.resize(nChars - 1);
211 for (size_t i = 0; i < nSegs; ++i) {
212 ByteStringView segment = pStrs[i].AsStringView();
214 while (offset < segment.GetLength()) {
215 DCHECK(index < m_CharCodes.size());
216 m_CharCodes[index++] = pFont->GetNextChar(segment, &offset);
218 if (i != nSegs - 1) {
219 m_CharPos[index - 1] = kernings[i];
220 m_CharCodes[index++] = CPDF_Font::kInvalidCharCode;
225void CPDF_TextObject::
SetText(
const ByteString& str) {
226 SetSegments(&str, std::vector<
float>(), 1);
227 CalcPositionDataInternal(
GetFont());
234 const CPDF_CIDFont* pCIDFont = pFont->AsCIDFont();
235 if (!IsVertWritingCIDFont(pCIDFont))
236 return pFont->GetCharWidthF(charcode) * fontsize;
261 const float curpos = CalcPositionDataInternal(pFont);
262 if (IsVertWritingCIDFont(pFont->AsCIDFont()))
264 return {curpos * horz_scale, 0};
267float CPDF_TextObject::CalcPositionDataInternal(
270 float min_x = 10000.0f;
271 float max_x = -10000.0f;
272 float min_y = 10000.0f;
273 float max_y = -10000.0f;
274 const CPDF_CIDFont* pCIDFont = pFont->AsCIDFont();
275 const bool bVertWriting = IsVertWritingCIDFont(pCIDFont);
278 for (size_t i = 0; i < m_CharCodes.size(); ++i) {
279 const uint32_t charcode = m_CharCodes[i];
282 curpos -= (m_CharPos[i - 1] * fontsize) / 1000;
285 m_CharPos[i - 1] = curpos;
288 FX_RECT char_rect = pFont->GetCharBBox(charcode);
292 CFX_Point16 vertical_origin = pCIDFont->GetVertOrigin(cid);
293 char_rect
.Offset(-vertical_origin.x
, -vertical_origin.y
);
295 min_x,
static_cast<
float>(
std::min(char_rect
.left, char_rect
.right)));
297 max_x,
static_cast<
float>(
std::max(char_rect
.left, char_rect
.right)));
298 const float char_top = curpos + char_rect
.top * fontsize / 1000;
299 const float char_bottom = curpos + char_rect
.bottom * fontsize / 1000;
300 min_y =
std::min(min_y,
std::min(char_top, char_bottom));
301 max_y =
std::max(max_y,
std::max(char_top, char_bottom));
305 min_y,
static_cast<
float>(
std::min(char_rect
.top, char_rect
.bottom)));
307 max_y,
static_cast<
float>(
std::max(char_rect
.top, char_rect
.bottom)));
308 const float char_left = curpos + char_rect
.left * fontsize / 1000;
309 const float char_right = curpos + char_rect
.right * fontsize / 1000;
310 min_x =
std::min(min_x,
std::min(char_left, char_right));
311 max_x =
std::max(max_x,
std::max(char_left, char_right));
312 charwidth = pFont->GetCharWidthF(charcode) * fontsize / 1000;
322 min_x = min_x * fontsize / 1000;
323 max_x = max_x * fontsize / 1000;
325 min_y = min_y * fontsize / 1000;
326 max_y = max_y * fontsize / 1000;
331 if (TextRenderingModeIsStrokeMode(
text_state().GetTextMode())) {
334 const float half_width =
graph_state().GetLineWidth() / 2;
constexpr CFX_FloatRect(float l, float b, float r, float t)
void Inflate(float x, float y)
CFX_Matrix operator*(const CFX_Matrix &right) const
uint16_t CIDFromCharCode(uint32_t charcode) const
int16_t GetVertWidth(uint16_t cid) const
bool IsVertWriting() const override
int GetCharSize(uint32_t charcode) const
static constexpr uint32_t kInvalidCharCode
void SetRect(const CFX_FloatRect &rect)
const CFX_FloatRect & GetOriginalRect() const
void SetDirty(bool value)
CPDF_PageObject(int32_t content_stream)
void SetOriginalRect(const CFX_FloatRect &rect)
static constexpr int32_t kNoContentStream
CPDF_TextState & mutable_text_state()
const CPDF_TextState & text_state() const
const CFX_GraphState & graph_state() const
RetainPtr< CPDF_Font > GetFont() const
void SetText(const ByteString &str)
CPDF_TextObject * AsText() override
Item GetItemInfo(size_t index) const
void SetTextRenderMode(TextRenderingMode mode)
CPDF_TextObject(int32_t content_stream)
CFX_Matrix GetTextMatrix() const
WideString GetWordString(int nWordIndex) const
std::unique_ptr< CPDF_TextObject > Clone() const
Type GetType() const override
bool IsText() const override
uint32_t GetCharCode(size_t index) const
Item GetCharInfo(size_t index) const
float GetCharWidth(uint32_t charcode) const
float GetFontSize() const
TextRenderingMode GetTextRenderMode() const
~CPDF_TextObject() override
size_t CountChars() const
const CPDF_TextObject * AsText() const override
size_t CountItems() const
void Transform(const CFX_Matrix &matrix) override
CFX_PointF CalcPositionData(float horz_scale)
void SetSegments(const ByteString *pStrs, const std::vector< float > &kernings, size_t nSegs)
void SetTextMatrix(const CFX_Matrix &matrix)
void Offset(int dx, int dy)