Commit ebce446f for tesseract
commit ebce446fe15de649a35ce35fbc54c8d92a0a6011
Author: Stefan Weil <sw@weilnetz.de>
Date: Thu Nov 26 08:06:22 2026 +0100
Write a valid ToUnicode CMap for the glyphless PDF font
The ToUnicode CMap mapped all codes with the single range
<0000> <FFFF> <0000>. A range may only increment the last byte of its
destination, which must not exceed 255 (ISO 32000-1, 9.10.3), so the
mapping of all codes above 00FF was undefined. Many PDF readers
increment the whole value and extract the text nevertheless, but
readers which follow the standard do not.
Write one range for each value of the first byte, in blocks of not more
than 100 ranges (Adobe Technical Note 5014). The 256 ranges would add
5412 bytes to each PDF file, so compress the CMap like the other
streams. It adds 1362 bytes then. Fix also the name of the CMap,
Adobe-Identity-UCS.
Characters outside of the BMP are still written as two codes which map
to the two surrogates.
The new unit test checks the ranges, the length of the CMap stream and
the cross reference table of a PDF file without pages. It needs no
model.
Fixes #4718
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/Makefile.am b/Makefile.am
index 1f2be8d7..9e7a173f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1253,6 +1253,7 @@ if ENABLE_TRAINING
check_PROGRAMS += pango_font_info_test
endif # ENABLE_TRAINING
check_PROGRAMS += paragraphs_test
+check_PROGRAMS += pdfrenderer_test
check_PROGRAMS += plumbing_dimensions_test
check_PROGRAMS += plumbing_test
if !DISABLED_LEGACY_ENGINE
@@ -1555,6 +1556,10 @@ paragraphs_test_SOURCES = unittest/paragraphs_test.cc
paragraphs_test_CPPFLAGS = $(unittest_CPPFLAGS)
paragraphs_test_LDADD = $(TESS_LIBS)
+pdfrenderer_test_SOURCES = unittest/pdfrenderer_test.cc
+pdfrenderer_test_CPPFLAGS = $(unittest_CPPFLAGS)
+pdfrenderer_test_LDADD = $(TESS_LIBS) $(LEPTONICA_LIBS)
+
plumbing_dimensions_test_SOURCES = unittest/plumbing_dimensions_test.cc
plumbing_dimensions_test_CPPFLAGS = $(unittest_CPPFLAGS)
plumbing_dimensions_test_LDADD = $(TESS_LIBS)
diff --git a/src/api/pdfrenderer.cpp b/src/api/pdfrenderer.cpp
index 5173b75b..b99f226c 100644
--- a/src/api/pdfrenderer.cpp
+++ b/src/api/pdfrenderer.cpp
@@ -28,6 +28,7 @@
#include <tesseract/baseapi.h>
#include <tesseract/publictypes.h> // for PTIsTextType()
#include <tesseract/renderer.h>
+#include <algorithm> // for std::min
#include <cmath>
#include <cstring>
#include <fstream> // for std::ifstream
@@ -591,40 +592,76 @@ bool TessPDFRenderer::BeginDocumentHandler() {
objsize += AppendData("endobj\n"sv);
AppendPDFObjectDIY(objsize);
- const char stream2[] =
- "/CIDInit /ProcSet findresource begin\n"
- "12 dict begin\n"
- "begincmap\n"
- "/CIDSystemInfo\n"
- "<<\n"
- " /Registry (Adobe)\n"
- " /Ordering (UCS)\n"
- " /Supplement 0\n"
- ">> def\n"
- "/CMapName /Adobe-Identify-UCS def\n"
- "/CMapType 2 def\n"
- "1 begincodespacerange\n"
- "<0000> <FFFF>\n"
- "endcodespacerange\n"
- "1 beginbfrange\n"
- "<0000> <FFFF> <0000>\n"
- "endbfrange\n"
- "endcmap\n"
- "CMapName currentdict /CMap defineresource pop\n"
- "end\n"
- "end\n";
-
// TOUNICODE
+ // The CID of a character is its UTF-16 code unit, so each code maps to
+ // itself. A range may only increment the last byte of its destination
+ // (ISO 32000-1, 9.10.3), so each value of the first byte needs its own
+ // range, and a block must not have more than 100 ranges (Adobe Technical
+ // Note #5014).
+ std::stringstream cmap;
+ // Use "C" locale (needed for int values larger than 999).
+ cmap.imbue(std::locale::classic());
+ cmap << "/CIDInit /ProcSet findresource begin\n"
+ "12 dict begin\n"
+ "begincmap\n"
+ "/CIDSystemInfo\n"
+ "<<\n"
+ " /Registry (Adobe)\n"
+ " /Ordering (UCS)\n"
+ " /Supplement 0\n"
+ ">> def\n"
+ "/CMapName /Adobe-Identity-UCS def\n"
+ "/CMapType 2 def\n"
+ "1 begincodespacerange\n"
+ "<0000> <FFFF>\n"
+ "endcodespacerange\n";
+ const int kNumRanges = 256;
+ const int kMaxRangesPerBlock = 100;
+ for (int first = 0; first < kNumRanges; first += kMaxRangesPerBlock) {
+ const int end = std::min(first + kMaxRangesPerBlock, kNumRanges);
+ cmap << (end - first) << " beginbfrange\n";
+ for (int high = first; high < end; high++) {
+ char range[32];
+ snprintf(range, sizeof(range), "<%02X00> <%02XFF> <%02X00>\n", high, high, high);
+ cmap << range;
+ }
+ cmap << "endbfrange\n";
+ }
+ cmap << "endcmap\n"
+ "CMapName currentdict /CMap defineresource pop\n"
+ "end\n"
+ "end\n";
+ const std::string tounicode = cmap.str();
+ len = tounicode.size();
+#ifndef NO_PDF_COMPRESSION
+ comp = zlibCompress(reinterpret_cast<const unsigned char *>(tounicode.data()), tounicode.size(),
+ &len);
+ if (comp == nullptr) {
+ return false;
+ }
+#endif
stream.str("");
stream << "6 0 obj\n"
"<< /Length "
- << (sizeof(stream2) - 1)
- << " >>\n"
- "stream\n"
- << stream2
- << "endstream\n"
- "endobj\n";
- AppendPDFObject(stream.str().c_str());
+ << len
+ << ""
+#ifndef NO_PDF_COMPRESSION
+ " /Filter /FlateDecode"
+#endif
+ " >>\n"
+ "stream\n";
+ AppendString(stream.str().c_str());
+ objsize = stream.str().size();
+#ifndef NO_PDF_COMPRESSION
+ AppendData(reinterpret_cast<char *>(comp), len);
+ lept_free(comp);
+#else
+ AppendData(tounicode.data(), len);
+#endif
+ objsize += len;
+ objsize += AppendData("endstream\n"sv);
+ objsize += AppendData("endobj\n"sv);
+ AppendPDFObjectDIY(objsize);
// FONT DESCRIPTOR
stream.str("");
diff --git a/unittest/pdfrenderer_test.cc b/unittest/pdfrenderer_test.cc
new file mode 100644
index 00000000..3b59ed17
--- /dev/null
+++ b/unittest/pdfrenderer_test.cc
@@ -0,0 +1,197 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <tesseract/renderer.h>
+
+#include "include_gunit.h"
+
+#include <allheaders.h> // for zlibUncompress
+
+#include <cstdlib> // for strtoul
+#include <filesystem>
+#include <fstream>
+#include <iterator> // for std::istreambuf_iterator
+#include <sstream>
+#include <string>
+#include <vector>
+
+namespace tesseract {
+
+namespace {
+
+// Writes a PDF file without pages and returns its contents.
+std::string MakeEmptyPdf() {
+ std::error_code ec;
+ std::filesystem::create_directories(FLAGS_test_tmpdir, ec);
+ const std::string outputbase = std::string(FLAGS_test_tmpdir) + "/pdfrenderer_test";
+ {
+ // The data directory has no pdf.ttf, so the built-in font is used.
+ TessPDFRenderer renderer(outputbase.c_str(), FLAGS_test_tmpdir, true);
+ EXPECT_TRUE(renderer.BeginDocument("pdfrenderer_test"));
+ EXPECT_TRUE(renderer.EndDocument());
+ }
+ std::ifstream input(outputbase + ".pdf", std::ios::in | std::ios::binary);
+ return std::string(std::istreambuf_iterator<char>(input), {});
+}
+
+// Returns the number at the start of the given text, or -1 if there is none.
+long ParseNumber(const std::string &text, size_t pos = 0) {
+ if (pos >= text.size() || text[pos] < '0' || text[pos] > '9') {
+ return -1;
+ }
+ return strtol(text.c_str() + pos, nullptr, 10);
+}
+
+// Gets the value of a 2 byte code written like <01FF>.
+bool ParseCode(const std::string &token, unsigned *code) {
+ if (token.size() != 6 || token.front() != '<' || token.back() != '>') {
+ return false;
+ }
+ char *end;
+ *code = strtoul(token.c_str() + 1, &end, 16);
+ return end == token.c_str() + 5;
+}
+
+// Returns the decoded stream of the object with the given number.
+// Fails if the stream does not have the size given by /Length.
+std::string GetStream(const std::string &pdf, int objnum) {
+ const std::string header = "\n" + std::to_string(objnum) + " 0 obj\n";
+ const std::string length_key = "/Length ";
+ const std::string stream_key = "stream\n";
+ const std::string end_key = "endstream\n";
+ auto pos = pdf.find(header);
+ if (pos == std::string::npos) {
+ ADD_FAILURE() << "No object " << objnum;
+ return "";
+ }
+ const auto dictionary = pos;
+ pos = pdf.find(length_key, pos);
+ if (pos == std::string::npos) {
+ ADD_FAILURE() << "No /Length in object " << objnum;
+ return "";
+ }
+ const long length = ParseNumber(pdf, pos + length_key.size());
+ pos = pdf.find(stream_key, pos);
+ if (length < 0 || pos == std::string::npos) {
+ ADD_FAILURE() << "No stream in object " << objnum;
+ return "";
+ }
+ const bool compressed =
+ pdf.substr(dictionary, pos - dictionary).find("/FlateDecode") != std::string::npos;
+ pos += stream_key.size();
+ EXPECT_EQ(pdf.compare(pos + length, end_key.size(), end_key), 0)
+ << "Wrong /Length " << length << " in object " << objnum;
+ if (!compressed) {
+ return pdf.substr(pos, length);
+ }
+ size_t size = 0;
+ l_uint8 *data =
+ zlibUncompress(reinterpret_cast<const l_uint8 *>(pdf.data()) + pos, length, &size);
+ if (data == nullptr) {
+ ADD_FAILURE() << "Cannot decode the stream of object " << objnum;
+ return "";
+ }
+ const std::string stream(reinterpret_cast<const char *>(data), size);
+ lept_free(data);
+ return stream;
+}
+
+} // namespace
+
+// Tests that the ToUnicode CMap of the glyphless font maps each 2 byte code
+// to the same UTF-16 code unit with ranges which are allowed by the PDF
+// standard (https://github.com/tesseract-ocr/tesseract/issues/4718).
+TEST(PDFRendererTest, ToUnicodeRanges) {
+ const std::string pdf = MakeEmptyPdf();
+ ASSERT_NE(pdf.find("/ToUnicode 6 0 R"), std::string::npos);
+ std::istringstream cmap(GetStream(pdf, 6));
+
+ const unsigned kNumCodes = 0x10000;
+ // A block must not have more than 100 ranges (Adobe Technical Note #5014).
+ const int kMaxRangesPerBlock = 100;
+ std::vector<long> unicode(kNumCodes, -1);
+ int num_blocks = 0;
+ std::string token;
+ std::string previous;
+ while (cmap >> token) {
+ if (token == "beginbfrange") {
+ num_blocks++;
+ int num_ranges = 0;
+ std::string first;
+ std::string last;
+ std::string destination;
+ while (cmap >> first && first != "endbfrange") {
+ ASSERT_TRUE(cmap >> last >> destination);
+ num_ranges++;
+ unsigned code1;
+ unsigned code2;
+ unsigned unicode1;
+ ASSERT_TRUE(ParseCode(first, &code1)) << first;
+ ASSERT_TRUE(ParseCode(last, &code2)) << last;
+ ASSERT_TRUE(ParseCode(destination, &unicode1)) << destination;
+ ASSERT_LE(code1, code2);
+ const std::string range = first + " " + last + " " + destination;
+ // Only the last byte of the code changes in a range.
+ EXPECT_EQ(code1 >> 8, code2 >> 8) << range;
+ // Only the last byte of the destination is incremented, so it must
+ // not exceed 255 for the last code (ISO 32000-1, 9.10.3).
+ EXPECT_LE((unicode1 & 0xFF) + (code2 - code1), 255U) << range;
+ for (unsigned code = code1; code <= code2; code++) {
+ EXPECT_EQ(unicode[code], -1) << "Code in more than one range: " << range;
+ unicode[code] = unicode1 + (code - code1);
+ }
+ }
+ EXPECT_EQ(first, "endbfrange");
+ EXPECT_EQ(num_ranges, ParseNumber(previous));
+ EXPECT_LE(num_ranges, kMaxRangesPerBlock);
+ }
+ previous = token;
+ }
+ EXPECT_GT(num_blocks, 0);
+ unsigned num_wrong_codes = 0;
+ for (unsigned code = 0; code < kNumCodes; code++) {
+ if (unicode[code] != static_cast<long>(code)) {
+ num_wrong_codes++;
+ }
+ }
+ EXPECT_EQ(num_wrong_codes, 0U);
+}
+
+// Tests that each entry of the cross reference table gives the position of
+// its object.
+TEST(PDFRendererTest, CrossReferenceTable) {
+ const std::string pdf = MakeEmptyPdf();
+ const std::string startxref_key = "startxref\n";
+ const std::string xref_key = "xref\n0 ";
+ const size_t kEntrySize = 20;
+ auto pos = pdf.rfind(startxref_key);
+ ASSERT_NE(pos, std::string::npos);
+ const long xref = ParseNumber(pdf, pos + startxref_key.size());
+ ASSERT_GE(xref, 0);
+ ASSERT_EQ(pdf.compare(xref, xref_key.size(), xref_key), 0);
+ const long num_objects = ParseNumber(pdf, xref + xref_key.size());
+ // The file has at least the 8 objects of the catalog, the pages and the font.
+ ASSERT_GT(num_objects, 8);
+ pos = pdf.find('\n', xref + xref_key.size());
+ ASSERT_NE(pos, std::string::npos);
+ pos++;
+ ASSERT_LE(pos + num_objects * kEntrySize, pdf.size());
+ EXPECT_EQ(pdf.substr(pos, kEntrySize), "0000000000 65535 f \n");
+ for (long objnum = 1; objnum < num_objects; objnum++) {
+ const std::string entry = pdf.substr(pos + objnum * kEntrySize, kEntrySize);
+ EXPECT_EQ(entry.substr(10), " 00000 n \n") << "Object " << objnum;
+ const long offset = ParseNumber(entry);
+ ASSERT_GE(offset, 0) << "Object " << objnum;
+ const std::string header = std::to_string(objnum) + " 0 obj\n";
+ EXPECT_EQ(pdf.compare(offset, header.size(), header), 0) << "Object " << objnum;
+ }
+}
+
+} // namespace tesseract