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_type3glyphmap.h"
8
9 #include <math.h>
10
11 #include <algorithm>
12 #include <utility>
13
14 #include "core/fxcrt/fx_system.h"
15 #include "core/fxge/cfx_glyphbitmap.h"
16 #include "core/fxge/fx_font.h"
17
18 namespace {
19
20 constexpr int kType3MaxBlues = 16;
21
AdjustBlueHelper(float pos,std::vector<int> * blues)22 int AdjustBlueHelper(float pos, std::vector<int>* blues) {
23 float min_distance = 1000000.0f;
24 int closest_pos = -1;
25 for (int i = 0; i < static_cast<int>(blues->size()); ++i) {
26 float distance = fabs(pos - static_cast<float>(blues->at(i)));
27 if (distance < std::min(0.8f, min_distance)) {
28 min_distance = distance;
29 closest_pos = i;
30 }
31 }
32 if (closest_pos >= 0)
33 return blues->at(closest_pos);
34 int new_pos = FXSYS_roundf(pos);
35 if (blues->size() < kType3MaxBlues)
36 blues->push_back(new_pos);
37 return new_pos;
38 }
39
40 } // namespace
41
42 CPDF_Type3GlyphMap::CPDF_Type3GlyphMap() = default;
43
44 CPDF_Type3GlyphMap::~CPDF_Type3GlyphMap() = default;
45
AdjustBlue(float top,float bottom)46 std::pair<int, int> CPDF_Type3GlyphMap::AdjustBlue(float top, float bottom) {
47 return std::make_pair(AdjustBlueHelper(top, &m_TopBlue),
48 AdjustBlueHelper(bottom, &m_BottomBlue));
49 }
50
GetBitmap(uint32_t charcode) const51 const CFX_GlyphBitmap* CPDF_Type3GlyphMap::GetBitmap(uint32_t charcode) const {
52 auto it = m_GlyphMap.find(charcode);
53 return it != m_GlyphMap.end() ? it->second.get() : nullptr;
54 }
55
SetBitmap(uint32_t charcode,std::unique_ptr<CFX_GlyphBitmap> pMap)56 void CPDF_Type3GlyphMap::SetBitmap(uint32_t charcode,
57 std::unique_ptr<CFX_GlyphBitmap> pMap) {
58 m_GlyphMap[charcode] = std::move(pMap);
59 }
60