Bound FPDFText_GetLooseCharBox() top/bottom by /FontBBox

Validate a font's /Ascent and /Descent values against the /FontBBox.
Code is AI-generated.

Bug: 502757960
Change-Id: I2f022a44e03cd24731f62626ec062f02508f4d5c
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/146371
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 e1b668a..7aa6973 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -309,8 +309,13 @@
       return char_box;
     }
 
-    const int ascent = font->GetTypeAscent();
-    const int descent = font->GetTypeDescent();
+    int ascent = font->GetTypeAscent();
+    int descent = font->GetTypeDescent();
+    const FX_RECT& font_bbox = font->GetFontBBox();
+    if (font_bbox.top > font_bbox.bottom) {
+      ascent = std::min(ascent, font_bbox.top);
+      descent = std::max(descent, font_bbox.bottom);
+    }
     if (ascent != descent) {
       // Compute `left` and `right` based on the individual character's `width`.
       float width = text_object->GetCharWidth(charinfo.char_code());
diff --git a/fpdfsdk/fpdf_text_embeddertest.cpp b/fpdfsdk/fpdf_text_embeddertest.cpp
index 97bb962..ee6326c 100644
--- a/fpdfsdk/fpdf_text_embeddertest.cpp
+++ b/fpdfsdk/fpdf_text_embeddertest.cpp
@@ -2149,15 +2149,14 @@
   static constexpr double kExpectedChar0Height = 8.256;
   static constexpr double kExpectedChar0Top = 38.256;
   static constexpr float kExpectedLooseChar0Width = 6.672f;
-  // TODO(crbug.com/502757960): Fix the extremely large values.
-  static constexpr float kExpectedLooseChar0Height = 720002.688f;
-  static constexpr float kExpectedLooseChar0Top = 720030.0f;
+  static constexpr float kExpectedLooseChar0Height = 14.676f;
+  static constexpr float kExpectedLooseChar0Top = 42.0f;
 
   static constexpr double kExpectedChar1Width = 8.184;
   static constexpr double kExpectedChar1Height = 7.860;
   static constexpr double kExpectedChar1Top = 37.860;
   static constexpr float kExpectedLooseChar1Width = 8.664f;
-  static constexpr float kExpectedLooseChar1Height = 600010.688f;
+  static constexpr float kExpectedLooseChar1Height = 14.376f;
   static constexpr float kExpectedLooseChar1Top = 40.692f;
 
   ASSERT_TRUE(OpenDocument("text_large_ascent_descent.pdf"));