forked from aseprite/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.h
More file actions
175 lines (143 loc) · 4.13 KB
/
Copy pathalgorithm.h
File metadata and controls
175 lines (143 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// LAF FreeType Wrapper
// Copyright (c) 2016-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef FT_ALGORITHM_H_INCLUDED
#define FT_ALGORITHM_H_INCLUDED
#pragma once
#include "base/string.h"
#include "ft/freetype_headers.h"
#include "ft/hb_shaper.h"
#include "gfx/rect.h"
namespace ft {
template<typename FaceFT>
class DefaultShaper {
public:
typedef typename FaceFT::Glyph Glyph;
DefaultShaper(FaceFT& face) : m_face(face) {
}
bool initialize(const base::utf8_const_iterator& it,
const base::utf8_const_iterator& end) {
m_begin = m_it = it;
m_end = end;
return (m_it != end);
}
bool nextChar() {
++m_it;
return (m_it != m_end);
}
int unicodeChar() const {
return *m_it;
}
int charIndex() {
return m_it - m_begin;
}
unsigned int glyphIndex() {
return m_face.cache().getGlyphIndex(m_face, unicodeChar());
}
void glyphOffsetXY(Glyph* glyph) {
// Do nothing
}
void glyphAdvanceXY(const Glyph* glyph, double& x, double& y) {
x += glyph->ft_glyph->advance.x / double(1 << 16);
y += glyph->ft_glyph->advance.y / double(1 << 16);
}
private:
FaceFT& m_face;
base::utf8_const_iterator m_begin, m_end, m_it;
};
template<typename FaceFT,
typename Shaper = HBShaper<FaceFT> >
class ForEachGlyph {
public:
typedef typename FaceFT::Glyph Glyph;
ForEachGlyph(FaceFT& face)
: m_face(face)
, m_shaper(face)
, m_glyph(nullptr)
, m_useKerning(FT_HAS_KERNING(((FT_Face)face)) ? true: false)
, m_prevGlyph(0)
, m_x(0.0), m_y(0.0) {
}
~ForEachGlyph() {
unloadGlyph();
}
int unicodeChar() { return m_shaper.unicodeChar(); }
int charIndex() { return m_shaper.charIndex(); }
const Glyph* glyph() const { return m_glyph; }
bool initialize(const base::utf8_const_iterator& it,
const base::utf8_const_iterator& end) {
bool res = m_shaper.initialize(it, end);
if (res)
prepareGlyph();
return res;
}
bool nextChar() {
m_prevGlyph = m_shaper.glyphIndex();
if (m_shaper.nextChar()) {
prepareGlyph();
return true;
}
else
return false;
}
private:
void prepareGlyph() {
FT_UInt glyphIndex = m_shaper.glyphIndex();
double initialX = m_x;
if (m_useKerning && m_prevGlyph && glyphIndex) {
FT_Vector kerning;
FT_Get_Kerning(m_face, m_prevGlyph, glyphIndex,
FT_KERNING_DEFAULT, &kerning);
m_x += kerning.x / 64.0;
}
unloadGlyph();
// Load new glyph
m_glyph = m_face.cache().loadGlyph(m_face, glyphIndex, m_face.antialias());
if (m_glyph) {
m_glyph->bitmap = &FT_BitmapGlyph(m_glyph->ft_glyph)->bitmap;
m_glyph->x = m_x
+ m_glyph->bearingX;
m_glyph->y = m_y
+ m_face.height()
+ m_face.descender() // descender is negative
- m_glyph->bearingY;
m_shaper.glyphOffsetXY(m_glyph);
m_shaper.glyphAdvanceXY(m_glyph, m_x, m_y);
m_glyph->startX = initialX;
m_glyph->endX = m_x;
}
}
void unloadGlyph() {
if (m_glyph) {
m_face.cache().doneGlyph(m_glyph);
m_glyph = nullptr;
}
}
private:
FaceFT& m_face;
Shaper m_shaper;
Glyph* m_glyph;
bool m_useKerning;
FT_UInt m_prevGlyph;
double m_x, m_y;
};
template<typename FaceFT>
gfx::Rect calc_text_bounds(FaceFT& face, const std::string& str) {
gfx::Rect bounds(0, 0, 0, 0);
ForEachGlyph<FaceFT> feg(face);
if (feg.initialize(base::utf8_const_iterator(str.begin()),
base::utf8_const_iterator(str.end()))) {
do {
if (auto glyph = feg.glyph())
bounds |= gfx::Rect(int(glyph->x),
int(glyph->y),
glyph->bitmap->width,
glyph->bitmap->rows);
} while (feg.nextChar());
}
return bounds;
}
} // namespace ft
#endif