Add FPDFText_LoadStandardFont to public API

Bug: pdfium:978
Change-Id: I0dcffdfd1b19b83e5234da7791cb3f3e52cc257b
Reviewed-on: https://pdfium-review.googlesource.com/35110
Commit-Queue: Nicolás Peña Moreno <npm@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index 3d2e090..0a119b9 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -931,6 +931,7 @@
   FPDF_ClosePage(page);
 }
 
+// Tests adding text from standard font using FPDFPageObj_NewTextObj.
 TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
   // Start with a blank page
   FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
@@ -1001,6 +1002,73 @@
   FPDF_ClosePage(page);
 }
 
+// Tests adding text from standard font using FPDFText_LoadStandardFont.
+TEST_F(FPDFEditEmbeddertest, AddStandardFontText2) {
+  // Start with a blank page
+  ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
+
+  // Load a standard font.
+  FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
+  ASSERT_TRUE(font);
+
+  // Add some text to the page.
+  FPDF_PAGEOBJECT text_object =
+      FPDFPageObj_CreateTextObj(document(), font, 12.0f);
+  EXPECT_TRUE(text_object);
+  std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
+      GetFPDFWideString(L"I'm at the bottom of the page");
+  EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
+  FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
+  FPDFPage_InsertObject(page.get(), text_object);
+  ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page.get(), nullptr, 0);
+#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
+  const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
+#else
+  const char md5[] = "eacaa24573b8ce997b3882595f096f00";
+#endif
+  CompareBitmap(page_bitmap.get(), 612, 792, md5);
+}
+
+TEST_F(FPDFEditEmbeddertest, LoadStandardFonts) {
+  CreateNewDocument();
+  const char* standard_font_names[] = {"Arial",
+                                       "Arial-Bold",
+                                       "Arial-BoldItalic",
+                                       "Arial-Italic",
+                                       "Courier",
+                                       "Courier-BoldOblique",
+                                       "Courier-Oblique",
+                                       "Courier-Bold",
+                                       "CourierNew",
+                                       "CourierNew-Bold",
+                                       "CourierNew-BoldItalic",
+                                       "CourierNew-Italic",
+                                       "Helvetica",
+                                       "Helvetica-Bold",
+                                       "Helvetica-BoldOblique",
+                                       "Helvetica-Oblique",
+                                       "Symbol",
+                                       "TimesNewRoman",
+                                       "TimesNewRoman-Bold",
+                                       "TimesNewRoman-BoldItalic",
+                                       "TimesNewRoman-Italic",
+                                       "ZapfDingbats"};
+  for (auto* const font_name : standard_font_names) {
+    FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
+    EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
+  }
+  const char* not_standard_font_names[] = {
+      "Abcdefg",      "ArialB",    "Arial-Style",
+      "Font Name",    "FontArial", "NotAStandardFontName",
+      "TestFontName", "Quack",     "Symbol-Italic",
+      "Zapf"};
+  for (auto* const font_name : not_standard_font_names) {
+    FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
+    EXPECT_FALSE(font) << font_name
+                       << " should not be considered a standard font.";
+  }
+}
+
 TEST_F(FPDFEditEmbeddertest, GraphicsData) {
   // New page
   ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 2ead789..8186d8d 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -462,6 +462,16 @@
           : LoadSimpleFont(pDoc, std::move(pFont), data, size, font_type));
 }
 
+FPDF_EXPORT FPDF_FONT FPDF_CALLCONV
+FPDFText_LoadStandardFont(FPDF_DOCUMENT document, FPDF_BYTESTRING font) {
+  CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
+  if (!pDoc)
+    return nullptr;
+
+  return FPDFFontFromCPDFFont(
+      CPDF_Font::GetStockFont(pDoc, ByteStringView(font)));
+}
+
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
 FPDFText_SetFillColor(FPDF_PAGEOBJECT text_object,
                       unsigned int R,
diff --git a/fpdfsdk/fpdf_view_c_api_test.c b/fpdfsdk/fpdf_view_c_api_test.c
index 34ac551..543a7d1 100644
--- a/fpdfsdk/fpdf_view_c_api_test.c
+++ b/fpdfsdk/fpdf_view_c_api_test.c
@@ -196,6 +196,7 @@
     CHK(FPDFPath_SetStrokeColor);
     CHK(FPDFPath_SetStrokeWidth);
     CHK(FPDFText_LoadFont);
+    CHK(FPDFText_LoadStandardFont);
     CHK(FPDFText_SetFillColor);
     CHK(FPDFText_SetText);
     CHK(FPDF_CreateNewDocument);
diff --git a/public/fpdf_edit.h b/public/fpdf_edit.h
index 338b42b..d3d7c4b 100644
--- a/public/fpdf_edit.h
+++ b/public/fpdf_edit.h
@@ -1022,7 +1022,7 @@
 // type.
 // cid        - a boolean specifying if the font is a CID font or not.
 //
-// The loaded font can be closed using FPDF_Font_Close.
+// The loaded font can be closed using FPDFFont_Close.
 //
 // Returns NULL on failure
 FPDF_EXPORT FPDF_FONT FPDF_CALLCONV FPDFText_LoadFont(FPDF_DOCUMENT document,
@@ -1031,6 +1031,21 @@
                                                       int font_type,
                                                       FPDF_BOOL cid);
 
+// Experimental API.
+// Loads one of the standard 14 fonts per PDF spec 1.7 page 416. The preferred
+// way of using font style is using a dash to separate the name from the style,
+// for example 'Helvetica-BoldItalic'.
+//
+// document   - handle to the document.
+// font       - string containing the font name, without spaces.
+//
+// The loaded font should NOT be closed using FPDFFont_Close. It will be
+// unloaded during the document's destruction.
+//
+// Returns NULL on failure.
+FPDF_EXPORT FPDF_FONT FPDF_CALLCONV
+FPDFText_LoadStandardFont(FPDF_DOCUMENT document, FPDF_BYTESTRING font);
+
 // DEPRECATED as of May 2018. This API will be removed in the future. Please
 // use FPDFPageObj_SetFillColor instead.
 //