summaryrefslogtreecommitdiff
path: root/scripts/generate-style-code.js
blob: b26e0520f6363c1409fdc47c9474bc4e15289013 (plain)
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
'use strict';

const fs = require('fs');
const ejs = require('ejs');
const spec = require('mapbox-gl-style-spec').latest;
var parseCSSColor = require('csscolorparser').parseCSSColor;

global.camelize = function (str) {
  return str.replace(/(?:^|-)(.)/g, function (_, x) {
    return x.toUpperCase();
  });
}

global.camelizeWithLeadingLowercase = function (str) {
  return str.replace(/-(.)/g, function (_, x) {
    return x.toUpperCase();
  });
}

global.propertyType = function (property) {
  if (/-translate-anchor$/.test(property.name)) {
    return 'TranslateAnchorType';
  }
  if (/-rotation-alignment$/.test(property.name)) {
    return 'RotationAlignmentType';
  }
  switch (property.type) {
  case 'boolean':
    return 'bool';
  case 'number':
    return 'float';
  case 'string':
    return 'std::string';
  case 'enum':
    return `${camelize(property.name)}Type`;
  case 'color':
    return `Color`;
  case 'array':
    if (property.length) {
      return `std::array<${propertyType({type: property.value})}, ${property.length}>`;
    } else {
      return `std::vector<${propertyType({type: property.value})}>`;
    }
  default: throw new Error(`unknown type for ${property.name}`)
  }
}

global.defaultValue = function (property) {
  switch (property.type) {
  case 'number':
    return property.default;
  case 'string':
    return JSON.stringify(property.default || "");
  case 'enum':
    return `${propertyType(property)}::${camelize(property.default)}`;
  case 'color':
    return `{{ ${parseCSSColor(property.default).join(', ')} }}`
  case 'array':
    const defaults = (property.default || []).map((e) => defaultValue({ type: property.value, default: e }));
    if (property.length) {
      return `{{ ${defaults.join(', ')} }}`;
    } else {
      return `{ ${defaults.join(', ')} }`;
    }
  default:
    return property.default;
  }
}

const layerHpp = ejs.compile(`<%
  const type = locals.type;
  const layoutProperties = locals.layoutProperties;
  const paintProperties = locals.paintProperties;
-%>
// This file is generated. Do not edit.

#pragma once

#include <mbgl/style/layer.hpp>
#include <mbgl/style/property_value.hpp>

<% if (type === 'line' || type === 'symbol') { -%>
#include <vector>

<% } -%>
namespace mbgl {

class <%- camelize(type) %>Layer : public Layer {
public:
    <%- camelize(type) %>Layer(const std::string& layerID);
    ~<%- camelize(type) %>Layer() final;

<% if (type === 'raster') { -%>
    // Source

    void setSource(const std::string& sourceID);
    const std::string& getSourceID() const;

<% } else if (type !== 'background') { -%>
    // Source

    void setSource(const std::string& sourceID, const std::string& sourceLayer);
    const std::string& getSourceID() const;
    const std::string& getSourceLayer() const;

<% } -%>
<% if (layoutProperties.length) { -%>
    // Layout properties

<% for (const property of layoutProperties) { -%>
    PropertyValue<<%- propertyType(property) %>> get<%- camelize(property.name) %>() const;
    void set<%- camelize(property.name) %>(PropertyValue<<%- propertyType(property) %>>);

<% } -%>
<% } -%>
    // Paint properties

<% for (const property of paintProperties) { -%>
    PropertyValue<<%- propertyType(property) %>> get<%- camelize(property.name) %>() const;
    void set<%- camelize(property.name) %>(PropertyValue<<%- propertyType(property) %>>);

<% } -%>
    // Private implementation

    class Impl;
    Impl* const impl;

    <%- camelize(type) %>Layer(const Impl&);
    <%- camelize(type) %>Layer(const <%- camelize(type) %>Layer&) = delete;
};

template <>
inline bool Layer::is<<%- camelize(type) %>Layer>() const {
    return type == Type::<%- camelize(type) %>;
}

} // namespace mbgl
`, {strict: true});

