Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 116 lines (93 sloc) 3.554 kb
ca19ae4 @springmeyer node_mapnik namespace for header only code, and move fonts and plugins t...
springmeyer authored
1 #ifndef __NODE_MAPNIK_FONTS_H__
2 #define __NODE_MAPNIK_FONTS_H__
3
4
5 // v8
6 #include <v8.h>
7
8 // node
9 #include <node.h>
10
11 // mapnik
8f17e83 @springmeyer first pass to support mapnik 2.0.x in same codebase that targets 2.1.x
springmeyer authored
12 #include <mapnik/version.hpp>
ca19ae4 @springmeyer node_mapnik namespace for header only code, and move fonts and plugins t...
springmeyer authored
13 #include <mapnik/font_engine_freetype.hpp>
14
15 // stl
16 #include <vector>
17
18 #include "utils.hpp"
19
20 using namespace v8;
21 using namespace node;
22
23 namespace node_mapnik {
24
25 static inline Handle<Value> register_fonts(const Arguments& args)
26 {
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
27 HandleScope scope;
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
28
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
29 try
30 {
31 if (!args.Length() >= 1 || !args[0]->IsString())
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
32 return ThrowException(Exception::TypeError(
33 String::New("first argument must be a path to a directory of fonts")));
34
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
35 bool found = false;
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
36
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
37 std::vector<std::string> const names_before = mapnik::freetype_engine::face_names();
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
38
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
39 // option hash
40 if (args.Length() == 2){
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
41 if (!args[1]->IsObject())
42 return ThrowException(Exception::TypeError(
43 String::New("second argument is optional, but if provided must be an object, eg. { recurse:Boolean }")));
44
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
45 Local<Object> options = args[1]->ToObject();
46 if (options->Has(String::New("recurse")))
47 {
48 Local<Value> recurse_opt = options->Get(String::New("recurse"));
49 if (!recurse_opt->IsBoolean())
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
50 return ThrowException(Exception::TypeError(
51 String::New("'recurse' must be a Boolean")));
52
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
53 bool recurse = recurse_opt->BooleanValue();
2db3420 @springmeyer avoid possibility of taking reference to temporary
springmeyer authored
54 std::string path = TOSTR(args[0]);
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
55 found = mapnik::freetype_engine::register_fonts(path,recurse);
56 }
57 }
58 else
59 {
2db3420 @springmeyer avoid possibility of taking reference to temporary
springmeyer authored
60 std::string path = TOSTR(args[0]);
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
61 found = mapnik::freetype_engine::register_fonts(path);
62 }
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
63
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
64 std::vector<std::string> const& names_after = mapnik::freetype_engine::face_names();
65 if (names_after.size() == names_before.size())
66 found = false;
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
67
68 return scope.Close(Boolean::New(found));
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
69 }
38dd2ab @springmeyer better c++ style as per mapnik conventions
springmeyer authored
70 catch (std::exception const& ex)
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
71 {
72 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
73 String::New(ex.what())));
8f06f9c @springmeyer proper exception handling for font and datasource registration
springmeyer authored
74 }
ca19ae4 @springmeyer node_mapnik namespace for header only code, and move fonts and plugins t...
springmeyer authored
75 }
76
77 static inline Handle<Value> available_font_faces(const Arguments& args)
78 {
79 HandleScope scope;
80 std::vector<std::string> const& names = mapnik::freetype_engine::face_names();
81 Local<Array> a = Array::New(names.size());
82 for (unsigned i = 0; i < names.size(); ++i)
83 {
84 a->Set(i, String::New(names[i].c_str()));
85 }
86 return scope.Close(a);
87 }
88
2fa66f6 @springmeyer add mapnik.fontFiles api to expose where face_names were registered from...
springmeyer authored
89 static inline Handle<Value> available_font_files(const Arguments& args)
90 {
91 HandleScope scope;
8f17e83 @springmeyer first pass to support mapnik 2.0.x in same codebase that targets 2.1.x
springmeyer authored
92 #if MAPNIK_VERSION >= 200100
65ce6de upgrade to new mapnik::freetype_engine::get_mapping signature
Artem Pavlenko authored
93 std::map<std::string,std::pair<int,std::string> > const& mapping = mapnik::freetype_engine::get_mapping();
2fa66f6 @springmeyer add mapnik.fontFiles api to expose where face_names were registered from...
springmeyer authored
94 Local<Object> obj = Object::New();
65ce6de upgrade to new mapnik::freetype_engine::get_mapping signature
Artem Pavlenko authored
95 std::map<std::string,std::pair<int,std::string> >::const_iterator itr;
2fa66f6 @springmeyer add mapnik.fontFiles api to expose where face_names were registered from...
springmeyer authored
96 for (itr = mapping.begin();itr!=mapping.end();++itr)
97 {
65ce6de upgrade to new mapnik::freetype_engine::get_mapping signature
Artem Pavlenko authored
98 obj->Set(String::NewSymbol(itr->first.c_str()),String::New(itr->second.second.c_str()));
2fa66f6 @springmeyer add mapnik.fontFiles api to expose where face_names were registered from...
springmeyer authored
99 }
8f17e83 @springmeyer first pass to support mapnik 2.0.x in same codebase that targets 2.1.x
springmeyer authored
100 #else
101 std::map<std::string,std::string> const& mapping = mapnik::freetype_engine::get_mapping();
102 Local<Object> obj = Object::New();
103 std::map<std::string,std::string>::const_iterator itr;
104 for (itr = mapping.begin();itr!=mapping.end();++itr)
105 {
106 obj->Set(String::NewSymbol(itr->first.c_str()),String::New(itr->second.c_str()));
107 }
108 #endif
2fa66f6 @springmeyer add mapnik.fontFiles api to expose where face_names were registered from...
springmeyer authored
109 return scope.Close(obj);
110 }
111
112
ca19ae4 @springmeyer node_mapnik namespace for header only code, and move fonts and plugins t...
springmeyer authored
113 }
114
e779f06 @springmeyer newlines at end of files to avoid compiler warning on RHEL
springmeyer authored
115 #endif // __NODE_MAPNIK_FONTS_H__
Something went wrong with that request. Please try again.