John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #include "../../include/fxedit/fxet_stub.h" |
| 8 | #include "../../include/fxedit/fx_edit.h" |
| 9 | #include "../../include/fxedit/fxet_edit.h" |
| 10 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 11 | CFX_ByteString GetPDFWordString(IFX_Edit_FontMap* pFontMap, |
| 12 | int32_t nFontIndex, |
| 13 | FX_WORD Word, |
| 14 | FX_WORD SubWord) { |
| 15 | ASSERT(pFontMap != NULL); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | CFX_ByteString sWord; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 18 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 19 | if (CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex)) { |
| 20 | if (SubWord > 0) { |
| 21 | Word = SubWord; |
| 22 | } else { |
| 23 | FX_DWORD dwCharCode = -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 25 | if (pPDFFont->IsUnicodeCompatible()) |
| 26 | dwCharCode = pPDFFont->CharCodeFromUnicode(Word); |
| 27 | else |
| 28 | dwCharCode = pFontMap->CharCodeFromUnicode(nFontIndex, Word); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 29 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 30 | if (dwCharCode > 0) { |
| 31 | pPDFFont->AppendChar(sWord, dwCharCode); |
| 32 | return sWord; |
| 33 | } |
| 34 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 35 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | pPDFFont->AppendChar(sWord, Word); |
| 37 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 38 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | return sWord; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | static CFX_ByteString GetWordRenderString(const CFX_ByteString& strWords) { |
| 43 | if (strWords.GetLength() > 0) |
| 44 | return PDF_EncodeString(strWords) + " Tj\n"; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 45 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 49 | static CFX_ByteString GetFontSetString(IFX_Edit_FontMap* pFontMap, |
| 50 | int32_t nFontIndex, |
| 51 | FX_FLOAT fFontSize) { |
| 52 | CFX_ByteTextBuf sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 53 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | if (pFontMap) { |
| 55 | CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | if (sFontAlias.GetLength() > 0 && fFontSize > 0) |
| 58 | sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n"; |
| 59 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 61 | return sRet.GetByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | CFX_ByteString IFX_Edit::GetEditAppearanceStream( |
| 65 | IFX_Edit* pEdit, |
| 66 | const CPDF_Point& ptOffset, |
| 67 | const CPVT_WordRange* pRange /* = NULL*/, |
| 68 | FX_BOOL bContinuous /* = TRUE*/, |
| 69 | FX_WORD SubWord /* = 0*/) { |
| 70 | CFX_ByteTextBuf sEditStream, sWords; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | CPDF_Point ptOld(0.0f, 0.0f), ptNew(0.0f, 0.0f); |
| 73 | int32_t nCurFontIndex = -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | if (IFX_Edit_Iterator* pIterator = pEdit->GetIterator()) { |
| 76 | if (pRange) |
| 77 | pIterator->SetAt(pRange->BeginPos); |
| 78 | else |
| 79 | pIterator->SetAt(0); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | CPVT_WordPlace oldplace; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 82 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | while (pIterator->NextWord()) { |
| 84 | CPVT_WordPlace place = pIterator->GetAt(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 85 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | if (pRange && place.WordCmp(pRange->EndPos) > 0) |
| 87 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | if (bContinuous) { |
| 90 | if (place.LineCmp(oldplace) != 0) { |
| 91 | if (sWords.GetSize() > 0) { |
| 92 | sEditStream << GetWordRenderString(sWords.GetByteString()); |
| 93 | sWords.Clear(); |
| 94 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 95 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | CPVT_Word word; |
| 97 | if (pIterator->GetWord(word)) { |
| 98 | ptNew = CPDF_Point(word.ptWord.x + ptOffset.x, |
| 99 | word.ptWord.y + ptOffset.y); |
| 100 | } else { |
| 101 | CPVT_Line line; |
| 102 | pIterator->GetLine(line); |
| 103 | ptNew = CPDF_Point(line.ptLine.x + ptOffset.x, |
| 104 | line.ptLine.y + ptOffset.y); |
| 105 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) { |
| 108 | sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y |
| 109 | << " Td\n"; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 110 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | ptOld = ptNew; |
| 112 | } |
| 113 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 114 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | CPVT_Word word; |
| 116 | if (pIterator->GetWord(word)) { |
| 117 | if (word.nFontIndex != nCurFontIndex) { |
| 118 | if (sWords.GetSize() > 0) { |
| 119 | sEditStream << GetWordRenderString(sWords.GetByteString()); |
| 120 | sWords.Clear(); |
| 121 | } |
| 122 | sEditStream << GetFontSetString(pEdit->GetFontMap(), |
| 123 | word.nFontIndex, word.fFontSize); |
| 124 | nCurFontIndex = word.nFontIndex; |
| 125 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 126 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 127 | sWords << GetPDFWordString(pEdit->GetFontMap(), nCurFontIndex, |
| 128 | word.Word, SubWord); |
| 129 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 130 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | oldplace = place; |
| 132 | } else { |
| 133 | CPVT_Word word; |
| 134 | if (pIterator->GetWord(word)) { |
| 135 | ptNew = CPDF_Point(word.ptWord.x + ptOffset.x, |
| 136 | word.ptWord.y + ptOffset.y); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 137 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) { |
| 139 | sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y |
| 140 | << " Td\n"; |
| 141 | ptOld = ptNew; |
| 142 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 143 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 144 | if (word.nFontIndex != nCurFontIndex) { |
| 145 | sEditStream << GetFontSetString(pEdit->GetFontMap(), |
| 146 | word.nFontIndex, word.fFontSize); |
| 147 | nCurFontIndex = word.nFontIndex; |
| 148 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 149 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | sEditStream << GetWordRenderString(GetPDFWordString( |
| 151 | pEdit->GetFontMap(), nCurFontIndex, word.Word, SubWord)); |
| 152 | } |
| 153 | } |
| 154 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 155 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | if (sWords.GetSize() > 0) { |
| 157 | sEditStream << GetWordRenderString(sWords.GetByteString()); |
| 158 | sWords.Clear(); |
| 159 | } |
| 160 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 161 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | CFX_ByteTextBuf sAppStream; |
| 163 | if (sEditStream.GetSize() > 0) { |
| 164 | int32_t nHorzScale = pEdit->GetHorzScale(); |
| 165 | if (nHorzScale != 100) { |
| 166 | sAppStream << nHorzScale << " Tz\n"; |
| 167 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 168 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | FX_FLOAT fCharSpace = pEdit->GetCharSpace(); |
| 170 | if (!FX_EDIT_IsFloatZero(fCharSpace)) { |
| 171 | sAppStream << fCharSpace << " Tc\n"; |
| 172 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 173 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 174 | sAppStream << sEditStream; |
| 175 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 176 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | return sAppStream.GetByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 180 | CFX_ByteString IFX_Edit::GetSelectAppearanceStream( |
| 181 | IFX_Edit* pEdit, |
| 182 | const CPDF_Point& ptOffset, |
| 183 | const CPVT_WordRange* pRange /*= NULL*/) { |
| 184 | CFX_ByteTextBuf sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 185 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | if (pRange && pRange->IsExist()) { |
| 187 | if (IFX_Edit_Iterator* pIterator = pEdit->GetIterator()) { |
| 188 | pIterator->SetAt(pRange->BeginPos); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 189 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | while (pIterator->NextWord()) { |
| 191 | CPVT_WordPlace place = pIterator->GetAt(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 192 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | if (pRange && place.WordCmp(pRange->EndPos) > 0) |
| 194 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 195 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | CPVT_Word word; |
| 197 | CPVT_Line line; |
| 198 | if (pIterator->GetWord(word) && pIterator->GetLine(line)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | sRet << word.ptWord.x + ptOffset.x << " " |
| 200 | << line.ptLine.y + line.fLineDescent << " " << word.fWidth << " " |
| 201 | << line.fLineAscent - line.fLineDescent << " re\nf\n"; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 206 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 207 | return sRet.GetByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 208 | } |