Home | History | Annotate | Download | only in font
      1 // Copyright 2017 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include "xfa/fgas/font/cfgas_defaultfontmanager.h"
      8 
      9 #include "xfa/fgas/font/fgas_fontutils.h"
     10 
     11 CFGAS_DefaultFontManager::CFGAS_DefaultFontManager() {}
     12 
     13 CFGAS_DefaultFontManager::~CFGAS_DefaultFontManager() {}
     14 
     15 RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetFont(
     16     CFGAS_FontMgr* pFontMgr,
     17     const WideStringView& wsFontFamily,
     18     uint32_t dwFontStyles) {
     19   WideString wsFontName(wsFontFamily);
     20   RetainPtr<CFGAS_GEFont> pFont =
     21       pFontMgr->LoadFont(wsFontName.c_str(), dwFontStyles, 0xFFFF);
     22   if (!pFont) {
     23     const FGAS_FontInfo* pCurFont =
     24         FGAS_FontInfoByFontName(wsFontName.AsStringView());
     25     if (pCurFont && pCurFont->pReplaceFont) {
     26       uint32_t dwStyle = 0;
     27       // TODO(dsinclair): Why doesn't this check the other flags?
     28       if (FontStyleIsBold(dwFontStyles))
     29         dwStyle |= FXFONT_BOLD;
     30       if (FontStyleIsItalic(dwFontStyles))
     31         dwStyle |= FXFONT_ITALIC;
     32 
     33       const wchar_t* pReplace = pCurFont->pReplaceFont;
     34       int32_t iLength = wcslen(pReplace);
     35       while (iLength > 0) {
     36         const wchar_t* pNameText = pReplace;
     37         while (*pNameText != L',' && iLength > 0) {
     38           pNameText++;
     39           iLength--;
     40         }
     41         WideString wsReplace = WideString(pReplace, pNameText - pReplace);
     42         pFont = pFontMgr->LoadFont(wsReplace.c_str(), dwStyle, 0xFFFF);
     43         if (pFont)
     44           break;
     45 
     46         iLength--;
     47         pNameText++;
     48         pReplace = pNameText;
     49       }
     50     }
     51   }
     52   if (pFont)
     53     m_CacheFonts.push_back(pFont);
     54   return pFont;
     55 }
     56 
     57 RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetDefaultFont(
     58     CFGAS_FontMgr* pFontMgr,
     59     const WideStringView& wsFontFamily,
     60     uint32_t dwFontStyles) {
     61   RetainPtr<CFGAS_GEFont> pFont =
     62       pFontMgr->LoadFont(L"Arial Narrow", dwFontStyles, 0xFFFF);
     63   if (!pFont) {
     64     pFont = pFontMgr->LoadFont(static_cast<const wchar_t*>(nullptr),
     65                                dwFontStyles, 0xFFFF);
     66   }
     67   if (pFont)
     68     m_CacheFonts.push_back(pFont);
     69   return pFont;
     70 }
     71