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:
@@ -129,7 +129,9 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, {
|
||||
'strokeOpacity': 1
|
||||
}, style);
|
||||
this.features[feature.id] = [feature, style];
|
||||
this.geometryMap[feature.geometry.id] = feature.id;
|
||||
if (feature.geometry) {
|
||||
this.geometryMap[feature.geometry.id] = feature.id;
|
||||
}
|
||||
this.redraw();
|
||||
},
|
||||
|
||||
@@ -142,7 +144,6 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, {
|
||||
* Parameters:
|
||||
* geometry - {<OpenLayers.Geometry>}
|
||||
* style - {Object}
|
||||
* featureId - {<String>}
|
||||
*/
|
||||
drawGeometry: function(geometry, style) {
|
||||
var className = geometry.CLASS_NAME;
|
||||
@@ -334,6 +335,52 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, {
|
||||
); // inner rings are 'empty'
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: drawText
|
||||
* This method is only called by the renderer itself.
|
||||
*
|
||||
* Parameters:
|
||||
* location - {<OpenLayers.Point>}
|
||||
* style - {Object}
|
||||
*/
|
||||
drawText: function(location, style) {
|
||||
var pt = this.getLocalXY(location);
|
||||
|
||||
this.setCanvasStyle("reset");
|
||||
this.canvas.fillStyle = style.fontColor;
|
||||
this.canvas.globalAlpha = 1;
|
||||
var fontStyle = style.fontWeight + " " + style.fontSize + " " + style.fontFamily;
|
||||
if (this.canvas.fillText) {
|
||||
// HTML5
|
||||
var labelAlign =
|
||||
OpenLayers.Renderer.Canvas.LABEL_ALIGN[style.labelAlign[0]] ||
|
||||
"middle";
|
||||
this.canvas.font = fontStyle;
|
||||
this.canvas.textAlign = labelAlign;
|
||||
this.canvas.fillText(style.label, pt[0], pt[1]);
|
||||
} else if (this.canvas.mozDrawText) {
|
||||
// Mozilla pre-Gecko1.9.1 (<FF3.1)
|
||||
this.canvas.mozTextStyle = fontStyle;
|
||||
// No built-in text alignment, so we measure and adjust the position
|
||||
var len = this.canvas.mozMeasureText(style.label);
|
||||
switch(style.labelAlign[0]) {
|
||||
case "l":
|
||||
break;
|
||||
case "r":
|
||||
pt[0] -= len;
|
||||
break;
|
||||
case "c":
|
||||
default:
|
||||
pt[0] -= len / 2;
|
||||
}
|
||||
this.canvas.translate(pt[0], pt[1]);
|
||||
|
||||
this.canvas.mozDrawText(style.label);
|
||||
this.canvas.translate(-1*pt[0], -1*pt[1]);
|
||||
}
|
||||
this.setCanvasStyle("reset");
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: getLocalXY
|
||||
@@ -415,13 +462,34 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, {
|
||||
redraw: function() {
|
||||
if (!this.locked) {
|
||||
this.clear();
|
||||
var labelMap = [];
|
||||
var feature, style;
|
||||
for (var id in this.features) {
|
||||
if (!this.features.hasOwnProperty(id)) { continue; }
|
||||
if (!this.features[id][0].geometry) { continue; }
|
||||
this.drawGeometry(this.features[id][0].geometry, this.features[id][1]);
|
||||
feature = this.features[id][0];
|
||||
style = this.features[id][1];
|
||||
if (!feature.geometry) { continue; }
|
||||
this.drawGeometry(feature.geometry, style);
|
||||
if(style.label) {
|
||||
labelMap.push([feature, style]);
|
||||
}
|
||||
}
|
||||
var item;
|
||||
for (var i=0; len=labelMap.length, i<len; ++i) {
|
||||
item = labelMap[i];
|
||||
this.drawText(item[0].geometry.getCentroid(), item[1]);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
CLASS_NAME: "OpenLayers.Renderer.Canvas"
|
||||
});
|
||||
|
||||
/**
|
||||
* Constant: OpenLayers.Renderer.Canvas.LABEL_ALIGN
|
||||
* {Object}
|
||||
*/
|
||||
OpenLayers.Renderer.Canvas.LABEL_ALIGN = {
|
||||
"l": "left",
|
||||
"r": "right"
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user