Commit 102fc24e for tesseract

commit 102fc24e8dc461a2b490f85ff0b3aab5f63da310
Author: Stefan Weil <sw@weilnetz.de>
Date:   Mon Nov 23 11:34:33 2026 +0100

    Estimate font styles for rotated and vertical text

    The estimation of bold and italic for words recognized by LSTM skipped
    all text blocks which are rotated against the image. So the words of a
    page which is not upright and the words of vertical text lines got no
    font attributes at all.

    Turn the image of such a word in the same way as it is done for the
    recognition, which gives a horizontal text line. Vertical lines of
    Chinese, Japanese and Korean text have upright characters in the image,
    so words in these scripts are turned back by the rotation for the
    classifier. The block has the same rotations for both kinds of vertical
    lines, so the recognized characters decide.

    English pages which are rotated by 90, 180 or 270 degrees now give the
    same results as upright pages (42 rendered pages, bold 99.8 % / 93.6 %,
    italic 98.9 % / 97.7 % precision / recall).

    Assisted-by: Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/ccmain/fontstyle.cpp b/src/ccmain/fontstyle.cpp
index 307bdd76..08feb0da 100644
--- a/src/ccmain/fontstyle.cpp
+++ b/src/ccmain/fontstyle.cpp
@@ -260,6 +260,54 @@ static bool StyleFromNeighbours(const std::vector<WordStyle> &styles, size_t ind
   return style.*attribute;
 }

