Allow Grayscale Colorspaces for Bug 42271133

- Update FPDFSaveEmbedderTest::42271133()
- Add CPDF_Color::IsColorSpaceGray()
- Update CPDF_ContentGenerator::WriteColorToStream() to check for
grayscale color space

Note: Regression test added in https://pdfium-review.googlesource.com/c/pdfium/+/118890

Change-Id: Ia1e33d568a05eb9a7c32243670263257d0c59930
Bug: 42271133
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/115330
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Thomas Sepez <tsepez@google.com>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index cf30dac..f0c6f5e 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -55,7 +55,7 @@
 
 // Returns whether it wrote to `buf` or not.
 bool WriteColorToStream(fxcrt::ostringstream& buf, const CPDF_Color* color) {
-  if (!color || !color->IsColorSpaceRGB()) {
+  if (!color || (!color->IsColorSpaceRGB() && !color->IsColorSpaceGray())) {
     return false;
   }
 
diff --git a/core/fpdfapi/page/cpdf_color.cpp b/core/fpdfapi/page/cpdf_color.cpp
index 83a60b1..17988cc 100644
--- a/core/fpdfapi/page/cpdf_color.cpp
+++ b/core/fpdfapi/page/cpdf_color.cpp
@@ -79,6 +79,11 @@
          CPDF_ColorSpace::GetStockCS(CPDF_ColorSpace::Family::kDeviceRGB);
 }
 
+bool CPDF_Color::IsColorSpaceGray() const {
+  return m_pCS ==
+         CPDF_ColorSpace::GetStockCS(CPDF_ColorSpace::Family::kDeviceGray);
+}
+
 std::optional<FX_COLORREF> CPDF_Color::GetColorRef() const {
   if (IsPatternInternal()) {
     if (m_pValue) {
diff --git a/core/fpdfapi/page/cpdf_color.h b/core/fpdfapi/page/cpdf_color.h
index 875417f..d4f36f5 100644
--- a/core/fpdfapi/page/cpdf_color.h
+++ b/core/fpdfapi/page/cpdf_color.h
@@ -39,6 +39,7 @@
 
   uint32_t ComponentCount() const;
   bool IsColorSpaceRGB() const;
+  bool IsColorSpaceGray() const;
   std::optional<FX_COLORREF> GetColorRef() const;
 
   // Should only be called if IsPattern() returns true.
diff --git a/fpdfsdk/fpdf_save_embeddertest.cpp b/fpdfsdk/fpdf_save_embeddertest.cpp
index f221d10..4f33c2a 100644
--- a/fpdfsdk/fpdf_save_embeddertest.cpp
+++ b/fpdfsdk/fpdf_save_embeddertest.cpp
@@ -123,10 +123,9 @@
   unsigned int b;
   unsigned int a;
   ASSERT_TRUE(FPDFPageObj_GetFillColor(path_obj, &r, &g, &b, &a));
-  // TODO(crbug.com/42271133): Change the assertion to EXPECT_NE().
-  EXPECT_EQ(0u, r);
-  EXPECT_EQ(0u, g);
-  EXPECT_EQ(0u, b);
+  EXPECT_EQ(180u, r);
+  EXPECT_EQ(180u, g);
+  EXPECT_EQ(180u, b);
 
   CloseSavedPage(saved_page);
   CloseSavedDocument();