diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 656b946c9c..42f088ae7a 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -275,6 +275,19 @@ ol.format.KML.DEFAULT_STROKE_STYLE_ = new ol.style.Stroke({ }); +/** + * @const + * @type {ol.style.Text} + * @private + */ +ol.format.KML.DEFAULT_TEXT_STYLE_ = new ol.style.Text({ + font: 'normal 16px Helvetica', + fill: ol.format.KML.DEFAULT_FILL_STYLE_, + stroke: ol.format.KML.DEFAULT_STROKE_STYLE_, + scale: 1 +}); + + /** * @const * @type {ol.style.Style} @@ -283,7 +296,7 @@ ol.format.KML.DEFAULT_STROKE_STYLE_ = new ol.style.Stroke({ ol.format.KML.DEFAULT_STYLE_ = new ol.style.Style({ fill: ol.format.KML.DEFAULT_FILL_STYLE_, image: ol.format.KML.DEFAULT_IMAGE_STYLE_, - text: null, // FIXME + text: ol.format.KML.DEFAULT_TEXT_STYLE_, stroke: ol.format.KML.DEFAULT_STROKE_STYLE_, zIndex: 0 }); @@ -531,6 +544,34 @@ ol.format.KML.IconStyleParser_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + */ +ol.format.KML.LabelStyleParser_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'LabelStyle'); + // FIXME colorMode + var object = ol.xml.pushParseAndPop( + {}, ol.format.KML.LABEL_STYLE_PARSERS_, node, objectStack); + if (!goog.isDef(object)) { + return; + } + var styleObject = objectStack[objectStack.length - 1]; + goog.asserts.assert(goog.isObject(styleObject)); + var textStyle = new ol.style.Text({ + fill: new ol.style.Fill({ + color: /** @type {ol.Color} */ + (goog.object.get(object, 'color', ol.format.KML.DEFAULT_COLOR_)) + }), + scale: /** @type {number|undefined} */ + (goog.object.get(object, 'scale')) + }); + goog.object.set(styleObject, 'textStyle', textStyle); +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -910,6 +951,8 @@ ol.format.KML.readStyle_ = function(node, objectStack) { } var imageStyle = /** @type {ol.style.Image} */ (goog.object.get( styleObject, 'imageStyle', ol.format.KML.DEFAULT_IMAGE_STYLE_)); + var textStyle = /** @type {ol.style.Text} */ (goog.object.get( + styleObject, 'textStyle', ol.format.KML.DEFAULT_TEXT_STYLE_)); var strokeStyle = /** @type {ol.style.Stroke} */ (goog.object.get( styleObject, 'strokeStyle', ol.format.KML.DEFAULT_STROKE_STYLE_)); var outline = /** @type {boolean|undefined} */ @@ -921,7 +964,7 @@ ol.format.KML.readStyle_ = function(node, objectStack) { fill: fillStyle, image: imageStyle, stroke: strokeStyle, - text: null, // FIXME + text: textStyle, zIndex: undefined // FIXME })]; }; @@ -1238,6 +1281,18 @@ ol.format.KML.INNER_BOUNDARY_IS_PARSERS_ = ol.xml.makeParsersNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.KML.LABEL_STYLE_PARSERS_ = ol.xml.makeParsersNS( + ol.format.KML.NAMESPACE_URIS_, { + 'color': ol.xml.makeObjectPropertySetter(ol.format.KML.readColor_), + 'scale': ol.xml.makeObjectPropertySetter(ol.format.KML.readScale_) + }); + + /** * @const * @type {Object.>} @@ -1369,6 +1424,7 @@ ol.format.KML.SCHEMA_DATA_PARSERS_ = ol.xml.makeParsersNS( ol.format.KML.STYLE_PARSERS_ = ol.xml.makeParsersNS( ol.format.KML.NAMESPACE_URIS_, { 'IconStyle': ol.format.KML.IconStyleParser_, + 'LabelStyle': ol.format.KML.LabelStyleParser_, 'LineStyle': ol.format.KML.LineStyleParser_, 'PolyStyle': ol.format.KML.PolyStyleParser_ }); diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index d476ca2ebd..83b32c4a72 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -1267,7 +1267,7 @@ describe('ol.format.KML', function() { expect(imageStyle.getOrigin()).to.be(null); expect(imageStyle.getRotation()).to.eql(0); expect(imageStyle.getSize()).to.be(null); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getZIndex()).to.be(undefined); }); @@ -1310,7 +1310,42 @@ describe('ol.format.KML', function() { expect(imageStyle.getAnchor()).to.eql([24, 36]); expect(imageStyle.getOrigin()).to.eql([24, 108]); expect(imageStyle.getRotation()).to.eql(0); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); + expect(style.getZIndex()).to.be(undefined); + }); + + it('can read a feature\'s LabelStyle', function() { + var text = + '' + + ' ' + + ' ' + + ' ' + + ''; + var fs = format.readFeatures(text); + expect(fs).to.have.length(1); + var f = fs[0]; + expect(f).to.be.an(ol.Feature); + var styleFunction = f.getStyleFunction(); + expect(styleFunction).not.to.be(undefined); + var styleArray = styleFunction.call(f, 0); + expect(styleArray).to.be.an(Array); + expect(styleArray).to.have.length(1); + var style = styleArray[0]; + expect(style).to.be.an(ol.style.Style); + expect(style.getFill()).to.be(ol.format.KML.DEFAULT_FILL_STYLE_); + expect(style.getImage()).to.be(ol.format.KML.DEFAULT_IMAGE_STYLE_); + expect(style.getStroke()).to.be(ol.format.KML.DEFAULT_STROKE_STYLE_); + var textStyle = style.getText(); + expect(textStyle).to.be.an(ol.style.Text); + expect(textStyle.getScale()).to.be(0.5); + var textFillStyle = textStyle.getFill(); + expect(textFillStyle).to.be.an(ol.style.Fill); + expect(textFillStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]); expect(style.getZIndex()).to.be(undefined); }); @@ -1343,7 +1378,7 @@ describe('ol.format.KML', function() { expect(strokeStyle).to.be.an(ol.style.Stroke); expect(strokeStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]); expect(strokeStyle.getWidth()).to.be(9); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getZIndex()).to.be(undefined); }); @@ -1374,7 +1409,7 @@ describe('ol.format.KML', function() { expect(fillStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]); expect(style.getImage()).to.be(ol.format.KML.DEFAULT_IMAGE_STYLE_); expect(style.getStroke()).to.be(ol.format.KML.DEFAULT_STROKE_STYLE_); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getZIndex()).to.be(undefined); }); @@ -1415,7 +1450,7 @@ describe('ol.format.KML', function() { expect(strokeStyle).to.be.an(ol.style.Stroke); expect(strokeStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]); expect(strokeStyle.getWidth()).to.be(9); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getZIndex()).to.be(undefined); }); @@ -1452,7 +1487,7 @@ describe('ol.format.KML', function() { expect(strokeStyle).to.be.an(ol.style.Stroke); expect(strokeStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]); expect(strokeStyle.getWidth()).to.be(9); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getZIndex()).to.be(undefined); }); @@ -1488,7 +1523,7 @@ describe('ol.format.KML', function() { expect(fillStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]); expect(style.getImage()).to.be(ol.format.KML.DEFAULT_IMAGE_STYLE_); expect(style.getStroke()).to.be(null); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getZIndex()).to.be(undefined); }); @@ -1524,7 +1559,7 @@ describe('ol.format.KML', function() { expect(style.getFill()).to.be(null); expect(style.getImage()).to.be(ol.format.KML.DEFAULT_IMAGE_STYLE_); expect(style.getStroke()).to.be(null); - expect(style.getText()).to.be(null); + expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getZIndex()).to.be(undefined); });