+// Returns the number of clockwise quarter turns of an image which the given
+// rotation of a block stands for. Blocks are only rotated by quarter turns,
+// and the vector (0, 1) turns anticlockwise.
+static int QuarterTurns(const FCOORD &rotation) {
+  if (rotation.y() < 0.0f) {
+    return 1;
+  }
+  if (rotation.y() > 0.0f) {
+    return 3;
+  }
+  return rotation.x() < 0.0f ? 2 : 0;
+}
+
+// Returns true if the word has more characters of the scripts which keep
+// their characters upright in vertical text lines (Chinese, Japanese, Korean)
+// than of other scripts. Characters which many scripts share do not count.
+static bool HasUprightScript(const WERD_RES &word) {
+  const UNICHARSET &unicharset = *word.uch_set;
+  const WERD_CHOICE &choice = *word.best_choice;
+  int num_upright = 0;
+  int num_others = 0;
+  for (unsigned i = 0; i < choice.length(); ++i) {
+    const int script = unicharset.get_script(choice.unichar_id(i));
+    if (script == unicharset.null_sid() || script == unicharset.common_sid()) {
+      continue;
+    }
+    if (script == unicharset.han_sid() || script == unicharset.hiragana_sid() ||
+        script == unicharset.katakana_sid() || script == unicharset.hangul_sid()) {
+      ++num_upright;
+    } else {
+      ++num_others;
+    }
+  }
+  return num_upright > num_others;
+}
+
+// Measures the shape of the text in an image which needs the given number of
+// clockwise quarter turns to get upright characters.
+static bool MeasureTurnedText(Image pix, int turns, TextShape *shape) {
+  if (turns == 0) {
+    return MeasureTextShape(pix, shape);
+  }
+  Image upright = pixRotateOrth(pix, turns);
+  const bool measured = MeasureTextShape(upright, shape);
+  upright.destroy();
+  return measured;
+}
+
 /**
  * font_style_pass
  *
@@ -284,11 +332,20 @@ void Tesseract::font_style_pass(PAGE_RES *page_res) {
     if (block == nullptr || row == nullptr || row->x_height() <= 0.0f) {
       continue;
     }
-    if (block->re_rotation().x() <= 0.0f || block->re_rotation().y() != 0.0f) {
-      // The block is rotated, so its boxes do not refer to the image.
-      continue;
-    }
-    const TBOX box = word->word->bounding_box().intersection(image_box);
+    // The boxes refer to the block, which is rotated against the image if the
+    // page is not upright or if the text lines are vertical. Get the image of
+    // the word as it is in the block, with a horizontal text line.
+    const FCOORD re_rotation = block->re_rotation();
+    const int block_turns = (4 - QuarterTurns(re_rotation)) % 4;
+    // Vertical lines of a rotated page have upright characters in the block.
+    // Those of Chinese, Japanese and Korean text are turned in the block, and
+    // the rotation for the classifier makes them upright. The block is the
+    // same for both, so the script of the word decides.
+    const int upright_turns =
+        HasUprightScript(*word) ? QuarterTurns(block->classify_rotation()) : 0;
+    TBOX box = word->word->bounding_box();
+    box.rotate(re_rotation);
+    box = box.intersection(image_box);
     if (box.null_box()) {
       continue;
     }
@@ -296,9 +353,17 @@ void Tesseract::font_style_pass(PAGE_RES *page_res) {
         boxCreate(box.left(), image_box.top() - box.top(), box.width(), box.height());
     Image word_pix = pixClipRectangle(pix, clip_box, nullptr);
     boxDestroy(&clip_box);
+    if (word_pix != nullptr && block_turns != 0) {
+      Image turned = pixRotateOrth(word_pix, block_turns);
+      word_pix.destroy();
+      word_pix = turned;
+    }
     if (word_pix == nullptr) {
       continue;
     }
+    // From here on the box is the part of the word in the image, in the
+    // coordinates of the block.
+    box.rotate(FCOORD(re_rotation.x(), -re_rotation.y()));
     // Invert white text on black. Such text has more set pixels than others,
     // but so can heavy bold text. The upper and lower edge of the box are
     // above and below the text, so they must show the background, too.
@@ -313,7 +378,7 @@ void Tesseract::font_style_pass(PAGE_RES *page_res) {
       pixInvert(word_pix, word_pix);
     }
     TextShape shape;
-    if (!MeasureTextShape(word_pix, &shape)) {
+    if (!MeasureTurnedText(word_pix, upright_turns, &shape)) {
       word_pix.destroy();
       continue;
     }
@@ -338,7 +403,8 @@ void Tesseract::font_style_pass(PAGE_RES *page_res) {
       }
       if (erased) {
         TextShape stem_shape;
-        shape.has_slant = MeasureTextShape(word_pix, &stem_shape) && stem_shape.has_slant;
+        shape.has_slant =
+            MeasureTurnedText(word_pix, upright_turns, &stem_shape) && stem_shape.has_slant;
         shape.slant = stem_shape.slant;
       }
     }
diff --git a/unittest/fontstyle_test.cc b/unittest/fontstyle_test.cc
index 8c4483fb..d59b8456 100644
--- a/unittest/fontstyle_test.cc
+++ b/unittest/fontstyle_test.cc
@@ -375,6 +375,41 @@ TEST_F(FontStyleApiTest, FindsItalicAndBoldLines) {
   EXPECT_EQ(count("<strong>"), num_bold_line_words);
 }

+// The words of a page which lies on its side must get the same attributes as
+// those of the upright page.
+TEST_F(FontStyleApiTest, FindsStylesInRotatedPage) {
+  const int kItalicLine = 3;
+  const int kBoldLine = 5;
+  ASSERT_TRUE(api_.SetVariable("lstm_font_attributes", "1"));
+  std::vector<StyledWord> upright = Recognize();
+  ASSERT_EQ(line_boxes_.size(), 8u);
+  ChangeLine(kItalicLine, [](Image line) {
+    return Image(pixHShear(nullptr, line, pixGetHeight(line) / 2, std::atan(0.25f),
+                           L_BRING_IN_WHITE));
+  });
+  ChangeLine(kBoldLine, [](Image line) { return Image(pixDilateBrick(nullptr, line, 3, 3)); });
+  upright = Recognize();
+  // The layout analysis finds the vertical text lines of a page which is
+  // turned clockwise by a quarter without a detection of the orientation.
+  Image turned = pixRotateOrth(pix_, 1);
+  ASSERT_TRUE(turned != nullptr);
+  pix_.destroy();
+  pix_ = turned;
+  const std::vector<StyledWord> rotated = Recognize();
+  ASSERT_EQ(rotated.size(), upright.size());
+  int num_italic = 0;
+  int num_bold = 0;
+  for (size_t i = 0; i < rotated.size(); ++i) {
+    EXPECT_EQ(rotated[i].text, upright[i].text);
+    EXPECT_EQ(rotated[i].italic, upright[i].italic) << rotated[i].text;
+    EXPECT_EQ(rotated[i].bold, upright[i].bold) << rotated[i].text;
+    num_italic += rotated[i].italic;
+    num_bold += rotated[i].bold;
+  }
+  EXPECT_GE(num_italic, 6);
+  EXPECT_GE(num_bold, 6);
+}
+
 // White text on black must give the same attributes as black text on white.
 TEST_F(FontStyleApiTest, FindsItalicInInverseText) {
   const int kItalicLine = 3;