[SkiaPaths] Vertical texts were drawn in reversed direction.

In ChS_simple.pdf, the Chinese characters are displayed vertically and
when characters are displayed in order, the sentence direction was
going upwards instead of downwards. This is because when the main font
indicates vertical display ("Identity-V"), its descendant font should
inherit the vertical setup since there is only one way to display the
text object. LoadSubstFont() is the only method to sync the descendant
font's |m_bVertical| with the main font, but it was skipped in
CPDF_CIDFont::Load() due to the main font is embedded.

To fix this issue, need to add a step to sync the descendant font's
|m_bVertical| value with the main font if it indicates vertical
display (not default value), regardless whether the main font is
embedded or not. Also this CL calls LoadFontDescriptor() after
|m_pCMap| is initialized to make sure IsVertWriting() returns the
correct value.

Bug: pdfium:1376
Change-Id: I7e935b59745b063b82ec709afd847c6bc289937d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/58611
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 6a9e4fc..e4128ea 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -345,9 +345,6 @@
       !IsEmbedded()) {
     m_bAdobeCourierStd = true;
   }
-  const CPDF_Dictionary* pFontDesc = pCIDFontDict->GetDictFor("FontDescriptor");
-  if (pFontDesc)
-    LoadFontDescriptor(pFontDesc);
 
   CPDF_Object* pEncoding = m_pFontDict->GetDirectObjectFor("Encoding");
   if (!pEncoding)
@@ -368,6 +365,10 @@
     return false;
   }
 
+  const CPDF_Dictionary* pFontDesc = pCIDFontDict->GetDictFor("FontDescriptor");
+  if (pFontDesc)
+    LoadFontDescriptor(pFontDesc);
+
   m_Charset = m_pCMap->GetCharset();
   if (m_Charset == CIDSET_UNKNOWN) {
     const CPDF_Dictionary* pCIDInfo = pCIDFontDict->GetDictFor("CIDSystemInfo");
@@ -816,18 +817,17 @@
 
 void CPDF_CIDFont::LoadGB2312() {
   m_BaseFontName = m_pFontDict->GetStringFor("BaseFont");
-  const CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictFor("FontDescriptor");
-  if (pFontDesc)
-    LoadFontDescriptor(pFontDesc);
-
   m_Charset = CIDSET_GB1;
 
   CPDF_CMapManager* manager = CPDF_FontGlobals::GetInstance()->GetCMapManager();
   m_pCMap = manager->GetPredefinedCMap("GBK-EUC-H");
   m_pCID2UnicodeMap = manager->GetCID2UnicodeMap(m_Charset);
+  const CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictFor("FontDescriptor");
+  if (pFontDesc)
+    LoadFontDescriptor(pFontDesc);
+
   if (!IsEmbedded())
     LoadSubstFont();
-
   CheckFontMetrics();
   m_bAnsiWidthsFixed = true;
 }
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 4b1ddcc..089d61c 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -215,7 +215,7 @@
   if (!m_pFontFile)
     return;
 
-  if (!m_Font.LoadEmbedded(m_pFontFile->GetSpan())) {
+  if (!m_Font.LoadEmbedded(m_pFontFile->GetSpan(), IsVertWriting())) {
     pData->MaybePurgeFontFileStreamAcc(m_pFontFile->GetStream()->AsStream());
     m_pFontFile = nullptr;
   }
diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp
index fc33e209..1989c2e 100644
--- a/core/fxge/cfx_font.cpp
+++ b/core/fxge/cfx_font.cpp
@@ -377,7 +377,10 @@
   return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face->GetRec()), horiAdvance);
 }
 
-bool CFX_Font::LoadEmbedded(pdfium::span<const uint8_t> src_span) {
+bool CFX_Font::LoadEmbedded(pdfium::span<const uint8_t> src_span,
+                            bool bForceAsVertical) {
+  if (bForceAsVertical)
+    m_bVertical = true;
   m_pFontDataAllocation =
       std::vector<uint8_t>(src_span.begin(), src_span.end());
   m_Face = CFX_GEModule::Get()->GetFontMgr()->NewFixedFace(
diff --git a/core/fxge/cfx_font.h b/core/fxge/cfx_font.h
index c28ce5e..57cc8f8 100644
--- a/core/fxge/cfx_font.h
+++ b/core/fxge/cfx_font.h
@@ -48,7 +48,8 @@
                  int CharsetCP,
                  bool bVertical);
 
-  bool LoadEmbedded(pdfium::span<const uint8_t> src_span);
+  bool LoadEmbedded(pdfium::span<const uint8_t> src_span,
+                    bool bForceAsVertical);
   RetainPtr<CFX_Face> GetFace() const { return m_Face; }
   FXFT_FaceRec* GetFaceRec() const {
     return m_Face ? m_Face->GetRec() : nullptr;
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 9ed5601..5d19781 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -490,7 +490,7 @@
   // TODO(npm): Maybe use FT_Get_X11_Font_Format to check format? Otherwise, we
   // are allowing giving any font that can be loaded on freetype and setting it
   // as any font type.
-  if (!pFont->LoadEmbedded(span))
+  if (!pFont->LoadEmbedded(span, false))
     return nullptr;
 
   // Caller takes ownership.