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