1 // Copyright 2016 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 "core/fpdfapi/font/cpdf_type3char.h" 8 9 #include <utility> 10 11 #include "core/fxge/dib/cfx_dibitmap.h" 12 #include "core/fxge/fx_dib.h" 13 #include "third_party/base/ptr_util.h" 14 15 namespace { 16 17 constexpr float kTextUnitInGlyphUnit = 1000.0f; 18 19 } // namespace 20 21 CPDF_Type3Char::CPDF_Type3Char() = default; 22 23 CPDF_Type3Char::~CPDF_Type3Char() = default; 24 25 // static TextUnitToGlyphUnit(float fTextUnit)26float CPDF_Type3Char::TextUnitToGlyphUnit(float fTextUnit) { 27 return fTextUnit * kTextUnitInGlyphUnit; 28 } 29 30 // static TextUnitRectToGlyphUnitRect(CFX_FloatRect * pRect)31void CPDF_Type3Char::TextUnitRectToGlyphUnitRect(CFX_FloatRect* pRect) { 32 pRect->Scale(kTextUnitInGlyphUnit); 33 } 34 LoadBitmapFromSoleImageOfForm()35bool CPDF_Type3Char::LoadBitmapFromSoleImageOfForm() { 36 if (m_pBitmap || !m_pForm) 37 return true; 38 39 if (m_bColored) 40 return false; 41 42 auto result = m_pForm->GetBitmapAndMatrixFromSoleImageOfForm(); 43 if (!result.has_value()) 44 return false; 45 46 std::tie(m_pBitmap, m_ImageMatrix) = result.value(); 47 m_pForm.reset(); 48 return true; 49 } 50 InitializeFromStreamData(bool bColored,const float * pData)51void CPDF_Type3Char::InitializeFromStreamData(bool bColored, 52 const float* pData) { 53 m_bColored = bColored; 54 m_Width = FXSYS_roundf(TextUnitToGlyphUnit(pData[0])); 55 m_BBox.left = FXSYS_roundf(TextUnitToGlyphUnit(pData[2])); 56 m_BBox.bottom = FXSYS_roundf(TextUnitToGlyphUnit(pData[3])); 57 m_BBox.right = FXSYS_roundf(TextUnitToGlyphUnit(pData[4])); 58 m_BBox.top = FXSYS_roundf(TextUnitToGlyphUnit(pData[5])); 59 } 60 WillBeDestroyed()61void CPDF_Type3Char::WillBeDestroyed() { 62 // Break cycles. 63 m_pForm.reset(); 64 } 65 Transform(CPDF_Font::FormIface * pForm,const CFX_Matrix & matrix)66void CPDF_Type3Char::Transform(CPDF_Font::FormIface* pForm, 67 const CFX_Matrix& matrix) { 68 m_Width = m_Width * matrix.GetXUnit() + 0.5f; 69 70 CFX_FloatRect char_rect; 71 if (m_BBox.right <= m_BBox.left || m_BBox.bottom >= m_BBox.top) { 72 char_rect = pForm->CalcBoundingBox(); 73 TextUnitRectToGlyphUnitRect(&char_rect); 74 } else { 75 char_rect = CFX_FloatRect(m_BBox); 76 } 77 78 m_BBox = matrix.TransformRect(char_rect).ToRoundedFxRect(); 79 } 80 SetForm(std::unique_ptr<CPDF_Font::FormIface> pForm)81void CPDF_Type3Char::SetForm(std::unique_ptr<CPDF_Font::FormIface> pForm) { 82 m_pForm = std::move(pForm); 83 } 84 GetBitmap()85RetainPtr<CFX_DIBitmap> CPDF_Type3Char::GetBitmap() { 86 return m_pBitmap; 87 } 88 GetBitmap() const89const RetainPtr<CFX_DIBitmap>& CPDF_Type3Char::GetBitmap() const { 90 return m_pBitmap; 91 } 92