Commit 26228d00 for tesseract

commit 26228d001e6686bdf5cffbb420d750d186ff7d4c
Author: Stefan Weil <sw@weilnetz.de>
Date:   Fri Nov 20 22:21:16 2026 +0100

    Don't write a baseline for words in PAGE output

    With page_xml_level 1 each element Word had an element Baseline. The
    PAGE schema (2019-07-15) allows a baseline for a text line, but not
    for a word, so files with words were not valid: an XML validator
    reports invalid content starting with the element Baseline in the
    first word.

    Write only the baseline of the text line. The baselines of the words
    are still used to calculate it. Output without words does not change.

    A new unit test checks that there is one baseline for each text line
    and none in a word.

    PAGE files with words for eight images of the test repository (Latin,
    Hebrew and Devanagari text, also rotated) were all invalid before and
    are valid with this change, with page_xml_polygon 0 and 1.

    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 5ffa0296..7fa95ab0 100644
--- a/src/api/pagerenderer.cpp
+++ b/src/api/pagerenderer.cpp
@@ -963,8 +963,7 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
         word_top_pts = TransposePolygonline(word_top_pts);
       }
       AddPointsToPAGE(word_top_pts, word_str);
-      word_str << "\t\t\t\t\t";
-      AddBaselinePtsToPAGE(word_baseline_pts, word_str);
+      // The PAGE schema allows a baseline for a text line, but not for a word.
       word_str << "\t\t\t\t\t<TextEquiv index=\"1\" conf=\""
                << std::setprecision(4) << word_conf << "\">\n"
                << "\t\t\t\t\t\t<Unicode>" << word_content.str()
diff --git a/unittest/baseapi_test.cc b/unittest/baseapi_test.cc
index 4e4e563c..786cb0e6 100644
--- a/unittest/baseapi_test.cc
+++ b/unittest/baseapi_test.cc
@@ -888,6 +888,55 @@ TEST_F(TesseractTest, PAGEXMLRightToLeftPolygon) {
   EXPECT_THAT(page_str, HasSubstr("<Coords points=\""));
 }

+// The PAGE schema allows the element Baseline for a text line, but not for a
+// word. Word level output had one in each word, so the files were not valid.
+TEST_F(TesseractTest, PAGEXMLWordHasNoBaseline) {
+  REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+  tesseract::TessBaseAPI api;
+  ASSERT_EQ(0, api.Init(TessdataPath().c_str(), "eng"));
+  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();
+  for (const char *polygon : {"0", "1"}) {
+    api.SetVariable("page_xml_polygon", polygon);
+    api.SetVariable("page_xml_level", "1");
+    std::unique_ptr<const char[]> page(api.GetPAGEText(0));
+    ASSERT_TRUE(page != nullptr);
+    const std::string page_str(page.get());
+    auto count = [&page_str](const std::string &text) {
+      size_t num = 0;
+      for (size_t pos = page_str.find(text); pos != std::string::npos;
+           pos = page_str.find(text, pos + 1)) {
+        ++num;
+      }
+      return num;
+    };
+    EXPECT_GT(count("<Word "), 40u);
+    EXPECT_GT(count("<TextLine "), 5u);
+    // Each text line has a baseline, and there are no other baselines.
+    EXPECT_EQ(count("<Baseline "), count("<TextLine "));
+    // No baseline is inside a word.
+    size_t num_words = 0;
+    for (size_t pos = page_str.find("<Word "); pos != std::string::npos;
+         pos = page_str.find("<Word ", pos + 1)) {
+      const size_t end = page_str.find("</Word>", pos);
+      ASSERT_NE(end, std::string::npos);
+      EXPECT_EQ(page_str.substr(pos, end - pos).find("<Baseline"), std::string::npos)
+          << page_str.substr(pos, end - pos);
+      ++num_words;
+    }
+    EXPECT_EQ(num_words, count("<Word "));
+    // Line level output is unchanged: no words, one baseline for each line.
+    api.SetVariable("page_xml_level", "0");
+    std::unique_ptr<const char[]> lines(api.GetPAGEText(0));
+    ASSERT_TRUE(lines != nullptr);
+    const std::string lines_str(lines.get());
+    EXPECT_EQ(lines_str.find("<Word "), std::string::npos);
+    EXPECT_NE(lines_str.find("<Baseline "), std::string::npos);
+  }
+}
+
 // 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