summaryrefslogtreecommitdiff
path: root/core/fpdfdoc/cpdf_variabletext.h
blob: 2e6caf63d6099fcb75c548da1fdfb978619330d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2016 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef CORE_FPDFDOC_CPDF_VARIABLETEXT_H_
#define CORE_FPDFDOC_CPDF_VARIABLETEXT_H_

#include <memory>
#include <vector>

#include "core/fpdfdoc/cpvt_floatrect.h"
#include "core/fpdfdoc/cpvt_line.h"
#include "core/fpdfdoc/cpvt_lineinfo.h"
#include "core/fpdfdoc/cpvt_wordplace.h"
#include "core/fpdfdoc/cpvt_wordrange.h"
#include "core/fxcrt/fx_coordinates.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/fx_system.h"
#include "core/fxge/fx_font.h"

class CPVT_Word;
class CSection;
class IPVT_FontMap;
struct CPVT_SecProps;
struct CPVT_Section;
struct CPVT_SectionInfo;

struct CPVT_WordInfo;
struct CPVT_WordProps;

#define VARIABLETEXT_HALF 0.5f

class CPDF_VariableText {
 public:
  enum class ScriptType { Normal, Super, Sub };

  class Iterator {
   public:
    explicit Iterator(CPDF_VariableText* pVT);
    ~Iterator();

    bool NextWord();
    bool PrevWord();
    bool NextLine();
    bool PrevLine();
    bool NextSection();
    bool PrevSection();
    bool SetWord(const CPVT_Word& word);
    bool GetWord(CPVT_Word& word) const;
    bool GetLine(CPVT_Line& line) const;
    bool GetSection(CPVT_Section& section) const;
    bool SetSection(const CPVT_Section& section);
    void SetAt(int32_t nWordIndex);
    void SetAt(const CPVT_WordPlace& place);
    const CPVT_WordPlace& GetAt() const { return m_CurPos; }

   private:
    CPVT_WordPlace m_CurPos;
    CPDF_VariableText* const m_pVT;
  };

  class Provider {
   public:
    explicit Provider(IPVT_FontMap* pFontMap);
    virtual ~Provider();

    virtual int32_t GetCharWidth(int32_t nFontIndex, uint16_t word);
    virtual int32_t GetTypeAscent(int32_t nFontIndex);
    virtual int32_t GetTypeDescent(int32_t nFontIndex);
    virtual int32_t GetWordFontIndex(uint16_t word,
                                     int32_t charset,
                                     int32_t nFontIndex);
    virtual bool IsLatinWord(uint16_t word);
    virtual int32_t GetDefaultFontIndex();

   private:
    IPVT_FontMap* const m_pFontMap;
  };

  CPDF_VariableText();
  ~CPDF_VariableText();

  void SetProvider(CPDF_VariableText::Provider* pProvider);
  CPDF_VariableText::Iterator* GetIterator();

  void SetContentRect(const CPVT_FloatRect& rect);
  CFX_FloatRect GetContentRect() const;
  void SetPlateRect(const CFX_FloatRect& rect);
  const CFX_FloatRect& GetPlateRect() const;

  void SetAlignment(int32_t nFormat) { m_nAlignment = nFormat; }
  void SetPasswordChar(uint16_t wSubWord) { m_wSubWord = wSubWord; }
  void SetLimitChar(int32_t nLimitChar) { m_nLimitChar = nLimitChar; }
  void SetCharSpace(float fCharSpace) { m_fCharSpace = fCharSpace; }
  void SetMultiLine(bool bMultiLine) { m_bMultiLine = bMultiLine; }
  void SetAutoReturn(bool bAuto) { m_bLimitWidth = bAuto; }
  void SetFontSize(float fFontSize) { m_fFontSize = fFontSize; }
  void SetCharArray(int32_t nCharArray) { m_nCharArray = nCharArray; }
  void SetAutoFontSize(bool bAuto) { m_bAutoFontSize = bAuto; }
  void Initialize();

  bool IsValid() const { return m_bInitialized; }

  void RearrangeAll();
  void RearrangePart(const CPVT_WordRange& PlaceRange);
  void ResetAll();
  void SetText(const CFX_WideString& text);
  CPVT_WordPlace InsertWord(const CPVT_WordPlace& place,
                            uint16_t word,
                            int32_t charset,
                            const CPVT_WordProps* pWordProps);
  CPVT_WordPlace InsertSection(const CPVT_WordPlace& place,
                               const CPVT_SecProps* pSecProps,
                               const CPVT_WordProps* pWordProps);
  CPVT_WordPlace InsertText(const CPVT_WordPlace& place, const wchar_t* text);
  CPVT_WordPlace DeleteWords(const CPVT_WordRange& PlaceRange);
  CPVT_WordPlace DeleteWord(const CPVT_WordPlace& place);
  CPVT_WordPlace BackSpaceWord(const CPVT_WordPlace& place);

