diff --git a/app.js b/app.js index 9270cac0..1dd465f0 100755 --- a/app.js +++ b/app.js @@ -4,6 +4,7 @@ var path = require('path'); var fs = require('fs'); var _ = require('underscore'); var semver = require('semver'); +const setICUEnvVariable = require('./lib/cartodb/utils/icu_data_env_setter'); // jshint undef:false var log = console.log.bind(console); @@ -16,6 +17,9 @@ if (!semver.satisfies(nodejsVersion, '>=6.9.0')) { process.exit(1); } +// This function should be called before the require('yargs'). +setICUEnvVariable(); + var argv = require('yargs') .usage('Usage: $0 [options]') .help('h') diff --git a/lib/cartodb/utils/icu_data_env_setter.js b/lib/cartodb/utils/icu_data_env_setter.js new file mode 100644 index 00000000..a4678821 --- /dev/null +++ b/lib/cartodb/utils/icu_data_env_setter.js @@ -0,0 +1,19 @@ +'use strict'; + +const glob = require('glob'); +const path = require('path'); + +// See https://github.com/CartoDB/support/issues/984 +// CartoCSS properties text-wrap-width/text-wrap-character not working +function setICUEnvVariable() { + if (process.env.ICU_DATA === undefined) { + const regexedPath = '/node_modules/mapnik/lib/binding/*/share/mapnik/icu/'; + const directory = glob.sync(path.join(__dirname, '../../..', regexedPath)); + + if (directory && directory.length > 0) { + process.env.ICU_DATA = directory[0]; + } + } +} + +module.exports = setICUEnvVariable; diff --git a/package.json b/package.json index 8443a49b..d523c029 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "dot": "~1.0.2", "express": "~4.16.0", "fastly-purge": "~1.0.1", + "glob": "^7.1.2", "log4js": "cartodb/log4js-node#cdb", "lru-cache": "2.6.5", "lzma": "~2.3.2", diff --git a/test/acceptance/label-wrap.js b/test/acceptance/label-wrap.js new file mode 100644 index 00000000..d4482b01 --- /dev/null +++ b/test/acceptance/label-wrap.js @@ -0,0 +1,69 @@ +require('../support/test_helper'); +var TestClient = require('../support/test-client'); + +var assert = require('../support/assert'); +var IMAGE_TOLERANCE = 5; + +describe('CartoCSS wrap', function () { + const options = { + sql: ` + SELECT + 5 as cartodb_id, + ST_Transform(ST_SetSRID(ST_MakePoint(-57.65625,-15.6230368),4326),3857) as the_geom_webmercator, + ST_SetSRID(ST_MakePoint(-57.65625,-15.62303683),4326) as the_geom, + 'South America' as continent + `, + cartocss: ` + #continent_points::labels { + text-name: [continent]; + text-face-name: 'DejaVu Sans Book'; + text-size: 10; + text-fill: lighten(#000,40); + text-transform: uppercase; + text-wrap-width: 30; + text-character-spacing: 2; + text-placement: point; + text-placement-type: dummy; + [zoom >= 3]{ + text-character-spacing: 2; + text-size: 11; + } + } + `, + cartocss_version: '3.0.12' + }; + + const type = 'mapnik'; + + const mapConfig = { + version: '1.6.0', + layers: [ + { + type, + id: 'layerLabel', + options + } + ] + }; + + + var keysToDelete; + + beforeEach(function () { + keysToDelete = {}; + }); + + afterEach(function (done) { + if (this.testClient) { + this.testClient.drain(done); + } + }); + + it("Label should be text-wrapped", function (done) { + this.testClient = new TestClient(mapConfig); + this.testClient.getTile(1, 0, 1, { layers: [0] }, (err, res, body) => { + var textWrapPath = './test/fixtures/text_wrap.png'; + assert.imageIsSimilarToFile(body, textWrapPath, IMAGE_TOLERANCE, done); + }); + }); +}); diff --git a/test/fixtures/text_wrap.png b/test/fixtures/text_wrap.png new file mode 100644 index 00000000..6841686e Binary files /dev/null and b/test/fixtures/text_wrap.png differ diff --git a/test/fixtures/text_wrap_bad.png b/test/fixtures/text_wrap_bad.png new file mode 100644 index 00000000..36f0ded6 Binary files /dev/null and b/test/fixtures/text_wrap_bad.png differ diff --git a/test/support/test_helper.js b/test/support/test_helper.js index 96a109ff..7d5c6a7c 100644 --- a/test/support/test_helper.js +++ b/test/support/test_helper.js @@ -15,12 +15,14 @@ var redis = require('redis'); var nock = require('nock'); var log4js = require('log4js'); var pg = require('pg'); +const setICUEnvVariable = require('../../lib/cartodb/utils/icu_data_env_setter'); // set environment specific variables global.environment = require(__dirname + '/../../config/environments/test'); global.environment.name = 'test'; process.env.NODE_ENV = 'test'; +setICUEnvVariable(); // don't output logs in test environment to reduce noise log4js.configure({ appenders: [] });