Commit f4cb819a for tesseract
commit f4cb819ad546396a68d49a1b2ffa0eb3f74af275
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 20 21:21:06 2026 +0100
Write font attributes in ALTO and PAGE output
hOCR output marks bold and italic words with <strong> and <em>. ALTO
and PAGE output had no font attributes at all.
Write the attributes bold, italic and small caps which
WordFontAttributes reports for a word:
- ALTO: as attribute STYLE of the element String, for example
STYLE="bold italics".
- PAGE: as element TextStyle with the attributes bold, italic and
smallCaps, for example <TextStyle bold="true"/>. Each word gets it
with page_xml_level 1. A text line gets it for the attributes which
all of its words have, because the default output has no words.
Nothing is written for a word or a line without such an attribute, so
the output only changes where the OCR engine reports them. Today that
is the legacy engine.
The new unit test sets the fonts of the words itself, so it does not
depend on the fonts which an engine finds. It is skipped in a build
without the legacy engine, where WordFontAttributes reports nothing.
ALTO output with the new attribute is valid for the ALTO 3.0 schema,
and PAGE output with the new element for the PAGE schema 2019-07-15,
with and without words.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/Makefile.am b/Makefile.am
index c03fefef..129ede5f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1176,6 +1176,7 @@ if !DISABLED_LEGACY_ENGINE
check_PROGRAMS += equationdetect_test
endif # !DISABLED_LEGACY_ENGINE
check_PROGRAMS += fileio_test
+check_PROGRAMS += fontattributes_test
check_PROGRAMS += fullyconnected_test
check_PROGRAMS += functions_test
check_PROGRAMS += genericvector_test
@@ -1351,6 +1352,10 @@ fileio_test_SOURCES = unittest/fileio_test.cc
fileio_test_CPPFLAGS = $(unittest_CPPFLAGS)
fileio_test_LDADD = $(TRAINING_LIBS)
+fontattributes_test_SOURCES = unittest/fontattributes_test.cc
+fontattributes_test_CPPFLAGS = $(unittest_CPPFLAGS)
+fontattributes_test_LDADD = $(TESS_LIBS) $(LEPTONICA_LIBS)
+
fullyconnected_test_SOURCES = unittest/fullyconnected_test.cc
fullyconnected_test_CPPFLAGS = $(unittest_CPPFLAGS)
fullyconnected_test_LDADD = $(TESS_LIBS)
diff --git a/doc/tesseract.1.asc b/doc/tesseract.1.asc
index 66f90637..a9da4839 100644
--- a/doc/tesseract.1.asc
+++ b/doc/tesseract.1.asc
@@ -454,6 +454,8 @@ OUTPUT FORMAT PARAMETERS
Write ALTO XML output to a `.xml` file. ALTO (Analyzed Layout and Text
Object) is a standard XML schema for describing the layout and content of
pages. Use the *alto* config file to enable this format.
+ Words which have the font attributes bold, italic or small caps get the
+ attribute `STYLE`, for example `STYLE="bold italics"`.
*tessedit_create_page_xml* (bool, default: 0) [Both]::
Write PAGE XML output to a `.page.xml` file. PAGE (Page Analysis and
@@ -462,6 +464,9 @@ OUTPUT FORMAT PARAMETERS
tools such as Transkribus and eScriptorium.
See https://github.com/PRImA-Research-Lab/PAGE-XML for the specification.
Use the *page* config file to enable this format.
+ The font attributes bold, italic and small caps are written as an element
+ `TextStyle`, for example `<TextStyle bold="true"/>`: for a text line if all
+ of its words have the attribute, and for each word with *page_xml_level* 1.
*page_xml_polygon* (bool, default: 1) [Both]::
When writing PAGE XML output, create polygon outlines around text regions
diff --git a/src/api/altorenderer.cpp b/src/api/altorenderer.cpp
index 8e72aca2..064e3723 100644
--- a/src/api/altorenderer.cpp
+++ b/src/api/altorenderer.cpp
@@ -24,6 +24,7 @@
#include <locale> // for std::locale::classic
#include <memory>
#include <sstream> // for std::stringstream
+#include <string>
namespace tesseract {
@@ -141,6 +142,31 @@ TessAltoRenderer::TessAltoRenderer(const char *outputbase)
: TessResultRenderer(outputbase, "xml"),
begin_document(false) {}
+///
+/// Get the font styles of the current word as a value for the attribute STYLE.
+/// The result is empty for a word without such styles.
+///
+static std::string GetWordStyle(const ResultIterator *it) {
+ bool bold, italic, underlined, monospace, serif, smallcaps;
+ int pointsize, font_id;
+ it->WordFontAttributes(&bold, &italic, &underlined, &monospace, &serif, &smallcaps, &pointsize,
+ &font_id);
+ std::string style;
+ if (bold) {
+ style += "bold ";
+ }
+ if (italic) {
+ style += "italics ";
+ }
+ if (smallcaps) {
+ style += "smallcaps ";
+ }
+ if (!style.empty()) {
+ style.pop_back();
+ }
+ return style;
+}
+
///
/// Make an XML-formatted string with ALTO markup from the internal
/// data structures.
@@ -232,6 +258,7 @@ char *TessBaseAPI::GetAltoText(ETEXT_DESC *monitor, int page_number) {
alto_str << "\t\t\t\t\t\t\t<String ID=\"" << GetID("string", page_number, wcnt) << "\"";
AddBoxToAlto(res_it.get(), RIL_WORD, alto_str);
+ const std::string style = GetWordStyle(res_it.get());
alto_str << " CONTENT=\"";
bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
@@ -248,7 +275,11 @@ char *TessBaseAPI::GetAltoText(ETEXT_DESC *monitor, int page_number) {
res_it->Next(RIL_SYMBOL);
} while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
- alto_str << "\"/>";
+ alto_str << "\"";
+ if (!style.empty()) {
+ alto_str << " STYLE=\"" << style << "\"";
+ }
+ alto_str << "/>";
wcnt++;
diff --git a/src/api/pagerenderer.cpp b/src/api/pagerenderer.cpp
index 905d1fab..5d2485fc 100644
--- a/src/api/pagerenderer.cpp
+++ b/src/api/pagerenderer.cpp
@@ -604,6 +604,48 @@ static Pta *FitBaselineIntoLinePolygon(Pta *bottom_pts, Pta *baseline_pts,
}
#endif
+///
+/// Font styles of a word or of a text line
+///
+struct FontStyle {
+ bool bold = false;
+ bool italic = false;
+ bool smallcaps = false;
+};
+
+///
+/// Get the font styles of the current word
+///
+static FontStyle GetWordFontStyle(const ResultIterator *it) {
+ FontStyle style;
+ bool underlined, monospace, serif;
+ int pointsize, font_id;
+ it->WordFontAttributes(&style.bold, &style.italic, &underlined, &monospace, &serif,
+ &style.smallcaps, &pointsize, &font_id);
+ return style;
+}
+
+///
+/// Add the element TextStyle to the output if a font style is set
+///
+static void AddTextStyleToPAGE(const FontStyle &style, const char *indent,
+ std::stringstream &str) {
+ if (!style.bold && !style.italic && !style.smallcaps) {
+ return;
+ }
+ str << indent << "<TextStyle";
+ if (style.bold) {
+ str << " bold=\"true\"";
+ }
+ if (style.italic) {
+ str << " italic=\"true\"";
+ }
+ if (style.smallcaps) {
+ str << " smallCaps=\"true\"";
+ }
+ str << "/>\n";
+}
+
/// Convert writing direction to string representation
static const char *WritingDirectionToStr(int wd) {
switch (wd) {
@@ -787,6 +829,8 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
float block_conf = 0;
float line_conf = 0;
+ // Font styles which all words of the current text line have.
+ FontStyle line_style;
while (!res_it->Empty(RIL_BLOCK)) {
if (res_it->Empty(RIL_WORD)) {
@@ -874,6 +918,9 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
// writing_direction_before = writing_direction;
line_conf = res_it->Confidence(RIL_TEXTLINE) / 100;
+ line_style.bold = true;
+ line_style.italic = true;
+ line_style.smallcaps = true;
char *utf8text = res_it->GetUTF8Text(RIL_TEXTLINE);
std::string textline = utf8text;
delete[] utf8text;
@@ -904,6 +951,10 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
bool last_word_in_cblock = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
float word_conf = res_it->Confidence(RIL_WORD) / 100;
+ const FontStyle word_style = GetWordFontStyle(res_it.get());
+ line_style.bold &= word_style.bold;
+ line_style.italic &= word_style.italic;
+ line_style.smallcaps &= word_style.smallcaps;
// Create word stream if word level output is active
if (LEVELFLAG > 0) {
@@ -972,8 +1023,9 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
<< std::setprecision(4) << word_conf << "\">\n"
<< "\t\t\t\t\t\t<Unicode>" << word_content.str()
<< "</Unicode>\n"
- << "\t\t\t\t\t</TextEquiv>\n"
- << "\t\t\t\t</Word>\n";
+ << "\t\t\t\t\t</TextEquiv>\n";
+ AddTextStyleToPAGE(word_style, "\t\t\t\t\t", word_str);
+ word_str << "\t\t\t\t</Word>\n";
}
if (LEVELFLAG > 0 || (POLYGONFLAG && !skewed_flag)) {
// Add wordbaseline to linebaseline
@@ -1081,6 +1133,7 @@ char *TessBaseAPI::GetPAGEText(ETEXT_DESC *monitor, int /*page_number*/) {
<< std::setprecision(4) << line_conf << "\">\n"
<< "\t\t\t\t\t<Unicode>" << line_content.str() << "</Unicode>\n"
<< "\t\t\t\t</TextEquiv>\n";
+ AddTextStyleToPAGE(line_style, "\t\t\t\t", line_str);
line_str << "\t\t\t</TextLine>\n";
region_content << line_content.str();
line_content.str("");
diff --git a/unittest/fontattributes_test.cc b/unittest/fontattributes_test.cc
new file mode 100644
index 00000000..55122e33
--- /dev/null
+++ b/unittest/fontattributes_test.cc
@@ -0,0 +1,298 @@
+///////////////////////////////////////////////////////////////////////
+// File: fontattributes_test.cc
+// Description: Unit tests for the font attributes (bold, italic, small
+// caps) in ALTO and PAGE output.
+//
+// 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 "include_gunit.h"
+
+#include "fontinfo.h"
+#include "image.h"
+#include "pageres.h"
+
+#include <allheaders.h>
+#include <tesseract/baseapi.h>
+#include <tesseract/resultiterator.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace tesseract {
+
+// Gives access to the results of the recognition, so the tests can set the
+// font of each word. The output then does not depend on the fonts which an
+// OCR engine finds.
+class FriendlyTessBaseAPI : public TessBaseAPI {
+public:
+ PAGE_RES *page_res() {
+ return page_res_;
+ }
+};
+
+// Returns the attributes of the first element with the given name in the
+// text, without the name of the element, or "none" if there is no such
+// element. The name must end with the space before the attributes.
+static std::string Attributes(const std::string &text, const std::string &name) {
+ const size_t start = text.find("<" + name);
+ if (start == std::string::npos) {
+ return "none";
+ }
+ const size_t end = text.find('>', start);
+ std::string attributes = text.substr(start + 1 + name.size(), end - start - 1 - name.size());
+ if (!attributes.empty() && attributes.back() == '/') {
+ attributes.pop_back();
+ }
+ return attributes;
+}
+
+// Returns the parts of the text which start with the given string.
+static std::vector<std::string> Split(const std::string &text, const std::string &start) {
+ std::vector<std::string> parts;
+ size_t pos = text.find(start);
+ while (pos != std::string::npos) {
+ const size_t next = text.find(start, pos + 1);
+ parts.push_back(text.substr(pos, next == std::string::npos ? next : next - pos));
+ pos = next;
+ }
+ return parts;
+}
+
+// The tests recognize phototest.tif, which has eight text lines, and give
+// the words of the first five lines these fonts:
+// line 0: bold
+// line 1: italic
+// line 2: bold and italic, and the words are small caps
+// line 3: bold for the first word, a regular font for the other words
+// line 4: bold and bold italic in turns
+// The words of the other lines get no font.
+class FontAttributesTest : public testing::Test {
+protected:
+ void SetUp() override {
+ REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+ if (api_.Init(TESSDATA_DIR, "eng") == -1) {
+ // eng.traineddata not found.
+ GTEST_SKIP();
+ }
+ api_.SetPageSegMode(PSM_AUTO);
+ char names[4][12] = {"Regular", "Italic", "Bold", "Bold Italic"};
+ for (unsigned i = 0; i < 4; ++i) {
+ names_[i] = names[i];
+ fonts_[i].name = &names_[i][0];
+ // The properties are those of FontInfo: 1 = italic, 2 = bold.
+ fonts_[i].properties = i;
+ fonts_[i].universal_id = i;
+ }
+ }
+
+ // Recognizes the image and sets the fonts. Skips the test if the API
+ // does not report the font attributes of a word, which is the case for
+ // some builds without the legacy engine.
+ void Recognize() {
+ Image pix = pixRead(TESTING_DIR "/phototest.tif");
+ ASSERT_TRUE(pix != nullptr);
+ api_.SetImage(pix);
+ pix.destroy();
+ ASSERT_EQ(api_.Recognize(nullptr), 0);
+ ASSERT_NE(api_.page_res(), nullptr);
+ int line = -1;
+ int word_in_line = 0;
+ const ROW_RES *row = nullptr;
+ PAGE_RES_IT it(api_.page_res());
+ for (it.restart_page(); it.word() != nullptr; it.forward()) {
+ if (it.row() != row) {
+ row = it.row();
+ ++line;
+ word_in_line = 0;
+ expected_lines_.push_back("");
+ }
+ WERD_RES *word = it.word();
+ bool bold = false;
+ bool italic = false;
+ bool smallcaps = false;
+ bool has_font = true;
+ switch (line) {
+ case 0:
+ bold = true;
+ break;
+ case 1:
+ italic = true;
+ break;
+ case 2:
+ bold = italic = smallcaps = true;
+ break;
+ case 3:
+ bold = word_in_line == 0;
+ break;
+ case 4:
+ bold = true;
+ italic = word_in_line % 2 == 1;
+ break;
+ default:
+ has_font = false;
+ }
+ word->fontinfo = has_font ? &fonts_[(italic ? 1 : 0) | (bold ? 2 : 0)] : nullptr;
+ word->small_caps = smallcaps;
+ expected_alto_.push_back(std::string(bold ? "bold " : "") + (italic ? "italics " : "") +
+ (smallcaps ? "smallcaps " : ""));
+ if (!expected_alto_.back().empty()) {
+ expected_alto_.back().pop_back();
+ }
+ expected_page_.push_back(std::string(bold ? " bold=\"true\"" : "") +
+ (italic ? " italic=\"true\"" : "") +
+ (smallcaps ? " smallCaps=\"true\"" : ""));
+ ++word_in_line;
+ }
+ ASSERT_GE(line, 5);
+ // A line has the attributes which all of its words have.
+ expected_lines_[0] = " bold=\"true\"";
+ expected_lines_[1] = " italic=\"true\"";
+ expected_lines_[2] = " bold=\"true\" italic=\"true\" smallCaps=\"true\"";
+ expected_lines_[4] = " bold=\"true\"";
+
+ std::unique_ptr<ResultIterator> result(api_.GetIterator());
+ ASSERT_NE(result, nullptr);
+ bool bold, italic, underlined, monospace, serif, smallcaps;
+ int pointsize, font_id;
+ result->WordFontAttributes(&bold, &italic, &underlined, &monospace, &serif, &smallcaps,
+ &pointsize, &font_id);
+ if (!bold) {
+ LOG(INFO) << "Skip test because the API reports no font attributes\n";
+ GTEST_SKIP();
+ }
+ }
+
+ // Returns the text of the element TextStyle in the given part of a PAGE
+ // file as it is expected by the tests: the attributes or an empty string.
+ static std::string Style(const std::string &text) {
+ const std::string style = Attributes(text, "TextStyle");
+ return style == "none" ? "" : style;
+ }
+
+ // The fonts must live longer than the results which refer to them.
+ std::string names_[4];
+ FontInfo fonts_[4];
+ FriendlyTessBaseAPI api_;
+ // Expected value of the attribute STYLE for each word in ALTO.
+ std::vector<std::string> expected_alto_;
+ // Expected attributes of the element TextStyle for each word in PAGE.
+ std::vector<std::string> expected_page_;
+ // The same for each text line.
+ std::vector<std::string> expected_lines_;
+};
+
+TEST_F(FontAttributesTest, Alto) {
+ ASSERT_NO_FATAL_FAILURE(Recognize());
+ if (IsSkipped()) {
+ return;
+ }
+ std::unique_ptr<char[]> alto(api_.GetAltoText(0));
+ ASSERT_NE(alto, nullptr);
+ const std::vector<std::string> words = Split(alto.get(), "<String ");
+ ASSERT_EQ(words.size(), expected_alto_.size());
+ for (size_t i = 0; i < words.size(); ++i) {
+ const std::string attributes = Attributes(words[i], "String ");
+ const size_t pos = attributes.find(" STYLE=\"");
+ std::string style;
+ if (pos != std::string::npos) {
+ style = attributes.substr(pos + 8, attributes.find('"', pos + 8) - pos - 8);
+ // The attribute is only written with a value.
+ EXPECT_FALSE(style.empty()) << words[i];
+ }
+ EXPECT_EQ(style, expected_alto_[i]) << "word " << i << ": " << words[i];
+ }
+}
+
+TEST_F(FontAttributesTest, PageWordLevel) {
+ ASSERT_TRUE(api_.SetVariable("page_xml_level", "1"));
+ ASSERT_NO_FATAL_FAILURE(Recognize());
+ if (IsSkipped()) {
+ return;
+ }
+ std::unique_ptr<char[]> page(api_.GetPAGEText(0));
+ ASSERT_NE(page, nullptr);
+ const std::vector<std::string> lines = Split(page.get(), "<TextLine ");
+ ASSERT_EQ(lines.size(), expected_lines_.size());
+ size_t num_words = 0;
+ for (size_t l = 0; l < lines.size(); ++l) {
+ const std::vector<std::string> words = Split(lines[l], "<Word ");
+ ASSERT_FALSE(words.empty()) << "line " << l;
+ for (const auto &word : words) {
+ ASSERT_LT(num_words, expected_page_.size());
+ // The style follows the text of the word.
+ const size_t text_end = word.find("</TextEquiv>");
+ const size_t word_end = word.find("</Word>");
+ ASSERT_NE(text_end, std::string::npos);
+ ASSERT_NE(word_end, std::string::npos);
+ EXPECT_EQ(Style(word.substr(0, text_end)), "") << word;
+ EXPECT_EQ(Style(word.substr(text_end, word_end - text_end)), expected_page_[num_words])
+ << "word " << num_words << ": " << word;
+ ++num_words;
+ }
+ // The style of the line follows its words and its text.
+ const size_t line_end = lines[l].find("</TextLine>");
+ ASSERT_NE(line_end, std::string::npos);
+ const std::string line = lines[l].substr(0, line_end);
+ const size_t last_word_end = line.rfind("</Word>");
+ const size_t text_end = line.rfind("</TextEquiv>");
+ ASSERT_LT(last_word_end, text_end);
+ EXPECT_EQ(Style(line.substr(text_end)), expected_lines_[l]) << "line " << l;
+ }
+ EXPECT_EQ(num_words, expected_page_.size());
+}
+
+TEST_F(FontAttributesTest, PageLineLevel) {
+ ASSERT_NO_FATAL_FAILURE(Recognize());
+ if (IsSkipped()) {
+ return;
+ }
+ std::unique_ptr<char[]> page(api_.GetPAGEText(0));
+ ASSERT_NE(page, nullptr);
+ const std::string text(page.get());
+ EXPECT_EQ(text.find("<Word "), std::string::npos);
+ const std::vector<std::string> lines = Split(text, "<TextLine ");
+ ASSERT_EQ(lines.size(), expected_lines_.size());
+ for (size_t l = 0; l < lines.size(); ++l) {
+ const size_t text_end = lines[l].find("</TextEquiv>");
+ const size_t line_end = lines[l].find("</TextLine>");
+ ASSERT_LT(text_end, line_end);
+ EXPECT_EQ(Style(lines[l].substr(0, text_end)), "") << "line " << l;
+ EXPECT_EQ(Style(lines[l].substr(text_end, line_end - text_end)), expected_lines_[l])
+ << "line " << l;
+ }
+}
+
+// Words without font attributes must not change the output.
+TEST_F(FontAttributesTest, NoAttributes) {
+ Image pix = pixRead(TESTING_DIR "/phototest.tif");
+ ASSERT_TRUE(pix != nullptr);
+ api_.SetImage(pix);
+ pix.destroy();
+ ASSERT_EQ(api_.Recognize(nullptr), 0);
+ PAGE_RES_IT it(api_.page_res());
+ for (it.restart_page(); it.word() != nullptr; it.forward()) {
+ it.word()->fontinfo = nullptr;
+ it.word()->small_caps = false;
+ }
+ std::unique_ptr<char[]> alto(api_.GetAltoText(0));
+ ASSERT_NE(alto, nullptr);
+ EXPECT_EQ(std::string(alto.get()).find("STYLE"), std::string::npos);
+ for (const char *level : {"0", "1"}) {
+ ASSERT_TRUE(api_.SetVariable("page_xml_level", level));
+ std::unique_ptr<char[]> page(api_.GetPAGEText(0));
+ ASSERT_NE(page, nullptr);
+ EXPECT_EQ(std::string(page.get()).find("TextStyle"), std::string::npos);
+ }
+}
+
+} // namespace tesseract