forked from mapnik/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapnik_fonts.hpp
115 lines (95 loc) · 3.2 KB
/
mapnik_fonts.hpp
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
#ifndef __NODE_MAPNIK_FONTS_H__
#define __NODE_MAPNIK_FONTS_H__
// mapnik
#include <mapnik/version.hpp>
#include <mapnik/font_engine_freetype.hpp>
// stl
#include <vector>
#include "utils.hpp"
#include "mapnik3x_compatibility.hpp"
using namespace v8;
namespace node_mapnik {
static inline NAN_METHOD(register_fonts)
{
NanScope();
try
{
if (args.Length() == 0 || !args[0]->IsString())
{
NanThrowTypeError("first argument must be a path to a directory of fonts");
NanReturnUndefined();
}
bool found = false;
std::vector<std::string> const names_before = mapnik::freetype_engine::face_names();
// option hash
if (args.Length() == 2){
if (!args[1]->IsObject())
{
NanThrowTypeError("second argument is optional, but if provided must be an object, eg. { recurse:Boolean }");
NanReturnUndefined();
}
Local<Object> options = args[1].As<Object>();
if (options->Has(NanNew("recurse")))
{
Local<Value> recurse_opt = options->Get(NanNew("recurse"));
if (!recurse_opt->IsBoolean())
{
NanThrowTypeError("'recurse' must be a Boolean");
NanReturnUndefined();
}
bool recurse = recurse_opt->BooleanValue();
std::string path = TOSTR(args[0]);
found = mapnik::freetype_engine::register_fonts(path,recurse);
}
}
else
{
std::string path = TOSTR(args[0]);
found = mapnik::freetype_engine::register_fonts(path);
}
std::vector<std::string> const& names_after = mapnik::freetype_engine::face_names();
if (names_after.size() == names_before.size())
found = false;
NanReturnValue(NanNew(found));
}
catch (std::exception const& ex)
{
NanThrowError(ex.what());
NanReturnUndefined();
}
}
static inline NAN_METHOD(available_font_faces)
{
NanScope();
std::vector<std::string> const& names = mapnik::freetype_engine::face_names();
Local<Array> a = NanNew<Array>(names.size());
for (unsigned i = 0; i < names.size(); ++i)
{
a->Set(i, NanNew(names[i].c_str()));
}
NanReturnValue(a);
}
static inline NAN_METHOD(available_font_files)
{
NanScope();
#if MAPNIK_VERSION >= 200100
std::map<std::string,std::pair<int,std::string> > const& mapping = mapnik::freetype_engine::get_mapping();
Local<Object> obj = NanNew<Object>();
std::map<std::string,std::pair<int,std::string> >::const_iterator itr;
for (itr = mapping.begin();itr!=mapping.end();++itr)
{
obj->Set(NanNew(itr->first.c_str()), NanNew(itr->second.second.c_str()));
}
#else
std::map<std::string,std::string> const& mapping = mapnik::freetype_engine::get_mapping();
Local<Object> obj = NanNew<Object>();
std::map<std::string,std::string>::const_iterator itr;
for (itr = mapping.begin();itr!=mapping.end();++itr)
{
obj->Set(NanNew(itr->first.c_str()), NanNew(itr->second.c_str()));
}
#endif
NanReturnValue(obj);
}
}
#endif // __NODE_MAPNIK_FONTS_H__