-
Notifications
You must be signed in to change notification settings - Fork 0
/
msdf.cc
189 lines (165 loc) · 5.51 KB
/
msdf.cc
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// See LICENSE for license details.
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <cassert>
#include <cctype>
#include <cmath>
#include <string>
#include <memory>
#include <vector>
#include <map>
#include <tuple>
#include <algorithm>
#include <atomic>
#include <mutex>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_MODULE_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
#include "core/arithmetics.hpp"
#include "core/Vector2.h"
#include "core/Scanline.h"
#include "core/Shape.h"
#include "core/BitmapRef.hpp"
#include "core/Bitmap.h"
#include "core/pixel-conversion.hpp"
#include "core/edge-coloring.h"
#include "core/render-sdf.h"
#include "core/rasterization.h"
#include "core/estimate-sdf-error.h"
#include "core/save-bmp.h"
#include "core/save-tiff.h"
#include "core/shape-description.h"
#include "ext/save-png.h"
#include "ext/import-svg.h"
#include "ext/import-font.h"
#include "msdfgen.h"
#include "binpack.h"
#include "utf8.h"
#include "image.h"
#include "draw.h"
#include "font.h"
#include "glyph.h"
#include "msdf.h"
typedef unsigned uint;
struct FtContext {
msdfgen::Shape *shape;
msdfgen::Point2 position;
msdfgen::Contour *contour;
};
static msdfgen::Point2 ftPoint2(const FT_Vector &vector) {
return msdfgen::Point2(vector.x/64., vector.y/64.);
}
static int ftMoveTo(const FT_Vector *to, void *user) {
FtContext *context = static_cast<FtContext *>(user);
context->contour = &context->shape->addContour();
context->position = ftPoint2(*to);
return 0;
}
static int ftLineTo(const FT_Vector *to, void *user) {
FtContext *context = static_cast<FtContext *>(user);
context->contour->addEdge(new msdfgen::LinearSegment(context->position,
ftPoint2(*to)));
context->position = ftPoint2(*to);
return 0;
}
static int ftConicTo(const FT_Vector *control, const FT_Vector *to, void *user) {
FtContext *context = static_cast<FtContext *>(user);
context->contour->addEdge(new msdfgen::QuadraticSegment(context->position,
ftPoint2(*control), ftPoint2(*to)));
context->position = ftPoint2(*to);
return 0;
}
static int ftCubicTo(const FT_Vector *control1, const FT_Vector *control2,
const FT_Vector *to, void *user) {
FtContext *context = static_cast<FtContext *>(user);
context->contour->addEdge(new msdfgen::CubicSegment(context->position,
ftPoint2(*control1), ftPoint2(*control2), ftPoint2(*to)));
context->position = ftPoint2(*to);
return 0;
}
/*
* glyph_renderer_msdf
*/
atlas_entry glyph_renderer_msdf::render(font_atlas *atlas, font_face_ft *face,
int font_size, int glyph)
{
msdfgen::Shape shape;
msdfgen::Vector2 translate, scale = { 1, 1 };
FT_GlyphSlot ftglyph;
FT_Error error;
FT_Outline_Funcs ftFunctions;
FtContext context = { &shape };
atlas_entry ae;
int char_height = 128 * 64; /* magic - shader uses textureSize() */
int horz_resolution = font_manager::dpi;
double range = 8;
bool overlapSupport = true;
bool scanlinePass = true;
double angleThreshold = 3;
double edgeThreshold = 1.001;
uint coloringSeed = 0;
msdfgen::FillRule fillRule = msdfgen::FILL_NONZERO;
error = FT_Set_Char_Size(face->ftface, 0, char_height, horz_resolution,
horz_resolution);
if (error) {
return atlas_entry(-1);
}
error = FT_Load_Glyph(face->ftface, glyph, FT_LOAD_NO_HINTING);
if (error) {
return atlas_entry(-1);
}
ftFunctions.move_to = ftMoveTo;
ftFunctions.line_to = ftLineTo;
ftFunctions.conic_to = ftConicTo;
ftFunctions.cubic_to = ftCubicTo;
ftFunctions.shift = 0;
ftFunctions.delta = 0;
error = FT_Outline_Decompose(&face->ftface->glyph->outline, &ftFunctions,
&context);
if (error) {
return atlas_entry(-1);
}
/* font dimensions */
ftglyph = face->ftface->glyph;
int ox = (int)floorf((float)ftglyph->metrics.horiBearingX / 64.0f) - 1;
int oy = (int)floorf((float)(ftglyph->metrics.horiBearingY -
ftglyph->metrics.height) / 64.0f) - 1;
int w = (int)ceilf(ftglyph->metrics.width / 64.0f) + 2;
int h = (int)ceilf(ftglyph->metrics.height / 64.0f) + 2;
translate.x = -ox;
translate.y = -oy;
msdfgen::Bitmap<float, 3> msdf(w, h);
msdfgen::edgeColoringSimple(shape, angleThreshold, coloringSeed);
msdfgen::generateMSDF(msdf, shape, range, scale, translate,
scanlinePass ? 0 : edgeThreshold, overlapSupport);
msdfgen::distanceSignCorrection(msdf, shape, scale, translate, fillRule);
if (edgeThreshold > 0) {
msdfgen::msdfErrorCorrection(msdf, edgeThreshold/(scale*range));
}
/* create atlas entry and copy rasterized glyph into the atlas */
ae = atlas->create(face, 0, glyph, char_height, ox, oy, w, h);
if (ae.bin_id >= 0) {
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int r = msdfgen::pixelFloatToByte(msdf(x,y)[0]);
int g = msdfgen::pixelFloatToByte(msdf(x,y)[1]);
int b = msdfgen::pixelFloatToByte(msdf(x,y)[2]);
size_t dst = ((ae.y + y) * atlas->width + ae.x + x) * 4;
uint32_t color = r | g << 8 | b << 16 | 0xff000000;
*(uint32_t*)&atlas->pixels[dst] = color;
}
}
}
/* clients expect font metrics for the font size to be loaded */
face->get_metrics(font_size);
/*
* note: we recurse into the font atlas lookup code to trigger
* generation of an atlas entry for the requested font size.
*/
return ae;
}