• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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/render/cpdf_type3cache.h"
8 
9 #include <math.h>
10 
11 #include <memory>
12 #include <utility>
13 
14 #include "core/fpdfapi/font/cpdf_type3char.h"
15 #include "core/fpdfapi/font/cpdf_type3font.h"
16 #include "core/fpdfapi/render/cpdf_type3glyphmap.h"
17 #include "core/fxcrt/compiler_specific.h"
18 #include "core/fxcrt/fx_coordinates.h"
19 #include "core/fxcrt/fx_safe_types.h"
20 #include "core/fxge/cfx_glyphbitmap.h"
21 #include "core/fxge/dib/cfx_dibitmap.h"
22 #include "core/fxge/dib/fx_dib.h"
23 
24 namespace {
25 
IsScanLine1bpp(const uint8_t * pBuf,int width)26 bool IsScanLine1bpp(const uint8_t* pBuf, int width) {
27   int size = width / 8;
28   for (int i = 0; i < size; i++) {
29     if (UNSAFE_TODO(pBuf[i])) {
30       return true;
31     }
32   }
33   return (width % 8) &&
34          (UNSAFE_TODO(pBuf[width / 8]) & (0xff << (8 - width % 8)));
35 }
36 
IsScanLine8bpp(const uint8_t * pBuf,int width)37 bool IsScanLine8bpp(const uint8_t* pBuf, int width) {
38   for (int i = 0; i < width; i++) {
39     if (UNSAFE_TODO(pBuf[i]) > 0x40) {
40       return true;
41     }
42   }
43   return false;
44 }
45 
IsScanLineBpp(int bpp,const uint8_t * pBuf,int width)46 bool IsScanLineBpp(int bpp, const uint8_t* pBuf, int width) {
47   if (bpp == 1)
48     return IsScanLine1bpp(pBuf, width);
49   if (bpp > 8)
50     width *= bpp / 8;
51   return IsScanLine8bpp(pBuf, width);
52 }
53 
DetectFirstScan(const RetainPtr<CFX_DIBitmap> & pBitmap)54 int DetectFirstScan(const RetainPtr<CFX_DIBitmap>& pBitmap) {
55   const int height = pBitmap->GetHeight();
56   const int width = pBitmap->GetWidth();
57   const int bpp = pBitmap->GetBPP();
58   for (int line = 0; line < height; ++line) {
59     const uint8_t* pBuf = pBitmap->GetScanline(line).data();
60     if (IsScanLineBpp(bpp, pBuf, width))
61       return line;
62   }
63   return -1;
64 }
65 
DetectLastScan(const RetainPtr<CFX_DIBitmap> & pBitmap)66 int DetectLastScan(const RetainPtr<CFX_DIBitmap>& pBitmap) {
67   const int height = pBitmap->GetHeight();
68   const int bpp = pBitmap->GetBPP();
69   const int width = pBitmap->GetWidth();
70   for (int line = height - 1; line >= 0; --line) {
71     const uint8_t* pBuf = pBitmap->GetScanline(line).data();
72     if (IsScanLineBpp(bpp, pBuf, width))
73       return line;
74   }
75   return -1;
76 }
77 
78 }  // namespace
79 
CPDF_Type3Cache(CPDF_Type3Font * pFont)80 CPDF_Type3Cache::CPDF_Type3Cache(CPDF_Type3Font* pFont) : m_pFont(pFont) {}
81 
82 CPDF_Type3Cache::~CPDF_Type3Cache() = default;
83 
LoadGlyph(uint32_t charcode,const CFX_Matrix & mtMatrix)84 const CFX_GlyphBitmap* CPDF_Type3Cache::LoadGlyph(uint32_t charcode,
85                                                   const CFX_Matrix& mtMatrix) {
86   SizeKey keygen = {
87       FXSYS_roundf(mtMatrix.a * 10000),
88       FXSYS_roundf(mtMatrix.b * 10000),
89       FXSYS_roundf(mtMatrix.c * 10000),
90       FXSYS_roundf(mtMatrix.d * 10000),
91   };
92   CPDF_Type3GlyphMap* pSizeCache;
93   auto it = m_SizeMap.find(keygen);
94   if (it == m_SizeMap.end()) {
95     auto pNew = std::make_unique<CPDF_Type3GlyphMap>();
96     pSizeCache = pNew.get();
97     m_SizeMap[keygen] = std::move(pNew);
98   } else {
99     pSizeCache = it->second.get();
100   }
101   const CFX_GlyphBitmap* pExisting = pSizeCache->GetBitmap(charcode);
102   if (pExisting)
103     return pExisting;
104 
105   std::unique_ptr<CFX_GlyphBitmap> pNewBitmap =
106       RenderGlyph(pSizeCache, charcode, mtMatrix);
107   CFX_GlyphBitmap* pGlyphBitmap = pNewBitmap.get();
108   pSizeCache->SetBitmap(charcode, std::move(pNewBitmap));
109   return pGlyphBitmap;
110 }
111 
RenderGlyph(CPDF_Type3GlyphMap * pSize,uint32_t charcode,const CFX_Matrix & mtMatrix)112 std::unique_ptr<CFX_GlyphBitmap> CPDF_Type3Cache::RenderGlyph(
113     CPDF_Type3GlyphMap* pSize,
114     uint32_t charcode,
115     const CFX_Matrix& mtMatrix) {
116   CPDF_Type3Char* pChar = m_pFont->LoadChar(charcode);
117   if (!pChar)
118     return nullptr;
119 
120   RetainPtr<CFX_DIBitmap> pBitmap = pChar->GetBitmap();
121   if (!pBitmap)
122     return nullptr;
123 
124   CFX_Matrix text_matrix(mtMatrix.a, mtMatrix.b, mtMatrix.c, mtMatrix.d, 0, 0);
125   CFX_Matrix image_matrix = pChar->matrix() * text_matrix;
126 
127   RetainPtr<CFX_DIBitmap> pResBitmap;
128   int left = 0;
129   int top = 0;
130   if (fabs(image_matrix.b) < fabs(image_matrix.a) / 100 &&
131       fabs(image_matrix.c) < fabs(image_matrix.d) / 100) {
132     int top_line = DetectFirstScan(pBitmap);
133     int bottom_line = DetectLastScan(pBitmap);
134     if (top_line == 0 && bottom_line == pBitmap->GetHeight() - 1) {
135       float top_y = image_matrix.d + image_matrix.f;
136       float bottom_y = image_matrix.f;
137       bool bFlipped = top_y > bottom_y;
138       if (bFlipped)
139         std::swap(top_y, bottom_y);
140       std::tie(top_line, bottom_line) = pSize->AdjustBlue(top_y, bottom_y);
141       FX_SAFE_INT32 safe_height = bFlipped ? top_line : bottom_line;
142       safe_height -= bFlipped ? bottom_line : top_line;
143       if (!safe_height.IsValid())
144         return nullptr;
145 
146       pResBitmap = pBitmap->StretchTo(static_cast<int>(image_matrix.a),
147                                       safe_height.ValueOrDie(),
148                                       FXDIB_ResampleOptions(), nullptr);
149       top = top_line;
150       if (image_matrix.a < 0)
151         left = FXSYS_roundf(image_matrix.e + image_matrix.a);
152       else
153         left = FXSYS_roundf(image_matrix.e);
154     }
155   }
156   if (!pResBitmap)
157     pResBitmap = pBitmap->TransformTo(image_matrix, &left, &top);
158   if (!pResBitmap)
159     return nullptr;
160 
161   auto pGlyph = std::make_unique<CFX_GlyphBitmap>(left, -top);
162   pGlyph->GetBitmap()->TakeOver(std::move(pResBitmap));
163   return pGlyph;
164 }
165