const layerCpp = ejs.compile(`<%
  const type = locals.type;
  const layoutProperties = locals.layoutProperties;
  const paintProperties = locals.paintProperties;
-%>
// This file is generated. Edit scripts/generate-style-code.js, then run \`make style-code\`.

#include <mbgl/layer/<%- type %>_layer.hpp>
#include <mbgl/layer/<%- type %>_layer_impl.hpp>

namespace mbgl {

<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(const std::string& layerID)
    : Layer(Type::<%- camelize(type) %>, std::make_unique<Impl>())
    , impl(static_cast<Impl*>(baseImpl.get())) {
    impl->id = layerID;
}

<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(const Impl& other)
    : Layer(Type::<%- camelize(type) %>, std::make_unique<Impl>(other))
    , impl(static_cast<Impl*>(baseImpl.get())) {
}

<%- camelize(type) %>Layer::~<%- camelize(type) %>Layer() = default;

std::unique_ptr<Layer> <%- camelize(type) %>Layer::Impl::clone() const {
    return std::make_unique<<%- camelize(type) %>Layer>(*this);
}

<% if (type === 'raster') { -%>
// Source

void <%- camelize(type) %>Layer::setSource(const std::string& sourceID) {
    impl->source = sourceID;
}

const std::string& <%- camelize(type) %>Layer::getSourceID() const {
    return impl->source;
}
<% } else if (type !== 'background') { -%>
// Source

void <%- camelize(type) %>Layer::setSource(const std::string& sourceID, const std::string& sourceLayer) {
    impl->source = sourceID;
    impl->sourceLayer = sourceLayer;
}

const std::string& <%- camelize(type) %>Layer::getSourceID() const {
    return impl->source;
}

const std::string& <%- camelize(type) %>Layer::getSourceLayer() const {
    return impl->sourceLayer;
}
<% } -%>

// Layout properties

<% for (const property of layoutProperties) { -%>
PropertyValue<<%- propertyType(property) %>> <%- camelize(type) %>Layer::get<%- camelize(property.name) %>() const {
    return impl->layout.<%- camelizeWithLeadingLowercase(property.name) %>.get();
}

void <%- camelize(type) %>Layer::set<%- camelize(property.name) %>(PropertyValue<<%- propertyType(property) %>> value) {
    impl->layout.<%- camelizeWithLeadingLowercase(property.name) %>.set(value);
}
<% } -%>

// Paint properties
<% for (const property of paintProperties) { %>
PropertyValue<<%- propertyType(property) %>> <%- camelize(type) %>Layer::get<%- camelize(property.name) %>() const {
    return impl->paint.<%- camelizeWithLeadingLowercase(property.name) %>.get();
}

void <%- camelize(type) %>Layer::set<%- camelize(property.name) %>(PropertyValue<<%- propertyType(property) %>> value) {
    impl->paint.<%- camelizeWithLeadingLowercase(property.name) %>.set(value);
}
<% } -%>

} // namespace mbgl
`, {strict: true});

