From 5e86ffcca5e19069cc095044d48471505949bf08 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 7 Oct 2013 15:43:17 -0600 Subject: [PATCH] Add stroke properties to text symbolizer literal --- src/ol/style/textliteral.js | 37 +++++++++++++++++++ test/spec/ol/style/textliteral.test.js | 50 ++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/ol/style/textliteral.js b/src/ol/style/textliteral.js index ec3036bed3..23e06cad90 100644 --- a/src/ol/style/textliteral.js +++ b/src/ol/style/textliteral.js @@ -10,6 +10,9 @@ goog.require('ol.style.Literal'); * fontSize: number, * text: string, * opacity: number, + * strokeColor: (string|undefined), + * strokeOpacity: (number|undefined), + * strokeWidth: (number|undefined), * zIndex: number}} */ ol.style.TextLiteralOptions; @@ -43,6 +46,37 @@ ol.style.TextLiteral = function(options) { /** @type {number} */ this.opacity = options.opacity; + /** @type {string|undefined} */ + this.strokeColor = options.strokeColor; + if (goog.isDef(this.strokeColor)) { + goog.asserts.assertString( + this.strokeColor, 'strokeColor must be a string'); + } + + /** @type {number|undefined} */ + this.strokeOpacity = options.strokeOpacity; + if (goog.isDef(this.strokeOpacity)) { + goog.asserts.assertNumber( + this.strokeOpacity, 'strokeOpacity must be a number'); + } + + /** @type {number|undefined} */ + this.strokeWidth = options.strokeWidth; + if (goog.isDef(this.strokeWidth)) { + goog.asserts.assertNumber( + this.strokeWidth, 'strokeWidth must be a number'); + } + + // if any stroke property is defined, all must be defined + var strokeDef = goog.isDef(this.strokeColor) && + goog.isDef(this.strokeOpacity) && + goog.isDef(this.strokeWidth); + var strokeUndef = !goog.isDef(this.strokeColor) && + !goog.isDef(this.strokeOpacity) && + !goog.isDef(this.strokeWidth); + goog.asserts.assert(strokeDef || strokeUndef, + 'If any stroke property is defined, all must be defined'); + goog.asserts.assertNumber(options.zIndex, 'zIndex must be a number'); /** @type {number} */ this.zIndex = options.zIndex; @@ -59,5 +93,8 @@ ol.style.TextLiteral.prototype.equals = function(other) { this.fontFamily == other.fontFamily && this.fontSize == other.fontSize && this.opacity == other.opacity && + this.strokeColor == other.strokeColor && + this.strokeOpacity == other.strokeOpacity && + this.strokeWidth == other.strokeWidth && this.zIndex == other.zIndex; }; diff --git a/test/spec/ol/style/textliteral.test.js b/test/spec/ol/style/textliteral.test.js index b3b945675a..0ad48b7835 100644 --- a/test/spec/ol/style/textliteral.test.js +++ b/test/spec/ol/style/textliteral.test.js @@ -2,6 +2,54 @@ goog.provide('ol.test.style.TextLiteral'); describe('ol.style.TextLiteral', function() { + describe('constructor', function() { + + it('creates a new literal', function() { + var literal = new ol.style.TextLiteral({ + color: '#ff0000', + fontFamily: 'Arial', + fontSize: 11, + text: 'Test', + opacity: 0.5, + zIndex: 0 + }); + expect(literal).to.be.a(ol.style.Literal); + expect(literal).to.be.a(ol.style.TextLiteral); + }); + + it('accepts stroke properties', function() { + var literal = new ol.style.TextLiteral({ + color: '#ff0000', + fontFamily: 'Arial', + fontSize: 11, + text: 'Test', + opacity: 0.5, + strokeColor: '#ff0000', + strokeWidth: 2, + strokeOpacity: 0.5, + zIndex: 0 + }); + expect(literal).to.be.a(ol.style.TextLiteral); + }); + + it('throws with incomplete stroke properties', function() { + expect(function() { + new ol.style.TextLiteral({ + color: '#ff0000', + fontFamily: 'Arial', + fontSize: 11, + text: 'Test', + opacity: 0.5, + strokeColor: '#ff0000', + zIndex: 0 + }); + }).throwException(function(err) { + expect(err).to.be.a(goog.asserts.AssertionError); + }); + }); + + }); + describe('#equals()', function() { it('identifies equal literals', function() { @@ -82,4 +130,6 @@ describe('ol.style.TextLiteral', function() { }); +goog.require('goog.asserts.AssertionError'); +goog.require('ol.style.Literal'); goog.require('ol.style.TextLiteral');