Skip to content

Commit 79a542b

Browse files
committed
Fixes utfgrid support
1 parent 7d31852 commit 79a542b

File tree

6 files changed

+1371
-25
lines changed

6 files changed

+1371
-25
lines changed

src/js_grid_utils.hpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <mapnik/grid/grid.hpp> // for grid
77

88
#include "utils.hpp"
9+
#include "utf8.hpp"
910

1011
// stl
1112
#include <cmath> // ceil
@@ -16,29 +17,30 @@
1617

1718
namespace node_mapnik {
1819

19-
typedef std::unique_ptr<uint16_t[]> grid_line_type;
20+
typedef std::unique_ptr<char[]> grid_line_type;
2021

2122
template <typename T>
2223
static void grid2utf(T const& grid_type,
2324
std::vector<grid_line_type> & lines,
2425
std::vector<typename T::lookup_type>& key_order,
2526
unsigned int resolution)
2627
{
27-
typedef std::map< typename T::lookup_type, typename T::value_type> keys_type;
28+
typedef std::map<typename T::lookup_type, typename T::value_type> keys_type;
2829
typedef typename keys_type::const_iterator keys_iterator;
2930

3031
typename T::feature_key_type const& feature_keys = grid_type.get_feature_keys();
3132
typename T::feature_key_type::const_iterator feature_pos;
3233

3334
keys_type keys;
3435
// start counting at utf8 codepoint 32, aka space character
35-
uint16_t codepoint = 32;
36+
node_mapnik::utf8_int32_t codepoint = 32;
3637

3738
unsigned array_size = std::ceil(grid_type.width()/static_cast<float>(resolution));
3839
for (unsigned y = 0; y < grid_type.height(); y=y+resolution)
3940
{
40-
uint16_t idx = 0;
41-
grid_line_type line(new uint16_t[array_size]);
41+
grid_line_type line(new char[array_size * 4 + 1]()); // utf8 has up to 4 bytes per character
42+
void* p = (char *)line.get();
43+
4244
typename T::value_type const* row = grid_type.get_row(y);
4345
for (unsigned x = 0; x < grid_type.width(); x=x+resolution)
4446
{
@@ -65,12 +67,15 @@ static void grid2utf(T const& grid_type,
6567
keys[val] = codepoint;
6668
key_order.push_back(val);
6769
}
68-
line[idx++] = static_cast<uint16_t>(codepoint);
70+
71+
node_mapnik::utf8_int32_t cp = codepoint;
72+
p = node_mapnik::utf8catcodepoint(p, cp, 4);
6973
++codepoint;
7074
}
7175
else
7276
{
73-
line[idx++] = static_cast<uint16_t>(key_pos->second);
77+
node_mapnik::utf8_int32_t cp = static_cast<node_mapnik::utf8_int32_t>(key_pos->second);
78+
p = node_mapnik::utf8catcodepoint(p, cp, 4);
7479
}
7580
}
7681
// else, shouldn't get here...

src/mapnik_grid.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ struct AsyncGridEncode : Napi::AsyncWorker
9191
// Create the return hash.
9292
Napi::Object json = Napi::Object::New(env);
9393
Napi::Array grid_array = Napi::Array::New(env, lines_.size());
94-
unsigned array_size = std::ceil(grid_type.width()/static_cast<float>(resolution_));
9594
for (std::size_t j = 0;j < lines_.size(); ++j)
9695
{
9796
node_mapnik::grid_line_type const & line = lines_[j];
98-
grid_array.Set(j, Napi::String::New(env, (char*)line.get(), array_size));
97+
grid_array.Set(j, Napi::String::New(env, (char*)line.get()));
9998
}
10099
json.Set("grid", grid_array);
101100
json.Set("keys", keys_a);
@@ -432,8 +431,6 @@ Napi::Value Grid::encodeSync(Napi::CallbackInfo const& info)
432431
keys_a.Set(i, Napi::String::New(env, *it));
433432
}
434433

435-
mapnik::grid const& grid_type = *grid_;
436-
437434
// gather feature data
438435
Napi::Object feature_data = Napi::Object::New(env);
439436
if (add_features)
@@ -446,12 +443,10 @@ Napi::Value Grid::encodeSync(Napi::CallbackInfo const& info)
446443
// Create the return hash.
447444
Napi::Object json = Napi::Object::New(env);
448445
Napi::Array grid_array = Napi::Array::New(env, lines.size());
449-
unsigned array_size = std::ceil(grid_type.width()/static_cast<float>(resolution));
450446
for (std::size_t j = 0; j < lines.size(); ++j)
451447
{
452448
node_mapnik::grid_line_type const & line = lines[j];
453-
char16_t const* data = (char16_t*)line.get();
454-
grid_array.Set(j, Napi::String::New(env, data, array_size));
449+
grid_array.Set(j, Napi::String::New(env, (char*)line.get()));
455450
}
456451
json.Set("grid", grid_array);
457452
json.Set("keys", keys_a);

src/mapnik_grid_view.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ struct AsyncGridViewEncode : Napi::AsyncWorker
106106
// Create the return hash.
107107
Napi::Object json = Napi::Object::New(env);
108108
Napi::Array grid_array = Napi::Array::New(env, lines_.size());
109-
unsigned array_size = std::ceil(grid_view_type.width()/static_cast<float>(resolution_));
110109
for (std::size_t j = 0;j < lines_.size(); ++j)
111110
{
112111
node_mapnik::grid_line_type const & line = lines_[j];
113-
grid_array.Set(j, Napi::String::New(env, (char*)line.get(), array_size));
112+
grid_array.Set(j, Napi::String::New(env, (char*)line.get()));
114113
}
115114
json.Set("grid", grid_array);
116115
json.Set("keys", keys_a);
@@ -358,11 +357,10 @@ Napi::Value GridView::encodeSync(Napi::CallbackInfo const& info)
358357
// Create the return hash.
359358
Napi::Object json = Napi::Object::New(env);
360359
Napi::Array grid_array = Napi::Array::New(env);
361-
unsigned array_size = std::ceil(grid_type.width()/static_cast<float>(resolution));
362360
for (std::size_t j = 0; j < lines.size(); ++j)
363361
{
364362
node_mapnik::grid_line_type const & line = lines[j];
365-
grid_array.Set(j, Napi::String::New(env, (char*)line.get(), array_size));
363+
grid_array.Set(j, Napi::String::New(env, (char*)line.get()));
366364
}
367365
json.Set("grid", grid_array);
368366
json.Set("keys", keys_a);

0 commit comments

Comments
 (0)