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 <memory>
10 #include <utility>
11
12 #include "core/fpdfapi/font/cpdf_type3char.h"
13 #include "core/fpdfapi/font/cpdf_type3font.h"
14 #include "core/fpdfapi/render/cpdf_type3glyphmap.h"
15 #include "core/fxcrt/fx_coordinates.h"
16 #include "core/fxcrt/fx_safe_types.h"
17 #include "core/fxge/cfx_glyphbitmap.h"
18 #include "core/fxge/dib/cfx_dibitmap.h"
19 #include "core/fxge/dib/fx_dib.h"
20
21 namespace {
22
23 struct CPDF_UniqueKeyGen {
24 void Generate(int count, ...);
25
26 int m_KeyLen;
27 char m_Key[128];
28 };
29
Generate(int count,...)30 void CPDF_UniqueKeyGen::Generate(int count, ...) {
31 va_list argList;
32 va_start(argList, count);
33 for (int i = 0; i < count; i++) {
34 int p = va_arg(argList, int);
35 (reinterpret_cast<uint32_t*>(m_Key))[i] = p;
36 }
37 va_end(argList);
38 m_KeyLen = count * sizeof(uint32_t);
39 }
40
IsScanLine1bpp(uint8_t * pBuf,int width)41 bool IsScanLine1bpp(uint8_t* pBuf, int width) {
42 int size = width / 8;
43 for (int i = 0; i < size; i++) {
44 if (pBuf[i])
45 return true;
46 }
47 return (width % 8) && (pBuf[width / 8] & (0xff << (8 - width % 8)));
48 }
49
IsScanLine8bpp(uint8_t * pBuf,int width)50 bool IsScanLine8bpp(uint8_t* pBuf, int width) {
51 for (int i = 0; i < width; i++) {
52 if (pBuf[i] > 0x40)
53 return true;
54 }
55 return false;
56 }
57
DetectFirstLastScan(const RetainPtr<CFX_DIBitmap> & pBitmap,bool bFirst)58 int DetectFirstLastScan(const RetainPtr<CFX_DIBitmap>& pBitmap, bool bFirst) {
59 int height = pBitmap->GetHeight();
60 int pitch = pBitmap->GetPitch();
61 int width = pBitmap->GetWidth();
62 int bpp = pBitmap->GetBPP();
63 if (bpp > 8)
64 width *= bpp / 8;
65 uint8_t* pBuf = pBitmap->GetBuffer();
66 int line = bFirst ? 0 : height - 1;
67 int line_step = bFirst ? 1 : -1;
68 int line_end = bFirst ? height : -1;
69 while (line != line_end) {
70 if (bpp == 1) {
71 if (IsScanLine1bpp(pBuf + line * pitch, width))
72 return line;
73 } else {
74 if (IsScanLine8bpp(pBuf + line * pitch, width))
75 return line;
76 }
77 line += line_step;
78 }
79 return -1;
80 }
81
82 } // namespace
83
CPDF_Type3Cache(CPDF_Type3Font * pFont)84 CPDF_Type3Cache::CPDF_Type3Cache(CPDF_Type3Font* pFont) : m_pFont(pFont) {}
85
86 CPDF_Type3Cache::~CPDF_Type3Cache() = default;
87
LoadGlyph(uint32_t charcode,const CFX_Matrix * pMatrix)88 const CFX_GlyphBitmap* CPDF_Type3Cache::LoadGlyph(uint32_t charcode,
89 const CFX_Matrix* pMatrix) {
90 CPDF_UniqueKeyGen keygen;
91 keygen.Generate(
92 4, FXSYS_roundf(pMatrix->a * 10000), FXSYS_roundf(pMatrix->b * 10000),
93 FXSYS_roundf(pMatrix->c * 10000), FXSYS_roundf(pMatrix->d * 10000));
94 ByteString FaceGlyphsKey(keygen.m_Key, keygen.m_KeyLen);
95 CPDF_Type3GlyphMap* pSizeCache;
96 auto it = m_SizeMap.find(FaceGlyphsKey);
97 if (it == m_SizeMap.end()) {
98 auto pNew = std::make_unique<CPDF_Type3GlyphMap>();
99 pSizeCache = pNew.get();
100 m_SizeMap[FaceGlyphsKey] = std::move(pNew);
101 } else {
102 pSizeCache = it->second.get();
103 }
104 const CFX_GlyphBitmap* pExisting = pSizeCache->GetBitmap(charcode);
105 if (pExisting)
106 return pExisting;
107
108 std::unique_ptr<CFX_GlyphBitmap> pNewBitmap =
109 RenderGlyph(pSizeCache, charcode, pMatrix);
110 CFX_GlyphBitmap* pGlyphBitmap = pNewBitmap.get();
111 pSizeCache->SetBitmap(charcode, std::move(pNewBitmap));
112 return pGlyphBitmap;
113 }
114
RenderGlyph(CPDF_Type3GlyphMap * pSize,uint32_t charcode,const CFX_Matrix * pMatrix)115 std::unique_ptr<CFX_GlyphBitmap> CPDF_Type3Cache::RenderGlyph(
116 CPDF_Type3GlyphMap* pSize,
117 uint32_t charcode,
118 const CFX_Matrix* pMatrix) {
119 const CPDF_Type3Char* pChar = m_pFont->LoadChar(charcode);
120 if (!pChar || !pChar->GetBitmap())
121 return nullptr;
122
123 CFX_Matrix text_matrix(pMatrix->a, pMatrix->b, pMatrix->c, pMatrix->d, 0, 0);
124 CFX_Matrix image_matrix = pChar->matrix() * text_matrix;
125
126 RetainPtr<CFX_DIBitmap> pBitmap = pChar->GetBitmap();
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 = DetectFirstLastScan(pBitmap, true);
133 int bottom_line = DetectFirstLastScan(pBitmap, false);
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