xref: /aosp_15_r20/external/pdfium/core/fpdfapi/font/cfx_stockfontarray.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
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 "core/fpdfapi/font/cfx_stockfontarray.h"
8 
9 #include <iterator>
10 #include <utility>
11 
12 #include "core/fpdfapi/font/cpdf_font.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "third_party/base/check_op.h"
15 
16 CFX_StockFontArray::CFX_StockFontArray() = default;
17 
~CFX_StockFontArray()18 CFX_StockFontArray::~CFX_StockFontArray() {
19   for (size_t i = 0; i < std::size(m_StockFonts); ++i) {
20     if (m_StockFonts[i]) {
21       // Ensure m_StockFonts[i]'s dict is cleared before releasing what
22       // may be the last reference to it.
23       RetainPtr<CPDF_Dictionary> destroy =
24           m_StockFonts[i]->GetMutableFontDict();
25       m_StockFonts[i]->ClearFontDict();
26     }
27   }
28 }
29 
GetFont(CFX_FontMapper::StandardFont index) const30 RetainPtr<CPDF_Font> CFX_StockFontArray::GetFont(
31     CFX_FontMapper::StandardFont index) const {
32   CHECK_LT(index, std::size(m_StockFonts));
33   return m_StockFonts[index];
34 }
35 
SetFont(CFX_FontMapper::StandardFont index,RetainPtr<CPDF_Font> pFont)36 void CFX_StockFontArray::SetFont(CFX_FontMapper::StandardFont index,
37                                  RetainPtr<CPDF_Font> pFont) {
38   if (index < std::size(m_StockFonts))
39     m_StockFonts[index] = std::move(pFont);
40 }
41