  int32_t GetTotalWords() const;
  float GetFontSize() const { return m_fFontSize; }
  int32_t GetAlignment() const { return m_nAlignment; }
  uint16_t GetPasswordChar() const { return GetSubWord(); }
  int32_t GetCharArray() const { return m_nCharArray; }
  int32_t GetLimitChar() const { return m_nLimitChar; }
  bool IsMultiLine() const { return m_bMultiLine; }
  int32_t GetHorzScale() const { return m_nHorzScale; }
  float GetCharSpace() const { return m_fCharSpace; }
  CPVT_WordPlace GetBeginWordPlace() const;
  CPVT_WordPlace GetEndWordPlace() const;
  CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace& place) const;
  CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace& place) const;
  CPVT_WordPlace SearchWordPlace(const CFX_PointF& point) const;
  CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace& place,
                                const CFX_PointF& point) const;
  CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace& place,
                                  const CFX_PointF& point) const;
  CPVT_WordPlace GetLineBeginPlace(const CPVT_WordPlace& place) const;
  CPVT_WordPlace GetLineEndPlace(const CPVT_WordPlace& place) const;
  CPVT_WordPlace GetSectionBeginPlace(const CPVT_WordPlace& place) const;
  CPVT_WordPlace GetSectionEndPlace(const CPVT_WordPlace& place) const;
  void UpdateWordPlace(CPVT_WordPlace& place) const;
  CPVT_WordPlace AdjustLineHeader(const CPVT_WordPlace& place,
                                  bool bPrevOrNext) const;
  int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const;
  CPVT_WordPlace WordIndexToWordPlace(int32_t index) const;

  uint16_t GetSubWord() const { return m_wSubWord; }

  float GetPlateWidth() const { return m_rcPlate.right - m_rcPlate.left; }
  float GetPlateHeight() const { return m_rcPlate.top - m_rcPlate.bottom; }
  CFX_SizeF GetPlateSize() const;
  CFX_PointF GetBTPoint() const;
  CFX_PointF GetETPoint() const;

  CFX_PointF InToOut(const CFX_PointF& point) const;
  CFX_PointF OutToIn(const CFX_PointF& point) const;
  CFX_FloatRect InToOut(const CPVT_FloatRect& rect) const;
  CPVT_FloatRect OutToIn(const CFX_FloatRect& rect) const;

 private:
  friend class CTypeset;
  friend class CSection;

  int32_t GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord);
  int32_t GetTypeAscent(int32_t nFontIndex);
  int32_t GetTypeDescent(int32_t nFontIndex);
  int32_t GetWordFontIndex(uint16_t word, int32_t charset, int32_t nFontIndex);
  int32_t GetDefaultFontIndex();
  bool IsLatinWord(uint16_t word);

  CPVT_WordPlace AddSection(const CPVT_WordPlace& place,
                            const CPVT_SectionInfo& secinfo);
  CPVT_WordPlace AddLine(const CPVT_WordPlace& place,
                         const CPVT_LineInfo& lineinfo);
  CPVT_WordPlace AddWord(const CPVT_WordPlace& place,
                         const CPVT_WordInfo& wordinfo);
  bool GetWordInfo(const CPVT_WordPlace& place, CPVT_WordInfo& wordinfo);
  bool SetWordInfo(const CPVT_WordPlace& place, const CPVT_WordInfo& wordinfo);
  bool GetLineInfo(const CPVT_WordPlace& place, CPVT_LineInfo& lineinfo);
  bool GetSectionInfo(const CPVT_WordPlace& place, CPVT_SectionInfo& secinfo);
  float GetWordFontSize(const CPVT_WordInfo& WordInfo);
  float GetWordWidth(int32_t nFontIndex,
                     uint16_t Word,
                     uint16_t SubWord,
                     float fCharSpace,
                     int32_t nHorzScale,
                     float fFontSize,
                     float fWordTail);
  float GetWordWidth(const CPVT_WordInfo& WordInfo);
  float GetWordAscent(const CPVT_WordInfo& WordInfo, float fFontSize);
  float GetWordDescent(const CPVT_WordInfo& WordInfo, float fFontSize);
  float GetWordAscent(const CPVT_WordInfo& WordInfo);
  float GetWordDescent(const CPVT_WordInfo& WordInfo);
  float GetLineAscent(const CPVT_SectionInfo& SecInfo);
  float GetLineDescent(const CPVT_SectionInfo& SecInfo);
  float GetFontAscent(int32_t nFontIndex, float fFontSize);
  float GetFontDescent(int32_t nFontIndex, float fFontSize);
  int32_t GetWordFontIndex(const CPVT_WordInfo& WordInfo);
  float GetCharSpace(const CPVT_WordInfo& WordInfo);
  int32_t GetHorzScale(const CPVT_WordInfo& WordInfo);
  float GetLineLeading(const CPVT_SectionInfo& SecInfo);
  float GetLineIndent(const CPVT_SectionInfo& SecInfo);
  int32_t GetAlignment(const CPVT_SectionInfo& SecInfo);

  void ClearSectionRightWords(const CPVT_WordPlace& place);

  bool ClearEmptySection(const CPVT_WordPlace& place);
  void ClearEmptySections(const CPVT_WordRange& PlaceRange);
  void LinkLatterSection(const CPVT_WordPlace& place);
  void ClearWords(const CPVT_WordRange& PlaceRange);
  CPVT_WordPlace ClearLeftWord(const CPVT_WordPlace& place);
  CPVT_WordPlace ClearRightWord(const CPVT_WordPlace& place);

  CPVT_FloatRect Rearrange(const CPVT_WordRange& PlaceRange);
  float GetAutoFontSize();
  bool IsBigger(float fFontSize) const;
  CPVT_FloatRect RearrangeSections(const CPVT_WordRange& PlaceRange);

  std::vector<std::unique_ptr<CSection>> m_SectionArray;
  int32_t m_nLimitChar;
  int32_t m_nCharArray;
  bool m_bMultiLine;
  bool m_bLimitWidth;
  bool m_bAutoFontSize;
  int32_t m_nAlignment;
  float m_fLineLeading;
  float m_fCharSpace;
  int32_t m_nHorzScale;
  uint16_t m_wSubWord;
  float m_fFontSize;
  bool m_bInitialized;
  CPDF_VariableText::Provider* m_pVTProvider;
  std::unique_ptr<CPDF_VariableText::Iterator> m_pVTIterator;
  CFX_FloatRect m_rcPlate;
  CPVT_FloatRect m_rcContent;
};

#endif  // CORE_FPDFDOC_CPDF_VARIABLETEXT_H_