Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGLSdfFontMaker.cxx
Go to the documentation of this file.
1#include "TGLSdfFontMaker.h"
2#include "TGLWidget.h"
3#include "TGClient.h"
4#include "TGLIncludes.h"
5#include "TASPngWriter.h"
6#include "RZip.h"
7#include "TError.h"
8
9#include <cstdio>
10#include <exception>
11
12#include "TGLSdfFontMakerLowLevel.icxx"
13
14/** \class TGLSdfFontMaker
15\ingroup opengl
16
17Helper class for generation of Signed Distance Field (SDF) fonts for REve.
18
19*/
20
21namespace {
22
23// cloned from THttpCallArg::CompressWithGzip()
24
25void gzip_compress_buffer(const char *objbuf, const size_t objlen, std::vector<char> &result)
26{
27 unsigned long objcrc = R__crc32(0, NULL, 0);
28 objcrc = R__crc32(objcrc, (const unsigned char *)objbuf, objlen);
29
30 // 10 bytes (ZIP header), compressed data, 8 bytes (CRC and original length)
31 int buflen = 10 + objlen + 8;
32 if (buflen < 512)
33 buflen = 512;
34
35 result.resize(buflen);
36
37 char *bufcur = result.data();
38
39 *bufcur++ = 0x1f; // first byte of ZIP identifier
40 *bufcur++ = 0x8b; // second byte of ZIP identifier
41 *bufcur++ = 0x08; // compression method
42 *bufcur++ = 0x00; // FLAG - empty, no any file names
43 *bufcur++ = 0; // empty timestamp
44 *bufcur++ = 0; //
45 *bufcur++ = 0; //
46 *bufcur++ = 0; //
47 *bufcur++ = 0; // XFL (eXtra FLags)
48 *bufcur++ = 3; // OS 3 means Unix
49
50 char dummy[8];
51 memcpy(dummy, bufcur - 6, 6);
52
53 // R__memcompress fills first 6 bytes with own header, therefore just overwrite them
54 unsigned long ziplen = R__memcompress(bufcur - 6, objlen + 6, (char *)objbuf, objlen);
55 if (!ziplen) {
56 // failure - return empty result
57 result.clear();
58 return;
59 }
60
61 memcpy(bufcur - 6, dummy, 6);
62
63 bufcur += (ziplen - 6); // jump over compressed data (6 byte is extra ROOT header)
64
65 // write CRC32
66 *bufcur++ = objcrc & 0xff;
67 *bufcur++ = (objcrc >> 8) & 0xff;
68 *bufcur++ = (objcrc >> 16) & 0xff;
69 *bufcur++ = (objcrc >> 24) & 0xff;
70
71 // write original data length
72 *bufcur++ = objlen & 0xff;
73 *bufcur++ = (objlen >> 8) & 0xff;
74 *bufcur++ = (objlen >> 16) & 0xff;
75 *bufcur++ = (objlen >> 24) & 0xff;
76
77 result.resize(bufcur - result.data());
78 result.shrink_to_fit();
79}
80} // namespace
81
82// struct SdfCreator
83// {
84// ...
85// int max_tex_size = 4096;
86// int width = 1024; // atlas image width in pixels
87// int height = 0; // atlas image height in pixels (optional, automatic)
88// int row_height = 96; // row height in pixels (without SDF border)
89// int border_size = 16; // SDF distance in pixels, default 16
90// ...
91// void parse_unicode_ranges( const std::string &nword );
92// e.g.: 'start1:end1,start:end2,single_codepoint' without spaces!
93// default: '31:126,0xffff'
94// };
95
96////////////////////////////////////////////////////////////////////////////////
97/// Converts TTF font 'ttf_font' into a SDF font texture atlas (png format) and
98/// a compressed font metrics JSON file. Both files are put into the directory
99/// given by 'output_prefix'.
100
101int TGLSdfFontMaker::MakeFont(const char *ttf_font, const char *output_prefix, bool verbose)
102{
103 if (verbose)
104 printf("TGLSdfFontMaker::MakeFont entering.\n");
105
106 const std::string base = "31:126";
107 const std::string accented = ",0x00e0:0x00fc,0x010c:0x010d,0x0160:0x0161,0x017d:0x017e";
108 const std::string greek = ",0x0391:0x03a9,0x03b1:0x03c9";
109 const std::string range_end = ",0xffff";
110
111 root_sdf_fonts::SdfCreator sc;
112
113 // Minimal gl-widget / context. Creation throws when no GL context can be had.
114 std::unique_ptr<TGLWidget> glw;
115 if (gClient) {
116 try {
117 glw.reset(TGLWidget::Create(TGLFormat(Rgl::kNone), gClient->GetDefaultRoot(), false, false, nullptr, 1, 1));
118 } catch (const std::exception &exc) {
119 ::Error("TGLSdfFontMaker::MakeFont", "Creation of the GL widget failed: %s", exc.what());
120 return 1;
121 }
122 }
123 if (!glw) {
124 ::Error("TGLSdfFontMaker::MakeFont", "No GL widget, font '%s' not generated.", ttf_font);
125 return 1;
126 }
127 glw->MakeCurrent();
128
129 glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &sc.max_tex_size);
130
131 std::string filename = ttf_font;
132 std::string res_filename = output_prefix;
133
134 if (filename.empty()) {
135 std::cerr << "Input file not specified" << std::endl;
136 return (1);
137 }
138
139 if (res_filename.empty()) {
140 size_t ext_dot = filename.find_last_of(".");
141 if (ext_dot == std::string::npos) {
142 res_filename = filename;
143 } else {
144 res_filename = filename.substr(0, ext_dot);
145 }
146 }
147
148 if (!sc.font.load_ttf_file(filename.c_str())) {
149 std::cerr << "Error reading TTF file '" << filename << "' " << std::endl;
150 return (1);
151 }
152
153 // Allocating glyph rects
154
155 sc.sdf_atlas.init(&sc.font, sc.width, sc.row_height, sc.border_size);
156
157 sc.parse_unicode_ranges(base + accented + greek + range_end);
158 sc.apply_unicode_ranges();
159
160 sc.sdf_atlas.draw_glyphs(sc.gp);
161
162 if (verbose) {
163 std::cout << "Allocated " << sc.sdf_atlas.glyph_count << " glyphs\n";
164 std::cout << "Atlas maximum height is " << sc.sdf_atlas.max_height << "\n";
165 }
166
167 if (sc.height == 0) {
168 sc.height = sc.sdf_atlas.max_height;
169 }
170
171 // GL initialization
172
173 sc.sdf_gl.init();
174
175 GLuint rbcolor;
176 glGenRenderbuffers(1, &rbcolor);
177 glBindRenderbuffer(GL_RENDERBUFFER, rbcolor);
178 glRenderbufferStorage(GL_RENDERBUFFER, GL_RED, sc.width, sc.height);
179 glBindRenderbuffer(GL_RENDERBUFFER, 0);
180
181 GLuint rbds;
182 glGenRenderbuffers(1, &rbds);
183 glBindRenderbuffer(GL_RENDERBUFFER, rbds);
184 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, sc.width, sc.height);
185 glBindRenderbuffer(GL_RENDERBUFFER, 0);
186
187 GLuint fbo;
188 glGenFramebuffers(1, &fbo);
189 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
190 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbcolor);
191 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbds);
192
193 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
194 std::cerr << "Error creating framebuffer!" << std::endl;
195 return (1);
196 }
197
198 // Rendering glyphs
199
200 uint8_t *picbuf = (uint8_t *)malloc(sc.width * sc.height);
201
202 glViewport(0, 0, sc.width, sc.height);
203 glClearColor(0.0, 0.0, 0.0, 0.0);
204 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
205
206 sc.sdf_gl.render_sdf({float(sc.width), float(sc.height)}, sc.gp.fp.vertices, sc.gp.lp.vertices);
207
208 glReadPixels(0, 0, sc.width, sc.height, GL_RED, GL_UNSIGNED_BYTE, picbuf);
209
210 glBindFramebuffer(GL_FRAMEBUFFER, 0);
211 glFinish();
212
213 if (verbose)
214 printf("Resulting GL buffer: w=%d, h=%d\n", sc.width, sc.height);
215
216 TASPngWriter pw(sc.width, sc.height, 0, 8);
217 pw.ref_row_pointers().resize(sc.height);
218 for (int iy = 0; iy < sc.height; ++iy) {
219 pw.ref_row_pointers()[sc.height - iy - 1] = picbuf + iy * sc.width;
220 }
221
222 pw.write_png_file(res_filename + ".png");
223
224 free(picbuf);
225
226 // Saving JSON
227
228 std::string json = sc.sdf_atlas.json(sc.height);
229 std::vector<char> json_gz;
230 gzip_compress_buffer(json.data(), json.size(), json_gz);
231
232 std::string json_filename = res_filename + ".js.gz";
233 FILE *json_file = fopen(json_filename.c_str(), "wb");
234 if (!json_file) {
235 perror("Error writing json file.");
236 return errno;
237 }
238 fwrite(json_gz.data(), json_gz.size(), 1, json_file);
239 fclose(json_file);
240
241 return 0;
242}
free(fBuffer)
nlohmann::json json
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
#define gClient
Definition TGClient.h:157
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
C++ wrapper over simple writer of PNG files for standard GL memory formats: LUMINANCE,...
Definition TASPngWriter.h:7
std::vector< unsigned char * > & ref_row_pointers()
int write_png_file(std::string_view filename)
Encapsulation of format / contents of an OpenGL buffer.
Definition TGLFormat.h:36
static int MakeFont(const char *ttf_font, const char *output_prefix, bool verbose=false)
Converts TTF font 'ttf_font' into a SDF font texture atlas (png format) and a compressed font metrics...
static TGLWidget * Create(const TGWindow *parent, Bool_t selectInput, Bool_t shareDefault, const TGLPaintDevice *shareDevice, UInt_t width, UInt_t height)
Static constructor for creating widget with default pixel format.
Definition TGLWidget.cxx:82
@ kNone
Definition TVirtualGL.h:127