web-doc 0.12.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -169,16 +169,30 @@ function replaceInPlace(context, target, text) {
|
|
|
169
169
|
context.withPage(target.location.pageIndex, (page) => {
|
|
170
170
|
setText(pdfium, lib.FPDFPage_GetObject(page, index), text);
|
|
171
171
|
});
|
|
172
|
-
const
|
|
172
|
+
const faithful = context.withPage(target.location.pageIndex, (page) => {
|
|
173
173
|
const textPage = lib.FPDFText_LoadPage(page);
|
|
174
174
|
try {
|
|
175
|
-
|
|
175
|
+
const object = lib.FPDFPage_GetObject(page, index);
|
|
176
|
+
const readBack = pdfium.readWideString((buffer, bytes) => lib.FPDFTextObj_GetText(object, textPage, buffer, bytes));
|
|
177
|
+
if (readBack === text)
|
|
178
|
+
return true;
|
|
179
|
+
// Reading-order extraction can append a generated gap before the next
|
|
180
|
+
// object. Verify the object's own characters instead, retaining every
|
|
181
|
+
// authored space: trimming would also hide a lost deliberate space.
|
|
182
|
+
let authored = "";
|
|
183
|
+
const count = lib.FPDFText_CountChars(textPage);
|
|
184
|
+
for (let at = 0; at < count; at += 1) {
|
|
185
|
+
if (lib.FPDFText_GetTextObject(textPage, at) === object &&
|
|
186
|
+
lib.FPDFText_IsGenerated(textPage, at) === 0)
|
|
187
|
+
authored += String.fromCodePoint(lib.FPDFText_GetUnicode(textPage, at));
|
|
188
|
+
}
|
|
189
|
+
return authored === text;
|
|
176
190
|
}
|
|
177
191
|
finally {
|
|
178
192
|
lib.FPDFText_ClosePage(textPage);
|
|
179
193
|
}
|
|
180
194
|
});
|
|
181
|
-
if (
|
|
195
|
+
if (faithful)
|
|
182
196
|
return true;
|
|
183
197
|
// Put the old text back so the fallback path starts from a known state.
|
|
184
198
|
context.withPage(target.location.pageIndex, (page) => {
|
|
@@ -75,6 +75,7 @@ export interface PdfiumFunctions {
|
|
|
75
75
|
FPDFText_GetCharBox(textPage: number, index: number, left: number, right: number, bottom: number, top: number): boolean;
|
|
76
76
|
FPDFText_GetTextObject(textPage: number, index: number): number;
|
|
77
77
|
FPDFText_GetUnicode(textPage: number, index: number): number;
|
|
78
|
+
FPDFText_IsGenerated(textPage: number, index: number): number;
|
|
78
79
|
FPDFText_GetCharIndexFromTextIndex(textPage: number, textIndex: number): number;
|
|
79
80
|
/** Advance box of a character as an FS_RECTF {left, top, right, bottom} of floats. */
|
|
80
81
|
FPDFText_GetLooseCharBox(textPage: number, index: number, rect: number): boolean;
|
|
@@ -9997,22 +9997,28 @@ function replaceInPlace(context, target, text) {
|
|
|
9997
9997
|
context.withPage(target.location.pageIndex, (page) => {
|
|
9998
9998
|
setText(pdfium, lib.FPDFPage_GetObject(page, index), text);
|
|
9999
9999
|
});
|
|
10000
|
-
const
|
|
10000
|
+
const faithful = context.withPage(target.location.pageIndex, (page) => {
|
|
10001
10001
|
const textPage = lib.FPDFText_LoadPage(page);
|
|
10002
10002
|
try {
|
|
10003
|
-
|
|
10004
|
-
|
|
10005
|
-
|
|
10006
|
-
textPage,
|
|
10007
|
-
buffer,
|
|
10008
|
-
bytes2
|
|
10009
|
-
)
|
|
10003
|
+
const object = lib.FPDFPage_GetObject(page, index);
|
|
10004
|
+
const readBack = pdfium.readWideString(
|
|
10005
|
+
(buffer, bytes2) => lib.FPDFTextObj_GetText(object, textPage, buffer, bytes2)
|
|
10010
10006
|
);
|
|
10007
|
+
if (readBack === text) return true;
|
|
10008
|
+
let authored = "";
|
|
10009
|
+
const count = lib.FPDFText_CountChars(textPage);
|
|
10010
|
+
for (let at = 0; at < count; at += 1) {
|
|
10011
|
+
if (lib.FPDFText_GetTextObject(textPage, at) === object && lib.FPDFText_IsGenerated(textPage, at) === 0)
|
|
10012
|
+
authored += String.fromCodePoint(
|
|
10013
|
+
lib.FPDFText_GetUnicode(textPage, at)
|
|
10014
|
+
);
|
|
10015
|
+
}
|
|
10016
|
+
return authored === text;
|
|
10011
10017
|
} finally {
|
|
10012
10018
|
lib.FPDFText_ClosePage(textPage);
|
|
10013
10019
|
}
|
|
10014
10020
|
});
|
|
10015
|
-
if (
|
|
10021
|
+
if (faithful) return true;
|
|
10016
10022
|
context.withPage(target.location.pageIndex, (page) => {
|
|
10017
10023
|
setText(pdfium, lib.FPDFPage_GetObject(page, index), previous);
|
|
10018
10024
|
});
|