Make sure loose char box is at least as big as regular char box

Fix the issue with FPDFText_GetLooseCharBox() and diacritics by taking a
union of the 2 char boxes.

Bug: 395640955
Change-Id: I183cfb78dee41eeb5145537d286e12bedd18a80b
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/128730
Reviewed-by: Thomas Sepez <tsepez@google.com>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index 60eb513..4e63f15 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -285,7 +285,9 @@
       float right = left + font_size;
       float top = charinfo.origin().y + offsety;
       float bottom = top + height;
-      return CFX_FloatRect(left, bottom, right, top);
+      CFX_FloatRect char_box(left, bottom, right, top);
+      char_box.Union(charinfo.char_box());
+      return char_box;
     }
 
     int ascent = charinfo.text_object()->GetFont()->GetTypeAscent();
@@ -302,7 +304,9 @@
       float bottom = original_origin.y + descent * font_scale;
       float top = original_origin.y + ascent * font_scale;
       CFX_FloatRect char_box(left, bottom, right, top);
-      return charinfo.matrix().TransformRect(char_box);
+      char_box = charinfo.matrix().TransformRect(char_box);
+      char_box.Union(charinfo.char_box());
+      return char_box;
     }
   }
 
diff --git a/fpdfsdk/fpdf_text_embeddertest.cpp b/fpdfsdk/fpdf_text_embeddertest.cpp
index f37f562..b54bab7 100644
--- a/fpdfsdk/fpdf_text_embeddertest.cpp
+++ b/fpdfsdk/fpdf_text_embeddertest.cpp
@@ -1979,8 +1979,8 @@
   FS_RECTF rect;
   ASSERT_TRUE(FPDFText_GetLooseCharBox(text_page.get(), 0, &rect));
   EXPECT_NEAR(7.824f, rect.right - rect.left, 0.001f);
-  EXPECT_NEAR(12.0f, rect.top - rect.bottom, 0.001f);
-  EXPECT_NEAR(749.25f, rect.top, 0.001f);
+  EXPECT_NEAR(12.988f, rect.top - rect.bottom, 0.001f);
+  EXPECT_NEAR(750.238f, rect.top, 0.001f);
 
   EXPECT_EQ(u'Ă', FPDFText_GetUnicode(text_page.get(), 2));
 
@@ -1992,8 +1992,8 @@
 
   ASSERT_TRUE(FPDFText_GetLooseCharBox(text_page.get(), 2, &rect));
   EXPECT_NEAR(7.824f, rect.right - rect.left, 0.001f);
-  EXPECT_NEAR(12.0f, rect.top - rect.bottom, 0.001f);
-  EXPECT_NEAR(749.250f, rect.top, 0.001f);
+  EXPECT_NEAR(13.24f, rect.top - rect.bottom, 0.001f);
+  EXPECT_NEAR(750.49f, rect.top, 0.001f);
 }
 
 TEST_F(FPDFTextEmbedderTest, SmallType3Glyph) {