7#include "xfa/fde/cfde_textout.h"
12#include "build/build_config.h"
13#include "core/fxcrt/fx_coordinates.h"
14#include "core/fxcrt/fx_extension.h"
15#include "core/fxcrt/fx_system.h"
16#include "core/fxcrt/stl_util.h"
17#include "core/fxge/cfx_font.h"
18#include "core/fxge/cfx_path.h"
19#include "core/fxge/cfx_renderdevice.h"
20#include "core/fxge/cfx_substfont.h"
21#include "core/fxge/cfx_textrenderoptions.h"
22#include "core/fxge/fx_font.h"
23#include "core/fxge/text_char_pos.h"
24#include "third_party/base/check.h"
25#include "third_party/base/check_op.h"
26#include "third_party/base/numerics/safe_conversions.h"
27#include "xfa/fgas/font/cfgas_gefont.h"
28#include "xfa/fgas/layout/cfgas_txtbreak.h"
52 DCHECK(!pCharPos.empty());
54 CFX_Font* pFxFont = pFont->GetDevFont();
55 if (FontStyleIsItalic(pFont->GetFontStyles()) && !pFxFont->IsItalic()) {
56 for (
auto& pos : pCharPos) {
57 static constexpr float mc = 0.267949f;
58 pos.m_AdjustMatrix[2] += mc * pos.m_AdjustMatrix[0];
59 pos.m_AdjustMatrix[3] += mc * pos.m_AdjustMatrix[1];
64 uint32_t dwFontStyle = pFont->GetFontStyles();
66 auto SubstFxFont = std::make_unique<CFX_SubstFont>();
67 SubstFxFont->m_Weight = FontStyleIsForceBold(dwFontStyle) ? 700 : 400;
68 SubstFxFont->m_ItalicAngle = FontStyleIsItalic(dwFontStyle) ? -12 : 0;
69 SubstFxFont->m_WeightCJK = SubstFxFont->m_Weight;
70 SubstFxFont->m_bItalicCJK = FontStyleIsItalic(dwFontStyle);
71 FxFont.SetSubstFont(std::move(SubstFxFont));
78 for (
auto& pos : pCharPos) {
79 RetainPtr<CFGAS_GEFont> pSTFont =
80 pFont->GetSubstFont(
static_cast<int32_t>(pos.m_GlyphIndex));
81 pos.m_GlyphIndex &= 0x00FFFFFF;
82 pos.m_bFontStyle =
false;
83 if (pCurFont != pSTFont) {
85 pFxFont = pCurFont->GetDevFont();
89 FxFont.SetFace(pFxFont->GetFace());
90 FxFont.SetFontSpan(pFxFont->GetFontSpan());
96 device->DrawNormalText(pdfium::make_span(pCurCP, count), font,
97 -fFontSize, matrix, color, kOptions);
108 if (pCurFont && count) {
109 pFxFont = pCurFont->GetDevFont();
111#if !BUILDFLAG(IS_WIN)
112 FxFont.SetFace(pFxFont->GetFace());
113 FxFont.SetFontSpan(pFxFont->GetFontSpan());
120 -fFontSize
, matrix
, color
, kOptions
);
139 m_pFont = std::move(pFont);
140 m_pTxtBreak->SetFont(m_pFont);
144 DCHECK(fFontSize > 0);
145 m_fFontSize = fFontSize;
146 m_pTxtBreak->SetFontSize(fFontSize);
151 m_dwTxtBkStyles = m_Styles.single_line_
152 ? CFGAS_Break::LayoutStyle::kSingleLine
153 : CFGAS_Break::LayoutStyle::kNone;
155 m_pTxtBreak->SetLayoutStyles(m_dwTxtBkStyles);
159 m_iAlignment = iAlignment;
161 int32_t txtBreakAlignment = 0;
162 switch (m_iAlignment) {
174 m_pTxtBreak->SetAlignment(txtBreakAlignment);
178 DCHECK(fLineSpace > 1.0f);
179 m_fLineSpace = fLineSpace;
183 m_fTolerance = fTolerance;
184 m_pTxtBreak->SetLineBreakTolerance(m_fTolerance);
188 CFX_RectF rtText(0.0f, 0.0f, pSize->width, pSize->height);
189 CalcLogicSize(str, &rtText);
190 *pSize = rtText.Size();
201 DCHECK(m_fFontSize >= 1.0f);
205 pRect
->width = m_fFontSize * 1000.0f;
207 m_pTxtBreak->SetLineWidth(pRect->Width());
212 float fHeight = 0.0f;
215 bool break_char_is_set =
false;
216 for (
const wchar_t& wch : str) {
217 if (!break_char_is_set && (wch == L'\n' || wch == L'\r')) {
218 break_char_is_set =
true;
219 m_pTxtBreak->SetParagraphBreakChar(wch);
221 dwBreakStatus = m_pTxtBreak->AppendChar(wch);
222 if (!CFX_BreakTypeNoneOrPiece(dwBreakStatus))
223 RetrieveLineWidth(dwBreakStatus, &fStartPos, &fWidth, &fHeight);
226 dwBreakStatus = m_pTxtBreak->EndBreak(CFGAS_Char::BreakType::kParagraph);
228 RetrieveLineWidth(dwBreakStatus, &fStartPos, &fWidth, &fHeight);
230 m_pTxtBreak->Reset();
232 if (TextAlignmentVerticallyCentered(m_iAlignment))
234 else if (IsTextAlignmentTop(m_iAlignment))
242 pRect
->height -= m_fLineSpace - m_fFontSize;
252 float fLineStep =
std::max(m_fLineSpace, m_fFontSize);
253 float fLineWidth = 0.0f;
254 for (int32_t i = 0; i < m_pTxtBreak->CountBreakPieces(); i++) {
256 fLineWidth +=
static_cast<
float>(pPiece
->GetWidth()) / 20000.0f;
257 *pStartPos =
std::min(*pStartPos,
260 m_pTxtBreak->ClearBreakPieces();
262 if (dwBreakStatus == CFGAS_Char::BreakType::kParagraph)
263 m_pTxtBreak->Reset();
265 *pWidth += fLineWidth;
267 *pWidth =
std::max(*pWidth, fLineWidth);
268 *pHeight += fLineStep;
275 const WideString& str,
278 DCHECK(m_fFontSize >= 1.0f);
285 float fLineWidth = rect
.width;
286 m_pTxtBreak->SetLineWidth(fLineWidth);
294 if (!device || m_ttoLines.empty())
302 for (
auto& line : m_ttoLines) {
303 for (size_t i = 0; i < line.GetSize(); ++i) {
304 const Piece* pPiece = line.GetPieceAtIndex(i);
305 size_t szCount = GetDisplayPos(pPiece);
309 CFDE_TextOut::DrawString(device, m_TxtColor, m_pFont,
310 {m_CharPos.data(), szCount}, m_fFontSize,
318 DCHECK(!str.IsEmpty());
322 if (m_CharWidths.size() < str.GetLength())
323 m_CharWidths.resize(str.GetLength(), 0);
325 float fLineStep =
std::max(m_fLineSpace, m_fFontSize);
327 m_fLinePos = rect
.top;
328 size_t start_char = 0;
329 int32_t iPieceWidths = 0;
332 for (
const auto& wch : str) {
333 dwBreakStatus = m_pTxtBreak->AppendChar(wch);
338 RetrievePieces(dwBreakStatus,
false, rect, &start_char, &iPieceWidths);
344 m_fLinePos += fLineStep;
346 if (m_fLinePos + fLineStep > fLineStop) {
347 size_t iCurLine = bEndofLine ? m_iCurLine - 1 : m_iCurLine;
348 CHECK_LT(m_iCurLine, m_ttoLines.size());
349 m_ttoLines[iCurLine].set_new_reload(
true);
355 dwBreakStatus = m_pTxtBreak->EndBreak(CFGAS_Char::BreakType::kParagraph);
356 if (!CFX_BreakTypeNoneOrPiece(dwBreakStatus) && !bRet)
357 RetrievePieces(dwBreakStatus,
false, rect, &start_char, &iPieceWidths);
359 m_pTxtBreak->ClearBreakPieces();
360 m_pTxtBreak->Reset();
367 int32_t* pPieceWidths) {
368 float fLineStep =
std::max(m_fLineSpace, m_fFontSize);
369 bool bNeedReload =
false;
371 int32_t iCount = m_pTxtBreak->CountBreakPieces();
373 size_t chars_to_skip = *pStartChar;
374 for (int32_t i = 0; i < iCount; i++) {
376 size_t iPieceChars = pPiece->GetLength();
377 if (chars_to_skip > iPieceChars) {
378 chars_to_skip -= iPieceChars;
382 size_t iChar = *pStartChar;
384 size_t j = chars_to_skip;
385 for (; j < iPieceChars; j++) {
389 if (iLineWidth - *pPieceWidths - iWidth < iCurCharWidth) {
394 iWidth += iCurCharWidth;
395 m_CharWidths[iChar++] = iCurCharWidth;
398 if (j == chars_to_skip && !bReload) {
399 CHECK_LT(m_iCurLine, m_ttoLines.size());
400 m_ttoLines[m_iCurLine].set_new_reload(
true);
401 }
else if (j > chars_to_skip) {
403 piece.start_char = *pStartChar;
404 piece.char_count = j - chars_to_skip;
408 m_fLinePos
, iWidth / 20000.0f
, fLineStep
);
413 AppendPiece(piece, bNeedReload, (bReload && i == iCount - 1));
415 *pStartChar += iPieceChars;
416 *pPieceWidths += iWidth;
418 m_pTxtBreak->ClearBreakPieces();
427 if (m_iCurLine >= m_ttoLines.size()) {
429 ttoLine.set_new_reload(bNeedReload);
431 m_iCurPiece = ttoLine.AddPiece(m_iCurPiece, piece);
432 m_ttoLines.push_back(ttoLine);
433 m_iCurLine = m_ttoLines.size() - 1;
435 Line* pLine = &m_ttoLines[m_iCurLine];
436 pLine->set_new_reload(bNeedReload);
438 m_iCurPiece = pLine->AddPiece(m_iCurPiece, piece);
440 size_t iPieces = pLine->GetSize();
441 if (m_iCurPiece < iPieces)
442 pLine->RemoveLast(iPieces - m_iCurPiece - 1);
445 if (!bEnd && bNeedReload)
451 for (
auto& line : m_ttoLines) {
452 if (line.new_reload()) {
455 ReloadLinePiece(&line, rect);
462 pdfium::span<
const wchar_t> text_span = m_wsText.span();
463 size_t start_char = 0;
464 size_t piece_count = line->GetSize();
465 int32_t piece_widths = 0;
467 for (size_t piece_index = 0; piece_index < piece_count; ++piece_index) {
468 const Piece* piece = line->GetPieceAtIndex(piece_index);
469 if (piece_index == 0)
470 m_fLinePos = piece->bounds
.top;
472 start_char = piece->start_char;
473 const size_t end = piece->start_char + piece->char_count;
474 for (size_t char_index = start_char; char_index < end; ++char_index) {
475 break_status = m_pTxtBreak->AppendChar(text_span[char_index]);
476 if (!CFX_BreakTypeNoneOrPiece(break_status))
477 RetrievePieces(break_status,
true, rect, &start_char, &piece_widths);
481 break_status = m_pTxtBreak->EndBreak(CFGAS_Char::BreakType::kParagraph);
482 if (!CFX_BreakTypeNoneOrPiece(break_status))
483 RetrievePieces(break_status,
true, rect, &start_char, &piece_widths);
485 m_pTxtBreak->Reset();
489 if (m_ttoLines.empty())
492 const Piece* pFirstPiece = m_ttoLines.back().GetPieceAtIndex(0);
497 if (TextAlignmentVerticallyCentered(m_iAlignment))
499 else if (IsTextAlignmentTop(m_iAlignment))
505 for (
auto& line : m_ttoLines) {
506 for (size_t i = 0; i < line.GetSize(); ++i)
507 line.GetPieceAtIndex(i)->bounds.top += fInc;
511size_t
CFDE_TextOut::GetDisplayPos(
const Piece* pPiece) {
512 if (m_CharPos.size() < pPiece->char_count)
513 m_CharPos.resize(pPiece->char_count, TextCharPos());
515 CFGAS_TxtBreak::
Run tr;
516 tr.wsStr = m_wsText.Substr(pPiece->start_char);
517 tr.pWidths = &m_CharWidths[pPiece->start_char];
518 tr
.iLength = pdfium::base::checked_cast<int32_t>(pPiece->char_count);
521 tr.dwStyles = m_dwTxtBkStyles;
523 tr.pRect = &pPiece->bounds;
525 return m_pTxtBreak->GetDisplayPos(tr, m_CharPos.data());
534size_t
CFDE_TextOut::Line::AddPiece(size_t index,
const Piece& piece) {
535 if (index >= pieces_.size()) {
536 pieces_.push_back(piece);
537 return pieces_.size();
539 pieces_[index] = piece;
544 return pieces_.size();
548 size_t index)
const {
549 CHECK(fxcrt::IndexInBounds(pieces_, index));
550 return &pieces_[index];
554 CHECK(fxcrt::IndexInBounds(pieces_, index));
555 return &pieces_[index];
559 pieces_.erase(pieces_.end() - std::min(count, pieces_.size()), pieces_.end());
@ CFX_TxtLineAlignment_Right
@ CFX_TxtLineAlignment_Center
@ CFX_TxtLineAlignment_Left
#define FX_TXTCHARSTYLE_OddBidiLevel
bool CFX_BreakTypeNoneOrPiece(CFGAS_Char::BreakType type)
static bool DrawString(CFX_RenderDevice *device, FX_ARGB color, const RetainPtr< CFGAS_GEFont > &pFont, pdfium::span< TextCharPos > pCharPos, float fFontSize, const CFX_Matrix &matrix)
void SetAlignment(FDE_TextAlignment iAlignment)
void SetFont(RetainPtr< CFGAS_GEFont > pFont)
void SetStyles(const FDE_TextStyle &dwStyles)
void SetLineSpace(float fLineSpace)
void DrawLogicText(CFX_RenderDevice *device, const WideString &str, const CFX_RectF &rect)
void CalcLogicSize(WideStringView str, CFX_SizeF *pSize)
void SetLineBreakTolerance(float fTolerance)
void SetFontSize(float fFontSize)
void CalcLogicSize(WideStringView str, CFX_RectF *pRect)
int32_t GetStartPos() const
uint32_t GetCharStyles() const
int32_t GetBidiLevel() const
CFGAS_Char * GetChar(int32_t index) const
CFX_RectF TransformRect(const CFX_RectF &rect) const
constexpr CFX_RectF()=default
FX_RECT GetOuterRect() const
CFX_RectF & operator=(const CFX_RectF &other)=default
constexpr CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height)
bool SetClip_Rect(const FX_RECT &pRect)
bool DrawNormalText(pdfium::span< const TextCharPos > pCharPos, CFX_Font *pFont, float font_size, const CFX_Matrix &mtText2Device, uint32_t fill_color, const CFX_TextRenderOptions &options)
void RestoreState(bool bKeepSaved)
int FXSYS_roundf(float f)
constexpr CFX_TextRenderOptions(AliasingType type)