Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cxfa_textlayout.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
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 "xfa/fxfa/cxfa_textlayout.h"
8
9#include <math.h>
10
11#include <algorithm>
12#include <utility>
13
14#include "core/fxcrt/check.h"
15#include "core/fxcrt/compiler_specific.h"
16#include "core/fxcrt/css/cfx_csscomputedstyle.h"
17#include "core/fxcrt/css/cfx_cssstyleselector.h"
18#include "core/fxcrt/notreached.h"
19#include "core/fxcrt/stl_util.h"
20#include "core/fxcrt/xml/cfx_xmlelement.h"
21#include "core/fxcrt/xml/cfx_xmlnode.h"
22#include "core/fxcrt/xml/cfx_xmltext.h"
23#include "core/fxge/cfx_fillrenderoptions.h"
24#include "core/fxge/cfx_graphstatedata.h"
25#include "core/fxge/cfx_path.h"
26#include "core/fxge/cfx_renderdevice.h"
27#include "core/fxge/text_char_pos.h"
28#include "fxjs/xfa/cjx_object.h"
29#include "xfa/fde/cfde_textout.h"
30#include "xfa/fgas/font/cfgas_gefont.h"
31#include "xfa/fgas/layout/cfgas_linkuserdata.h"
32#include "xfa/fgas/layout/cfgas_rtfbreak.h"
33#include "xfa/fgas/layout/cfgas_textuserdata.h"
34#include "xfa/fxfa/cxfa_ffdoc.h"
35#include "xfa/fxfa/cxfa_textparser.h"
36#include "xfa/fxfa/cxfa_textprovider.h"
37#include "xfa/fxfa/cxfa_texttabstopscontext.h"
38#include "xfa/fxfa/parser/cxfa_font.h"
39#include "xfa/fxfa/parser/cxfa_node.h"
40#include "xfa/fxfa/parser/cxfa_para.h"
41
42namespace {
43
44constexpr float kHeightTolerance = 0.001f;
45
46void ProcessText(WideString* pText) {
47 size_t iLen = pText->GetLength();
48 if (iLen == 0)
49 return;
50
51 size_t iTrimFront = 0;
52 {
53 // Span's lifetime must end before ReleaseBuffer() below.
54 pdfium::span<wchar_t> psz = pText->GetBuffer(iLen);
55 wchar_t wPrev = 0;
56 for (size_t i = 0; i < iLen; i++) {
57 wchar_t wch = psz[i];
58 if (wch < 0x20)
59 wch = 0x20;
60 if (wch == 0x20 && wPrev == 0x20)
61 continue;
62
63 wPrev = wch;
64 psz[iTrimFront++] = wch;
65 }
66 }
67 pText->ReleaseBuffer(iTrimFront);
68}
69
70} // namespace
71
72CXFA_TextLayout::TextPiece::TextPiece() = default;
73
74CXFA_TextLayout::TextPiece::~TextPiece() = default;
75
76CXFA_TextLayout::PieceLine::PieceLine() = default;
77
78CXFA_TextLayout::PieceLine::~PieceLine() = default;
79
80CXFA_TextLayout::LoaderContext::LoaderContext() = default;
81
82CXFA_TextLayout::LoaderContext::~LoaderContext() = default;
83
84void CXFA_TextLayout::LoaderContext::Trace(cppgc::Visitor* visitor) const {
85 visitor->Trace(pNode);
86}
87
88CXFA_TextLayout::CXFA_TextLayout(CXFA_FFDoc* doc,
89 CXFA_TextProvider* pTextProvider)
90 : m_pDoc(doc),
91 m_pTextProvider(pTextProvider),
92 m_pTextParser(cppgc::MakeGarbageCollected<CXFA_TextParser>(
93 doc->GetHeap()->GetAllocationHandle())) {
94 DCHECK(m_pTextProvider);
95}
96
97CXFA_TextLayout::~CXFA_TextLayout() = default;
98
99void CXFA_TextLayout::Trace(cppgc::Visitor* visitor) const {
100 visitor->Trace(m_pDoc);
101 visitor->Trace(m_pTextProvider);
102 visitor->Trace(m_pTextDataNode);
103 visitor->Trace(m_pTextParser);
104 visitor->Trace(m_pLoader);
105}
106
107void CXFA_TextLayout::Unload() {
108 m_pieceLines.clear();
109 m_pBreak.reset();
110}
111
112WideString CXFA_TextLayout::GetLinkURLAtPoint(const CFX_PointF& point) {
113 for (const auto& pPieceLine : m_pieceLines) {
114 for (const auto& pPiece : pPieceLine->m_textPieces) {
115 if (pPiece->pLinkData && pPiece->rtPiece.Contains(point))
116 return pPiece->pLinkData->GetLinkURL();
117 }
118 }
119 return WideString();
120}
121
122void CXFA_TextLayout::GetTextDataNode() {
123 CXFA_Node* pNode = m_pTextProvider->GetTextNode(&m_bRichText);
124 if (pNode && m_bRichText)
125 m_pTextParser->Reset();
126
127 m_pTextDataNode = pNode;
128}
129
130CFX_XMLNode* CXFA_TextLayout::GetXMLContainerNode() {
131 if (!m_bRichText)
132 return nullptr;
133
134 CFX_XMLNode* pXMLRoot = m_pTextDataNode->GetXMLMappingNode();
135 if (!pXMLRoot)
136 return nullptr;
137
138 for (CFX_XMLNode* pXMLChild = pXMLRoot->GetFirstChild(); pXMLChild;
139 pXMLChild = pXMLChild->GetNextSibling()) {
140 CFX_XMLElement* pXMLElement = ToXMLElement(pXMLChild);
141 if (!pXMLElement)
142 continue;
143 WideString wsTag = pXMLElement->GetLocalTagName();
144 if (wsTag.EqualsASCII("body") || wsTag.EqualsASCII("html"))
145 return pXMLChild;
146 }
147 return nullptr;
148}
149
150std::unique_ptr<CFGAS_RTFBreak> CXFA_TextLayout::CreateBreak(bool bDefault) {
151 Mask<CFGAS_Break::LayoutStyle> dwStyle = CFGAS_Break::LayoutStyle::kExpandTab;
152 if (!bDefault)
154
155 auto pBreak = std::make_unique<CFGAS_RTFBreak>(dwStyle);
156 pBreak->SetLineBreakTolerance(1);
157 pBreak->SetFont(
158 m_pTextParser->GetFont(m_pDoc.Get(), m_pTextProvider, nullptr));
159 pBreak->SetFontSize(m_pTextParser->GetFontSize(m_pTextProvider, nullptr));
160 return pBreak;
161}
162
163void CXFA_TextLayout::InitBreak(float fLineWidth) {
164 CXFA_Para* para = m_pTextProvider->GetParaIfExists();
165 float fStart = 0;
166 float fStartPos = 0;
167 if (para) {
168 CFGAS_RTFBreak::LineAlignment iAlign = CFGAS_RTFBreak::LineAlignment::Left;
169 switch (para->GetHorizontalAlign()) {
170 case XFA_AttributeValue::Center:
171 iAlign = CFGAS_RTFBreak::LineAlignment::Center;
172 break;
173 case XFA_AttributeValue::Right:
174 iAlign = CFGAS_RTFBreak::LineAlignment::Right;
175 break;
176 case XFA_AttributeValue::Justify:
177 iAlign = CFGAS_RTFBreak::LineAlignment::Justified;
178 break;
179 case XFA_AttributeValue::JustifyAll:
180 iAlign = CFGAS_RTFBreak::LineAlignment::Distributed;
181 break;
182 case XFA_AttributeValue::Left:
183 case XFA_AttributeValue::Radix:
184 break;
185 default:
187 }
188 m_pBreak->SetAlignment(iAlign);
189
190 fStart = para->GetMarginLeft();
191 if (m_pTextProvider->IsCheckButtonAndAutoWidth()) {
192 if (iAlign != CFGAS_RTFBreak::LineAlignment::Left)
193 fLineWidth -= para->GetMarginRight();
194 } else {
195 fLineWidth -= para->GetMarginRight();
196 }
197 if (fLineWidth < 0)
198 fLineWidth = fStart;
199
200 fStartPos = fStart;
201 float fIndent = para->GetTextIndent();
202 if (fIndent > 0)
203 fStartPos += fIndent;
204 }
205
206 m_pBreak->SetLineBoundary(fStart, fLineWidth);
207 m_pBreak->SetLineStartPos(fStartPos);
208
209 CXFA_Font* font = m_pTextProvider->GetFontIfExists();
210 if (font) {
211 m_pBreak->SetHorizontalScale(
212 static_cast<int32_t>(font->GetHorizontalScale()));
213 m_pBreak->SetVerticalScale(static_cast<int32_t>(font->GetVerticalScale()));
214 m_pBreak->SetCharSpace(font->GetLetterSpacing());
215 }
216
217 float fFontSize = m_pTextParser->GetFontSize(m_pTextProvider, nullptr);
218 m_pBreak->SetFontSize(fFontSize);
219 m_pBreak->SetFont(
220 m_pTextParser->GetFont(m_pDoc.Get(), m_pTextProvider, nullptr));
221 m_pBreak->SetLineBreakTolerance(fFontSize * 0.2f);
222}
223
224void CXFA_TextLayout::InitBreak(CFX_CSSComputedStyle* pStyle,
225 CFX_CSSDisplay eDisplay,
226 float fLineWidth,
227 const CFX_XMLNode* pXMLNode,
228 CFX_CSSComputedStyle* pParentStyle) {
229 if (!pStyle) {
230 InitBreak(fLineWidth);
231 return;
232 }
233
234 if (eDisplay == CFX_CSSDisplay::Block ||
235 eDisplay == CFX_CSSDisplay::ListItem) {
236 CFGAS_RTFBreak::LineAlignment iAlign = CFGAS_RTFBreak::LineAlignment::Left;
237 switch (pStyle->GetTextAlign()) {
239 iAlign = CFGAS_RTFBreak::LineAlignment::Right;
240 break;
242 iAlign = CFGAS_RTFBreak::LineAlignment::Center;
243 break;
245 iAlign = CFGAS_RTFBreak::LineAlignment::Justified;
246 break;
248 iAlign = CFGAS_RTFBreak::LineAlignment::Distributed;
249 break;
250 default:
251 break;
252 }
253 m_pBreak->SetAlignment(iAlign);
254
255 float fStart = 0;
256 const CFX_CSSRect* pRect = pStyle->GetMarginWidth();
257 const CFX_CSSRect* pPaddingRect = pStyle->GetPaddingWidth();
258 if (pRect) {
259 fStart = pRect->left.GetValue();
260 fLineWidth -= pRect->right.GetValue();
261 if (pPaddingRect) {
262 fStart += pPaddingRect->left.GetValue();
263 fLineWidth -= pPaddingRect->right.GetValue();
264 }
265 if (eDisplay == CFX_CSSDisplay::ListItem) {
266 const CFX_CSSRect* pParRect = pParentStyle->GetMarginWidth();
267 const CFX_CSSRect* pParPaddingRect = pParentStyle->GetPaddingWidth();
268 if (pParRect) {
269 fStart += pParRect->left.GetValue();
270 fLineWidth -= pParRect->right.GetValue();
271 if (pParPaddingRect) {
272 fStart += pParPaddingRect->left.GetValue();
273 fLineWidth -= pParPaddingRect->right.GetValue();
274 }
275 }
276 CFX_CSSRect pNewRect;
281 pStyle->SetMarginWidth(pNewRect);
282 }
283 }
284 m_pBreak->SetLineBoundary(fStart, fLineWidth);
285 float fIndent = pStyle->GetTextIndent().GetValue();
286 if (fIndent > 0)
287 fStart += fIndent;
288
289 m_pBreak->SetLineStartPos(fStart);
290 m_pBreak->SetTabWidth(m_pTextParser->GetTabInterval(pStyle));
291 if (!m_pTabstopContext)
292 m_pTabstopContext = std::make_unique<CXFA_TextTabstopsContext>();
293 m_pTextParser->GetTabstops(pStyle, m_pTabstopContext.get());
294 for (const auto& stop : m_pTabstopContext->m_tabstops)
295 m_pBreak->AddPositionedTab(stop.fTabstops);
296 }
297 float fFontSize = m_pTextParser->GetFontSize(m_pTextProvider, pStyle);
298 m_pBreak->SetFontSize(fFontSize);
299 m_pBreak->SetLineBreakTolerance(fFontSize * 0.2f);
300 m_pBreak->SetFont(
301 m_pTextParser->GetFont(m_pDoc.Get(), m_pTextProvider, pStyle));
302 m_pBreak->SetHorizontalScale(
303 m_pTextParser->GetHorScale(m_pTextProvider, pStyle, pXMLNode));
304 m_pBreak->SetVerticalScale(
305 m_pTextParser->GetVerScale(m_pTextProvider, pStyle));
306 m_pBreak->SetCharSpace(pStyle->GetLetterSpacing().GetValue());
307}
308
309float CXFA_TextLayout::GetLayoutHeight() {
310 if (!m_pLoader)
311 return 0;
312
313 if (m_pLoader->lineHeights.empty() && m_pLoader->fWidth > 0) {
314 CFX_SizeF szMax(m_pLoader->fWidth, m_pLoader->fHeight);
315 m_pLoader->bSaveLineHeight = true;
316 m_pLoader->fLastPos = 0;
317 CFX_SizeF szDef = CalcSize(szMax, szMax);
318 m_pLoader->bSaveLineHeight = false;
319 return szDef.height;
320 }
321
322 float fHeight = m_pLoader->fHeight;
323 if (fHeight < 0.1f) {
324 fHeight = 0;
325 for (float value : m_pLoader->lineHeights)
326 fHeight += value;
327 }
328 return fHeight;
329}
330
331float CXFA_TextLayout::StartLayout(float fWidth) {
332 if (!m_pLoader)
333 m_pLoader = cppgc::MakeGarbageCollected<LoaderContext>(
334 m_pDoc->GetHeap()->GetAllocationHandle());
335
336 if (fWidth < 0 ||
337 (m_pLoader->fWidth > -1 && fabs(fWidth - m_pLoader->fWidth) > 0)) {
338 m_pLoader->lineHeights.clear();
339 m_Blocks.clear();
340 Unload();
341 m_pLoader->fStartLineOffset = 0;
342 }
343 m_pLoader->fWidth = fWidth;
344
345 if (fWidth >= 0)
346 return fWidth;
347
348 CFX_SizeF szMax;
349
350 m_pLoader->bSaveLineHeight = true;
351 m_pLoader->fLastPos = 0;
352 CFX_SizeF szDef = CalcSize(szMax, szMax);
353 m_pLoader->bSaveLineHeight = false;
354 return szDef.width;
355}
356
357float CXFA_TextLayout::DoLayout(float fTextHeight) {
358 if (!m_pLoader)
359 return fTextHeight;
360
361 UpdateLoaderHeight(fTextHeight);
362 return fTextHeight;
363}
364
365float CXFA_TextLayout::DoSplitLayout(size_t szBlockIndex,
366 float fCalcHeight,
367 float fTextHeight) {
368 if (!m_pLoader)
369 return fCalcHeight;
370
371 UpdateLoaderHeight(fTextHeight);
372
373 if (fCalcHeight < 0)
374 return fCalcHeight;
375
376 m_bHasBlock = true;
377 if (m_Blocks.empty() && m_pLoader->fHeight > 0) {
378 float fHeight = fTextHeight - GetLayoutHeight();
379 if (fHeight > 0) {
380 XFA_AttributeValue iAlign = m_pTextParser->GetVAlign(m_pTextProvider);
381 if (iAlign == XFA_AttributeValue::Middle)
382 fHeight /= 2.0f;
383 else if (iAlign != XFA_AttributeValue::Bottom)
384 fHeight = 0;
385 m_pLoader->fStartLineOffset = fHeight;
386 }
387 }
388
389 float fLinePos = m_pLoader->fStartLineOffset;
390 size_t szLineIndex = 0;
391 if (!m_Blocks.empty()) {
392 if (szBlockIndex < m_Blocks.size())
393 szLineIndex = m_Blocks[szBlockIndex].szIndex;
394 else
395 szLineIndex = GetNextIndexFromLastBlockData();
396 for (size_t i = 0;
397 i < std::min(szBlockIndex, m_pLoader->blockHeights.size()); ++i) {
398 fLinePos -= m_pLoader->blockHeights[i].fHeight;
399 }
400 }
401
402 if (szLineIndex >= m_pLoader->lineHeights.size())
403 return fCalcHeight;
404
405 if (m_pLoader->lineHeights[szLineIndex] - fCalcHeight > kHeightTolerance)
406 return 0;
407
408 for (size_t i = szLineIndex; i < m_pLoader->lineHeights.size(); ++i) {
409 float fLineHeight = m_pLoader->lineHeights[i];
410 if (fLinePos + fLineHeight - fCalcHeight <= kHeightTolerance) {
411 fLinePos += fLineHeight;
412 continue;
413 }
414
415 if (szBlockIndex < m_Blocks.size())
416 m_Blocks[szBlockIndex] = {szLineIndex, i - szLineIndex};
417 else
418 m_Blocks.push_back({szLineIndex, i - szLineIndex});
419
420 if (i != szLineIndex)
421 return fLinePos;
422
423 if (fCalcHeight > fLinePos)
424 return fCalcHeight;
425
426 if (szBlockIndex < m_pLoader->blockHeights.size() &&
427 m_pLoader->blockHeights[szBlockIndex].szBlockIndex == szBlockIndex) {
428 m_pLoader->blockHeights[szBlockIndex].fHeight = fCalcHeight;
429 } else {
430 m_pLoader->blockHeights.push_back({szBlockIndex, fCalcHeight});
431 }
432 return fCalcHeight;
433 }
434 return fCalcHeight;
435}
436
437size_t CXFA_TextLayout::CountBlocks() const {
438 size_t szCount = m_Blocks.size();
439 return szCount > 0 ? szCount : 1;
440}
441
442size_t CXFA_TextLayout::GetNextIndexFromLastBlockData() const {
443 return m_Blocks.back().szIndex + m_Blocks.back().szLength;
444}
445
446void CXFA_TextLayout::UpdateLoaderHeight(float fTextHeight) {
447 m_pLoader->fHeight = fTextHeight;
448 if (m_pLoader->fHeight < 0)
449 m_pLoader->fHeight = GetLayoutHeight();
450}
451
452CFX_SizeF CXFA_TextLayout::CalcSize(const CFX_SizeF& minSize,
453 const CFX_SizeF& maxSize) {
454 float width = maxSize.width;
455 if (width < 1)
456 width = 0xFFFF;
457
458 m_pBreak = CreateBreak(false);
459 float fLinePos = 0;
460 m_iLines = 0;
461 m_fMaxWidth = 0;
462 Loader(width, &fLinePos, false);
463 if (fLinePos < 0.1f)
464 fLinePos = m_pTextParser->GetFontSize(m_pTextProvider, nullptr);
465
466 m_pTabstopContext.reset();
467 return CFX_SizeF(m_fMaxWidth, fLinePos);
468}
469
470float CXFA_TextLayout::Layout(const CFX_SizeF& size) {
471 if (size.width < 1)
472 return 0.f;
473
474 Unload();
475 m_pBreak = CreateBreak(true);
476 if (m_pLoader) {
477 m_pLoader->iTotalLines = -1;
478 m_pLoader->nCharIdx = 0;
479 }
480
481 m_iLines = 0;
482 float fLinePos = 0;
483 Loader(size.width, &fLinePos, true);
484 UpdateAlign(size.height, fLinePos);
485 m_pTabstopContext.reset();
486 return fLinePos;
487}
488
489bool CXFA_TextLayout::LayoutInternal(size_t szBlockIndex) {
490 DCHECK(szBlockIndex < CountBlocks());
491
492 if (!m_pLoader || m_pLoader->fWidth < 1)
493 return false;
494
495 m_pLoader->iTotalLines = -1;
496 m_iLines = 0;
497 float fLinePos = 0;
498 CXFA_Node* pNode = nullptr;
499 CFX_SizeF szText(m_pLoader->fWidth, m_pLoader->fHeight);
500 if (szBlockIndex < m_pLoader->blockHeights.size())
501 return true;
502 if (szBlockIndex == m_pLoader->blockHeights.size()) {
503 Unload();
504 m_pBreak = CreateBreak(true);
505 fLinePos = m_pLoader->fStartLineOffset;
506 for (size_t i = 0; i < m_pLoader->blockHeights.size(); ++i)
507 fLinePos -= m_pLoader->blockHeights[i].fHeight;
508
509 m_pLoader->nCharIdx = 0;
510 if (!m_Blocks.empty()) {
511 m_pLoader->iTotalLines =
512 pdfium::checked_cast<int32_t>(m_Blocks[szBlockIndex].szLength);
513 }
514 Loader(szText.width, &fLinePos, true);
515 if (m_Blocks.empty() && m_pLoader->fStartLineOffset < 0.1f)
516 UpdateAlign(szText.height, fLinePos);
517 } else if (m_pTextDataNode) {
518 if (!m_Blocks.empty() && szBlockIndex < m_Blocks.size() - 1) {
519 m_pLoader->iTotalLines =
520 pdfium::checked_cast<int32_t>(m_Blocks[szBlockIndex].szLength);
521 }
522 m_pBreak->Reset();
523 if (m_bRichText) {
524 CFX_XMLNode* pContainerNode = GetXMLContainerNode();
525 if (!pContainerNode)
526 return true;
527
528 const CFX_XMLNode* pXMLNode = m_pLoader->pXMLNode;
529 if (!pXMLNode)
530 return true;
531
532 const CFX_XMLNode* pSaveXMLNode = pXMLNode;
533 for (; pXMLNode; pXMLNode = pXMLNode->GetNextSibling()) {
534 if (!LoadRichText(pXMLNode, szText.width, &fLinePos,
535 m_pLoader->pParentStyle, true, nullptr, true, false,
536 0)) {
537 break;
538 }
539 }
540 while (!pXMLNode) {
541 pXMLNode = pSaveXMLNode->GetParent();
542 if (pXMLNode == pContainerNode)
543 break;
544 if (!LoadRichText(pXMLNode, szText.width, &fLinePos,
545 m_pLoader->pParentStyle, true, nullptr, false, false,
546 0)) {
547 break;
548 }
549 pSaveXMLNode = pXMLNode;
550 pXMLNode = pXMLNode->GetNextSibling();
551 if (!pXMLNode)
552 continue;
553 for (; pXMLNode; pXMLNode = pXMLNode->GetNextSibling()) {
554 if (!LoadRichText(pXMLNode, szText.width, &fLinePos,
555 m_pLoader->pParentStyle, true, nullptr, true, false,
556 0)) {
557 break;
558 }
559 }
560 }
561 } else {
562 pNode = m_pLoader->pNode.Get();
563 if (!pNode)
564 return true;
565 LoadText(pNode, szText.width, &fLinePos, true);
566 }
567 }
568 return true;
569}
570
571void CXFA_TextLayout::ItemBlocks(const CFX_RectF& rtText, size_t szBlockIndex) {
572 if (!m_pLoader)
573 return;
574
575 if (m_pLoader->lineHeights.empty())
576 return;
577
578 float fLinePos = m_pLoader->fStartLineOffset;
579 size_t szLineIndex = 0;
580 if (szBlockIndex > 0) {
581 if (szBlockIndex <= m_pLoader->blockHeights.size()) {
582 for (size_t i = 0; i < szBlockIndex; ++i)
583 fLinePos -= m_pLoader->blockHeights[i].fHeight;
584 } else {
585 fLinePos = 0;
586 }
587 szLineIndex = GetNextIndexFromLastBlockData();
588 }
589
590 size_t i;
591 for (i = szLineIndex; i < m_pLoader->lineHeights.size(); ++i) {
592 float fLineHeight = m_pLoader->lineHeights[i];
593 if (fLinePos + fLineHeight - rtText.height > kHeightTolerance) {
594 m_Blocks.push_back({szLineIndex, i - szLineIndex});
595 return;
596 }
597 fLinePos += fLineHeight;
598 }
599 if (i > szLineIndex)
600 m_Blocks.push_back({szLineIndex, i - szLineIndex});
601}
602
603bool CXFA_TextLayout::DrawString(CFX_RenderDevice* pFxDevice,
604 const CFX_Matrix& mtDoc2Device,
605 const CFX_RectF& rtClip,
606 size_t szBlockIndex) {
607 if (!pFxDevice)
608 return false;
609
610 pFxDevice->SaveState();
611 pFxDevice->SetClip_Rect(rtClip.GetOuterRect());
612
613 if (m_pieceLines.empty()) {
614 size_t szBlockCount = CountBlocks();
615 for (size_t i = 0; i < szBlockCount; ++i)
616 LayoutInternal(i);
617 m_pTabstopContext.reset();
618 m_pLoader.Clear();
619 }
620
621 std::vector<TextCharPos> char_pos(1);
622 size_t szLineStart = 0;
623 size_t szPieceLines = m_pieceLines.size();
624 if (!m_Blocks.empty()) {
625 if (szBlockIndex < m_Blocks.size()) {
626 szLineStart = m_Blocks[szBlockIndex].szIndex;
627 szPieceLines = m_Blocks[szBlockIndex].szLength;
628 } else {
629 szPieceLines = 0;
630 }
631 }
632
633 for (size_t i = 0; i < szPieceLines; ++i) {
634 if (i + szLineStart >= m_pieceLines.size())
635 break;
636
637 PieceLine* pPieceLine = m_pieceLines[i + szLineStart].get();
638 for (size_t j = 0; j < pPieceLine->m_textPieces.size(); ++j) {
639 const TextPiece* pPiece = pPieceLine->m_textPieces[j].get();
640 int32_t iChars = pPiece->iChars;
641 if (fxcrt::CollectionSize<int32_t>(char_pos) < iChars)
642 char_pos.resize(iChars);
643 RenderString(pFxDevice, pPieceLine, j, char_pos, mtDoc2Device);
644 }
645 for (size_t j = 0; j < pPieceLine->m_textPieces.size(); ++j)
646 RenderPath(pFxDevice, pPieceLine, j, char_pos, mtDoc2Device);
647 }
648 pFxDevice->RestoreState(false);
649 return szPieceLines > 0;
650}
651
652void CXFA_TextLayout::UpdateAlign(float fHeight, float fBottom) {
653 fHeight -= fBottom;
654 if (fHeight < 0.1f)
655 return;
656
657 switch (m_pTextParser->GetVAlign(m_pTextProvider)) {
658 case XFA_AttributeValue::Middle:
659 fHeight /= 2.0f;
660 break;
661 case XFA_AttributeValue::Bottom:
662 break;
663 default:
664 return;
665 }
666
667 for (const auto& pPieceLine : m_pieceLines) {
668 for (const auto& pPiece : pPieceLine->m_textPieces)
669 pPiece->rtPiece.top += fHeight;
670 }
671}
672
673void CXFA_TextLayout::Loader(float textWidth,
674 float* pLinePos,
675 bool bSavePieces) {
676 GetTextDataNode();
677 if (!m_pTextDataNode)
678 return;
679
680 if (!m_bRichText) {
681 LoadText(m_pTextDataNode, textWidth, pLinePos, bSavePieces);
682 return;
683 }
684
685 const CFX_XMLNode* pXMLContainer = GetXMLContainerNode();
686 if (!pXMLContainer)
687 return;
688
689 if (!m_pTextParser->IsParsed())
690 m_pTextParser->DoParse(pXMLContainer, m_pTextProvider);
691
692 auto pRootStyle = m_pTextParser->CreateRootStyle(m_pTextProvider);
693 LoadRichText(pXMLContainer, textWidth, pLinePos, std::move(pRootStyle),
694 bSavePieces, nullptr, true, false, 0);
695}
696
697void CXFA_TextLayout::LoadText(CXFA_Node* pNode,
698 float textWidth,
699 float* pLinePos,
700 bool bSavePieces) {
701 InitBreak(textWidth);
702
703 CXFA_Para* para = m_pTextProvider->GetParaIfExists();
704 float fSpaceAbove = 0;
705 if (para) {
706 fSpaceAbove = para->GetSpaceAbove();
707 if (fSpaceAbove < 0.1f)
708 fSpaceAbove = 0;
709
710 switch (para->GetVerticalAlign()) {
711 case XFA_AttributeValue::Top:
712 case XFA_AttributeValue::Middle:
713 case XFA_AttributeValue::Bottom: {
714 *pLinePos += fSpaceAbove;
715 break;
716 }
717 default:
719 }
720 }
721
722 WideString wsText = pNode->JSObject()->GetContent(false);
723 wsText.TrimBack(L" ");
724 bool bRet = AppendChar(wsText, pLinePos, fSpaceAbove, bSavePieces);
725 if (bRet && m_pLoader)
726 m_pLoader->pNode = pNode;
727 else
728 EndBreak(CFGAS_Char::BreakType::kParagraph, pLinePos, bSavePieces);
729}
730
731bool CXFA_TextLayout::LoadRichText(const CFX_XMLNode* pXMLNode,
732 float textWidth,
733 float* pLinePos,
734 RetainPtr<CFX_CSSComputedStyle> pParentStyle,
735 bool bSavePieces,
736 RetainPtr<CFGAS_LinkUserData> pLinkData,
737 bool bEndBreak,
738 bool bIsOl,
739 int32_t iLiCount) {
740 if (!pXMLNode)
741 return false;
742
743 CXFA_TextParser::Context* pContext =
744 m_pTextParser->GetParseContextFromMap(pXMLNode);
746 bool bContentNode = false;
747 float fSpaceBelow = 0;
748 RetainPtr<CFX_CSSComputedStyle> pStyle;
749 WideString wsName;
750 if (bEndBreak) {
751 bool bCurOl = false;
752 bool bCurLi = false;
753 const CFX_XMLElement* pElement = nullptr;
754 if (pContext) {
755 if (pXMLNode->GetType() == CFX_XMLNode::Type::kText) {
756 bContentNode = true;
757 } else if (pXMLNode->GetType() == CFX_XMLNode::Type::kElement) {
758 pElement = static_cast<const CFX_XMLElement*>(pXMLNode);
759 wsName = pElement->GetLocalTagName();
760 }
761 if (wsName.EqualsASCII("ol")) {
762 bIsOl = true;
763 bCurOl = true;
764 }
765
766 eDisplay = pContext->GetDisplay();
767 if (eDisplay != CFX_CSSDisplay::Block &&
768 eDisplay != CFX_CSSDisplay::Inline &&
769 eDisplay != CFX_CSSDisplay::ListItem) {
770 return true;
771 }
772
773 pStyle = m_pTextParser->ComputeStyle(pXMLNode, pParentStyle);
774 InitBreak(bContentNode ? pParentStyle.Get() : pStyle.Get(), eDisplay,
775 textWidth, pXMLNode, pParentStyle.Get());
776 if ((eDisplay == CFX_CSSDisplay::Block ||
777 eDisplay == CFX_CSSDisplay::ListItem) &&
778 pStyle &&
779 (wsName.IsEmpty() ||
780 !(wsName.EqualsASCII("body") || wsName.EqualsASCII("html") ||
781 wsName.EqualsASCII("ol") || wsName.EqualsASCII("ul")))) {
782 const CFX_CSSRect* pRect = pStyle->GetMarginWidth();
783 if (pRect) {
784 *pLinePos += pRect->top.GetValue();
785 fSpaceBelow = pRect->bottom.GetValue();
786 }
787 }
788
789 if (wsName.EqualsASCII("a")) {
790 WideString wsLinkContent = pElement->GetAttribute(L"href");
791 if (!wsLinkContent.IsEmpty())
792 pLinkData = pdfium::MakeRetain<CFGAS_LinkUserData>(wsLinkContent);
793 }
794
795 int32_t iTabCount = m_pTextParser->CountTabs(
796 bContentNode ? pParentStyle.Get() : pStyle.Get());
797 bool bSpaceRun = m_pTextParser->IsSpaceRun(
798 bContentNode ? pParentStyle.Get() : pStyle.Get());
799 WideString wsText;
800 if (bContentNode && iTabCount == 0) {
801 wsText = ToXMLText(pXMLNode)->GetText();
802 } else if (wsName.EqualsASCII("br")) {
803 wsText = WideString(L'\n');
804 } else if (wsName.EqualsASCII("li")) {
805 bCurLi = true;
806 if (bIsOl) {
807 wsText = WideString::Format(L"%d. ", iLiCount);
808 } else {
809 wsText = 0x00B7 + WideStringView(L" ");
810 }
811 } else if (!bContentNode) {
812 if (iTabCount > 0) {
813 while (iTabCount-- > 0)
814 wsText += L'\t';
815 } else {
816 std::optional<WideString> obj =
817 m_pTextParser->GetEmbeddedObj(m_pTextProvider, pXMLNode);
818 if (obj.has_value())
819 wsText = obj.value();
820 }
821 }
822
823 if (!wsText.IsEmpty() && bContentNode && !bSpaceRun)
824 ProcessText(&wsText);
825
826 if (m_pLoader) {
827 if (wsText.GetLength() > 0 && m_pLoader->bFilterSpace) {
828 wsText.TrimFront(L" ");
829 }
830 if (CFX_CSSDisplay::Block == eDisplay) {
831 m_pLoader->bFilterSpace = true;
832 } else if (CFX_CSSDisplay::Inline == eDisplay &&
833 m_pLoader->bFilterSpace) {
834 m_pLoader->bFilterSpace = false;
835 } else if (wsText.GetLength() > 0 && wsText.Back() == 0x20) {
836 m_pLoader->bFilterSpace = true;
837 } else if (wsText.GetLength() != 0) {
838 m_pLoader->bFilterSpace = false;
839 }
840 }
841
842 if (wsText.GetLength() > 0) {
843 if (!m_pLoader || m_pLoader->nCharIdx == 0) {
844 auto pUserData = pdfium::MakeRetain<CFGAS_TextUserData>(
845 bContentNode ? pParentStyle : pStyle, pLinkData);
846 m_pBreak->SetUserData(pUserData);
847 }
848
849 if (AppendChar(wsText, pLinePos, 0, bSavePieces)) {
850 if (m_pLoader)
851 m_pLoader->bFilterSpace = false;
852 if (!IsEnd(bSavePieces))
853 return true;
854 if (m_pLoader && m_pLoader->iTotalLines > -1) {
855 m_pLoader->pXMLNode = pXMLNode;
856 m_pLoader->pParentStyle = pParentStyle;
857 }
858 return false;
859 }
860 }
861 }
862
863 for (CFX_XMLNode* pChildNode = pXMLNode->GetFirstChild(); pChildNode;
864 pChildNode = pChildNode->GetNextSibling()) {
865 if (bCurOl)
866 iLiCount++;
867
868 if (!LoadRichText(pChildNode, textWidth, pLinePos,
869 pContext ? pStyle : pParentStyle, bSavePieces,
870 pLinkData, true, bIsOl, iLiCount))
871 return false;
872 }
873
874 if (m_pLoader) {
875 if (CFX_CSSDisplay::Block == eDisplay)
876 m_pLoader->bFilterSpace = true;
877 }
878 if (bCurLi)
879 EndBreak(CFGAS_Char::BreakType::kLine, pLinePos, bSavePieces);
880 } else {
881 if (pContext)
882 eDisplay = pContext->GetDisplay();
883 }
884
885 if (!pContext || bContentNode)
886 return true;
887
888 CFGAS_Char::BreakType dwStatus = (eDisplay == CFX_CSSDisplay::Block)
891 EndBreak(dwStatus, pLinePos, bSavePieces);
892 if (eDisplay == CFX_CSSDisplay::Block) {
893 *pLinePos += fSpaceBelow;
894 if (m_pTabstopContext)
895 m_pTabstopContext->RemoveAll();
896 }
897 if (!IsEnd(bSavePieces))
898 return true;
899
900 if (m_pLoader && m_pLoader->iTotalLines > -1) {
901 m_pLoader->pXMLNode = pXMLNode->GetNextSibling();
902 m_pLoader->pParentStyle = pParentStyle;
903 }
904 return false;
905}
906
907bool CXFA_TextLayout::AppendChar(const WideString& wsText,
908 float* pLinePos,
909 float fSpaceAbove,
910 bool bSavePieces) {
912 size_t iChar = m_pLoader ? m_pLoader->nCharIdx : 0;
913 size_t iLength = wsText.GetLength();
914 for (size_t i = iChar; i < iLength; i++) {
915 wchar_t wch = wsText[i];
916 if (wch == 0xA0)
917 wch = 0x20;
918
919 dwStatus = m_pBreak->AppendChar(wch);
920 if (dwStatus != CFGAS_Char::BreakType::kNone &&
921 dwStatus != CFGAS_Char::BreakType::kPiece) {
922 AppendTextLine(dwStatus, pLinePos, bSavePieces, false);
923 if (IsEnd(bSavePieces)) {
924 if (m_pLoader)
925 m_pLoader->nCharIdx = i;
926 return true;
927 }
928 if (dwStatus == CFGAS_Char::BreakType::kParagraph && m_bRichText)
929 *pLinePos += fSpaceAbove;
930 }
931 }
932 if (m_pLoader)
933 m_pLoader->nCharIdx = 0;
934
935 return false;
936}
937
938bool CXFA_TextLayout::IsEnd(bool bSavePieces) {
939 if (!bSavePieces)
940 return false;
941 if (m_pLoader && m_pLoader->iTotalLines > 0)
942 return m_iLines >= m_pLoader->iTotalLines;
943 return false;
944}
945
946void CXFA_TextLayout::EndBreak(CFGAS_Char::BreakType dwStatus,
947 float* pLinePos,
948 bool bSavePieces) {
949 dwStatus = m_pBreak->EndBreak(dwStatus);
950 if (dwStatus != CFGAS_Char::BreakType::kNone &&
952 AppendTextLine(dwStatus, pLinePos, bSavePieces, true);
953}
954
955void CXFA_TextLayout::DoTabstops(CFX_CSSComputedStyle* pStyle,
956 PieceLine* pPieceLine) {
957 if (!pStyle || !pPieceLine)
958 return;
959
960 if (!m_pTabstopContext || m_pTabstopContext->m_tabstops.empty())
961 return;
962
963 int32_t iPieces = fxcrt::CollectionSize<int32_t>(pPieceLine->m_textPieces);
964 if (iPieces == 0)
965 return;
966
967 TextPiece* pPiece = pPieceLine->m_textPieces[iPieces - 1].get();
968 int32_t& iTabstopsIndex = m_pTabstopContext->m_iTabIndex;
969 int32_t iCount = m_pTextParser->CountTabs(pStyle);
970 if (!fxcrt::IndexInBounds(m_pTabstopContext->m_tabstops, iTabstopsIndex))
971 return;
972
973 if (iCount > 0) {
974 iTabstopsIndex++;
975 m_pTabstopContext->m_bHasTabstops = true;
976 float fRight = 0;
977 if (iPieces > 1) {
978 const TextPiece* p = pPieceLine->m_textPieces[iPieces - 2].get();
979 fRight = p->rtPiece.right();
980 }
981 m_pTabstopContext->m_fTabWidth =
982 pPiece->rtPiece.width + pPiece->rtPiece.left - fRight;
983 } else if (iTabstopsIndex > -1) {
984 float fLeft = 0;
985 if (m_pTabstopContext->m_bHasTabstops) {
986 uint32_t dwAlign = m_pTabstopContext->m_tabstops[iTabstopsIndex].dwAlign;
987 if (dwAlign == FX_HashCode_GetW(L"center")) {
988 fLeft = pPiece->rtPiece.width / 2.0f;
989 } else if (dwAlign == FX_HashCode_GetW(L"right") ||
990 dwAlign == FX_HashCode_GetW(L"before")) {
991 fLeft = pPiece->rtPiece.width;
992 } else if (dwAlign == FX_HashCode_GetW(L"decimal")) {
993 int32_t iChars = pPiece->iChars;
994 for (int32_t i = 0; i < iChars; i++) {
995 if (pPiece->szText[i] == L'.')
996 break;
997
998 fLeft += pPiece->Widths[i] / 20000.0f;
999 }
1000 }
1001 m_pTabstopContext->m_fLeft =
1002 std::min(fLeft, m_pTabstopContext->m_fTabWidth);
1003 m_pTabstopContext->m_bHasTabstops = false;
1004 m_pTabstopContext->m_fTabWidth = 0;
1005 }
1006 pPiece->rtPiece.left -= m_pTabstopContext->m_fLeft;
1007 }
1008}
1009
1010void CXFA_TextLayout::AppendTextLine(CFGAS_Char::BreakType dwStatus,
1011 float* pLinePos,
1012 bool bSavePieces,
1013 bool bEndBreak) {
1014 int32_t iPieces = m_pBreak->CountBreakPieces();
1015 if (iPieces < 1)
1016 return;
1017
1018 RetainPtr<CFX_CSSComputedStyle> pStyle;
1019 if (bSavePieces) {
1020 auto pNew = std::make_unique<PieceLine>();
1021 PieceLine* pPieceLine = pNew.get();
1022 m_pieceLines.push_back(std::move(pNew));
1023 if (m_pTabstopContext)
1024 m_pTabstopContext->Reset();
1025
1026 float fLineStep = 0;
1027 float fBaseLine = 0;
1028 int32_t i = 0;
1029 for (i = 0; i < iPieces; i++) {
1030 const CFGAS_BreakPiece* pPiece = m_pBreak->GetBreakPieceUnstable(i);
1031 const CFGAS_TextUserData* pUserData = pPiece->GetUserData();
1032 if (pUserData)
1033 pStyle = pUserData->m_pStyle;
1034 float fVerScale = pPiece->GetVerticalScale() / 100.0f;
1035
1036 auto pTP = std::make_unique<TextPiece>();
1037 pTP->iChars = pPiece->GetCharCount();
1038 pTP->szText = pPiece->GetString();
1039 pTP->Widths = pPiece->GetWidths();
1040 pTP->iBidiLevel = pPiece->GetBidiLevel();
1041 pTP->iHorScale = pPiece->GetHorizontalScale();
1042 pTP->iVerScale = pPiece->GetVerticalScale();
1043 pTP->iUnderline =
1044 m_pTextParser->GetUnderline(m_pTextProvider, pStyle.Get());
1045 pTP->iPeriod =
1046 m_pTextParser->GetUnderlinePeriod(m_pTextProvider, pStyle.Get());
1047 pTP->iLineThrough =
1048 m_pTextParser->GetLinethrough(m_pTextProvider, pStyle.Get());
1049 pTP->dwColor = m_pTextParser->GetColor(m_pTextProvider, pStyle.Get());
1050 pTP->pFont =
1051 m_pTextParser->GetFont(m_pDoc.Get(), m_pTextProvider, pStyle.Get());
1052 pTP->fFontSize =
1053 m_pTextParser->GetFontSize(m_pTextProvider, pStyle.Get());
1054 pTP->rtPiece.left = pPiece->GetStartPos() / 20000.0f;
1055 pTP->rtPiece.width = pPiece->GetWidth() / 20000.0f;
1056 pTP->rtPiece.height =
1057 static_cast<float>(pPiece->GetFontSize()) * fVerScale / 20.0f;
1058 float fBaseLineTemp =
1059 m_pTextParser->GetBaseline(m_pTextProvider, pStyle.Get());
1060 pTP->rtPiece.top = fBaseLineTemp;
1061
1062 float fLineHeight = m_pTextParser->GetLineHeight(
1063 m_pTextProvider, pStyle.Get(), m_iLines == 0, fVerScale);
1064 if (fBaseLineTemp > 0) {
1065 float fLineHeightTmp = fBaseLineTemp + pTP->rtPiece.height;
1066 if (fLineHeight < fLineHeightTmp)
1067 fLineHeight = fLineHeightTmp;
1068 }
1069 fLineStep = std::max(fLineStep, fLineHeight);
1070 pTP->pLinkData = pUserData ? pUserData->m_pLinkData : nullptr;
1071 pPieceLine->m_textPieces.push_back(std::move(pTP));
1072 DoTabstops(pStyle.Get(), pPieceLine);
1073 }
1074 for (const auto& pTP : pPieceLine->m_textPieces) {
1075 float& fTop = pTP->rtPiece.top;
1076 float fBaseLineTemp = fTop;
1077 fTop = *pLinePos + fLineStep - pTP->rtPiece.height - fBaseLineTemp;
1078 fTop = std::max(0.0f, fTop);
1079 }
1080 *pLinePos += fLineStep + fBaseLine;
1081 } else {
1082 float fLineStep = 0;
1083 float fLineWidth = 0;
1084 for (int32_t i = 0; i < iPieces; i++) {
1085 const CFGAS_BreakPiece* pPiece = m_pBreak->GetBreakPieceUnstable(i);
1086 const CFGAS_TextUserData* pUserData = pPiece->GetUserData();
1087 if (pUserData)
1088 pStyle = pUserData->m_pStyle;
1089 float fVerScale = pPiece->GetVerticalScale() / 100.0f;
1090 float fBaseLine =
1091 m_pTextParser->GetBaseline(m_pTextProvider, pStyle.Get());
1092 float fLineHeight = m_pTextParser->GetLineHeight(
1093 m_pTextProvider, pStyle.Get(), m_iLines == 0, fVerScale);
1094 if (fBaseLine > 0) {
1095 float fLineHeightTmp =
1096 fBaseLine +
1097 static_cast<float>(pPiece->GetFontSize()) * fVerScale / 20.0f;
1098 if (fLineHeight < fLineHeightTmp) {
1099 fLineHeight = fLineHeightTmp;
1100 }
1101 }
1102 fLineStep = std::max(fLineStep, fLineHeight);
1103 fLineWidth += pPiece->GetWidth() / 20000.0f;
1104 }
1105 *pLinePos += fLineStep;
1106 m_fMaxWidth = std::max(m_fMaxWidth, fLineWidth);
1107 if (m_pLoader && m_pLoader->bSaveLineHeight) {
1108 float fHeight = *pLinePos - m_pLoader->fLastPos;
1109 m_pLoader->fLastPos = *pLinePos;
1110 m_pLoader->lineHeights.push_back(fHeight);
1111 }
1112 }
1113
1114 m_pBreak->ClearBreakPieces();
1115 if (dwStatus == CFGAS_Char::BreakType::kParagraph) {
1116 m_pBreak->Reset();
1117 if (!pStyle && bEndBreak) {
1118 CXFA_Para* para = m_pTextProvider->GetParaIfExists();
1119 if (para) {
1120 float fStartPos = para->GetMarginLeft();
1121 float fIndent = para->GetTextIndent();
1122 if (fIndent > 0)
1123 fStartPos += fIndent;
1124
1125 float fSpaceBelow = para->GetSpaceBelow();
1126 if (fSpaceBelow < 0.1f)
1127 fSpaceBelow = 0;
1128
1129 m_pBreak->SetLineStartPos(fStartPos);
1130 *pLinePos += fSpaceBelow;
1131 }
1132 }
1133 }
1134
1135 if (pStyle) {
1136 float fStart = 0;
1137 const CFX_CSSRect* pRect = pStyle->GetMarginWidth();
1138 if (pRect)
1139 fStart = pRect->left.GetValue();
1140
1141 float fTextIndent = pStyle->GetTextIndent().GetValue();
1142 if (fTextIndent < 0)
1143 fStart -= fTextIndent;
1144
1145 m_pBreak->SetLineStartPos(fStart);
1146 }
1147 m_iLines++;
1148}
1149
1150void CXFA_TextLayout::RenderString(CFX_RenderDevice* pDevice,
1151 PieceLine* pPieceLine,
1152 size_t szPiece,
1153 pdfium::span<TextCharPos> pCharPos,
1154 const CFX_Matrix& mtDoc2Device) {
1155 const TextPiece* pPiece = pPieceLine->m_textPieces[szPiece].get();
1156 size_t szCount = GetDisplayPos(pPiece, pCharPos);
1157 if (szCount > 0) {
1158 CFDE_TextOut::DrawString(pDevice, pPiece->dwColor, pPiece->pFont,
1159 pCharPos.first(szCount), pPiece->fFontSize,
1160 mtDoc2Device);
1161 }
1162 pPieceLine->m_charCounts.push_back(szCount);
1163}
1164
1165void CXFA_TextLayout::RenderPath(CFX_RenderDevice* pDevice,
1166 const PieceLine* pPieceLine,
1167 size_t szPiece,
1168 pdfium::span<TextCharPos> pCharPos,
1169 const CFX_Matrix& mtDoc2Device) {
1170 const TextPiece* pPiece = pPieceLine->m_textPieces[szPiece].get();
1171 bool bNoUnderline = pPiece->iUnderline < 1 || pPiece->iUnderline > 2;
1172 bool bNoLineThrough = pPiece->iLineThrough < 1 || pPiece->iLineThrough > 2;
1173 if (bNoUnderline && bNoLineThrough)
1174 return;
1175
1176 CFX_Path path;
1177 size_t szChars = GetDisplayPos(pPiece, pCharPos);
1178 if (szChars > 0) {
1179 CFX_PointF pt1;
1180 CFX_PointF pt2;
1181 float fEndY = pCharPos[0].m_Origin.y + 1.05f;
1182 if (pPiece->iPeriod == XFA_AttributeValue::Word) {
1183 for (int32_t i = 0; i < pPiece->iUnderline; i++) {
1184 for (size_t j = 0; j < szChars; j++) {
1185 pt1.x = pCharPos[j].m_Origin.x;
1186 pt2.x =
1187 pt1.x + pCharPos[j].m_FontCharWidth * pPiece->fFontSize / 1000.0f;
1188 pt1.y = pt2.y = fEndY;
1189 path.AppendLine(pt1, pt2);
1190 }
1191 fEndY += 2.0f;
1192 }
1193 } else {
1194 pt1.x = pCharPos[0].m_Origin.x;
1195 pt2.x =
1196 pCharPos[szChars - 1].m_Origin.x +
1197 pCharPos[szChars - 1].m_FontCharWidth * pPiece->fFontSize / 1000.0f;
1198 for (int32_t i = 0; i < pPiece->iUnderline; i++) {
1199 pt1.y = pt2.y = fEndY;
1200 path.AppendLine(pt1, pt2);
1201 fEndY += 2.0f;
1202 }
1203 }
1204 fEndY = pCharPos[0].m_Origin.y - pPiece->rtPiece.height * 0.25f;
1205 pt1.x = pCharPos[0].m_Origin.x;
1206 pt2.x = pCharPos[szChars - 1].m_Origin.x +
1207 pCharPos[szChars - 1].m_FontCharWidth * pPiece->fFontSize / 1000.0f;
1208 for (int32_t i = 0; i < pPiece->iLineThrough; i++) {
1209 pt1.y = pt2.y = fEndY;
1210 path.AppendLine(pt1, pt2);
1211 fEndY += 2.0f;
1212 }
1213 } else {
1214 if (bNoLineThrough &&
1215 (bNoUnderline || pPiece->iPeriod != XFA_AttributeValue::All)) {
1216 return;
1217 }
1218 bool bHasCount = false;
1219 size_t szPiecePrev = szPiece;
1220 size_t szPieceNext = szPiece;
1221 while (szPiecePrev > 0) {
1222 szPiecePrev--;
1223 if (pPieceLine->m_charCounts[szPiecePrev] > 0) {
1224 bHasCount = true;
1225 break;
1226 }
1227 }
1228 if (!bHasCount)
1229 return;
1230
1231 bHasCount = false;
1232 while (szPieceNext + 1 < pPieceLine->m_textPieces.size()) {
1233 ++szPieceNext;
1234 if (pPieceLine->m_charCounts[szPieceNext] > 0) {
1235 bHasCount = true;
1236 break;
1237 }
1238 }
1239 if (!bHasCount)
1240 return;
1241
1242 float fOrgX = 0.0f;
1243 float fEndX = 0.0f;
1244 pPiece = pPieceLine->m_textPieces[szPiecePrev].get();
1245 szChars = GetDisplayPos(pPiece, pCharPos);
1246 if (szChars < 1)
1247 return;
1248
1249 fOrgX = pCharPos[szChars - 1].m_Origin.x +
1250 pCharPos[szChars - 1].m_FontCharWidth * pPiece->fFontSize / 1000.0f;
1251 pPiece = pPieceLine->m_textPieces[szPieceNext].get();
1252 szChars = GetDisplayPos(pPiece, pCharPos);
1253 if (szChars < 1)
1254 return;
1255
1256 fEndX = pCharPos[0].m_Origin.x;
1257 CFX_PointF pt1;
1258 CFX_PointF pt2;
1259 pt1.x = fOrgX;
1260 pt2.x = fEndX;
1261 float fEndY = pCharPos[0].m_Origin.y + 1.05f;
1262 for (int32_t i = 0; i < pPiece->iUnderline; i++) {
1263 pt1.y = fEndY;
1264 pt2.y = fEndY;
1265 path.AppendLine(pt1, pt2);
1266 fEndY += 2.0f;
1267 }
1268 fEndY = pCharPos[0].m_Origin.y - pPiece->rtPiece.height * 0.25f;
1269 for (int32_t i = 0; i < pPiece->iLineThrough; i++) {
1270 pt1.y = fEndY;
1271 pt2.y = fEndY;
1272 path.AppendLine(pt1, pt2);
1273 fEndY += 2.0f;
1274 }
1275 }
1276
1277 CFX_GraphStateData graphState;
1280 graphState.m_LineWidth = 1;
1281 graphState.m_MiterLimit = 10;
1282 graphState.m_DashPhase = 0;
1283 pDevice->DrawPath(path, &mtDoc2Device, &graphState, 0, pPiece->dwColor,
1285}
1286
1287size_t CXFA_TextLayout::GetDisplayPos(const TextPiece* pPiece,
1288 pdfium::span<TextCharPos> pCharPos) {
1289 if (!pPiece || pPiece->iChars < 1) {
1290 return 0;
1291 }
1292 return m_pBreak->GetDisplayPos(pPiece, pCharPos);
1293}
CFX_CSSDisplay
Definition cfx_css.h:48
CFX_CSSTextAlign
Definition cfx_css.h:62
CFX_CSSLengthUnit
Definition cfx_css.h:40
CFX_XMLElement * ToXMLElement(CFX_XMLNode *pNode)
const CFX_XMLText * ToXMLText(const CFX_XMLNode *pNode)
Definition cfx_xmltext.h:42
#define DCHECK
Definition check.h:33
int32_t GetHorizontalScale() const
int32_t GetFontSize() const
int32_t GetCharCount() const
int32_t GetStartPos() const
const CFGAS_TextUserData * GetUserData() const
int32_t GetVerticalScale() const
int32_t GetBidiLevel() const
WideString GetString() const
int32_t GetWidth() const
void SetMarginWidth(const CFX_CSSRect &rect)
const CFX_CSSLength & GetTextIndent() const
const CFX_CSSRect * GetMarginWidth() const
CFX_CSSTextAlign GetTextAlign() const
const CFX_CSSRect * GetPaddingWidth() const
float GetValue() const
Definition cfx_css.h:116
CFX_CSSLength & Set(CFX_CSSLengthUnit eUnit, float fValue)
Definition cfx_css.h:108
CFX_CSSLength right
Definition cfx_css.h:151
CFX_CSSLength top
Definition cfx_css.h:150
CFX_CSSLength bottom
Definition cfx_css.h:152
CFX_CSSLength left
Definition cfx_css.h:149
void AppendLine(const CFX_PointF &pt1, const CFX_PointF &pt2)
Definition cfx_path.cpp:298
FX_RECT GetOuterRect() const
float right() const
bool SetClip_Rect(const FX_RECT &pRect)
bool DrawPath(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options)
void RestoreState(bool bKeepSaved)
WideString GetLocalTagName() const
WideString GetAttribute(const WideString &name) const
virtual Type GetType() const =0
const WideString & GetText() const
Definition cfx_xmltext.h:25
float GetMarginLeft()
Definition cxfa_para.cpp:73
float GetTextIndent()
Definition cxfa_para.cpp:89
float GetSpaceAbove()
Definition cxfa_para.cpp:81
float GetMarginRight()
Definition cxfa_para.cpp:77
XFA_AttributeValue GetHorizontalAlign()
Definition cxfa_para.cpp:57
XFA_AttributeValue GetVerticalAlign()
Definition cxfa_para.cpp:63
float GetSpaceBelow()
Definition cxfa_para.cpp:85
float StartLayout(float fWidth)
WideString GetLinkURLAtPoint(const CFX_PointF &point)
CFX_SizeF CalcSize(const CFX_SizeF &minSize, const CFX_SizeF &maxSize)
bool DrawString(CFX_RenderDevice *pFxDevice, const CFX_Matrix &mtDoc2Device, const CFX_RectF &rtClip, size_t szBlockIndex)
float DoLayout(float fTextHeight)
float DoSplitLayout(size_t szBlockIndex, float fCalcHeight, float fTextHeight)
float Layout(const CFX_SizeF &size)
void ItemBlocks(const CFX_RectF &rtText, size_t szBlockIndex)
CFX_CSSDisplay GetDisplay() const
WideString(wchar_t ch)
static WideString Format(const wchar_t *pFormat,...)
WideString & operator=(WideString &&that) noexcept
WideString()=default
WideString & operator+=(wchar_t ch)
WideString & operator=(const WideString &that)
bool EqualsASCII(ByteStringView that) const
Definition widestring.h:111
CFX_PTemplate< float > CFX_PointF
CFX_STemplate< float > CFX_SizeF
XFA_AttributeValue
Definition fxfa_basic.h:60
Definition heap.h:12
#define NOTREACHED_NORETURN()
Definition notreached.h:22
fxcrt::WideString WideString
Definition widestring.h:207