You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2026/09/26 18:46:13 UTC
svn commit: r1938542 - in pdfbox/branches/3.0: pdfbox-layout-awt/src/main/java/org/apache/pdfbox/glyphlayout/awt pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt pdfbox-layout-awt/src/test/resources/pdf pdfbox-layout-fop/src/main/java/...
Author: tilman
Date: Sat Sep 26 18:46:13 2026
New Revision: 1938542
Log:
PDFBOX-6256: introduce ActualText feature to layout processor, by Volker Kunert
Added:
pdfbox/branches/3.0/pdfbox-layout-awt/src/test/resources/pdf/GlyphLayoutDIN91379_ActualText.pdf (contents, props changed)
pdfbox/branches/3.0/pdfbox-layout-awt/src/test/resources/pdf/GlyphLayoutLigaturesAndKerning_ActualText.pdf (contents, props changed)
pdfbox/branches/3.0/pdfbox-layout-fop/src/test/resources/pdf/GlyphLayoutDIN91379_ActualText.pdf (contents, props changed)
Modified:
pdfbox/branches/3.0/pdfbox-layout-awt/src/main/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutProcessorAwt.java
pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutDin91379Test.java
pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutLigaturesAndKerningTest.java
pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/TestBase.java
pdfbox/branches/3.0/pdfbox-layout-fop/src/main/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutProcessorFop.java
pdfbox/branches/3.0/pdfbox-layout-fop/src/test/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutDin91379Test.java
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/AbstractGlyphLayoutProcessor.java
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ContentStreamForGlyphLayoutInterface.java
Modified: pdfbox/branches/3.0/pdfbox-layout-awt/src/main/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutProcessorAwt.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox-layout-awt/src/main/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutProcessorAwt.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox-layout-awt/src/main/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutProcessorAwt.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -49,7 +49,17 @@ import org.apache.pdfbox.pdmodel.font.PD
public class GlyphLayoutProcessorAwt extends AbstractGlyphLayoutProcessor implements GlyphLayoutProcessorInterface
{
- private final GlyphLayoutFontLoaderAwt glyphLayoutFontLoaderAwt;
+ private final GlyphLayoutFontLoaderAwt glyphLayoutFontLoaderAwt = new GlyphLayoutFontLoaderAwt();
+
+
+ /**
+ * Constructs a GlyphLayoutProcessorFop with options
+ *
+ */
+ public GlyphLayoutProcessorAwt(AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions options)
+ {
+ super(options);
+ }
/**
* Constructs a GlyphLayoutProcessorAwt
@@ -57,10 +67,12 @@ public class GlyphLayoutProcessorAwt ext
*/
public GlyphLayoutProcessorAwt()
{
- this.glyphLayoutFontLoaderAwt = new GlyphLayoutFontLoaderAwt();
+ super();
}
- /**
+
+
+ /**
* Checks if glyphs needed for text are missing in awtFont
*
* @param text text to be checked
Modified: pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutDin91379Test.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutDin91379Test.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutDin91379Test.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -26,8 +26,11 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
+
import org.apache.pdfbox.Loader;
+import org.apache.pdfbox.pdmodel.AbstractGlyphLayoutProcessor;
import org.junit.jupiter.api.Test;
import org.apache.pdfbox.pdmodel.PDDocument;
@@ -90,14 +93,52 @@ class GlyphLayoutDin91379Test extends Te
+ "⁹ ⁿ ₀ ₁ ₂ ₃ ₄ ₅ ₆ ₇ ₈ ₉ ™ ∞ ≤ ≥\n"
+ "Additional non-letters (not included in DIN 91379): – — •�";
+ /**
+ * Test, no ActualText
+ *
+ * @throws IOException
+ * @throws FontFormatException
+ * @throws URISyntaxException
+ */
+ @Test
+ void testGlyphLayoutDin91379NoActualText() throws IOException, FontFormatException, URISyntaxException
+ {
+ testGlyphLayoutDin91379(false, "");
+ }
+
+ /**
+ * Test with ActualText
+ *
+ * @throws IOException
+ * @throws FontFormatException
+ * @throws URISyntaxException
+ */
@Test
- void testGlyphLayoutDin91379() throws IOException, FontFormatException, URISyntaxException
+ void testGlyphLayoutDin91379UseActualText() throws IOException, FontFormatException, URISyntaxException
{
- GlyphLayoutProcessorAwt glyphLayoutProcessor = new GlyphLayoutProcessorAwt();
+ testGlyphLayoutDin91379(true, "_ActualText");
+ }
+
+ /**
+ * Test GlyphLayoutProcessorAwt with letters and sequences from DIN 91379
+ * @param useActualText
+ * @throws IOException
+ * @throws FontFormatException
+ * @throws URISyntaxException
+ */
+ void testGlyphLayoutDin91379(boolean useActualText, String sActualText) throws IOException, FontFormatException, URISyntaxException
+ {
+ AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions options = new AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions();
+ if (useActualText)
+ {
+ options.useActualText();
+ }
+ GlyphLayoutProcessorAwt glyphLayoutProcessor = new GlyphLayoutProcessorAwt(options);
+
+ String outputBaseName = String.format("GlyphLayoutDIN91379%s", sActualText);
+ String outputPDFFilename = "target/" + outputBaseName + ".pdf";
+ String outputTextFilename = "target/" + outputBaseName + ".txt";
- String outputName = "GlyphLayoutDIN91379.pdf";
- String outputPDFFilename = "target/" + outputName;
- String outputTextFilename = "target/GlyphLayoutDIN91379.txt";
float fontSize = 12.0f;
try (PDDocument doc = new PDDocument())
@@ -119,8 +160,9 @@ class GlyphLayoutDin91379Test extends Te
}
doc.save(outputPDFFilename);
}
- checkRenderIdent(outputName);
-
+ checkRenderIdent(outputBaseName + ".pdf");
+
+ // Extract text
try (PDDocument doc = Loader.loadPDF(new File(outputPDFFilename)))
{
assertEquals(1, doc.getNumberOfPages());
@@ -133,7 +175,7 @@ class GlyphLayoutDin91379Test extends Te
os.write (0xBB);
os.write (0xBF);
- try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, "utf-8")))
+ try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)))
{
//TODO compare this output with the input, like in TextStripper test
// Not yet correct as of 4.7.2026
@@ -158,8 +200,8 @@ class GlyphLayoutDin91379Test extends Te
if (!line.isEmpty())
{
showCompositesLine(cs, font, fontSize, x, y, line);
- y -= fontSize * 1.5;
+ y -= fontSize * 1.5f;
}
}
}
-}
+}
\ No newline at end of file
Modified: pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutLigaturesAndKerningTest.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutLigaturesAndKerningTest.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/GlyphLayoutLigaturesAndKerningTest.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -18,12 +18,24 @@
package org.apache.pdfbox.glyphlayout.awt;
import java.awt.FontFormatException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.pdfbox.Loader;
+import org.apache.pdfbox.pdmodel.AbstractGlyphLayoutProcessor;
+import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.jupiter.api.Test;
import org.apache.pdfbox.pdmodel.PDDocument;
@@ -85,13 +97,47 @@ class GlyphLayoutLigaturesAndKerningTest
}
}
+ /**
+ * Test, no ActualText
+ * @throws IOException
+ * @throws FontFormatException
+ * @throws URISyntaxException
+ */
@Test
- void testLigaturesAndKerning() throws IOException, FontFormatException, URISyntaxException
+ void testLigaturesAndKerningNoActualText() throws IOException, FontFormatException, URISyntaxException {
+ testLigaturesAndKerning(false, "");
+ }
+
+ /**
+ * Test with ActualText
+ * @throws IOException
+ * @throws FontFormatException
+ * @throws URISyntaxException
+ */
+ @Test
+ void testLigaturesAndKerningUseActualText() throws IOException, FontFormatException, URISyntaxException {
+ testLigaturesAndKerning(true, "_ActualText");
+ }
+
+ /**
+ * Test ligatures and kerning
+ * @param useActualText
+ * @throws IOException
+ * @throws FontFormatException
+ * @throws URISyntaxException
+ */
+ void testLigaturesAndKerning(boolean useActualText, String sActualText) throws IOException, FontFormatException, URISyntaxException
{
- GlyphLayoutProcessorAwt glyphLayoutProcessor = new GlyphLayoutProcessorAwt();
+ AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions options = new AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions();
+ if (useActualText) {
+ options.useActualText();
+ }
+ GlyphLayoutProcessorAwt glyphLayoutProcessor = new GlyphLayoutProcessorAwt(options);
+
+ String outputBaseName = String.format("GlyphLayoutLigaturesAndKerning%s", sActualText);
+ String outputPDFFilename = "target/" + outputBaseName + ".pdf";
+ String outputTextFilename = String.format("target/" + outputBaseName + ".txt");
- String outputName = "GlyphLayoutLigaturesAndKerning.pdf";
- String outputFilename = "target/" + outputName;
String firaPath = "/ttf/FiraCode-Regular.ttf";
String dejavuPath = "/ttf/DejaVuSans.ttf"; // ligatures not in Liberation nor in Arimo
String thaiPath = "/ttf/NotoSansThai-Regular.ttf";
@@ -169,9 +215,52 @@ class GlyphLayoutLigaturesAndKerningTest
cs.lineTo(x + f4, 676);
cs.stroke();
}
- doc.save(outputFilename);
+ doc.save(outputPDFFilename);
+ }
+
+ checkRenderIdent(outputBaseName + ".pdf");
+
+ // Extract text
+ try (PDDocument doc = Loader.loadPDF(new File(outputPDFFilename)))
+ {
+ assertEquals(1, doc.getNumberOfPages());
+
+ PDFTextStripper stripper = new PDFTextStripper();
+ String s = stripper.getText(doc);
+ String sStripped = s.replaceAll("\r", "").replaceAll(" +"," ")
+ .replaceAll(" *\\n", "\n")
+ .trim();
+
+ String text =
+ FIRACODE_STRING + "\n" +
+ FIRACODE_STRING + " (Ligatures)" + "\n" +
+ DEJAVU_STRING + "\n" +
+ DEJAVU_STRING + " (Ligatures)" + "\n" +
+ DEJAVU_STRING + " (Kerning)" + "\n" +
+ DEJAVU_STRING + " (Ligatures and kerning)" + "\n" +
+ THAI_STRING + "\n" +
+ BENGALI_STRING + " (ভারত)"+ "\n" +
+ BENGALI_STRING2 + " " + BENGALI_STRING2;
+ if (useActualText)
+ {
+ assertEquals(text, sStripped, "Extracted Text should equal the written text for " + outputPDFFilename);
+ }
+
+ try (OutputStream os = new FileOutputStream(outputTextFilename))
+ {
+ os.write (0xEF);
+ os.write (0xBB);
+ os.write (0xBF);
+
+ try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)))
+ {
+ //TODO compare this output with the input, like in TextStripper test
+ // Not yet correct as of 4.7.2026
+ writer.write(s);
+ }
+ }
}
- checkRenderIdent(outputName);
+
}
/**
Modified: pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/TestBase.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/TestBase.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox-layout-awt/src/test/java/org/apache/pdfbox/glyphlayout/awt/TestBase.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -22,6 +22,9 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Objects;
+
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
@@ -46,7 +49,10 @@ class TestBase
PDFRenderer r = new PDFRenderer(doc);
expectedImage = r.renderImage(0);
}
- try (PDDocument doc = Loader.loadPDF(new File(TestBase.class.getResource("/pdf/" + outputName).toURI())))
+ URL url = TestBase.class.getResource("/pdf/" + outputName);
+ Objects.requireNonNull(url, "Resource not found: " + outputName);
+
+ try (PDDocument doc = Loader.loadPDF(new File(url.toURI())))
{
PDFRenderer r = new PDFRenderer(doc);
actualImage = r.renderImage(0);
Added: pdfbox/branches/3.0/pdfbox-layout-awt/src/test/resources/pdf/GlyphLayoutDIN91379_ActualText.pdf
==============================================================================
Binary file. No diff available.
Added: pdfbox/branches/3.0/pdfbox-layout-awt/src/test/resources/pdf/GlyphLayoutLigaturesAndKerning_ActualText.pdf
==============================================================================
Binary file. No diff available.
Modified: pdfbox/branches/3.0/pdfbox-layout-fop/src/main/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutProcessorFop.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox-layout-fop/src/main/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutProcessorFop.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox-layout-fop/src/main/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutProcessorFop.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -45,7 +45,7 @@ import org.apache.pdfbox.pdmodel.font.PD
public class GlyphLayoutProcessorFop extends AbstractGlyphLayoutProcessor implements GlyphLayoutProcessorInterface
{
- private final GlyphLayoutFontLoaderFop glyphLayoutFontLoaderFop;
+ private final GlyphLayoutFontLoaderFop glyphLayoutFontLoaderFop = new GlyphLayoutFontLoaderFop();
/*
Before you call GlyphMapping.doGlyphMapping to position the glyphs,
@@ -55,12 +55,21 @@ public class GlyphLayoutProcessorFop ext
private static final float FOP_FONTSIZE_FACTOR = 1000f;
/**
+ * Constructs a GlyphLayoutProcessorFop with options
+ *
+ */
+ public GlyphLayoutProcessorFop(AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions options)
+ {
+ super(options);
+ }
+
+ /**
* Constructs a GlyphLayoutProcessorFop
*
*/
public GlyphLayoutProcessorFop()
{
- this.glyphLayoutFontLoaderFop = new GlyphLayoutFontLoaderFop();
+ super();
}
/**
Modified: pdfbox/branches/3.0/pdfbox-layout-fop/src/test/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutDin91379Test.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox-layout-fop/src/test/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutDin91379Test.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox-layout-fop/src/test/java/org/apache/pdfbox/glyphlayout/fop/GlyphLayoutDin91379Test.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -25,9 +25,11 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.apache.pdfbox.pdmodel.AbstractGlyphLayoutProcessor;
import org.junit.jupiter.api.Test;
import org.apache.pdfbox.Loader;
@@ -90,14 +92,50 @@ class GlyphLayoutDin91379Test extends Te
+ "⁹ ⁿ ₀ ₁ ₂ ₃ ₄ ₅ ₆ ₇ ₈ ₉ ™ ∞ ≤ ≥\n"
+ "Additional non-letters (not included in DIN 91379): – — •�";
+ /**
+ * Test, no ActualText
+ *
+ * @throws IOException
+ * @throws URISyntaxException
+ */
@Test
- void testGlyphLayoutDin91379() throws IOException, URISyntaxException
+ void testGlyphLayoutDin91379NoActualText() throws IOException, URISyntaxException
{
- GlyphLayoutProcessorFop glyphLayoutProcessor = new GlyphLayoutProcessorFop();
+ testGlyphLayoutDin91379(false, "");
+ }
+
+ /**
+ * Test with ActualText
+ *
+ * @throws IOException
+ * @throws URISyntaxException
+ */
+ @Test
+ void testGlyphLayoutDin91379UseActualText() throws IOException, URISyntaxException
+ {
+ testGlyphLayoutDin91379(true, "_ActualText");
+ }
- String outputName = "GlyphLayoutDIN91379.pdf";
+ /**
+ * Test GlyphLayoutProcessorFop with letters and sequences from DIN 91379
+ *
+ * @param useActualText
+ * @throws IOException
+ * @throws URISyntaxException
+ */
+ void testGlyphLayoutDin91379(boolean useActualText, String sActualText) throws IOException, URISyntaxException
+ {
+ AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions options = new AbstractGlyphLayoutProcessor.GlyphLayoutProcessorOptions();
+ if (useActualText)
+ {
+ options.useActualText();
+ }
+ GlyphLayoutProcessorFop glyphLayoutProcessor = new GlyphLayoutProcessorFop(options);
+
+ String outputName = String.format("GlyphLayoutDIN91379%s.pdf", sActualText);
String outputPDFFilename = "target/" + outputName;
- String outputTextFilename = "target/GlyphLayoutDIN91379.txt";
+ String outputTextFilename = String.format("target/GlyphLayoutDIN91379%s.txt", sActualText);
+
float fontSize = 12.0f;
try (PDDocument doc = new PDDocument())
@@ -132,7 +170,7 @@ class GlyphLayoutDin91379Test extends Te
os.write (0xBB);
os.write (0xBF);
- try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, "utf-8")))
+ try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)))
{
//TODO compare this output with the input, like in TextStripper test
// Not yet correct as of 4.7.2026
@@ -157,8 +195,8 @@ class GlyphLayoutDin91379Test extends Te
if (!line.isEmpty())
{
showCompositesLine(cs, font, fontSize, x, y, line);
- y -= fontSize * 1.5;
+ y -= fontSize * 1.5f;
}
}
}
-}
+}
\ No newline at end of file
Added: pdfbox/branches/3.0/pdfbox-layout-fop/src/test/resources/pdf/GlyphLayoutDIN91379_ActualText.pdf
==============================================================================
Binary file. No diff available.
Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -561,6 +561,7 @@ public final class COSName extends COSBa
public static final COSName SORT = new COSName("Sort");
public static final COSName SOUND = new COSName("Sound");
public static final COSName SPLIT = new COSName("Split");
+ public static final COSName SPAN = new COSName("Span");
public static final COSName SS = new COSName("SS");
public static final COSName ST = new COSName("St");
public static final COSName STANDARD_ENCODING = new COSName("StandardEncoding");
Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/AbstractGlyphLayoutProcessor.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/AbstractGlyphLayoutProcessor.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/AbstractGlyphLayoutProcessor.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -17,6 +17,10 @@
package org.apache.pdfbox.pdmodel;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSString;
+import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import java.io.IOException;
@@ -34,6 +38,41 @@ import java.util.Objects;
public abstract class AbstractGlyphLayoutProcessor implements GlyphLayoutProcessorInterface
{
/**
+ *
+ */
+ private final boolean useActualText;
+
+ /**
+ * Options for GlyphLayoutProcessor
+ */
+ public static class GlyphLayoutProcessorOptions
+ {
+
+ private boolean useActualText;
+
+ /**
+ * Turn usage of ActualText on
+ *
+ * @return this
+ */
+ public GlyphLayoutProcessorOptions useActualText()
+ {
+ useActualText = true;
+ return this;
+ }
+
+ /**
+ * Returns the state of useActualText
+ *
+ * @return true, if ActualText is used, falls otherwise
+ */
+ public boolean getUseActualText()
+ {
+ return useActualText;
+ }
+ }
+
+ /**
* Class for text and Bidi-Level
*/
protected static class TextAndBidiLevel
@@ -59,7 +98,26 @@ public abstract class AbstractGlyphLayou
}
/**
+ * Creates an AbstractGlyphLayoutProcessor with the given options
+ *
+ * @param options Options for creation
+ */
+ public AbstractGlyphLayoutProcessor(GlyphLayoutProcessorOptions options)
+ {
+ useActualText = options.useActualText;
+ }
+
+ /**
+ * Creates an AbstractGlyphLayoutProcessor with standard options
+ */
+ public AbstractGlyphLayoutProcessor()
+ {
+ useActualText = false;
+ }
+
+ /**
* Compute the string width for a unidirectional string
+ *
* @param font to be used
* @param fontSize font size
* @param text text
@@ -89,6 +147,31 @@ public abstract class AbstractGlyphLayou
}
/**
+ * Begin a marked content sequence for ActualText
+ *
+ * @param contentStream the content stream
+ * @param text the text to be written as ActualText
+ * @throws IOException If the content stream could not be written
+ */
+ protected void beginMarkedContentForActualText(ContentStreamForGlyphLayoutInterface contentStream, String text) throws IOException
+ {
+ COSDictionary dict = new COSDictionary();
+ dict.setItem(COSName.ACTUAL_TEXT, new COSString(text));
+ PDPropertyList propertyList = PDPropertyList.create(dict);
+ contentStream.beginMarkedContent(COSName.SPAN, propertyList);
+ }
+
+ /**
+ * End a marked content sequence.
+ *
+ * @throws IOException If the content stream could not be written
+ */
+ protected void endMarkedContent(ContentStreamForGlyphLayoutInterface contentStream) throws IOException
+ {
+ contentStream.endMarkedContent();
+ }
+
+ /**
* Shows unidirectional text using glyph positioning (if needed)
*
* @param contentStream the content stream
@@ -117,11 +200,19 @@ public abstract class AbstractGlyphLayou
public void showText(ContentStreamForGlyphLayoutInterface contentStream, PDType0Font font, float fontSize, String text)
throws IOException
{
+ if (useActualText)
+ {
+ beginMarkedContentForActualText(contentStream, text);
+ }
List<TextAndBidiLevel> textAndBidiLevels = doBidiSplittingAndReordering(text);
- for (TextAndBidiLevel textAndBidiLevel: textAndBidiLevels)
+ for (TextAndBidiLevel textAndBidiLevel : textAndBidiLevels)
{
showTextUni(contentStream, font, fontSize, textAndBidiLevel.getText(), textAndBidiLevel.getBidiLevel());
}
+ if (useActualText)
+ {
+ endMarkedContent(contentStream);
+ }
}
/**
Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ContentStreamForGlyphLayoutInterface.java
==============================================================================
--- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ContentStreamForGlyphLayoutInterface.java Sat Sep 26 18:45:03 2026 (r1938541)
+++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ContentStreamForGlyphLayoutInterface.java Sat Sep 26 18:46:13 2026 (r1938542)
@@ -16,6 +16,9 @@
*/
package org.apache.pdfbox.pdmodel;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList;
+
import java.io.IOException;
public interface ContentStreamForGlyphLayoutInterface
@@ -46,4 +49,21 @@ public interface ContentStreamForGlyphLa
* @throws IOException If the content stream could not be written.
*/
void setTextRise(float rise) throws IOException;
+
+
+ /**
+ * Begin a marked content sequence with a reference to an entry in the page resources' Properties dictionary.
+ *
+ * @param tag the tag to be added to the content stream
+ * @param propertyList property list to be added to the content stream
+ * @throws IOException If the content stream could not be written
+ */
+ void beginMarkedContent(COSName tag, PDPropertyList propertyList) throws IOException;
+
+ /**
+ * End a marked content sequence.
+ *
+ * @throws IOException If the content stream could not be written
+ */
+ void endMarkedContent() throws IOException;
}