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