1 // Copyright 2014 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 <algorithm>
8 #include <limits>
9 #include <vector>
10 
11 #include "core/fxcodec/fx_codec.h"
12 #include "core/fxcrt/fx_safe_types.h"
13 #include "core/fxge/cfx_pathdata.h"
14 #include "core/fxge/fx_freetype.h"
15 #include "core/fxge/ge/fx_text_int.h"
16 #include "core/fxge/ifx_renderdevicedriver.h"
17 
18 namespace {
19 
ResetTransform(FT_Face face)20 void ResetTransform(FT_Face face) {
21   FXFT_Matrix matrix;
22   matrix.xx = 0x10000L;
23   matrix.xy = 0;
24   matrix.yx = 0;
25   matrix.yy = 0x10000L;
26   FXFT_Set_Transform(face, &matrix, 0);
27 }
28 
29 }  // namespace
30 
FXTEXT_GLYPHPOS()31 FXTEXT_GLYPHPOS::FXTEXT_GLYPHPOS() : m_pGlyph(nullptr) {}
32 
33 FXTEXT_GLYPHPOS::FXTEXT_GLYPHPOS(const FXTEXT_GLYPHPOS&) = default;
34 
~FXTEXT_GLYPHPOS()35 FXTEXT_GLYPHPOS::~FXTEXT_GLYPHPOS(){};
36 
ScopedFontTransform(FT_Face face,FXFT_Matrix * matrix)37 ScopedFontTransform::ScopedFontTransform(FT_Face face, FXFT_Matrix* matrix)
38     : m_Face(face) {
39   FXFT_Set_Transform(m_Face, matrix, 0);
40 }
41 
~ScopedFontTransform()42 ScopedFontTransform::~ScopedFontTransform() {
43   ResetTransform(m_Face);
44 }
45 
FXGE_GetGlyphsBBox(const std::vector<FXTEXT_GLYPHPOS> & glyphs,int anti_alias,FX_FLOAT retinaScaleX,FX_FLOAT retinaScaleY)46 FX_RECT FXGE_GetGlyphsBBox(const std::vector<FXTEXT_GLYPHPOS>& glyphs,
47                            int anti_alias,
48                            FX_FLOAT retinaScaleX,
49                            FX_FLOAT retinaScaleY) {
50   FX_RECT rect(0, 0, 0, 0);
51   bool bStarted = false;
52   for (const FXTEXT_GLYPHPOS& glyph : glyphs) {
53     const CFX_GlyphBitmap* pGlyph = glyph.m_pGlyph;
54     if (!pGlyph)
55       continue;
56 
57     FX_SAFE_INT32 char_left = glyph.m_Origin.x;
58     char_left += pGlyph->m_Left;
59     if (!char_left.IsValid())
60       continue;
61 
62     FX_SAFE_INT32 char_width = pGlyph->m_Bitmap.GetWidth();
63     char_width /= retinaScaleX;
64     if (anti_alias == FXFT_RENDER_MODE_LCD)
65       char_width /= 3;
66     if (!char_width.IsValid())
67       continue;
68 
69     FX_SAFE_INT32 char_right = char_left + char_width;
70     if (!char_right.IsValid())
71       continue;
72 
73     FX_SAFE_INT32 char_top = glyph.m_Origin.y;
74     char_top -= pGlyph->m_Top;
75     if (!char_top.IsValid())
76       continue;
77 
78     FX_SAFE_INT32 char_height = pGlyph->m_Bitmap.GetHeight();
79     char_height /= retinaScaleY;
80     if (!char_height.IsValid())
81       continue;
82 
83     FX_SAFE_INT32 char_bottom = char_top + char_height;
84     if (!char_bottom.IsValid())
85       continue;
86 
87     if (bStarted) {
88       rect.left = pdfium::base::ValueOrDieForType<int32_t>(
89           pdfium::base::CheckMin(rect.left, char_left));
90       rect.right = pdfium::base::ValueOrDieForType<int32_t>(
91           pdfium::base::CheckMax(rect.right, char_right));
92       rect.top = pdfium::base::ValueOrDieForType<int32_t>(
93           pdfium::base::CheckMin(rect.top, char_top));
94       rect.bottom = pdfium::base::ValueOrDieForType<int32_t>(
95           pdfium::base::CheckMax(rect.bottom, char_bottom));
96       continue;
97     }
98 
99     rect.left = char_left.ValueOrDie();
100     rect.right = char_right.ValueOrDie();
101     rect.top = char_top.ValueOrDie();
102     rect.bottom = char_bottom.ValueOrDie();
103     bStarted = true;
104   }
105   return rect;
106 }
107 
CFX_SizeGlyphCache()108 CFX_SizeGlyphCache::CFX_SizeGlyphCache() {}
109 
~CFX_SizeGlyphCache()110 CFX_SizeGlyphCache::~CFX_SizeGlyphCache() {
111   for (const auto& pair : m_GlyphMap) {
112     delete pair.second;
113   }
114   m_GlyphMap.clear();
115 }
116 
Generate(int count,...)117 void _CFX_UniqueKeyGen::Generate(int count, ...) {
118   va_list argList;
119   va_start(argList, count);
120   for (int i = 0; i < count; i++) {
121     int p = va_arg(argList, int);
122     ((uint32_t*)m_Key)[i] = p;
123   }
124   va_end(argList);
125   m_KeyLen = count * sizeof(uint32_t);
126 }
127