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