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