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