// Copyright 2017 PDFium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include #include #include "core/fpdfapi/cpdf_modulemgr.h" #include "core/fpdfapi/font/cpdf_font.h" #include "core/fpdfapi/font/cpdf_type1font.h" #include "core/fpdfapi/page/cpdf_textobject.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/parser/cpdf_name.h" #include "core/fpdfapi/parser/cpdf_number.h" #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fxge/cfx_fontmgr.h" #include "core/fxge/fx_font.h" #include "fpdfsdk/fsdk_define.h" #include "public/fpdf_edit.h" namespace { CPDF_Dictionary* LoadFontDesc(CPDF_Document* pDoc, const CFX_ByteString& font_name, CFX_Font* pFont, const uint8_t* data, uint32_t size, int font_type) { CPDF_Dictionary* fontDesc = pDoc->NewIndirect(); fontDesc->SetNewFor("Type", "FontDescriptor"); fontDesc->SetNewFor("FontName", font_name); int flags = 0; if (FXFT_Is_Face_fixedwidth(pFont->GetFace())) flags |= FXFONT_FIXED_PITCH; if (font_name.Find("Serif") > -1) flags |= FXFONT_SERIF; if (FXFT_Is_Face_Italic(pFont->GetFace())) flags |= FXFONT_ITALIC; if (FXFT_Is_Face_Bold(pFont->GetFace())) flags |= FXFONT_BOLD; // TODO(npm): How do I know if a font is symbolic, script, allcap, smallcap flags |= FXFONT_NONSYMBOLIC; fontDesc->SetNewFor("Flags", flags); FX_RECT bbox; pFont->GetBBox(bbox); auto pBBox = pdfium::MakeUnique(); pBBox->AddNew(bbox.left); pBBox->AddNew(bbox.bottom); pBBox->AddNew(bbox.right); pBBox->AddNew(bbox.top); fontDesc->SetFor("FontBBox", std::move(pBBox)); // TODO(npm): calculate italic angle correctly fontDesc->SetNewFor("ItalicAngle", pFont->IsItalic() ? -12 : 0); fontDesc->SetNewFor("Ascent", pFont->GetAscent()); fontDesc->SetNewFor("Descent", pFont->GetDescent()); // TODO(npm): calculate the capheight, stemV correctly fontDesc->SetNewFor("CapHeight", pFont->GetAscent()); fontDesc->SetNewFor("StemV", pFont->IsBold() ? 120 : 70); CPDF_Stream* pStream = pDoc->NewIndirect(); pStream->SetData(data, size); CFX_ByteString fontFile = font_type == FPDF_FONT_TYPE1 ? "FontFile" : "FontFile2"; fontDesc->SetNewFor(fontFile, pDoc, pStream->GetObjNum()); return fontDesc; } void* LoadSimpleFont(CPDF_Document* pDoc, std::unique_ptr pFont, const uint8_t* data, uint32_t size, int font_type) { CPDF_Dictionary* fontDict = pDoc->NewIndirect(); fontDict->SetNewFor("Type", "Font"); fontDict->SetNewFor( "Subtype", font_type == FPDF_FONT_TYPE1 ? "Type1" : "TrueType"); CFX_ByteString name = pFont->GetFaceName(); if (name.IsEmpty()) name = "Unnamed"; fontDict->SetNewFor("BaseFont", name); uint32_t glyphIndex; int currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); fontDict->SetNewFor("FirstChar", currentChar); CPDF_Array* widthsArray = pDoc->NewIndirect(); while (true) { int width = pFont->GetGlyphWidth(glyphIndex); widthsArray->AddNew(width); int nextChar = FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); // Simple fonts have 1-byte charcodes only. if (nextChar > 0xff || glyphIndex == 0) break; for (int i = currentChar + 1; i < nextChar; i++) widthsArray->AddNew(0); currentChar = nextChar; } fontDict->SetNewFor("LastChar", currentChar); fontDict->SetNewFor("Widths", pDoc, widthsArray->GetObjNum()); CPDF_Dictionary* fontDesc = LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); fontDict->SetNewFor("FontDescriptor", pDoc, fontDesc->GetObjNum()); return pDoc->LoadFont(fontDict); } void* LoadCompositeFont(CPDF_Document* pDoc, std::unique_ptr pFont, const uint8_t* data, uint32_t size, int font_type) { CPDF_Dictionary* fontDict = pDoc->NewIndirect(); fontDict->SetNewFor("Type", "Font"); fontDict->SetNewFor("Subtype", "Type0"); // TODO(npm): Get the correct encoding, if it's not identity. CFX_ByteString encoding = "Identity-H"; fontDict->SetNewFor("Encoding", encoding); CFX_ByteString name = pFont->GetFaceName(); if (name.IsEmpty()) name = "Unnamed"; fontDict->SetNewFor( "BaseFont", font_type == FPDF_FONT_TYPE1 ? name + "-" + encoding : name); CPDF_Dictionary* pCIDFont = pDoc->NewIndirect(); pCIDFont->SetNewFor("Type", "Font"); pCIDFont->SetNewFor("Subtype", font_type == FPDF_FONT_TYPE1 ? "CIDFontType0" : "CIDFontType2"); pCIDFont->SetNewFor("BaseFont", name); // TODO(npm): Maybe use FT_Get_CID_Registry_Ordering_Supplement to get the // CIDSystemInfo CPDF_Dictionary* pCIDSystemInfo = pDoc->NewIndirect(); pCIDSystemInfo->SetNewFor("Registry", "Adobe"); pCIDSystemInfo->SetNewFor("Ordering", "Identity"); pCIDSystemInfo->SetNewFor("Supplement", 0); pCIDFont->SetNewFor("CIDSystemInfo", pDoc, pCIDSystemInfo->GetObjNum()); CPDF_Dictionary* fontDesc = LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); pCIDFont->SetNewFor("FontDescriptor", pDoc, fontDesc->GetObjNum()); uint32_t glyphIndex; int currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); CPDF_Array* widthsArray = pDoc->NewIndirect(); while (true) { int width = pFont->GetGlyphWidth(glyphIndex); int nextChar = FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); if (glyphIndex == 0) { // Only one char left, use format c [w] auto oneW = pdfium::MakeUnique(); oneW->AddNew(width); widthsArray->AddNew(currentChar); widthsArray->Add(std::move(oneW)); break; } int nextWidth = pFont->GetGlyphWidth(glyphIndex); if (nextChar == currentChar + 1 && nextWidth == width) { // The array can have a group c_first c_last w: all CIDs in the range from // c_first to c_last will have width w widthsArray->AddNew(currentChar); currentChar = nextChar; while (true) { nextChar = FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); if (glyphIndex == 0) break; nextWidth = pFont->GetGlyphWidth(glyphIndex); if (nextChar != currentChar + 1 || nextWidth != width) break; currentChar = nextChar; } widthsArray->AddNew(currentChar); widthsArray->AddNew(width); } else { // Otherwise we can have a group of the form c [w1 w2 ...]: c has width // w1, c+1 has width w2, etc. widthsArray->AddNew(currentChar); auto curWidthArray = pdfium::MakeUnique(); curWidthArray->AddNew(width); while (nextChar == currentChar + 1) { curWidthArray->AddNew(nextWidth); currentChar = nextChar; nextChar = FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); if (glyphIndex == 0) break; nextWidth = pFont->GetGlyphWidth(glyphIndex); } widthsArray->Add(std::move(curWidthArray)); } if (glyphIndex == 0) break; currentChar = nextChar; } pCIDFont->SetNewFor("W", pDoc, widthsArray->GetObjNum()); // TODO(npm): Support vertical writing auto pDescendant = pdfium::MakeUnique(); pDescendant->AddNew(pDoc, pCIDFont->GetObjNum()); fontDict->SetFor("DescendantFonts", std::move(pDescendant)); // TODO(npm): do we need a ToUnicode? return pDoc->LoadFont(fontDict); } } // namespace DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPageObj_NewTextObj(FPDF_DOCUMENT document, FPDF_BYTESTRING font, float font_size) { CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); if (!pDoc) return nullptr; CPDF_Font* pFont = CPDF_Font::GetStockFont(pDoc, CFX_ByteStringC(font)); if (!pFont) return nullptr; CPDF_TextObject* pTextObj = new CPDF_TextObject; pTextObj->m_TextState.SetFont(pFont); pTextObj->m_TextState.SetFontSize(font_size); pTextObj->DefaultStates(); return pTextObj; } DLLEXPORT FPDF_BOOL STDCALL FPDFText_SetText(FPDF_PAGEOBJECT text_object, FPDF_BYTESTRING text) { if (!text_object) return false; auto* pTextObj = reinterpret_cast(text_object); pTextObj->SetText(CFX_ByteString(text)); return true; } DLLEXPORT FPDF_FONT STDCALL FPDFText_LoadFont(FPDF_DOCUMENT document, const uint8_t* data, uint32_t size, int font_type, FPDF_BOOL cid) { CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); if (!pDoc || !data || size == 0 || (font_type != FPDF_FONT_TYPE1 && font_type != FPDF_FONT_TRUETYPE)) { return nullptr; } auto pFont = pdfium::MakeUnique(); // 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)) return nullptr; return cid ? LoadCompositeFont(pDoc, std::move(pFont), data, size, font_type) : LoadSimpleFont(pDoc, std::move(pFont), data, size, font_type); }