Introduce CFX_DIBitmap::PopulateFromSpan() and PopulateFrom1bppSpan()

Remove pixel packing from CFX_Face and put it into the bitmap code.

Change-Id: I2dd76700855de1e369f62583b3442575d39be6a2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/148653
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/cfx_face.cpp b/core/fxge/cfx_face.cpp
index c0aacd7..04f5d8a 100644
--- a/core/fxge/cfx_face.cpp
+++ b/core/fxge/cfx_face.cpp
@@ -780,30 +780,16 @@
   auto pGlyphBitmap = std::make_unique<CFX_GlyphBitmap>(
       glyph->bitmap_left, glyph->bitmap_top, new_bitmap);
 
-  const uint32_t dest_pitch = new_bitmap->GetPitch();
   const uint32_t src_pitch = abs(ft_bitmap.pitch);
-  pdfium::span<uint8_t> dest_span = new_bitmap->GetWritableBuffer();
   pdfium::span<const uint8_t> src_span =
       UNSAFE_TODO(pdfium::span<const uint8_t>(ft_bitmap.buffer,
                                               src_pitch * ft_bitmap.rows));
 
   if (anti_alias != FontAntiAliasingMode::kMono &&
       ft_bitmap.pixel_mode == FT_PIXEL_MODE_MONO) {
-    for (unsigned int i = 0; i < ft_bitmap.rows; i++) {
-      for (unsigned int n = 0; n < ft_bitmap.width; n++) {
-        dest_span[n] = (src_span[n / 8] & (0x80 >> (n % 8))) ? 255 : 0;
-      }
-      dest_span = dest_span.subspan(dest_pitch);
-      src_span = src_span.subspan(src_pitch);
-    }
+    new_bitmap->Populate8bbpMaskFrom1bppSpan(src_span, src_pitch);
   } else {
-    std::ranges::fill(dest_span, 0);
-    const uint32_t rowbytes = std::min(src_pitch, dest_pitch);
-    for (unsigned int row = 0; row < ft_bitmap.rows; row++) {
-      fxcrt::spancpy(dest_span, src_span.first(rowbytes));
-      dest_span = dest_span.subspan(dest_pitch);
-      src_span = src_span.subspan(src_pitch);
-    }
+    new_bitmap->PopulateFromSpan(src_span, src_pitch);
   }
 #if defined(PDF_ENABLE_SKIA_TYPEFACE_CHECKS)
   // TODO(https://crbug.com/42271123): Compute equivalent result via Skia or
diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp
index 3b88416..2abe0e5 100644
--- a/core/fxge/dib/cfx_dibitmap.cpp
+++ b/core/fxge/dib/cfx_dibitmap.cpp
@@ -890,6 +890,40 @@
 }
 #endif  // BUILDFLAG(IS_WIN) || defined(PDF_USE_AGG)
 
+void CFX_DIBitmap::Populate8bbpMaskFrom1bppSpan(
+    pdfium::span<const uint8_t> src_span,
+    uint32_t src_pitch) {
+  CHECK_EQ(GetFormat(), FXDIB_Format::k8bppMask);
+
+  const int width = GetWidth();
+  const int rows = GetHeight();
+  const uint32_t dest_pitch = GetPitch();
+  pdfium::span<uint8_t> dest_span = GetWritableBuffer();
+
+  for (int i = 0; i < rows; i++) {
+    for (int n = 0; n < width; n++) {
+      dest_span[n] = (src_span[n / 8] & (0x80 >> (n % 8))) ? 255 : 0;
+    }
+    dest_span = dest_span.subspan(dest_pitch);
+    src_span = src_span.subspan(src_pitch);
+  }
+}
+
+void CFX_DIBitmap::PopulateFromSpan(pdfium::span<const uint8_t> src_span,
+                                    uint32_t src_pitch) {
+  pdfium::span<uint8_t> dest_span = GetWritableBuffer();
+  std::ranges::fill(dest_span, 0);
+  const int rows = GetHeight();
+  const uint32_t dest_pitch = GetPitch();
+  const uint32_t rowbytes = std::min(src_pitch, dest_pitch);
+
+  for (int row = 0; row < rows; row++) {
+    fxcrt::spancpy(dest_span, src_span.first(rowbytes));
+    dest_span = dest_span.subspan(dest_pitch);
+    src_span = src_span.subspan(src_pitch);
+  }
+}
+
 bool CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format) {
   static constexpr FXDIB_Format kAllowedDestFormats[] = {
       FXDIB_Format::k8bppMask,
diff --git a/core/fxge/dib/cfx_dibitmap.h b/core/fxge/dib/cfx_dibitmap.h
index c8fb487..0c98f1c 100644
--- a/core/fxge/dib/cfx_dibitmap.h
+++ b/core/fxge/dib/cfx_dibitmap.h
@@ -91,6 +91,10 @@
   }
 
   bool ConvertFormat(FXDIB_Format format);
+  void Populate8bbpMaskFrom1bppSpan(pdfium::span<const uint8_t> src_span,
+                                    uint32_t src_pitch);
+  void PopulateFromSpan(pdfium::span<const uint8_t> src_span,
+                        uint32_t src_pitch);
   void Clear(uint32_t color);
 
 #if defined(PDF_USE_SKIA)