1 // Copyright 2014 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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
10 struct CPVT_SectionInfo;
17 class CPDF_EditContainer;
18 class CPDF_VariableText;
19 class CPDF_VariableText_Iterator;
20 #define IsFloatZero(f) ((f) < 0.0001 && (f) > -0.0001)
21 #define IsFloatBigger(fa,fb) ((fa) > (fb) && !IsFloatZero((fa) - (fb)))
22 #define IsFloatSmaller(fa,fb) ((fa) < (fb) && !IsFloatZero((fa) - (fb)))
23 template<class T> T FPDF_MIN (const T & i, const T & j)
25 return ((i < j) ? i : j);
27 template<class T> T FPDF_MAX (const T & i, const T & j)
29 return ((i > j) ? i : j);
34 CPVT_Size() : x(0.0f), y(0.0f)
37 CPVT_Size(FX_FLOAT x, FX_FLOAT y)
44 class CPVT_FloatRect : public CFX_FloatRect
49 left = top = right = bottom = 0.0f;
51 CPVT_FloatRect(FX_FLOAT left, FX_FLOAT top,
52 FX_FLOAT right, FX_FLOAT bottom)
57 this->bottom = bottom;
59 CPVT_FloatRect(const CPDF_Rect & rect)
61 this->left = rect.left;
63 this->right = rect.right;
64 this->bottom = rect.bottom;
68 left = top = right = bottom = 0.0f;
70 FX_FLOAT Height() const
72 if(this->top > this->bottom) {
73 return this->top - this->bottom;
75 return this->bottom - this->top;
79 struct CPVT_SectionInfo {
80 CPVT_SectionInfo() : rcSection(), nTotalLine(0), pSecProps(NULL), pWordProps(NULL)
83 virtual ~CPVT_SectionInfo()
92 CPVT_SectionInfo(const CPVT_SectionInfo & other): rcSection(), nTotalLine(0), pSecProps(NULL), pWordProps(NULL)
96 void operator = (const CPVT_SectionInfo & other)
101 this->rcSection = other.rcSection;
102 this->nTotalLine = other.nTotalLine;
103 if (other.pSecProps) {
105 *pSecProps = *other.pSecProps;
107 pSecProps = FX_NEW CPVT_SecProps(*other.pSecProps);
110 if (other.pWordProps) {
112 *pWordProps = *other.pWordProps;
114 pWordProps = FX_NEW CPVT_WordProps(*other.pWordProps);
118 CPVT_FloatRect rcSection;
120 CPVT_SecProps* pSecProps;
121 CPVT_WordProps* pWordProps;
123 struct CPVT_LineInfo {
124 CPVT_LineInfo() : nTotalWord(0), nBeginWordIndex(-1), nEndWordIndex(-1),
125 fLineX(0.0f), fLineY(0.0f), fLineWidth(0.0f), fLineAscent(0.0f), fLineDescent(0.0f)
129 FX_INT32 nBeginWordIndex;
130 FX_INT32 nEndWordIndex;
134 FX_FLOAT fLineAscent;
135 FX_FLOAT fLineDescent;
137 struct CPVT_WordInfo : public CFX_Object {
138 CPVT_WordInfo() : Word(0), nCharset(0),
139 fWordX(0.0f), fWordY(0.0f), fWordTail(0.0f), nFontIndex(-1), pWordProps(NULL)
142 CPVT_WordInfo(FX_WORD word, FX_INT32 charset, FX_INT32 fontIndex, CPVT_WordProps * pProps):
143 Word(word), nCharset(charset), fWordX(0.0f), fWordY(0.0f), fWordTail(0.0f),
144 nFontIndex(fontIndex), pWordProps(pProps)
147 virtual ~CPVT_WordInfo()
153 CPVT_WordInfo(const CPVT_WordInfo & word): Word(0), nCharset(0),
154 fWordX(0.0f), fWordY(0.0f), fWordTail(0.0f), nFontIndex(-1), pWordProps(NULL)
158 void operator = (const CPVT_WordInfo & word)
163 this->Word = word.Word;
164 this->nCharset = word.nCharset;
165 this->nFontIndex = word.nFontIndex;
166 if (word.pWordProps) {
168 *pWordProps = *word.pWordProps;
170 pWordProps = FX_NEW CPVT_WordProps(*word.pWordProps);
180 CPVT_WordProps* pWordProps;
182 struct CPVT_FloatRange {
183 CPVT_FloatRange() : fMin(0.0f), fMax(0.0f)
186 CPVT_FloatRange(FX_FLOAT min, FX_FLOAT max) : fMin(min), fMax(max)
189 FX_FLOAT Range() const
195 template<class TYPE> class CPVT_ArrayTemplate : public CFX_ArrayTemplate<TYPE>
200 return CFX_ArrayTemplate<TYPE>::GetSize() <= 0;
202 TYPE GetAt(int nIndex) const
204 if (nIndex >= 0 && nIndex < CFX_ArrayTemplate<TYPE>::GetSize()) {
205 return CFX_ArrayTemplate<TYPE>::GetAt(nIndex);
209 void RemoveAt(int nIndex)
211 if (nIndex >= 0 && nIndex < CFX_ArrayTemplate<TYPE>::GetSize()) {
212 CFX_ArrayTemplate<TYPE>::RemoveAt(nIndex);
216 class CLine : public CFX_Object
221 CPVT_WordPlace GetBeginWordPlace() const;
222 CPVT_WordPlace GetEndWordPlace() const;
223 CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace & place) const;
224 CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace & place) const;
225 CPVT_WordPlace LinePlace;
226 CPVT_LineInfo m_LineInfo;
231 CLines() : m_nTotal(0) {}
236 FX_INT32 GetSize() const
238 return m_Lines.GetSize();
240 CLine * GetAt(FX_INT32 nIndex) const
242 return m_Lines.GetAt(nIndex);
250 for (FX_INT32 i = 0, sz = GetSize(); i < sz; i++) {
256 FX_INT32 Add(const CPVT_LineInfo & lineinfo)
258 if (m_nTotal >= GetSize()) {
259 if (CLine * pLine = FX_NEW CLine) {
260 pLine->m_LineInfo = lineinfo;
266 if (CLine * pLine = GetAt(m_nTotal)) {
267 pLine->m_LineInfo = lineinfo;
274 for (FX_INT32 i = GetSize() - 1; i >= m_nTotal; i--) {
280 CPVT_ArrayTemplate<CLine*> m_Lines;
283 class CSection : public CFX_Object
285 friend class CTypeset;
287 CSection(CPDF_VariableText * pVT);
290 void ResetLineArray();
291 void ResetWordArray();
292 void ResetLinePlace();
293 CPVT_WordPlace AddWord(const CPVT_WordPlace & place, const CPVT_WordInfo & wordinfo);
294 CPVT_WordPlace AddLine(const CPVT_LineInfo & lineinfo);
295 void ClearWords(const CPVT_WordRange & PlaceRange);
296 void ClearWord(const CPVT_WordPlace & place);
297 CPVT_FloatRect Rearrange();
298 CPVT_Size GetSectionSize(FX_FLOAT fFontSize);
299 CPVT_WordPlace GetBeginWordPlace() const;
300 CPVT_WordPlace GetEndWordPlace() const;
301 CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace & place) const;
302 CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace & place) const;
303 void UpdateWordPlace(CPVT_WordPlace & place) const;
304 CPVT_WordPlace SearchWordPlace(const CPDF_Point & point) const;
305 CPVT_WordPlace SearchWordPlace(FX_FLOAT fx, const CPVT_WordPlace & lineplace) const;
306 CPVT_WordPlace SearchWordPlace(FX_FLOAT fx, const CPVT_WordRange & range) const;
308 CPVT_WordPlace SecPlace;
309 CPVT_SectionInfo m_SecInfo;
311 CPVT_ArrayTemplate<CPVT_WordInfo*> m_WordArray;
313 void ClearLeftWords(FX_INT32 nWordIndex);
314 void ClearRightWords(FX_INT32 nWordIndex);
315 void ClearMidWords(FX_INT32 nBeginIndex, FX_INT32 nEndIndex);
317 CPDF_VariableText *m_pVT;
322 CTypeset(CSection * pSection);
324 CPVT_Size GetEditSize(FX_FLOAT fFontSize);
325 CPVT_FloatRect Typeset();
326 CPVT_FloatRect CharArray();
328 void SplitLines(FX_BOOL bTypeset, FX_FLOAT fFontSize);
331 CPVT_FloatRect m_rcRet;
332 CPDF_VariableText * m_pVT;
333 CSection * m_pSection;
335 class CPDF_EditContainer
338 CPDF_EditContainer(): m_rcPlate(0, 0, 0, 0), m_rcContent(0, 0, 0, 0) {};
339 virtual ~CPDF_EditContainer() {};
340 virtual void SetPlateRect(const CPDF_Rect & rect)
344 virtual const CPDF_Rect & GetPlateRect() const
348 virtual void SetContentRect(const CPVT_FloatRect & rect)
352 virtual CPDF_Rect GetContentRect() const
356 FX_FLOAT GetPlateWidth() const
358 return m_rcPlate.right - m_rcPlate.left;
360 FX_FLOAT GetPlateHeight() const
362 return m_rcPlate.top - m_rcPlate.bottom;
364 CPVT_Size GetPlateSize() const
366 return CPVT_Size(GetPlateWidth(), GetPlateHeight());
368 CPDF_Point GetBTPoint() const
370 return CPDF_Point(m_rcPlate.left, m_rcPlate.top);
372 CPDF_Point GetETPoint() const
374 return CPDF_Point(m_rcPlate.right, m_rcPlate.bottom);
376 inline CPDF_Point InToOut(const CPDF_Point & point) const
378 return CPDF_Point(point.x + GetBTPoint().x, GetBTPoint().y - point.y);
380 inline CPDF_Point OutToIn(const CPDF_Point & point) const
382 return CPDF_Point(point.x - GetBTPoint().x, GetBTPoint().y - point.y);
384 inline CPDF_Rect InToOut(const CPVT_FloatRect & rect) const
386 CPDF_Point ptLeftTop = InToOut(CPDF_Point(rect.left, rect.top));
387 CPDF_Point ptRightBottom = InToOut(CPDF_Point(rect.right, rect.bottom));
388 return CPDF_Rect(ptLeftTop.x, ptRightBottom.y, ptRightBottom.x, ptLeftTop.y);
390 inline CPVT_FloatRect OutToIn(const CPDF_Rect & rect) const
392 CPDF_Point ptLeftTop = OutToIn(CPDF_Point(rect.left, rect.top));
393 CPDF_Point ptRightBottom = OutToIn(CPDF_Point(rect.right, rect.bottom));
394 return CPVT_FloatRect(ptLeftTop.x, ptLeftTop.y, ptRightBottom.x, ptRightBottom.y);
399 CPVT_FloatRect m_rcContent;
401 class CPDF_VariableText : public IPDF_VariableText, public CFX_Object, private CPDF_EditContainer
403 friend class CTypeset;
404 friend class CSection;
405 friend class CPDF_VariableText_Iterator;
408 virtual ~CPDF_VariableText();
409 IPDF_VariableText_Provider* SetProvider(IPDF_VariableText_Provider * pProvider);
410 IPDF_VariableText_Iterator* GetIterator();
411 void SetPlateRect(const CPDF_Rect & rect)
413 CPDF_EditContainer::SetPlateRect(rect);
415 void SetAlignment(FX_INT32 nFormat = 0)
417 m_nAlignment = nFormat;
419 void SetPasswordChar(FX_WORD wSubWord = '*')
421 m_wSubWord = wSubWord;
423 void SetLimitChar(FX_INT32 nLimitChar = 0)
425 m_nLimitChar = nLimitChar;
427 void SetCharSpace(FX_FLOAT fCharSpace = 0.0f)
429 m_fCharSpace = fCharSpace;
431 void SetHorzScale(FX_INT32 nHorzScale = 100)
433 m_nHorzScale = nHorzScale;
435 void SetMultiLine(FX_BOOL bMultiLine = TRUE)
437 m_bMultiLine = bMultiLine;
439 void SetAutoReturn(FX_BOOL bAuto = TRUE)
441 m_bLimitWidth = bAuto;
443 void SetFontSize(FX_FLOAT fFontSize)
445 m_fFontSize = fFontSize;
447 void SetCharArray(FX_INT32 nCharArray = 0)
449 m_nCharArray = nCharArray;
451 void SetAutoFontSize(FX_BOOL bAuto = TRUE)
453 m_bAutoFontSize = bAuto;
455 void SetRichText(FX_BOOL bRichText)
457 m_bRichText = bRichText;
459 void SetLineLeading(FX_FLOAT fLineLeading)
461 m_fLineLeading = fLineLeading;
464 FX_BOOL IsValid() const
468 FX_BOOL IsRichText() const
473 void RearrangePart(const CPVT_WordRange & PlaceRange);
475 void SetText(FX_LPCWSTR text, FX_INT32 charset = 1, const CPVT_SecProps * pSecProps = NULL,
476 const CPVT_WordProps * pWordProps = NULL);
477 CPVT_WordPlace InsertWord(const CPVT_WordPlace & place, FX_WORD word, FX_INT32 charset = 1,
478 const CPVT_WordProps * pWordProps = NULL);
479 CPVT_WordPlace InsertSection(const CPVT_WordPlace & place, const CPVT_SecProps * pSecProps = NULL,
480 const CPVT_WordProps * pWordProps = NULL);
481 CPVT_WordPlace InsertText(const CPVT_WordPlace & place, FX_LPCWSTR text, FX_INT32 charset = 1,
482 const CPVT_SecProps * pSecProps = NULL, const CPVT_WordProps * pWordProps = NULL);
483 CPVT_WordPlace DeleteWords(const CPVT_WordRange & PlaceRange);
484 CPVT_WordPlace DeleteWord(const CPVT_WordPlace & place);
485 CPVT_WordPlace BackSpaceWord(const CPVT_WordPlace & place);
486 const CPDF_Rect & GetPlateRect() const
488 return CPDF_EditContainer::GetPlateRect();
490 CPDF_Rect GetContentRect() const;
491 FX_INT32 GetTotalWords() const;
492 FX_FLOAT GetFontSize() const
496 FX_INT32 GetAlignment() const
500 FX_INT32 GetCharArray() const
504 FX_INT32 GetLimitChar() const
508 FX_BOOL IsMultiLine() const
512 FX_INT32 GetHorzScale() const
516 FX_FLOAT GetCharSpace() const
521 CPVT_WordPlace GetBeginWordPlace() const;
522 CPVT_WordPlace GetEndWordPlace() const;
523 CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace & place) const;
524 CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace & place) const;
525 CPVT_WordPlace SearchWordPlace(const CPDF_Point & point) const;
526 CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace & place, const CPDF_Point & point) const;
527 CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace & place, const CPDF_Point & point) const;
528 CPVT_WordPlace GetLineBeginPlace(const CPVT_WordPlace & place) const;
529 CPVT_WordPlace GetLineEndPlace(const CPVT_WordPlace & place) const;
530 CPVT_WordPlace GetSectionBeginPlace(const CPVT_WordPlace & place) const;
531 CPVT_WordPlace GetSectionEndPlace(const CPVT_WordPlace & place) const;
532 void UpdateWordPlace(CPVT_WordPlace & place) const;
533 FX_INT32 WordPlaceToWordIndex(const CPVT_WordPlace & place) const;
534 CPVT_WordPlace WordIndexToWordPlace(FX_INT32 index) const;
535 FX_WORD GetPasswordChar() const
539 FX_WORD GetSubWord() const
544 FX_INT32 GetCharWidth(FX_INT32 nFontIndex, FX_WORD Word, FX_WORD SubWord, FX_INT32 nWordStyle);
545 FX_INT32 GetTypeAscent(FX_INT32 nFontIndex);
546 FX_INT32 GetTypeDescent(FX_INT32 nFontIndex);
547 FX_INT32 GetWordFontIndex(FX_WORD word, FX_INT32 charset, FX_INT32 nFontIndex);
548 FX_INT32 GetDefaultFontIndex();
549 FX_BOOL IsLatinWord(FX_WORD word);
552 CPVT_WordPlace AddSection(const CPVT_WordPlace & place, const CPVT_SectionInfo & secinfo);
553 CPVT_WordPlace AddLine(const CPVT_WordPlace & place, const CPVT_LineInfo & lineinfo);
554 CPVT_WordPlace AddWord(const CPVT_WordPlace & place, const CPVT_WordInfo & wordinfo);
555 FX_BOOL GetWordInfo(const CPVT_WordPlace & place, CPVT_WordInfo & wordinfo);
556 FX_BOOL SetWordInfo(const CPVT_WordPlace & place, const CPVT_WordInfo & wordinfo);
557 FX_BOOL GetLineInfo(const CPVT_WordPlace & place, CPVT_LineInfo & lineinfo);
558 FX_BOOL GetSectionInfo(const CPVT_WordPlace & place, CPVT_SectionInfo & secinfo);
559 FX_FLOAT GetWordFontSize(const CPVT_WordInfo & WordInfo, FX_BOOL bFactFontSize = FALSE);
560 FX_FLOAT GetWordWidth(FX_INT32 nFontIndex, FX_WORD Word, FX_WORD SubWord,
561 FX_FLOAT fCharSpace, FX_INT32 nHorzScale,
562 FX_FLOAT fFontSize, FX_FLOAT fWordTail, FX_INT32 nWordStyle);
563 FX_FLOAT GetWordWidth(const CPVT_WordInfo & WordInfo);
564 FX_FLOAT GetWordAscent(const CPVT_WordInfo & WordInfo, FX_FLOAT fFontSize);
565 FX_FLOAT GetWordDescent(const CPVT_WordInfo & WordInfo, FX_FLOAT fFontSize);
566 FX_FLOAT GetWordAscent(const CPVT_WordInfo & WordInfo, FX_BOOL bFactFontSize = FALSE);
567 FX_FLOAT GetWordDescent(const CPVT_WordInfo & WordInfo, FX_BOOL bFactFontSize = FALSE);
568 FX_FLOAT GetLineAscent(const CPVT_SectionInfo & SecInfo);
569 FX_FLOAT GetLineDescent(const CPVT_SectionInfo & SecInfo);
570 FX_FLOAT GetFontAscent(FX_INT32 nFontIndex, FX_FLOAT fFontSize);
571 FX_FLOAT GetFontDescent(FX_INT32 nFontIndex, FX_FLOAT fFontSize);
572 FX_INT32 GetWordFontIndex(const CPVT_WordInfo & WordInfo);
573 FX_FLOAT GetCharSpace(const CPVT_WordInfo & WordInfo);
574 FX_INT32 GetHorzScale(const CPVT_WordInfo & WordInfo);
575 FX_FLOAT GetLineLeading(const CPVT_SectionInfo & SecInfo);
576 FX_FLOAT GetLineIndent(const CPVT_SectionInfo & SecInfo);
577 FX_INT32 GetAlignment(const CPVT_SectionInfo& SecInfo);
579 void ClearSectionRightWords(const CPVT_WordPlace & place);
580 CPVT_WordPlace AjustLineHeader(const CPVT_WordPlace & place, FX_BOOL bPrevOrNext) const;
581 FX_BOOL ClearEmptySection(const CPVT_WordPlace & place);
582 void ClearEmptySections(const CPVT_WordRange & PlaceRange);
583 void LinkLatterSection(const CPVT_WordPlace & place);
584 void ClearWords(const CPVT_WordRange & PlaceRange);
585 CPVT_WordPlace ClearLeftWord(const CPVT_WordPlace & place);
586 CPVT_WordPlace ClearRightWord(const CPVT_WordPlace & place);
588 CPVT_FloatRect Rearrange(const CPVT_WordRange & PlaceRange);
589 FX_FLOAT GetAutoFontSize();
590 FX_BOOL IsBigger(FX_FLOAT fFontSize);
591 CPVT_FloatRect RearrangeSections(const CPVT_WordRange & PlaceRange);
593 void ResetSectionArray();
595 CPVT_ArrayTemplate<CSection*> m_SectionArray;
596 FX_INT32 m_nLimitChar;
597 FX_INT32 m_nCharArray;
598 FX_BOOL m_bMultiLine;
599 FX_BOOL m_bLimitWidth;
600 FX_BOOL m_bAutoFontSize;
601 FX_INT32 m_nAlignment;
602 FX_FLOAT m_fLineLeading;
603 FX_FLOAT m_fCharSpace;
604 FX_INT32 m_nHorzScale;
606 FX_FLOAT m_fFontSize;
611 IPDF_VariableText_Provider * m_pVTProvider;
612 CPDF_VariableText_Iterator * m_pVTIterator;
614 class CPDF_VariableText_Iterator : public IPDF_VariableText_Iterator, public CFX_Object
617 CPDF_VariableText_Iterator(CPDF_VariableText * pVT);
618 virtual ~CPDF_VariableText_Iterator();
623 FX_BOOL NextSection();
624 FX_BOOL PrevSection();
625 FX_BOOL SetWord(const CPVT_Word & word);
626 FX_BOOL GetWord(CPVT_Word & word) const;
627 FX_BOOL GetLine(CPVT_Line & line) const;
628 FX_BOOL GetSection(CPVT_Section & section) const;
629 FX_BOOL SetSection(const CPVT_Section & section);
630 void SetAt(FX_INT32 nWordIndex);
631 void SetAt(const CPVT_WordPlace & place);
632 const CPVT_WordPlace & GetAt() const
637 CPVT_WordPlace m_CurPos;
638 CPDF_VariableText * m_pVT;