-
Notifications
You must be signed in to change notification settings - Fork 65
/
tile.cpp
826 lines (641 loc) · 26.4 KB
/
tile.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#include <node.h>
#include <node_buffer.h>
#include "tile.hpp"
#include "clipper.hpp"
#include "globals.hpp"
#include <pango/pangoft2.h>
#include "sdf_renderer.hpp"
#include "distmap.h"
#include <set>
#include <algorithm>
#include <iostream>
using namespace v8;
using namespace ClipperLib;
struct SimplifyBaton {
Persistent<Function> callback;
Tile *tile;
};
struct ShapeBaton {
Persistent<Function> callback;
Tile *tile;
std::string fontstack;
};
Persistent<FunctionTemplate> Tile::constructor;
void Tile::Init(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
Local<String> name = String::NewSymbol("Tile");
constructor = Persistent<FunctionTemplate>::New(tpl);
// ObjectWrap uses the first internal field to store the wrapped pointer.
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(name);
// Add all prototype methods, getters and setters here.
constructor->PrototypeTemplate()->SetAccessor(v8::String::NewSymbol("length"), Length);
NODE_SET_PROTOTYPE_METHOD(constructor, "serialize", Serialize);
NODE_SET_PROTOTYPE_METHOD(constructor, "simplify", Simplify);
NODE_SET_PROTOTYPE_METHOD(constructor, "shape", Shape);
NODE_SET_PROTOTYPE_METHOD(constructor, "base", Base);
// constructor->PrototypeTemplate()->SetIndexedPropertyHandler(GetGlyph);
// This has to be last, otherwise the properties won't show up on the
// object in JavaScript.
target->Set(name, constructor->GetFunction());
}
Tile::Tile(const char *data, size_t length)
: ObjectWrap() {
tile.ParseFromArray(data, length);
pthread_mutex_init(&mutex, NULL);
}
Tile::~Tile() {
pthread_mutex_destroy(&mutex);
}
Handle<Value> Tile::New(const v8::Arguments& args) {
if (!args.IsConstructCall()) {
return ThrowException(Exception::TypeError(String::New("Constructor must be called with new keyword")));
}
if (args.Length() < 1 || !node::Buffer::HasInstance(args[0])) {
return ThrowException(Exception::TypeError(String::New("First argument must be a buffer")));
}
Local<Object> buffer = args[0]->ToObject();
Tile* tile = new Tile(node::Buffer::Data(buffer), node::Buffer::Length(buffer));
tile->Wrap(args.This());
return args.This();
}
bool Tile::HasInstance(Handle<Value> val) {
if (!val->IsObject()) return false;
return constructor->HasInstance(val->ToObject());
}
Handle<Value> Tile::Length(Local<String> property, const AccessorInfo &info) {
HandleScope scope;
Tile* tile = ObjectWrap::Unwrap<Tile>(info.This());
Local<Number> length = Uint32::New(tile->tile.layers_size());
return scope.Close(length);
}
Handle<Value> Tile::Serialize(const v8::Arguments& args) {
HandleScope scope;
llmr::vector::tile& tile = ObjectWrap::Unwrap<Tile>(args.This())->tile;
std::string serialized = tile.SerializeAsString();
return scope.Close(node::Buffer::New(serialized.data(), serialized.length())->handle_);
}
template <typename T>
Polygons load_geometry(const T& vertices)
{
Polygons polygons;
Polygon polygon;
int cmd = -1;
const int cmd_bits = 3;
unsigned length = 0;
int x = 0, y = 0;
for (int k = 0; k < vertices.size();) {
if (!length) {
unsigned cmd_length = vertices.Get(k++);
cmd = cmd_length & ((1 << cmd_bits) - 1);
length = cmd_length >> cmd_bits;
}
if (length > 0) {
length--;
if (cmd == 1 || cmd == 7) { // moveto or closepolygon
if (polygon.size()) {
polygons.push_back(polygon);
polygon.clear();
}
}
if (cmd == 1 || cmd == 2) {
int32_t dx = vertices.Get(k++);
int32_t dy = vertices.Get(k++);
dx = ((dx >> 1) ^ (-(dx & 1)));
dy = ((dy >> 1) ^ (-(dy & 1)));
polygon.push_back(IntPoint(x += dx, y += dy));
} else if (cmd == 7) {
} else {
fprintf(stderr, "Unknown command type: %d\n", cmd);
}
}
}
if (polygon.size()) {
polygons.push_back(polygon);
}
return polygons;
}
void encode_point(llmr::vector::feature& feature, const IntPoint& pt, const IntPoint& pos) {
// Compute delta to the previous coordinate.
int32_t dx = pt.X - pos.X;
int32_t dy = pt.Y - pos.Y;
// Manual zigzag encoding.
feature.add_geometry((dx << 1) ^ (dx >> 31));
feature.add_geometry((dy << 1) ^ (dy >> 31));
}
void encode_command(llmr::vector::feature& feature, int cmd, unsigned length) {
const int cmd_bits = 3;
const int cmd_mask = (1 << cmd_bits) - 1;
feature.add_geometry((length << cmd_bits) | (cmd & cmd_mask));
}
uint32_t encode_geometry(llmr::vector::feature& feature, const Polygons& polygons, bool is_polygon = false)
{
IntPoint pos(0, 0);
uint32_t count = 0;
for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); it++) {
const Polygon& polygon = *it;
if (!polygon.size()) {
continue;
}
// Encode moveTo first point in the polygon.
encode_command(feature, 1, 1);
encode_point(feature, polygon[0], pos);
count++;
pos = polygon[0];
// Encode lineTo for the rest of the points.
encode_command(feature, 2, polygon.size() - 1);
for (size_t i = 1; i < polygon.size(); i++) {
encode_point(feature, polygon[i], pos);
count++;
pos = polygon[i];
}
if (is_polygon) {
// Encode closepolygon
encode_command(feature, 7, 1);
}
}
return count;
}
Handle<Value> Tile::Simplify(const v8::Arguments& args) {
HandleScope scope;
if (args.Length() < 1 || !args[0]->IsFunction()) {
return ThrowException(Exception::TypeError(
String::New("First argument must be a callback function")));
}
Local<Function> callback = Local<Function>::Cast(args[0]);
Tile *tile = ObjectWrap::Unwrap<Tile>(args.This());
SimplifyBaton* baton = new SimplifyBaton();
baton->callback = Persistent<Function>::New(callback);
baton->tile = tile;
uv_work_t *req = new uv_work_t();
req->data = baton;
int status = uv_queue_work(uv_default_loop(), req, AsyncSimplify, (uv_after_work_cb)SimplifyAfter);
assert(status == 0);
return Undefined();
}
void Tile::AsyncSimplify(uv_work_t* req) {
SimplifyBaton* baton = static_cast<SimplifyBaton*>(req->data);
pthread_mutex_lock(&baton->tile->mutex);
llmr::vector::tile& tile = baton->tile->tile;
llmr::vector::tile new_tile;
for (int i = 0; i < tile.layers_size(); i++) {
const llmr::vector::layer& layer = tile.layers(i);
if (!layer.features_size()) {
continue;
}
llmr::vector::layer *new_layer = new_tile.add_layers();
new_layer->set_version(2);
// Copy over information
new_layer->set_name(layer.name());
*new_layer->mutable_keys() = layer.keys();
*new_layer->mutable_values() = layer.values();
if (layer.has_extent()) {
new_layer->set_extent(layer.extent());
}
// Our water polygons contain lots of duplicates
if (layer.name() == "water") {
Clipper outerClipper;
outerClipper.ForceSimple(true);
for (int j = 0; j < layer.features_size(); j++) {
const llmr::vector::feature& feature = layer.features(j);
Polygons polygons = load_geometry(feature.geometry());
Clipper clipper;
clipper.AddPolygons(polygons, ptSubject);
clipper.Execute(ctUnion, polygons);
outerClipper.AddPolygons(polygons, ptSubject);
}
Polygons polygons;
outerClipper.Execute(ctUnion, polygons, pftNonZero, pftNonZero);
if (polygons.size()) {
llmr::vector::feature *new_feature = new_layer->add_features();
new_feature->set_type(llmr::vector::Polygon);
// encode polygons into feature
uint32_t count = encode_geometry(*new_feature, polygons, true);
new_feature->set_vertex_count(count);
}
} else {
for (int j = 0; j < layer.features_size(); j++) {
const llmr::vector::feature& feature = layer.features(j);
// Copy point features verbatim.
if (feature.type() == 1) {
llmr::vector::feature *new_feature = new_layer->add_features();
*new_feature = feature;
continue;
}
Polygons polygons = load_geometry(feature.geometry());
bool is_polygon = feature.type() == llmr::vector::Polygon;
if (is_polygon) {
// CleanPolygons(polygons, polygons, 0.001);
Clipper clipper;
clipper.ForceSimple(true);
clipper.AddPolygons(polygons, ptSubject);
clipper.Execute(ctUnion, polygons);
// SimplifyPolygons(polygons);
}
if (polygons.size()) {
llmr::vector::feature *new_feature = new_layer->add_features();
// Copy over information
if (feature.has_id()) new_feature->set_id(feature.id());
if (feature.has_type()) new_feature->set_type(feature.type());
*new_feature->mutable_tags() = feature.tags();
// encode polygons into feature
encode_geometry(*new_feature, polygons, is_polygon);
}
}
}
}
tile = new_tile;
pthread_mutex_unlock(&baton->tile->mutex);
}
void Tile::SimplifyAfter(uv_work_t* req) {
HandleScope scope;
SimplifyBaton* baton = static_cast<SimplifyBaton*>(req->data);
const unsigned argc = 1;
Local<Value> argv[argc] = { Local<Value>::New(Null()) };
TryCatch try_catch;
baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
baton->callback.Dispose();
delete baton;
delete req;
}
Handle<Value> Tile::Shape(const v8::Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
return ThrowException(Exception::TypeError(
String::New("First argument must be a font stack")));
}
if (args.Length() < 2 || !args[1]->IsFunction()) {
return ThrowException(Exception::TypeError(
String::New("Second argument must be a callback function")));
}
Local<Function> callback = Local<Function>::Cast(args[1]);
String::Utf8Value fontstack(args[0]->ToString());
Tile *tile = ObjectWrap::Unwrap<Tile>(args.This());
ShapeBaton* baton = new ShapeBaton();
baton->callback = Persistent<Function>::New(callback);
baton->tile = tile;
baton->fontstack = *fontstack;
uv_work_t *req = new uv_work_t();
req->data = baton;
int status = uv_queue_work(uv_default_loop(), req, AsyncShape, (uv_after_work_cb)ShapeAfter);
assert(status == 0);
return Undefined();
}
Handle<Value> Tile::Base(const v8::Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
return ThrowException(Exception::TypeError(
String::New("First argument must be a font stack")));
}
if (args.Length() < 2 || !args[1]->IsFunction()) {
return ThrowException(Exception::TypeError(
String::New("Second argument must be a callback function")));
}
Local<Function> callback = Local<Function>::Cast(args[1]);
String::Utf8Value fontstack(args[0]->ToString());
Tile *tile = ObjectWrap::Unwrap<Tile>(args.This());
ShapeBaton* baton = new ShapeBaton();
baton->callback = Persistent<Function>::New(callback);
baton->tile = tile;
baton->fontstack = *fontstack;
uv_work_t *req = new uv_work_t();
req->data = baton;
int status = uv_queue_work(uv_default_loop(), req, AsyncBase, (uv_after_work_cb)ShapeAfter);
assert(status == 0);
return Undefined();
}
struct Glyph {
uint32_t id;
std::string bitmap;
uint32_t width;
uint32_t height;
int32_t left;
int32_t top;
uint32_t advance;
};
class Face {
public:
Face(PangoFont *font) : font(font) {
g_object_ref(font);
}
~Face() {
g_object_unref(font);
}
const Glyph& glyph(uint32_t glyph_id) {
const std::map<uint32_t, Glyph>::iterator pos = glyphs.find(glyph_id);
if (pos != glyphs.end()) {
return pos->second;
} else {
Glyph& glyph = glyphs[glyph_id];
glyph.id = glyph_id;
PangoFcFont *fc_font = PANGO_FC_FONT(font);
FT_Face ft_face = pango_fc_font_lock_face(fc_font);
int size = 24;
int buffer = 3;
FT_Set_Char_Size(ft_face, 0, size * 64, 72, 72);
FT_Error error = FT_Load_Glyph(ft_face, glyph_id, FT_LOAD_NO_HINTING | FT_LOAD_RENDER | FT_LOAD_PEDANTIC);
if (error) {
fprintf(stderr, "glyph load error %d\n", error);
std::cerr<<glyph_id<<" "<<pango_font_description_to_string(pango_font_describe(font))<<'\n';
return glyph;
}
glyph.width = ft_face->glyph->bitmap.width;
glyph.height = ft_face->glyph->bitmap.rows;
glyph.left = ft_face->glyph->bitmap_left;
glyph.top = ft_face->glyph->bitmap_top;
glyph.advance = ft_face->glyph->metrics.horiAdvance / 64;
FT_GlyphSlot slot = ft_face->glyph;
int width = slot->bitmap.width;
int height = slot->bitmap.rows;
// Create a signed distance field for the glyph bitmap.
if (width > 0) {
unsigned int buffered_width = width + 2 * buffer;
unsigned int buffered_height = height + 2 * buffer;
unsigned char *distance = make_distance_map((unsigned char *)slot->bitmap.buffer, width, height, buffer);
glyph.bitmap.resize(buffered_width * buffered_height);
for (unsigned int y = 0; y < buffered_height; y++) {
memcpy((unsigned char *)glyph.bitmap.data() + buffered_width * y, distance + y * distmap_size, buffered_width);
}
free(distance);
}
pango_fc_font_unlock_face(fc_font);
return glyph;
}
}
private:
PangoFont *font;
std::map<uint32_t, Glyph> glyphs;
typedef std::map<PangoFont *, Face *> map;
static pthread_once_t init;
static pthread_key_t map_key;
static void init_map() {
pthread_key_create(&map_key, delete_map);
}
static void delete_map(void *fontmap) {
delete (map *)fontmap;
}
public:
static Face *face(PangoFont *font)
{
// Get a thread-specific font-/glyphmap.
pthread_once(&init, init_map);
map *fontmap = (map *)pthread_getspecific(map_key);
if (fontmap == NULL) {
pthread_setspecific(map_key, fontmap = new map);
}
map::const_iterator pos = fontmap->find(font);
if (pos != fontmap->end()) {
return pos->second;
} else {
Face *face = new Face(font);
(*fontmap)[font] = face;
return face;
}
}
};
pthread_once_t Face::init = PTHREAD_ONCE_INIT;
pthread_key_t Face::map_key = 0;
class TileFace {
public:
TileFace(PangoFont *font) : font(font) {
g_object_ref(font);
PangoFcFont *fc_font = PANGO_FC_FONT(font);
FT_Face ft_face = pango_fc_font_lock_face(fc_font);
family = ft_face->family_name;
style = ft_face->style_name;
pango_fc_font_unlock_face(fc_font);
}
~TileFace() {
g_object_unref(font);
}
inline void add_glyph(uint32_t glyph_id) {
glyphs.insert(glyph_id);
}
PangoFont *font;
std::string family;
std::string style;
std::set<uint32_t> glyphs;
};
void Tile::AsyncShape(uv_work_t* req) {
ShapeBaton* baton = static_cast<ShapeBaton*>(req->data);
pthread_mutex_lock(&baton->tile->mutex);
PangoRenderer *renderer = pango_renderer();
PangoFontDescription *desc = pango_font_description_from_string(baton->fontstack.c_str());
pango_font_description_set_absolute_size(desc, 24 * 1024);
PangoLayout *layout = pango_layout_new(pango_context());
pango_layout_set_font_description(layout, desc);
typedef std::map<PangoFont *, TileFace *> Faces;
Faces faces;
llmr::vector::tile& tile = baton->tile->tile;
// for every label
for (int i = 0; i < tile.layers_size(); i++) {
const llmr::vector::layer& layer = tile.layers(i);
typedef std::set<int> Strings;
Strings strings;
// Compile a set of all strings we need to shape.
for (int j = 0; j < layer.features_size(); j++) {
const llmr::vector::feature& feature = layer.features(j);
for (int k = 1; k < feature.tags_size(); k += 2) {
const std::string& key = layer.keys(feature.tags(k - 1));
if (key == "name") {
// TODO: handle multiple fonts stacks
strings.insert(feature.tags(k));
}
// TODO: extract all keys we need to shape
}
}
llmr::vector::layer* mutable_layer = tile.mutable_layers(i);
typedef std::vector<TileFace *> LayerFaces;
LayerFaces layer_faces;
// Process strings per layer.
for (Strings::const_iterator it = strings.begin(); it != strings.end(); it++) {
int key = *it;
const llmr::vector::value& value = layer.values(key);
std::string text;
if (value.has_string_value()) {
text = value.string_value();
}
if (text.size()) {
// Shape the text.
pango_layout_set_text(layout, text.data(), text.size());
pango_sdf_renderer_reset(PANGO_SDF_RENDERER(renderer));
pango_renderer_draw_layout (renderer, layout, 0, 0);
const PangoSDFGlyphs& glyphs = pango_sdf_renderer_get_glyphs(PANGO_SDF_RENDERER(renderer));
llmr::vector::label *label = mutable_layer->add_labels();
label->set_text(key);
label->set_stack(0); // TODO: support multiple font stacks
// Add all glyphs for this labels and add new font faces as they
// appear.
for (size_t j = 0; j < glyphs.size(); j++) {
const PangoSDFGlyph& glyph = glyphs[j];
// Try to find whether this font has already been used
// in this tile.
Faces::const_iterator global_pos = faces.find(glyph.font);
if (global_pos == faces.end()) {
TileFace *face = new TileFace(glyph.font);
std::pair<PangoFont *, TileFace *> keyed(glyph.font, face);
global_pos = faces.insert(keyed).first;
}
TileFace *face = global_pos->second;
// Find out whether this font has been used in this tile
// before; and get its position ID.s
std::vector<TileFace *>::iterator pos = std::find(layer_faces.begin(), layer_faces.end(), face);
if (pos == layer_faces.end()) {
// Do not ref this font object here since we already ref'ed
// it for the global font list.
layer_faces.push_back(face);
pos = layer_faces.end() - 1;
}
int layer_face_id = pos - layer_faces.begin();
face->add_glyph(glyph.glyph);
label->add_faces(layer_face_id);
label->add_glyphs(glyph.glyph);
label->add_x(glyph.x);
label->add_y(glyph.y);
}
}
}
// Add a textual representation of the font so that we can figure out
// later what font we need to use.
for (LayerFaces::const_iterator it = layer_faces.begin(); it != layer_faces.end(); it++) {
TileFace *face = *it;
mutable_layer->add_faces(face->family + " " + face->style);
// note: we don't delete the TileFace objects here because they
// are 'owned' by the global faces map and deleted later on.
}
// Insert FAKE stacks
mutable_layer->add_stacks(baton->fontstack);
}
// Insert SDF glyphs + bitmaps
for (Faces::const_iterator it = faces.begin(); it != faces.end(); it++) {
const std::pair<PangoFont *, TileFace *>& pair = *it;
TileFace *face = pair.second;
llmr::vector::face *mutable_face = tile.add_faces();
PangoFcFont *fc_font = PANGO_FC_FONT(face->font);
FT_Face ft_face = pango_fc_font_lock_face(fc_font);
mutable_face->set_family(ft_face->family_name);
mutable_face->set_style(ft_face->style_name);
// Determine ASCII glyphs
std::set<uint32_t> omit;
FT_UInt glyph_index;
FT_ULong char_code = FT_Get_First_Char(ft_face, &glyph_index);
while (glyph_index != 0 && char_code < 128) {
omit.insert(glyph_index);
char_code = FT_Get_Next_Char(ft_face, char_code, &glyph_index);
}
pango_fc_font_unlock_face(fc_font);
Face *_face = Face::face(face->font);
for (std::set<uint32_t>::const_iterator it = face->glyphs.begin(); it != face->glyphs.end(); it++) {
uint32_t id = *it;
// Omit ASCII glyphs we determined earlier
if (omit.find(id) != omit.end()) {
continue;
}
const Glyph& gl = _face->glyph(id);
llmr::vector::glyph *glyph = mutable_face->add_glyphs();
glyph->set_id(id);
glyph->set_width(gl.width);
glyph->set_height(gl.height);
glyph->set_left(gl.left);
glyph->set_top(gl.top);
glyph->set_advance(gl.advance);
if (gl.width > 0) {
glyph->set_bitmap(gl.bitmap);
}
}
delete face;
}
pthread_mutex_unlock(&baton->tile->mutex);
}
void Tile::AsyncBase(uv_work_t* req) {
ShapeBaton* baton = static_cast<ShapeBaton*>(req->data);
pthread_mutex_lock(&baton->tile->mutex);
PangoRenderer *renderer = pango_renderer();
PangoFontDescription *desc = pango_font_description_from_string(baton->fontstack.c_str());
pango_font_description_set_absolute_size(desc, 24 * 1024);
PangoLayout *layout = pango_layout_new(pango_context());
pango_layout_set_font_description(layout, desc);
typedef std::map<PangoFont *, TileFace *> Faces;
Faces faces;
llmr::vector::tile& tile = baton->tile->tile;
// Process each glyph.
for (gunichar i = 0; i < 938; i++) {
if (g_unichar_validate(i)) {
// Log font description and filename.
PangoContext *context = pango_layout_get_context(layout);
PangoFontMap *fontmap = pango_context_get_font_map(context);
PangoLanguage *lang = pango_context_get_language(context);
PangoFontset *fontset = pango_font_map_load_fontset(fontmap, context, desc, lang);
PangoFont *font = pango_fontset_get_font(fontset, i);
// Debugging
PangoFontDescription *fontDesc = pango_font_describe(font);
std::cerr<<i<<" "<<pango_font_description_to_string(fontDesc)<<'\n';
// Render glyph using best font
pango_sdf_renderer_reset(PANGO_SDF_RENDERER(renderer));
pango_sdf_renderer_draw_glyph(renderer, font, i, 0, 0);
const PangoSDFGlyphs& glyphs = pango_sdf_renderer_get_glyphs(PANGO_SDF_RENDERER(renderer));
// Add all glyphs for this labels and add new font faces as they
// appear.
for (size_t j = 0; j < glyphs.size(); j++) {
const PangoSDFGlyph& glyph = glyphs[j];
// Try to find whether this font has already been used
// in this tile.
Faces::const_iterator global_pos = faces.find(glyph.font);
if (global_pos == faces.end()) {
TileFace *face = new TileFace(glyph.font);
std::pair<PangoFont *, TileFace *> keyed(glyph.font, face);
global_pos = faces.insert(keyed).first;
}
TileFace *face = global_pos->second;
face->add_glyph(glyph.glyph);
}
g_object_unref(font);
}
}
// Insert SDF glyphs + bitmaps
for (Faces::const_iterator it = faces.begin(); it != faces.end(); it++) {
const std::pair<PangoFont *, TileFace *>& pair = *it;
TileFace *face = pair.second;
llmr::vector::face *mutable_face = tile.add_faces();
PangoFcFont *fc_font = PANGO_FC_FONT(face->font);
FT_Face ft_face = pango_fc_font_lock_face(fc_font);
mutable_face->set_family(ft_face->family_name);
mutable_face->set_style(ft_face->style_name);
pango_fc_font_unlock_face(fc_font);
Face *_face = Face::face(face->font);
for (std::set<uint32_t>::const_iterator it = face->glyphs.begin(); it != face->glyphs.end(); it++) {
uint32_t id = *it;
const Glyph& gl = _face->glyph(id);
llmr::vector::glyph *glyph = mutable_face->add_glyphs();
glyph->set_id(id);
glyph->set_width(gl.width);
glyph->set_height(gl.height);
glyph->set_left(gl.left);
glyph->set_top(gl.top);
glyph->set_advance(gl.advance);
if (gl.width > 0) {
glyph->set_bitmap(gl.bitmap);
}
}
delete face;
}
pthread_mutex_unlock(&baton->tile->mutex);
}
void Tile::ShapeAfter(uv_work_t* req) {
HandleScope scope;
ShapeBaton* baton = static_cast<ShapeBaton*>(req->data);
const unsigned argc = 1;
Local<Value> argv[argc] = { Local<Value>::New(Null()) };
TryCatch try_catch;
baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
baton->callback.Dispose();
delete baton;
delete req;
}