#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
Keep line-end hyphens when the text editor redraws a paragraph (#8881)
Anthony Stirling190e2c0409b8
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/lineEndHyphen.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/lineEndHyphen.test.tsnew file mode 100644index 0000000000..dd7e2609e4--- /dev/null+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/lineEndHyphen.test.ts@@ -0,0 +1,19 @@+// @vitest-environment node+import { describe, expect, it } from "vitest";+import {+ drawnCodePoint,+ drawnText,+} from "@editor/tools/pdfTextEditor/pdfium/lineEndHyphen";++describe("PDFium line-end hyphen marker", () => {+ it("reads the marker as the hyphen that is drawn on the page", () => {+ expect(drawnText("posi\u0002\ntion")).toBe("posi-\ntion");+ expect(drawnCodePoint(0x0002)).toBe(0x2d);+ });++ it("leaves every other character alone", () => {+ expect(drawnText("well-known\tline\n")).toBe("well-known\tline\n");+ expect(drawnCodePoint(0x41)).toBe(0x41);+ expect(drawnCodePoint(0x2d)).toBe(0x2d);+ });+});diff --git a/frontend/src/editor/core/tools/pdfTextEditor/charcode/ContentStreamResolver.ts b/frontend/src/editor/core/tools/pdfTextEditor/charcode/ContentStreamResolver.tsindex b1f4c59441..0bbc8e8b0d 100644--- a/frontend/src/editor/core/tools/pdfTextEditor/charcode/ContentStreamResolver.ts+++ b/frontend/src/editor/core/tools/pdfTextEditor/charcode/ContentStreamResolver.ts@@ -1,3 +1,4 @@+import { drawnCodePoint } from "@editor/tools/pdfTextEditor/pdfium/lineEndHyphen";import type {CharcodeResolver,CharcodeResolveResult,@@ -94,7 +95,7 @@ function buildPageMap(ctx: ResolverContext): Map<number, Map<number, number>> {// in a given font gets the next sequential CID starting at 1.const perFontNext = new Map<number, number>();for (let i = 0; i < count; i++) {- const unicode = tpMod.FPDFText_GetUnicode(textPage, i);+ const unicode = drawnCodePoint(tpMod.FPDFText_GetUnicode(textPage, i));if (!unicode) continue;const obj = tpMod.FPDFText_GetTextObject(textPage, i);if (!obj) continue;diff --git a/frontend/src/editor/core/tools/pdfTextEditor/charcode/FontHelpers.ts b/frontend/src/editor/core/tools/pdfTextEditor/charcode/FontHelpers.tsindex afe0655ded..2652fe31db 100644--- a/frontend/src/editor/core/tools/pdfTextEditor/charcode/FontHelpers.ts+++ b/frontend/src/editor/core/tools/pdfTextEditor/charcode/FontHelpers.ts@@ -1,3 +1,4 @@+import { drawnCodePoint } from "@editor/tools/pdfTextEditor/pdfium/lineEndHyphen";import type { ResolverContext } from "@editor/tools/pdfTextEditor/charcode/CharcodeStrategy";import {SCRATCH,@@ -164,7 +165,7 @@ export function findFontForChar(// right weight, slightly wrong shapes and advances.let fallback: number | null = null;for (let i = 0; i < count; i++) {- const u = tpMod.FPDFText_GetUnicode(textPage, i);+ const u = drawnCodePoint(tpMod.FPDFText_GetUnicode(textPage, i));if (u !== cp) continue;const obj = tpMod.FPDFText_GetTextObject(textPage, i);if (!obj) continue;diff --git a/frontend/src/editor/core/tools/pdfTextEditor/commands/ReflowWrapCommand.ts b/frontend/src/editor/core/tools/pdfTextEditor/commands/ReflowWrapCommand.tsindex bb34ed1fb2..c6865198eb 100644--- a/frontend/src/editor/core/tools/pdfTextEditor/commands/ReflowWrapCommand.ts+++ b/frontend/src/editor/core/tools/pdfTextEditor/commands/ReflowWrapCommand.ts@@ -1,3 +1,4 @@+import { drawnText } from "@editor/tools/pdfTextEditor/pdfium/lineEndHyphen";import i18n from "i18next";import type { Command } from "@editor/tools/pdfTextEditor/commands/Command";import type { EditorDocument } from "@editor/tools/pdfTextEditor/model/EditorDocument";@@ -554,7 +555,7 @@ function readObjText(if (len <= 2) return "";const buf = scratchPtr(m, SCRATCH.reflowText, len);m.FPDFTextObj_GetText(ptr, textPage, buf, len);- return readUtf16(m, buf, len);+ return drawnText(readUtf16(m, buf, len));} catch {return "";}diff --git a/frontend/src/editor/core/tools/pdfTextEditor/commands/editTextHelpers.ts b/frontend/src/editor/core/tools/pdfTextEditor/commands/editTextHelpers.tsindex aaf3cbf599..fa6c5c17ce 100644--- a/frontend/src/editor/core/tools/pdfTextEditor/commands/editTextHelpers.ts+++ b/frontend/src/editor/core/tools/pdfTextEditor/commands/editTextHelpers.ts@@ -1,3 +1,7 @@+import {+ drawnCodePoint,+ drawnText,+} from "@editor/tools/pdfTextEditor/pdfium/lineEndHyphen";import { readUtf16 } from "@editor/services/pdfiumService";import type {ParagraphLineSlot,@@ -439,7 +443,7 @@ function buildOnPageAdvMap(try {const count = mod.FPDFText_CountChars(tp);for (let i = 0; i < count; i++) {- const u = mod.FPDFText_GetUnicode(tp, i);+ const u = drawnCodePoint(mod.FPDFText_GetUnicode(tp, i));if (!u) continue;const obj = mod.FPDFText_GetTextObject(tp, i);if (!obj) continue;@@ -1132,7 +1136,7 @@ export function readObjTexts(}const buf = scratchPtr(m, SCRATCH.editRunText, len);mod.FPDFTextObj_GetText(objPtr, tp, buf, len);- out[i] = readUtf16(m, buf, len);+ out[i] = drawnText(readUtf16(m, buf, len));} catch {out[i] = null;}@@ -1167,7 +1171,7 @@ function readBackTextObj(if (len <= 2) return "";const buf = scratchPtr(m, SCRATCH.editObjText, len);mod.FPDFTextObj_GetText(objPtr, tp, buf, len);- return readUtf16(m, buf, len);+ return drawnText(readUtf16(m, buf, len));} catch {return null;} finally {diff --git a/frontend/src/editor/core/tools/pdfTextEditor/commands/textPieces.ts b/frontend/src/editor/core/tools/pdfTextEditor/commands/textPieces.tsindex 4d88790eef..b0ed025905 100644--- a/frontend/src/editor/core/tools/pdfTextEditor/commands/textPieces.ts+++ b/frontend/src/editor/core/tools/pdfTextEditor/commands/textPieces.ts@@ -4,6 +4,7 @@* object a marked-content sequence draws, so changing one object's marks* changes them all; a fresh object has marks of its own.*/+import { drawnCodePoint } from "@editor/tools/pdfTextEditor/pdfium/lineEndHyphen";import type { WrappedPdfiumModule } from "@embedpdf/pdfium";import { writeUtf16 } from "@editor/services/pdfiumService";import { removeAndDestroyObject } from "@editor/tools/pdfTextEditor/commands/editTextHelpers";@@ -100,7 +101,9 @@ export function charOrigins(const origins = found.get(objPtr);if (text === undefined || !origins) continue;if (m.FPDFText_IsGenerated(textPage, i) === 1) continue;- const char = String.fromCodePoint(m.FPDFText_GetUnicode(textPage, i));+ const char = String.fromCodePoint(+ drawnCodePoint(m.FPDFText_GetUnicode(textPage, i)),+ );const at = nextMatch(text, read.get(objPtr) ?? 0, char);if (at === null) {found.delete(objPtr);diff --git a/frontend/src/editor/core/tools/pdfTextEditor/pdfium/PdfiumTextReader.ts b/frontend/src/editor/core/tools/pdfTextEditor/pdfium/PdfiumTextReader.tsindex d6ef5ef83a..0ae9704c62 100644--- a/frontend/src/editor/core/tools/pdfTextEditor/pdfium/PdfiumTextReader.ts+++ b/frontend/src/editor/core/tools/pdfTextEditor/pdfium/PdfiumTextReader.ts@@ -1,3 +1,7 @@+import {+ drawnCodePoint,+ drawnText,+} from "@editor/tools/pdfTextEditor/pdfium/lineEndHyphen";import type { WrappedPdfiumModule } from "@embedpdf/pdfium";import { TextRun } from "@editor/tools/pdfTextEditor/model/TextRun";import { ImageObject } from "@editor/tools/pdfTextEditor/model/ImageObject";@@ -244,7 +248,7 @@ function collectCharGeometry(const yPtr = scratchPtr(m, SCRATCH.readerCharY, 8);const out: CharGeometry[] = [];for (let i = 0; i < charCount; i += 1) {- const cp = m.FPDFText_GetUnicode(textPagePtr, i);+ const cp = drawnCodePoint(m.FPDFText_GetUnicode(textPagePtr, i));const objPtr = m.FPDFText_GetTextObject(textPagePtr, i);const run = objPtr ? (ptrToRun.get(objPtr) ?? null) : null;const boxed = probe.FPDFText_GetLooseCharBox(textPagePtr, i, rectBuf);@@ -882,7 +886,7 @@ function readTextObjString(if (len <= 2) return "";const buf = scratchPtr(m, SCRATCH.readerReadTextA, len);m.FPDFTextObj_GetText(objPtr, textPagePtr, buf, len);- return readUtf16(m, buf, len);+ return drawnText(readUtf16(m, buf, len));}/** 6-letter "ABCDEF+" subset tag PDFium prefixes onto subset font names. */diff --git a/frontend/src/editor/core/tools/pdfTextEditor/pdfium/lineEndHyphen.ts b/frontend/src/editor/core/tools/pdfTextEditor/pdfium/lineEndHyphen.tsnew file mode 100644index 0000000000..596f79c488--- /dev/null+++ b/frontend/src/editor/core/tools/pdfTextEditor/pdfium/lineEndHyphen.ts@@ -0,0 +1,19 @@+/**+ * PDFium's text page reports a hyphen that ends a line as U+0002 instead of+ * U+002D, so a caller joining lines into prose can drop it. The editor keeps+ * the page as drawn, where that glyph is a hyphen: left as U+0002 no font can+ * encode it, and every re-emit of the line silently loses the hyphen.+ *+ * Every read of text or code points from a PDFium text page goes through here,+ * so the run text and the per-glyph code points it is matched against agree.+ */+const LINE_END_HYPHEN_MARKER = 0x0002;+const HYPHEN = 0x002d;++export function drawnCodePoint(codePoint: number): number {+ return codePoint === LINE_END_HYPHEN_MARKER ? HYPHEN : codePoint;+}++export function drawnText(text: string): string {+ return text.replaceAll("\u0002", "-");+}