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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef CORE_INCLUDE_FPDFDOC_FPDF_VT_H_
8 #define CORE_INCLUDE_FPDFDOC_FPDF_VT_H_
9 
10 #include "../fpdfapi/fpdf_parser.h"
11 #include "../fxcrt/fx_basic.h"
12 #include "../fxge/fx_dib.h"
13 
14 class IPDF_VariableText;
15 class IPDF_VariableText_Iterator;
16 class IPDF_VariableText_Provider;
17 struct CPVT_Line;
18 struct CPVT_Section;
19 struct CPVT_Word;
20 struct CPVT_WordPlace;
21 struct CPVT_WordRange;
22 
23 struct CPVT_WordPlace {
24 
CPVT_WordPlaceCPVT_WordPlace25     CPVT_WordPlace() : nSecIndex(-1), nLineIndex(-1), nWordIndex(-1)
26     {
27     }
28 
CPVT_WordPlaceCPVT_WordPlace29     CPVT_WordPlace(FX_INT32 nSecIndex, FX_INT32 nLineIndex, FX_INT32 nWordIndex)
30     {
31         this->nSecIndex = nSecIndex;
32         this->nLineIndex = nLineIndex;
33         this->nWordIndex = nWordIndex;
34     }
35 
DefaultCPVT_WordPlace36     void Default()
37     {
38         nSecIndex = nLineIndex = nWordIndex = -1;
39     }
40 
41     FX_BOOL operator == (const CPVT_WordPlace & wp) const
42     {
43         return wp.nSecIndex == this->nSecIndex && wp.nLineIndex == this->nLineIndex && wp.nWordIndex == this->nWordIndex;
44     }
45 
46     FX_BOOL operator != (const CPVT_WordPlace & wp) const
47     {
48         return wp.nSecIndex != this->nSecIndex || wp.nLineIndex != this->nLineIndex || wp.nWordIndex != this->nWordIndex;
49     }
50 
WordCmpCPVT_WordPlace51     inline FX_INT32 WordCmp(const CPVT_WordPlace & wp) const
52     {
53         if (this->nSecIndex > wp.nSecIndex) {
54             return 1;
55         }
56         if (this->nSecIndex < wp.nSecIndex) {
57             return -1;
58         }
59         if (this->nLineIndex > wp.nLineIndex) {
60             return 1;
61         }
62         if (this->nLineIndex < wp.nLineIndex) {
63             return -1;
64         }
65         if (this->nWordIndex > wp.nWordIndex) {
66             return 1;
67         }
68         if (this->nWordIndex < wp.nWordIndex) {
69             return -1;
70         }
71         return 0;
72     }
73 
LineCmpCPVT_WordPlace74     inline FX_INT32 LineCmp(const CPVT_WordPlace & wp) const
75     {
76         if (this->nSecIndex > wp.nSecIndex) {
77             return 1;
78         }
79         if (this->nSecIndex < wp.nSecIndex) {
80             return -1;
81         }
82         if (this->nLineIndex > wp.nLineIndex) {
83             return 1;
84         }
85         if (this->nLineIndex < wp.nLineIndex) {
86             return -1;
87         }
88         return 0;
89     }
90 
SecCmpCPVT_WordPlace91     inline FX_INT32 SecCmp(const CPVT_WordPlace & wp) const
92     {
93         if (this->nSecIndex > wp.nSecIndex) {
94             return 1;
95         }
96         if (this->nSecIndex < wp.nSecIndex) {
97             return -1;
98         }
99         return 0;
100     }
101 
102     FX_INT32							nSecIndex;
103 
104     FX_INT32							nLineIndex;
105 
106     FX_INT32							nWordIndex;
107 };
108 struct CPVT_WordRange {
109 
CPVT_WordRangeCPVT_WordRange110     CPVT_WordRange()
111     {
112     }
113 
CPVT_WordRangeCPVT_WordRange114     CPVT_WordRange(const CPVT_WordPlace & begin, const CPVT_WordPlace & end)
115     {
116         Set(begin, end);
117     }
118 
DefaultCPVT_WordRange119     void Default()
120     {
121         BeginPos.Default();
122         EndPos.Default();
123     }
124 
SetCPVT_WordRange125     void Set(const CPVT_WordPlace & begin, const CPVT_WordPlace & end)
126     {
127         this->BeginPos = begin;
128         this->EndPos = end;
129         SwapWordPlace();
130     }
131 
SetBeginPosCPVT_WordRange132     void SetBeginPos(const CPVT_WordPlace & begin)
133     {
134         this->BeginPos = begin;
135         SwapWordPlace();
136     }
137 
SetEndPosCPVT_WordRange138     void SetEndPos(const CPVT_WordPlace & end)
139     {
140         this->EndPos = end;
141         SwapWordPlace();
142     }
143 
IsExistCPVT_WordRange144     FX_BOOL IsExist() const
145     {
146         return this->BeginPos != this->EndPos;
147     }
148 
149     FX_BOOL operator != (const CPVT_WordRange & wr) const
150     {
151         return wr.BeginPos != this->BeginPos || wr.EndPos != this->EndPos;
152     }
153 
SwapWordPlaceCPVT_WordRange154     void SwapWordPlace()
155     {
156         if (BeginPos.WordCmp(EndPos) > 0) {
157             CPVT_WordPlace place = EndPos;
158             EndPos = BeginPos;
159             BeginPos = place;
160         }
161     }
162 
163     CPVT_WordPlace BeginPos;
164 
165     CPVT_WordPlace EndPos;
166 };
167 struct CPVT_SecProps  {
168 
CPVT_SecPropsCPVT_SecProps169     CPVT_SecProps() : fLineLeading(0.0f), fLineIndent(0.0f), nAlignment(0)
170     {
171     }
172 
CPVT_SecPropsCPVT_SecProps173     CPVT_SecProps(FX_FLOAT lineLeading, FX_FLOAT lineIndent, FX_INT32 alignment) :
174         fLineLeading(lineLeading), fLineIndent(lineIndent), nAlignment(alignment)
175     {
176     }
177 
CPVT_SecPropsCPVT_SecProps178     CPVT_SecProps(const CPVT_SecProps & other) :
179         fLineLeading(other.fLineLeading), fLineIndent(other.fLineIndent), nAlignment(other.nAlignment)
180     {
181     }
182 
183     FX_FLOAT			fLineLeading;
184 
185     FX_FLOAT			fLineIndent;
186 
187     FX_INT32			nAlignment;
188 };
189 struct CPVT_WordProps  {
190 
CPVT_WordPropsCPVT_WordProps191     CPVT_WordProps() : nFontIndex(-1), fFontSize(0.0f), dwWordColor(0), nScriptType(0), nWordStyle(0),
192         fCharSpace(0.0f), nHorzScale(0)
193     {
194     }
195 
196     CPVT_WordProps(FX_INT32	fontIndex, FX_FLOAT fontSize, FX_COLORREF wordColor = 0, FX_INT32 scriptType = 0, FX_INT32 wordStyle = 0,
197                    FX_FLOAT charSpace = 0, FX_INT32 horzScale = 100) :
nFontIndexCPVT_WordProps198         nFontIndex(fontIndex), fFontSize(fontSize), dwWordColor(wordColor), nScriptType(scriptType),
199         nWordStyle(wordStyle), fCharSpace(charSpace), nHorzScale(horzScale)
200     {
201     }
202 
CPVT_WordPropsCPVT_WordProps203     CPVT_WordProps(const CPVT_WordProps & other) :
204         nFontIndex(other.nFontIndex), fFontSize(other.fFontSize), dwWordColor(other.dwWordColor),
205         nScriptType(other.nScriptType), nWordStyle(other.nWordStyle), fCharSpace(other.fCharSpace),
206         nHorzScale(other.nHorzScale)
207     {
208     }
209 
210     FX_INT32					nFontIndex;
211 
212     FX_FLOAT					fFontSize;
213 
214     FX_COLORREF					dwWordColor;
215 
216     FX_INT32					nScriptType;
217 
218     FX_INT32					nWordStyle;
219 
220     FX_FLOAT					fCharSpace;
221 
222     FX_INT32					nHorzScale;
223 };
224 struct CPVT_Word {
225 
CPVT_WordCPVT_Word226     CPVT_Word() : Word(0), nCharset(0), ptWord(0, 0), fAscent(0.0f), fDescent(0.0f), fWidth(0.0f),
227         fFontSize(0), WordProps()
228     {
229     }
230 
231     FX_WORD						Word;
232 
233     FX_INT32					nCharset;
234 
235     CPVT_WordPlace				WordPlace;
236 
237     CPDF_Point					ptWord;
238 
239     FX_FLOAT					fAscent;
240 
241     FX_FLOAT					fDescent;
242 
243     FX_FLOAT					fWidth;
244 
245     FX_INT32					nFontIndex;
246 
247     FX_FLOAT					fFontSize;
248 
249     CPVT_WordProps				WordProps;
250 };
251 struct CPVT_Line {
252 
CPVT_LineCPVT_Line253     CPVT_Line() : ptLine(0, 0), fLineWidth(0.0f), fLineAscent(0.0f), fLineDescent(0.0f)
254     {
255     }
256 
257     CPVT_WordPlace				lineplace;
258 
259     CPVT_WordPlace				lineEnd;
260 
261     CPDF_Point					ptLine;
262 
263     FX_FLOAT					fLineWidth;
264 
265     FX_FLOAT					fLineAscent;
266 
267     FX_FLOAT					fLineDescent;
268 };
269 struct CPVT_Section {
270 
271     CPVT_WordPlace				secplace;
272 
273     CPDF_Rect					rcSection;
274 
275     CPVT_SecProps				SecProps;
276 
277     CPVT_WordProps				WordProps;
278 };
279 class IPDF_VariableText_Provider
280 {
281 public:
~IPDF_VariableText_Provider()282     virtual ~IPDF_VariableText_Provider() { }
283 
284     virtual FX_INT32						GetCharWidth(FX_INT32 nFontIndex, FX_WORD word, FX_INT32 nWordStyle) = 0;
285 
286     virtual FX_INT32						GetTypeAscent(FX_INT32 nFontIndex) = 0;
287 
288     virtual FX_INT32						GetTypeDescent(FX_INT32 nFontIndex) = 0;
289 
290     virtual FX_INT32						GetWordFontIndex(FX_WORD word, FX_INT32 charset, FX_INT32 nFontIndex) = 0;
291 
292     virtual FX_BOOL							IsLatinWord(FX_WORD word) = 0;
293 
294     virtual FX_INT32						GetDefaultFontIndex() = 0;
295 };
296 class IPDF_VariableText_Iterator
297 {
298 public:
~IPDF_VariableText_Iterator()299     virtual ~IPDF_VariableText_Iterator() { }
300 
301     virtual FX_BOOL							NextWord() = 0;
302 
303     virtual FX_BOOL							PrevWord() = 0;
304 
305     virtual FX_BOOL							NextLine() = 0;
306 
307     virtual FX_BOOL							PrevLine() = 0;
308 
309     virtual FX_BOOL							NextSection() = 0;
310 
311     virtual FX_BOOL							PrevSection() = 0;
312 
313     virtual FX_BOOL							GetWord(CPVT_Word & word) const = 0;
314 
315     virtual FX_BOOL							SetWord(const CPVT_Word & word) = 0;
316 
317     virtual FX_BOOL							GetLine(CPVT_Line & line) const = 0;
318 
319     virtual FX_BOOL							GetSection(CPVT_Section & section) const = 0;
320 
321     virtual	FX_BOOL							SetSection(const CPVT_Section & section) = 0;
322 
323     virtual void							SetAt(FX_INT32 nWordIndex) = 0;
324 
325     virtual void							SetAt(const CPVT_WordPlace & place) = 0;
326 
327     virtual const CPVT_WordPlace &			GetAt() const = 0;
328 };
329 class IPDF_VariableText
330 {
331 public:
332     static IPDF_VariableText*			NewVariableText();
333 
334     static void							DelVariableText(IPDF_VariableText* pVT);
335 
336     virtual IPDF_VariableText_Provider*		SetProvider(IPDF_VariableText_Provider * pProvider) = 0;
337 
338     virtual IPDF_VariableText_Iterator*		GetIterator() = 0;
339 
340     virtual void							SetPlateRect(const CPDF_Rect & rect) = 0;
341 
342     virtual void							SetAlignment(FX_INT32 nFormat = 0) = 0;
343 
344     virtual void							SetPasswordChar(FX_WORD wSubWord = '*') = 0;
345 
346     virtual void							SetLimitChar(FX_INT32 nLimitChar = 0) = 0;
347 
348     virtual void							SetCharArray(FX_INT32 nCharArray = 0) = 0;
349 
350     virtual void							SetCharSpace(FX_FLOAT fCharSpace = 0.0f) = 0;
351 
352     virtual void							SetHorzScale(FX_INT32 nHorzScale = 100) = 0;
353 
354     virtual void							SetMultiLine(FX_BOOL bMultiLine = TRUE) = 0;
355 
356     virtual void							SetAutoReturn(FX_BOOL bAuto = TRUE) = 0;
357 
358     virtual void							SetAutoFontSize(FX_BOOL bAuto = TRUE) = 0;
359 
360     virtual void							SetFontSize(FX_FLOAT fFontSize) = 0;
361 
362     virtual void							SetLineLeading(FX_FLOAT fLineLeading) = 0;
363 
364     virtual void							SetRichText(FX_BOOL bRichText) = 0;
365 
366     virtual void							Initialize() = 0;
367 
368     virtual FX_BOOL							IsValid() const = 0;
369 
370     virtual FX_BOOL							IsRichText() const = 0;
371 
372     virtual void							RearrangeAll() = 0;
373 
374     virtual void							RearrangePart(const CPVT_WordRange & PlaceRange) = 0;
375 
376     virtual void							ResetAll() = 0;
377 
378     virtual void							SetText(FX_LPCWSTR text, FX_INT32 charset = 1, const CPVT_SecProps * pSecProps = NULL,
379             const CPVT_WordProps * pWordProps = NULL) = 0;
380 
381     virtual CPVT_WordPlace					InsertWord(const CPVT_WordPlace & place, FX_WORD word, FX_INT32 charset = 1,
382             const CPVT_WordProps * pWordProps = NULL) = 0;
383 
384     virtual CPVT_WordPlace					InsertSection(const CPVT_WordPlace & place, const CPVT_SecProps * pSecProps = NULL,
385             const CPVT_WordProps * pWordProps = NULL) = 0;
386 
387     virtual CPVT_WordPlace					InsertText(const CPVT_WordPlace & place, FX_LPCWSTR text, FX_INT32 charset = 1,
388             const CPVT_SecProps * pSecProps = NULL,	const CPVT_WordProps * pWordProps = NULL) = 0;
389 
390     virtual CPVT_WordPlace					DeleteWords(const CPVT_WordRange & PlaceRange) = 0;
391 
392     virtual CPVT_WordPlace					DeleteWord(const CPVT_WordPlace & place) = 0;
393 
394     virtual CPVT_WordPlace					BackSpaceWord(const CPVT_WordPlace & place) = 0;
395 
396     virtual const CPDF_Rect &				GetPlateRect() const = 0;
397 
398     virtual CPDF_Rect						GetContentRect() const = 0;
399 
400     virtual FX_INT32						GetTotalWords() const = 0;
401 
402     virtual FX_FLOAT						GetFontSize() const = 0;
403 
404     virtual FX_INT32						GetAlignment() const = 0;
405 
406     virtual FX_WORD							GetPasswordChar() const = 0;
407 
408     virtual FX_INT32						GetCharArray() const = 0;
409 
410     virtual FX_INT32						GetLimitChar() const = 0;
411 
412     virtual FX_BOOL							IsMultiLine() const = 0;
413 
414     virtual FX_INT32						GetHorzScale() const = 0;
415 
416     virtual FX_FLOAT						GetCharSpace() const = 0;
417 
418     virtual CPVT_WordPlace					GetBeginWordPlace() const = 0;
419 
420     virtual CPVT_WordPlace					GetEndWordPlace() const = 0;
421 
422     virtual CPVT_WordPlace					GetPrevWordPlace(const CPVT_WordPlace & place) const = 0;
423 
424     virtual CPVT_WordPlace					GetNextWordPlace(const CPVT_WordPlace & place) const = 0;
425 
426     virtual CPVT_WordPlace					SearchWordPlace(const CPDF_Point & point) const = 0;
427 
428     virtual CPVT_WordPlace					GetUpWordPlace(const CPVT_WordPlace & place, const CPDF_Point & point) const = 0;
429 
430     virtual CPVT_WordPlace					GetDownWordPlace(const CPVT_WordPlace & place, const CPDF_Point & point) const = 0;
431 
432     virtual CPVT_WordPlace					GetLineBeginPlace(const CPVT_WordPlace & place) const = 0;
433 
434     virtual CPVT_WordPlace					GetLineEndPlace(const CPVT_WordPlace & place) const = 0;
435 
436     virtual CPVT_WordPlace					GetSectionBeginPlace(const CPVT_WordPlace & place) const = 0;
437 
438     virtual CPVT_WordPlace					GetSectionEndPlace(const CPVT_WordPlace & place) const = 0;
439 
440     virtual void							UpdateWordPlace(CPVT_WordPlace & place) const = 0;
441 
442     virtual CPVT_WordPlace					AjustLineHeader(const CPVT_WordPlace & place, FX_BOOL bPrevOrNext) const = 0;
443 
444     virtual FX_INT32						WordPlaceToWordIndex(const CPVT_WordPlace & place) const = 0;
445 
446     virtual CPVT_WordPlace					WordIndexToWordPlace(FX_INT32 index) const = 0;
447 
448 protected:
~IPDF_VariableText()449     ~IPDF_VariableText() { }
450 };
451 
452 #endif  // CORE_INCLUDE_FPDFDOC_FPDF_VT_H_
453