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