Use more spans in font loading code. Fix more nits in the file along the way. Change-Id: I9b4792a2fe082574065432de32d711201dd508cd Reviewed-on: https://pdfium-review.googlesource.com/c/47230 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp index 3e1067d..ca4d6f7 100644 --- a/fpdfsdk/fpdf_edittext.cpp +++ b/fpdfsdk/fpdf_edittext.cpp
@@ -60,8 +60,7 @@ CPDF_Dictionary* LoadFontDesc(CPDF_Document* pDoc, const ByteString& font_name, CFX_Font* pFont, - const uint8_t* data, - uint32_t size, + pdfium::span<const uint8_t> span, int font_type) { CPDF_Dictionary* pFontDesc = pDoc->NewIndirect<CPDF_Dictionary>(); pFontDesc->SetNewFor<CPDF_Name>("Type", "FontDescriptor"); @@ -95,11 +94,11 @@ pFontDesc->SetNewFor<CPDF_Number>("StemV", pFont->IsBold() ? 120 : 70); CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(); - pStream->SetData({data, size}); + pStream->SetData(span); // TODO(npm): Lengths for Type1 fonts. if (font_type == FPDF_FONT_TRUETYPE) { pStream->GetDict()->SetNewFor<CPDF_Number>("Length1", - static_cast<int>(size)); + static_cast<int>(span.size())); } ByteString fontFile = font_type == FPDF_FONT_TYPE1 ? "FontFile" : "FontFile2"; pFontDesc->SetFor(fontFile, pStream->MakeReference(pDoc)); @@ -261,12 +260,9 @@ return stream; } -const uint32_t kMaxSimpleFontChar = 0xFF; - CPDF_Font* LoadSimpleFont(CPDF_Document* pDoc, std::unique_ptr<CFX_Font> pFont, - const uint8_t* data, - uint32_t size, + pdfium::span<const uint8_t> span, int font_type) { CPDF_Dictionary* fontDict = pDoc->NewIndirect<CPDF_Dictionary>(); fontDict->SetNewFor<CPDF_Name>("Type", "Font"); @@ -279,6 +275,7 @@ uint32_t glyphIndex; uint32_t currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); + static constexpr uint32_t kMaxSimpleFontChar = 0xFF; if (currentChar > kMaxSimpleFontChar || glyphIndex == 0) return nullptr; fontDict->SetNewFor<CPDF_Number>("FirstChar", static_cast<int>(currentChar)); @@ -300,18 +297,15 @@ fontDict->SetNewFor<CPDF_Number>("LastChar", static_cast<int>(currentChar)); fontDict->SetFor("Widths", widthsArray->MakeReference(pDoc)); CPDF_Dictionary* pFontDesc = - LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); + LoadFontDesc(pDoc, name, pFont.get(), span, font_type); fontDict->SetFor("FontDescriptor", pFontDesc->MakeReference(pDoc)); return pDoc->LoadFont(fontDict); } -const uint32_t kMaxUnicode = 0x10FFFF; - CPDF_Font* LoadCompositeFont(CPDF_Document* pDoc, std::unique_ptr<CFX_Font> pFont, - const uint8_t* data, - uint32_t size, + pdfium::span<const uint8_t> span, int font_type) { CPDF_Dictionary* fontDict = pDoc->NewIndirect<CPDF_Dictionary>(); fontDict->SetNewFor<CPDF_Name>("Type", "Font"); @@ -341,11 +335,12 @@ pCIDFont->SetFor("CIDSystemInfo", pCIDSystemInfo->MakeReference(pDoc)); CPDF_Dictionary* pFontDesc = - LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); + LoadFontDesc(pDoc, name, pFont.get(), span, font_type); pCIDFont->SetFor("FontDescriptor", pFontDesc->MakeReference(pDoc)); uint32_t glyphIndex; uint32_t currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); + static constexpr uint32_t kMaxUnicode = 0x10FFFF; // If it doesn't have a single char, just fail if (glyphIndex == 0 || currentChar > kMaxUnicode) return nullptr; @@ -478,17 +473,18 @@ return nullptr; } + auto span = pdfium::make_span(data, size); auto pFont = pdfium::MakeUnique<CFX_Font>(); // 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({data, size})) + if (!pFont->LoadEmbedded(span)) return nullptr; return FPDFFontFromCPDFFont( - cid ? LoadCompositeFont(pDoc, std::move(pFont), data, size, font_type) - : LoadSimpleFont(pDoc, std::move(pFont), data, size, font_type)); + cid ? LoadCompositeFont(pDoc, std::move(pFont), span, font_type) + : LoadSimpleFont(pDoc, std::move(pFont), span, font_type)); } FPDF_EXPORT FPDF_FONT FPDF_CALLCONV @@ -604,8 +600,5 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetTextRenderMode(FPDF_PAGEOBJECT text) { CPDF_TextObject* pTextObj = CPDFTextObjectFromFPDFPageObject(text); - if (!pTextObj) - return -1; - - return static_cast<int>(pTextObj->m_TextState.GetTextMode()); + return pTextObj ? static_cast<int>(pTextObj->m_TextState.GetTextMode()) : -1; }