const propertiesHpp = ejs.compile(`<%
  const type = locals.type;
  const layoutProperties = locals.layoutProperties;
  const paintProperties = locals.paintProperties;
-%>
// This file is generated. Edit scripts/generate-style-code.js, then run \`make style-code\`.

#pragma once

#include <mbgl/style/layout_property.hpp>
#include <mbgl/style/paint_property.hpp>
#include <mbgl/util/rapidjson.hpp>

namespace mbgl {

class StyleCascadeParameters;
class StyleCalculationParameters;

<% if (layoutProperties.length) { -%>
class <%- camelize(type) %>LayoutProperties {
public:
    void parse(const JSValue&);
    void recalculate(const StyleCalculationParameters&);

<% for (const property of layoutProperties) { -%>
    LayoutProperty<<%- propertyType(property) %>> <%- camelizeWithLeadingLowercase(property.name) %> { <%- defaultValue(property) %> };
<% } -%>
};

<% } -%>
class <%- camelize(type) %>PaintProperties {
public:
    void parse(const JSValue&);
    void cascade(const StyleCascadeParameters&);
    bool recalculate(const StyleCalculationParameters&);

<% for (const property of paintProperties) { -%>
<% if (/-pattern$/.test(property.name) || property.name === 'line-dasharray') { -%>
    PaintProperty<<%- propertyType(property) %>, CrossFadedPropertyEvaluator> <%- camelizeWithLeadingLowercase(property.name) %> { <%- defaultValue(property) %> };
<% } else if (property.name === 'fill-outline-color') { -%>
    PaintProperty<<%- propertyType(property) %>> <%- camelizeWithLeadingLowercase(property.name) %> { {{ 0, 0, 0, -1 }} };
<% } else { -%>
    PaintProperty<<%- propertyType(property) %>> <%- camelizeWithLeadingLowercase(property.name) %> { <%- defaultValue(property) %> };
<% } -%>
<% } -%>
};

} // namespace mbgl
`, {strict: true});

const propertiesCpp = ejs.compile(`<%
  const type = locals.type;
  const layoutProperties = locals.layoutProperties;
  const paintProperties = locals.paintProperties;
-%>
// This file is generated. Edit scripts/generate-style-code.js, then run \`make style-code\`.

#include <mbgl/layer/<%- type %>_layer_properties.hpp>

namespace mbgl {

<% if (layoutProperties.length) { -%>
void <%- camelize(type) %>LayoutProperties::parse(const JSValue& value) {
<% for (const property of layoutProperties) { -%>
    <%- camelizeWithLeadingLowercase(property.name) %>.parse(<%- JSON.stringify(property.name) %>, value);
<% } -%>
}

void <%- camelize(type) %>LayoutProperties::recalculate(const StyleCalculationParameters& parameters) {
<% for (const property of layoutProperties) { -%>
    <%- camelizeWithLeadingLowercase(property.name) %>.calculate(parameters);
<% } -%>
}

<% } -%>
void <%- camelize(type) %>PaintProperties::parse(const JSValue& value) {
<% for (const property of paintProperties) { -%>
    <%- camelizeWithLeadingLowercase(property.name) %>.parse(<%- JSON.stringify(property.name) %>, value);
<% } -%>
}

void <%- camelize(type) %>PaintProperties::cascade(const StyleCascadeParameters& parameters) {
<% for (const property of paintProperties) { -%>
    <%- camelizeWithLeadingLowercase(property.name) %>.cascade(parameters);
<% } -%>
}

bool <%- camelize(type) %>PaintProperties::recalculate(const StyleCalculationParameters& parameters) {
    bool hasTransitions = false;

<% for (const property of paintProperties) { -%>
    hasTransitions |= <%- camelizeWithLeadingLowercase(property.name) %>.calculate(parameters);
<% } -%>

    return hasTransitions;
}

} // namespace mbgl
`, {strict: true});

for (const type of spec.layer.type.values) {
  const layoutProperties = Object.keys(spec[`layout_${type}`]).reduce((memo, name) => {
    if (name !== 'visibility') {
      spec[`layout_${type}`][name].name = name;
      memo.push(spec[`layout_${type}`][name]);
    }
    return memo;
  }, []);

  const paintProperties = Object.keys(spec[`paint_${type}`]).reduce((memo, name) => {
    spec[`paint_${type}`][name].name = name;
    memo.push(spec[`paint_${type}`][name]);
    return memo;
  }, []);

  const layer = {
    type: type,
    layoutProperties: layoutProperties,
    paintProperties: paintProperties,
  };

  fs.writeFileSync(`include/mbgl/layer/${type}_layer.hpp`, layerHpp(layer));
  fs.writeFileSync(`src/mbgl/layer/${type}_layer.cpp`, layerCpp(layer));

  fs.writeFileSync(`src/mbgl/layer/${type}_layer_properties.hpp`, propertiesHpp(layer));
  fs.writeFileSync(`src/mbgl/layer/${type}_layer_properties.cpp`, propertiesCpp(layer));
}