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