goog.require('ol.Feature'); goog.require('ol.geom.Point'); goog.require('ol.Map'); goog.require('ol.View'); goog.require('ol.layer.Vector'); goog.require('ol.source.Vector'); goog.require('ol.style.Text'); goog.require('ol.style.Fill'); goog.require('ol.style.Style'); goog.require('ol.style.Stroke'); describe('ol.rendering.style.Text', function() { var map, vectorSource; function createMap(renderer) { vectorSource = new ol.source.Vector(); var vectorLayer = new ol.layer.Vector({ source: vectorSource }); map = new ol.Map({ pixelRatio: 1, target: createMapDiv(200, 200), renderer: renderer, layers: [vectorLayer], view: new ol.View({ projection: 'EPSG:4326', center: [0, 0], resolution: 1 }) }); } afterEach(function() { if (map) { disposeMap(map); map = null; } }); describe('#render', function() { function createFeatures() { var feature; feature = new ol.Feature({ geometry: new ol.geom.Point([-20, 18]) }); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'hello', font: '10px' }) })); vectorSource.addFeature(feature); feature = new ol.Feature({ geometry: new ol.geom.Point([-10, 0]) }); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'hello', fill: new ol.style.Fill({ color: 'red', font: '12px' }), stroke: new ol.style.Stroke({ color: '#000', width: 3 }) }) })); vectorSource.addFeature(feature); feature = new ol.Feature({ geometry: new ol.geom.Point([20, 10]) }); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ rotateWithView: true, text: 'hello', font: '10px', stroke: new ol.style.Stroke({ color: [10, 10, 10, 0.5] }) }) })); vectorSource.addFeature(feature); } it('tests the canvas renderer without rotation', function(done) { createMap('canvas'); createFeatures(); expectResemble(map, 'rendering/ol/style/expected/text-canvas.png', IMAGE_TOLERANCE, done); }); it('tests the canvas renderer with rotation', function(done) { createMap('canvas'); createFeatures(); map.getView().setRotation(Math.PI / 7); expectResemble(map, 'rendering/ol/style/expected/text-rotated-canvas.png', IMAGE_TOLERANCE, done); }); it('renders multiline text with alignment options', function(done) { createMap('canvas'); var feature; feature = new ol.Feature(new ol.geom.Point([25, 0])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'Hello world\nleft', font: 'bold 14px sans-serif', textAlign: 'left' }) })); vectorSource.addFeature(feature); feature = new ol.Feature(new ol.geom.Point([-25, 0])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'Hello world\nright', font: 'bold 14px sans-serif', textAlign: 'right' }) })); vectorSource.addFeature(feature); feature = new ol.Feature(new ol.geom.Point([0, 25])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'Hello world\nbottom', font: 'bold 14px sans-serif', textBaseline: 'bottom' }) })); vectorSource.addFeature(feature); feature = new ol.Feature(new ol.geom.Point([0, -25])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'top\nHello world', font: 'bold 14px sans-serif', textBaseline: 'top' }) })); vectorSource.addFeature(feature); expectResemble(map, 'rendering/ol/style/expected/text-align-offset-canvas.png', 5, done); }); it('renders multiline text with positioning options', function(done) { createMap('canvas'); var feature; feature = new ol.Feature(new ol.geom.Point([0, 0])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'Hello world\nleft', font: 'bold 14px sans-serif', textAlign: 'left', offsetX: 25 }) })); vectorSource.addFeature(feature); feature = new ol.Feature(new ol.geom.Point([0, 0])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'Hello world\nright', font: 'bold 14px sans-serif', textAlign: 'right', offsetX: -25 }) })); vectorSource.addFeature(feature); feature = new ol.Feature(new ol.geom.Point([0, 0])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'Hello world\nbottom', font: 'bold 14px sans-serif', textBaseline: 'bottom', offsetY: -25 }) })); vectorSource.addFeature(feature); feature = new ol.Feature(new ol.geom.Point([0, 0])); feature.setStyle(new ol.style.Style({ text: new ol.style.Text({ text: 'top\nHello world', font: 'bold 14px sans-serif', textBaseline: 'top', offsetY: 25 }) })); vectorSource.addFeature(feature); expectResemble(map, 'rendering/ol/style/expected/text-align-offset-canvas.png', 5, done); }); where('WebGL').it('tests the webgl renderer without rotation', function(done) { createMap('webgl'); createFeatures(); expectResemble(map, 'rendering/ol/style/expected/text-webgl.png', 1.8, done); }); where('WebGL').it('tests the webgl renderer with rotation', function(done) { createMap('webgl'); createFeatures(); map.getView().setRotation(Math.PI / 7); expectResemble(map, 'rendering/ol/style/expected/text-rotated-webgl.png', 1.8, done); }); }); });