added support for text labels. This also adds getCentroid methods to all
geometries. Thanks crschmidt for the great help with this patch, and thanks to camptocamp for the initial work on this and rcoup for creating the first patches. r=crschmidt (closes #1895) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9262 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -45,6 +45,12 @@ OpenLayers.Renderer.SVG = OpenLayers.Class(OpenLayers.Renderer.Elements, {
|
||||
* {Object} Cache for symbol sizes according to their svg coordinate space
|
||||
*/
|
||||
symbolSize: {},
|
||||
|
||||
/**
|
||||
* Property: isGecko
|
||||
* {Boolean}
|
||||
*/
|
||||
isGecko: null,
|
||||
|
||||
/**
|
||||
* Constructor: OpenLayers.Renderer.SVG
|
||||
@@ -59,6 +65,7 @@ OpenLayers.Renderer.SVG = OpenLayers.Class(OpenLayers.Renderer.Elements, {
|
||||
OpenLayers.Renderer.Elements.prototype.initialize.apply(this,
|
||||
arguments);
|
||||
this.translationParameters = {x: 0, y: 0};
|
||||
this.isGecko = (navigator.userAgent.toLowerCase().indexOf("gecko/") != -1);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -337,6 +344,7 @@ OpenLayers.Renderer.SVG = OpenLayers.Class(OpenLayers.Renderer.Elements, {
|
||||
if (style.cursor != null) {
|
||||
node.setAttributeNS(null, "cursor", style.cursor);
|
||||
}
|
||||
|
||||
return node;
|
||||
},
|
||||
|
||||
@@ -416,11 +424,14 @@ OpenLayers.Renderer.SVG = OpenLayers.Class(OpenLayers.Renderer.Elements, {
|
||||
/**
|
||||
* Method: createRoot
|
||||
*
|
||||
* Parameter:
|
||||
* suffix - {String} suffix to append to the id
|
||||
*
|
||||
* Returns:
|
||||
* {DOMElement} The main root element to which we'll add vectors
|
||||
* {DOMElement}
|
||||
*/
|
||||
createRoot: function() {
|
||||
return this.nodeFactory(this.container.id + "_root", "g");
|
||||
createRoot: function(suffix) {
|
||||
return this.nodeFactory(this.container.id + suffix, "g");
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -633,7 +644,61 @@ OpenLayers.Renderer.SVG = OpenLayers.Class(OpenLayers.Renderer.Elements, {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: drawText
|
||||
* This method is only called by the renderer itself.
|
||||
*
|
||||
* Parameters:
|
||||
* featureId - {String}
|
||||
* style -
|
||||
* location - {<OpenLayers.Geometry.Point>}
|
||||
*/
|
||||
drawText: function(featureId, style, location) {
|
||||
var resolution = this.getResolution();
|
||||
|
||||
var x = (location.x / resolution + this.left);
|
||||
var y = (location.y / resolution - this.top);
|
||||
|
||||
var label = this.nodeFactory(featureId + this.LABEL_ID_SUFFIX, "text");
|
||||
var tspan = this.nodeFactory(featureId + this.LABEL_ID_SUFFIX + "_tspan", "tspan");
|
||||
|
||||
label.setAttributeNS(null, "x", x);
|
||||
label.setAttributeNS(null, "y", -y);
|
||||
label.setAttributeNS(null, "pointer-events", "none");
|
||||
|
||||
if (style.fontColor) {
|
||||
label.setAttributeNS(null, "fill", style.fontColor);
|
||||
}
|
||||
if (style.fontFamily) {
|
||||
label.setAttributeNS(null, "font-family", style.fontFamily);
|
||||
}
|
||||
if (style.fontSize) {
|
||||
label.setAttributeNS(null, "font-size", style.fontSize);
|
||||
}
|
||||
if (style.fontWeight) {
|
||||
label.setAttributeNS(null, "font-weight", style.fontWeight);
|
||||
}
|
||||
var align = style.labelAlign || "cm";
|
||||
label.setAttributeNS(null, "text-anchor",
|
||||
OpenLayers.Renderer.SVG.LABEL_ALIGN[align[0]] || "middle");
|
||||
|
||||
if (this.isGecko) {
|
||||
label.setAttributeNS(null, "dominant-baseline",
|
||||
OpenLayers.Renderer.SVG.LABEL_ALIGN[align[1]] || "central");
|
||||
} else {
|
||||
tspan.setAttributeNS(null, "baseline-shift",
|
||||
OpenLayers.Renderer.SVG.LABEL_VSHIFT[align[1]] || "-35%");
|
||||
}
|
||||
|
||||
tspan.textContent = style.label;
|
||||
|
||||
if(!label.parentNode) {
|
||||
label.appendChild(tspan);
|
||||
this.textRoot.appendChild(label);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: getComponentString
|
||||
*
|
||||
@@ -828,3 +893,28 @@ OpenLayers.Renderer.SVG = OpenLayers.Class(OpenLayers.Renderer.Elements, {
|
||||
|
||||
CLASS_NAME: "OpenLayers.Renderer.SVG"
|
||||
});
|
||||
|
||||
/**
|
||||
* Constant: OpenLayers.Renderer.SVG.LABEL_ALIGN
|
||||
* {Object}
|
||||
*/
|
||||
OpenLayers.Renderer.SVG.LABEL_ALIGN = {
|
||||
"l": "start",
|
||||
"r": "end",
|
||||
"b": "bottom",
|
||||
"t": "hanging"
|
||||
};
|
||||
|
||||
/**
|
||||
* Constant: OpenLayers.Renderer.SVG.LABEL_VSHIFT
|
||||
* {Object}
|
||||
*/
|
||||
OpenLayers.Renderer.SVG.LABEL_VSHIFT = {
|
||||
// according to
|
||||
// http://www.w3.org/Graphics/SVG/Test/20061213/htmlObjectHarness/full-text-align-02-b.html
|
||||
// a baseline-shift of -70% shifts the text exactly from the
|
||||
// bottom to the top of the baseline, so -35% moves the text to
|
||||
// the center of the baseline.
|
||||
"t": "-70%",
|
||||
"b": "0"
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user