Commit 69031805 for tesseract

commit 69031805b0bf465468ead416d29753defdf397d0
Author: Stefan Weil <sw@weilnetz.de>
Date:   Fri Nov 20 22:23:14 2026 +0100

    Don't write an empty reading order in PAGE output

    The PAGE renderer always wrote the element ReadingOrder with an
    OrderedGroup, and added a reference for each text region. For a page
    without text regions the group stayed empty. The PAGE schema
    (2019-07-15) needs at least one entry in an OrderedGroup, so the file
    was not valid: an XML validator reports that the content of the
    element OrderedGroup is not complete.

    Write the reading order only if it has a region. The element is
    optional in the schema. Output for pages with text does not change.

    A new unit test checks that a blank page has no reading order and
    that a page with text has one with all of its text regions.

    PAGE files for arabic.tif of the test repository, for which the model
    ara of tessdata finds no text, were invalid before and are valid with
    this change, with all values of page_xml_level and page_xml_polygon.
    The files for eight other images are identical with and without this
    change.

    Fixes: 577e8a8b93a9 ("Add PAGE XML renderer / export (#4214)")
    Assisted-by: Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/api/pagerenderer.cpp b/src/api/pagerenderer.cpp
index 7fa95ab0..905d1fab 100644
--- a/src/api/pagerenderer.cpp
+++ b/src/api/pagerenderer.cpp
@@ -725,7 +725,10 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {

   // Used variables

+  std::stringstream page_start_str;
   std::stringstream reading_order_str;
+  // The reading order has at least one region.
+  bool has_reading_order = false;
   std::stringstream region_content;
   std::stringstream line_content;
   std::stringstream word_content;
@@ -768,13 +771,13 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {

   // Use "C" locale (needed for int values larger than 999).
   page_str.imbue(std::locale::classic());
-  reading_order_str << "\t<Page " << "imageFilename=\"";
+  page_start_str << "\t<Page " << "imageFilename=\"";
   const char *input_name = GetInputName();
   if (input_name != nullptr) {
-    reading_order_str << HOcrEscape(input_name);
+    page_start_str << HOcrEscape(input_name);
   }
-  reading_order_str << "\" " << "imageWidth=\"" << rect_width_ << "\" "
-                    << "imageHeight=\"" << rect_height_ << "\">\n";
+  page_start_str << "\" " << "imageWidth=\"" << rect_width_ << "\" "
+                 << "imageHeight=\"" << rect_height_ << "\">\n";
   std::size_t ro_id = std::hash<std::string>{}(input_name ? input_name : "");
   reading_order_str << "\t\t<ReadingOrder>\n"
                     << "\t\t\t<OrderedGroup id=\"ro" << ro_id
@@ -825,6 +828,7 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
       // Add Block to reading order
       reading_order_str << "\t\t\t\t<RegionRefIndexed " << "index=\"" << rcnt
                         << "\" " << "regionRef=\"r" << rcnt << "\"/>\n";
+      has_reading_order = true;

       float deskew_angle;
       res_it->Orientation(&orientation_block, &writing_direction_block,
@@ -1132,16 +1136,15 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
   ptaDestroy(&line_baseline_ltr_pts);
   ptaDestroy(&line_baseline_pts);

-  reading_order_str << "\t\t\t</OrderedGroup>\n"
-                    << "\t\t</ReadingOrder>\n";
-
-  reading_order_str << page_str.str();
-  page_str.str("");
-  reading_order_str << "\t</Page>\n";
-  const std::string &text = reading_order_str.str();
-  reading_order_str.str("");
+  // The PAGE schema does not allow a reading order without regions, so a
+  // page without text regions gets none.
+  if (has_reading_order) {
+    page_start_str << reading_order_str.str() << "\t\t\t</OrderedGroup>\n"
+                   << "\t\t</ReadingOrder>\n";
+  }
+  page_start_str << page_str.str() << "\t</Page>\n";

-  return copy_string(text);
+  return copy_string(page_start_str.str());
 }

 } // namespace tesseract
diff --git a/unittest/baseapi_test.cc b/unittest/baseapi_test.cc
index 786cb0e6..d70e4a24 100644
--- a/unittest/baseapi_test.cc
+++ b/unittest/baseapi_test.cc
@@ -937,6 +937,51 @@ TEST_F(TesseractTest, PAGEXMLWordHasNoBaseline) {
   }
 }

+// The PAGE schema does not allow a reading order without regions. A page
+// without text had an empty one, so the file was not valid.
+TEST_F(TesseractTest, PAGEXMLNoReadingOrderWithoutText) {
+  REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+  tesseract::TessBaseAPI api;
+  ASSERT_EQ(0, api.Init(TessdataPath().c_str(), "eng"));
+  auto count = [](const std::string &text, const std::string &part) {
+    size_t num = 0;
+    for (size_t pos = text.find(part); pos != std::string::npos; pos = text.find(part, pos + 1)) {
+      ++num;
+    }
+    return num;
+  };
+
+  // An image without text: 1 bit per pixel, all pixels white.
+  Image blank_pix = pixCreate(400, 200, 1);
+  ASSERT_TRUE(blank_pix);
+  api.SetImage(blank_pix);
+  blank_pix.destroy();
+  std::unique_ptr<const char[]> blank(api.GetPAGEText(0));
+  ASSERT_TRUE(blank != nullptr);
+  const std::string blank_str(blank.get());
+  EXPECT_EQ(count(blank_str, "<Page "), 1u);
+  EXPECT_EQ(count(blank_str, "</Page>"), 1u);
+  EXPECT_EQ(count(blank_str, "<TextRegion "), 0u);
+  EXPECT_EQ(count(blank_str, "ReadingOrder"), 0u);
+  EXPECT_EQ(count(blank_str, "OrderedGroup"), 0u);
+
+  // An image with text still has a reading order with all text regions.
+  Image src_pix = pixRead(TestDataNameToPath("phototest.tif").c_str());
+  ASSERT_TRUE(src_pix) << "Could not read phototest.tif";
+  api.SetImage(src_pix);
+  src_pix.destroy();
+  std::unique_ptr<const char[]> page(api.GetPAGEText(0));
+  ASSERT_TRUE(page != nullptr);
+  const std::string page_str(page.get());
+  EXPECT_EQ(count(page_str, "<ReadingOrder>"), 1u);
+  EXPECT_EQ(count(page_str, "</ReadingOrder>"), 1u);
+  EXPECT_EQ(count(page_str, "<OrderedGroup "), 1u);
+  EXPECT_GE(count(page_str, "<TextRegion "), 1u);
+  EXPECT_EQ(count(page_str, "<RegionRefIndexed "), count(page_str, "<TextRegion "));
+  // The reading order comes before the regions.
+  EXPECT_LT(page_str.find("</ReadingOrder>"), page_str.find("<TextRegion "));
+}
+
 // The hOCR lang attribute carries the name the model was loaded under, which
 // must be escaped like the other strings in the output (input name, font
 // name, text). Install a copy of the eng model under a name that contains a