Allow fill, image, and stroke in ol.style.Style to be undefined

This commit is contained in:
Tom Payne
2013-11-12 17:17:18 +01:00
parent fc2fece872
commit c47dee7b8c
5 changed files with 110 additions and 58 deletions

View File

@@ -40,9 +40,9 @@ ol.render.canvas.Immediate = function(context, extent, transform) {
/** /**
* @private * @private
* @type {{fillStyle: ?ol.style.Fill, * @type {{fillStyle: ol.style.Fill,
* imageStyle: ?ol.style.Image, * imageStyle: ol.style.Image,
* strokeStyle: ?ol.style.Stroke}} * strokeStyle: ol.style.Stroke}}
*/ */
this.state_ = { this.state_ = {
fillStyle: null, fillStyle: null,
@@ -67,7 +67,7 @@ ol.render.canvas.Immediate.prototype.drawImages_ = function(geometry) {
var context = this.context_; var context = this.context_;
var imageStyle = this.state_.imageStyle; var imageStyle = this.state_.imageStyle;
if (!ol.extent.intersects(this.extent_, geometry.getExtent()) || if (!ol.extent.intersects(this.extent_, geometry.getExtent()) ||
goog.isNull(imageStyle)) { !goog.isDefAndNotNull(imageStyle)) {
return; return;
} }
var pixelCoordinates = ol.geom.transformGeometry2D( var pixelCoordinates = ol.geom.transformGeometry2D(
@@ -164,7 +164,7 @@ ol.render.canvas.Immediate.prototype.drawMultiPointGeometry =
ol.render.canvas.Immediate.prototype.drawLineStringGeometry = ol.render.canvas.Immediate.prototype.drawLineStringGeometry =
function(lineStringGeometry) { function(lineStringGeometry) {
if (!ol.extent.intersects(this.extent_, lineStringGeometry.getExtent()) || if (!ol.extent.intersects(this.extent_, lineStringGeometry.getExtent()) ||
goog.isNull(this.state_.strokeStyle)) { !goog.isDefAndNotNull(this.state_.strokeStyle)) {
return; return;
} }
var context = this.context_; var context = this.context_;
@@ -183,7 +183,7 @@ ol.render.canvas.Immediate.prototype.drawMultiLineStringGeometry =
function(multiLineStringGeometry) { function(multiLineStringGeometry) {
var geometryExtent = multiLineStringGeometry.getExtent(); var geometryExtent = multiLineStringGeometry.getExtent();
if (!ol.extent.intersects(this.extent_, geometryExtent) || if (!ol.extent.intersects(this.extent_, geometryExtent) ||
goog.isNull(this.state_.strokeStyle)) { !goog.isDefAndNotNull(this.state_.strokeStyle)) {
return; return;
} }
var context = this.context_; var context = this.context_;
@@ -209,7 +209,8 @@ ol.render.canvas.Immediate.prototype.drawPolygonGeometry =
return; return;
} }
var state = this.state_; var state = this.state_;
if (goog.isNull(this.fillStyle) && goog.isNull(this.strokeStyle)) { if (!goog.isDefAndNotNull(state.fillStyle) &&
!goog.isDefAndNotNull(state.strokeStyle)) {
return; return;
} }
var context = this.context_; var context = this.context_;
@@ -218,10 +219,10 @@ ol.render.canvas.Immediate.prototype.drawPolygonGeometry =
var ends = polygonGeometry.getEnds(); var ends = polygonGeometry.getEnds();
context.beginPath(); context.beginPath();
this.drawRings_(pixelCoordinates, 0, ends); this.drawRings_(pixelCoordinates, 0, ends);
if (!goog.isNull(state.fillStyle)) { if (goog.isDefAndNotNull(state.fillStyle)) {
context.fill(); context.fill();
} }
if (!goog.isNull(state.strokeStyle)) { if (goog.isDefAndNotNull(state.strokeStyle)) {
context.stroke(); context.stroke();
} }
}; };
@@ -236,7 +237,8 @@ ol.render.canvas.Immediate.prototype.drawMultiPolygonGeometry =
return; return;
} }
var state = this.state_; var state = this.state_;
if (goog.isNull(this.fillStyle) && goog.isNull(this.strokeStyle)) { if (!goog.isDefAndNotNull(state.fillStyle) &&
!goog.isDefAndNotNull(state.strokeStyle)) {
return; return;
} }
var context = this.context_; var context = this.context_;
@@ -249,10 +251,10 @@ ol.render.canvas.Immediate.prototype.drawMultiPolygonGeometry =
var ends = endss[i]; var ends = endss[i];
context.beginPath(); context.beginPath();
offset = this.drawRings_(pixelCoordinates, offset, ends); offset = this.drawRings_(pixelCoordinates, offset, ends);
if (!goog.isNull(state.fillStyle)) { if (goog.isDefAndNotNull(state.fillStyle)) {
context.fill(); context.fill();
} }
if (!goog.isNull(state.strokeStyle)) { if (goog.isDefAndNotNull(state.strokeStyle)) {
context.stroke(); context.stroke();
} }
} }
@@ -267,12 +269,16 @@ ol.render.canvas.Immediate.prototype.setFillStrokeStyle =
var context = this.context_; var context = this.context_;
var state = this.state_; var state = this.state_;
if (!ol.style.fill.equals(state.fillStyle, fillStyle)) { if (!ol.style.fill.equals(state.fillStyle, fillStyle)) {
if (goog.isDefAndNotNull(fillStyle)) {
context.fillStyle = fillStyle.color; context.fillStyle = fillStyle.color;
}
state.fillStyle = fillStyle; state.fillStyle = fillStyle;
} }
if (!ol.style.stroke.equals(state.strokeStyle, strokeStyle)) { if (!ol.style.stroke.equals(state.strokeStyle, strokeStyle)) {
if (goog.isDefAndNotNull(strokeStyle)) {
context.strokeStyle = strokeStyle.color; context.strokeStyle = strokeStyle.color;
context.lineWidth = strokeStyle.width; context.lineWidth = strokeStyle.width;
}
state.strokeStyle = strokeStyle; state.strokeStyle = strokeStyle;
} }
}; };

View File

@@ -227,7 +227,7 @@ ol.render.canvas.ImageReplay = function() {
/** /**
* @private * @private
* @type {?ol.style.Image} * @type {ol.style.Image}
*/ */
this.imageStyle_ = null; this.imageStyle_ = null;
@@ -255,7 +255,9 @@ ol.render.canvas.ImageReplay.prototype.drawCoordinates_ =
*/ */
ol.render.canvas.ImageReplay.prototype.drawPointGeometry = ol.render.canvas.ImageReplay.prototype.drawPointGeometry =
function(pointGeometry) { function(pointGeometry) {
goog.asserts.assert(!goog.isNull(this.imageStyle_)); if (!goog.isDefAndNotNull(this.imageStyle_)) {
return;
}
ol.extent.extend(this.extent_, pointGeometry.getExtent()); ol.extent.extend(this.extent_, pointGeometry.getExtent());
var flatCoordinates = pointGeometry.getFlatCoordinates(); var flatCoordinates = pointGeometry.getFlatCoordinates();
var stride = pointGeometry.getStride(); var stride = pointGeometry.getStride();
@@ -271,7 +273,9 @@ ol.render.canvas.ImageReplay.prototype.drawPointGeometry =
*/ */
ol.render.canvas.ImageReplay.prototype.drawMultiPointGeometry = ol.render.canvas.ImageReplay.prototype.drawMultiPointGeometry =
function(multiPointGeometry) { function(multiPointGeometry) {
goog.asserts.assert(!goog.isNull(this.imageStyle_)); if (!goog.isDefAndNotNull(this.imageStyle_)) {
return;
}
ol.extent.extend(this.extent_, multiPointGeometry.getExtent()); ol.extent.extend(this.extent_, multiPointGeometry.getExtent());
var flatCoordinates = multiPointGeometry.getFlatCoordinates(); var flatCoordinates = multiPointGeometry.getFlatCoordinates();
var stride = multiPointGeometry.getStride(); var stride = multiPointGeometry.getStride();
@@ -311,9 +315,9 @@ ol.render.canvas.LineStringReplay = function() {
/** /**
* @private * @private
* @type {{currentStrokeStyle: ?ol.style.Stroke, * @type {{currentStrokeStyle: ol.style.Stroke,
* lastStroke: number, * lastStroke: number,
* strokeStyle: ?ol.style.Stroke}|null} * strokeStyle: ol.style.Stroke}|null}
*/ */
this.state_ = { this.state_ = {
currentStrokeStyle: null, currentStrokeStyle: null,
@@ -336,15 +340,17 @@ goog.inherits(ol.render.canvas.LineStringReplay, ol.render.canvas.Replay);
ol.render.canvas.LineStringReplay.prototype.drawFlatCoordinates_ = ol.render.canvas.LineStringReplay.prototype.drawFlatCoordinates_ =
function(flatCoordinates, offset, end, stride) { function(flatCoordinates, offset, end, stride) {
var state = this.state_; var state = this.state_;
if (!ol.style.stroke.equals(state.currentStrokeStyle, state.strokeStyle)) { var strokeStyle = state.strokeStyle;
goog.asserts.assert(goog.isDefAndNotNull(strokeStyle));
if (!ol.style.stroke.equals(state.currentStrokeStyle, strokeStyle)) {
if (state.lastStroke != this.coordinates.length) { if (state.lastStroke != this.coordinates.length) {
this.instructions.push([ol.render.canvas.Instruction.STROKE]); this.instructions.push([ol.render.canvas.Instruction.STROKE]);
state.lastStroke = this.coordinates.length; state.lastStroke = this.coordinates.length;
} }
this.instructions.push( this.instructions.push(
[ol.render.canvas.Instruction.SET_STROKE_STYLE, state.strokeStyle], [ol.render.canvas.Instruction.SET_STROKE_STYLE, strokeStyle],
[ol.render.canvas.Instruction.BEGIN_PATH]); [ol.render.canvas.Instruction.BEGIN_PATH]);
state.currentStrokeStyle = state.strokeStyle; state.currentStrokeStyle = strokeStyle;
} }
var myEnd = this.appendFlatCoordinates( var myEnd = this.appendFlatCoordinates(
flatCoordinates, offset, end, stride, false); flatCoordinates, offset, end, stride, false);
@@ -358,7 +364,12 @@ ol.render.canvas.LineStringReplay.prototype.drawFlatCoordinates_ =
*/ */
ol.render.canvas.LineStringReplay.prototype.drawLineStringGeometry = ol.render.canvas.LineStringReplay.prototype.drawLineStringGeometry =
function(lineStringGeometry) { function(lineStringGeometry) {
goog.asserts.assert(!goog.isNull(this.state_)); var state = this.state_;
goog.asserts.assert(!goog.isNull(state));
var strokeStyle = state.strokeStyle;
if (!goog.isDefAndNotNull(strokeStyle)) {
return;
}
ol.extent.extend(this.extent_, lineStringGeometry.getExtent()); ol.extent.extend(this.extent_, lineStringGeometry.getExtent());
var flatCoordinates = lineStringGeometry.getFlatCoordinates(); var flatCoordinates = lineStringGeometry.getFlatCoordinates();
var stride = lineStringGeometry.getStride(); var stride = lineStringGeometry.getStride();
@@ -372,7 +383,12 @@ ol.render.canvas.LineStringReplay.prototype.drawLineStringGeometry =
*/ */
ol.render.canvas.LineStringReplay.prototype.drawMultiLineStringGeometry = ol.render.canvas.LineStringReplay.prototype.drawMultiLineStringGeometry =
function(multiLineStringGeometry) { function(multiLineStringGeometry) {
goog.asserts.assert(!goog.isNull(this.state_)); var state = this.state_;
goog.asserts.assert(!goog.isNull(state));
var strokeStyle = state.strokeStyle;
if (!goog.isDefAndNotNull(strokeStyle)) {
return;
}
ol.extent.extend(this.extent_, multiLineStringGeometry.getExtent()); ol.extent.extend(this.extent_, multiLineStringGeometry.getExtent());
var ends = multiLineStringGeometry.getEnds(); var ends = multiLineStringGeometry.getEnds();
var flatCoordinates = multiLineStringGeometry.getFlatCoordinates(); var flatCoordinates = multiLineStringGeometry.getFlatCoordinates();
@@ -423,10 +439,10 @@ ol.render.canvas.PolygonReplay = function() {
/** /**
* @private * @private
* @type {{currentFillStyle: ?ol.style.Fill, * @type {{currentFillStyle: ol.style.Fill,
* currentStrokeStyle: ?ol.style.Stroke, * currentStrokeStyle: ol.style.Stroke,
* fillStyle: ?ol.style.Fill, * fillStyle: ol.style.Fill,
* strokeStyle: ?ol.style.Stroke}|null} * strokeStyle: ol.style.Stroke}|null}
*/ */
this.state_ = { this.state_ = {
currentFillStyle: null, currentFillStyle: null,
@@ -463,10 +479,10 @@ ol.render.canvas.PolygonReplay.prototype.drawFlatCoordinatess_ =
} }
// FIXME is it quicker to fill and stroke each polygon individually, // FIXME is it quicker to fill and stroke each polygon individually,
// FIXME or all polygons together? // FIXME or all polygons together?
if (!goog.isNull(state.fillStyle)) { if (goog.isDefAndNotNull(state.fillStyle)) {
this.instructions.push([ol.render.canvas.Instruction.FILL]); this.instructions.push([ol.render.canvas.Instruction.FILL]);
} }
if (!goog.isNull(state.strokeStyle)) { if (goog.isDefAndNotNull(state.strokeStyle)) {
this.instructions.push([ol.render.canvas.Instruction.STROKE]); this.instructions.push([ol.render.canvas.Instruction.STROKE]);
} }
return offset; return offset;
@@ -478,7 +494,12 @@ ol.render.canvas.PolygonReplay.prototype.drawFlatCoordinatess_ =
*/ */
ol.render.canvas.PolygonReplay.prototype.drawPolygonGeometry = ol.render.canvas.PolygonReplay.prototype.drawPolygonGeometry =
function(polygonGeometry) { function(polygonGeometry) {
goog.asserts.assert(!goog.isNull(this.state_)); var state = this.state_;
goog.asserts.assert(!goog.isNull(state));
if (!goog.isDefAndNotNull(state.fillStyle) &&
!goog.isDefAndNotNull(state.strokeStyle)) {
return;
}
ol.extent.extend(this.extent_, polygonGeometry.getExtent()); ol.extent.extend(this.extent_, polygonGeometry.getExtent());
this.setFillStrokeStyles_(); this.setFillStrokeStyles_();
var ends = polygonGeometry.getEnds(); var ends = polygonGeometry.getEnds();
@@ -493,7 +514,12 @@ ol.render.canvas.PolygonReplay.prototype.drawPolygonGeometry =
*/ */
ol.render.canvas.PolygonReplay.prototype.drawMultiPolygonGeometry = ol.render.canvas.PolygonReplay.prototype.drawMultiPolygonGeometry =
function(multiPolygonGeometry) { function(multiPolygonGeometry) {
goog.asserts.assert(!goog.isNull(this.state_)); var state = this.state_;
goog.asserts.assert(!goog.isNull(state));
if (!goog.isDefAndNotNull(state.fillStyle) &&
!goog.isDefAndNotNull(state.strokeStyle)) {
return;
}
ol.extent.extend(this.extent_, multiPolygonGeometry.getExtent()); ol.extent.extend(this.extent_, multiPolygonGeometry.getExtent());
this.setFillStrokeStyles_(); this.setFillStrokeStyles_();
var endss = multiPolygonGeometry.getEndss(); var endss = multiPolygonGeometry.getEndss();
@@ -523,7 +549,6 @@ ol.render.canvas.PolygonReplay.prototype.finish = function() {
ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyle = ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyle =
function(fillStyle, strokeStyle) { function(fillStyle, strokeStyle) {
goog.asserts.assert(!goog.isNull(this.state_)); goog.asserts.assert(!goog.isNull(this.state_));
goog.asserts.assert(!goog.isNull(fillStyle) || !goog.isNull(strokeStyle));
this.state_.fillStyle = fillStyle; this.state_.fillStyle = fillStyle;
this.state_.strokeStyle = strokeStyle; this.state_.strokeStyle = strokeStyle;
}; };
@@ -534,13 +559,13 @@ ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyle =
*/ */
ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyles_ = function() { ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyles_ = function() {
var state = this.state_; var state = this.state_;
if (!goog.isNull(state.fillStyle) && if (goog.isDefAndNotNull(state.fillStyle) &&
!ol.style.fill.equals(state.currentFillStyle, state.fillStyle)) { !ol.style.fill.equals(state.currentFillStyle, state.fillStyle)) {
this.instructions.push( this.instructions.push(
[ol.render.canvas.Instruction.SET_FILL_STYLE, state.fillStyle]); [ol.render.canvas.Instruction.SET_FILL_STYLE, state.fillStyle]);
state.currentFillStyle = state.fillStyle; state.currentFillStyle = state.fillStyle;
} }
if (!goog.isNull(state.strokeStyle) && if (goog.isDefAndNotNull(state.strokeStyle) &&
!ol.style.stroke.equals(state.currentStrokeStyle, state.strokeStyle)) { !ol.style.stroke.equals(state.currentStrokeStyle, state.strokeStyle)) {
this.instructions.push( this.instructions.push(
[ol.render.canvas.Instruction.SET_STROKE_STYLE, state.strokeStyle]); [ol.render.canvas.Instruction.SET_STROKE_STYLE, state.strokeStyle]);

View File

@@ -66,8 +66,8 @@ ol.render.IRender.prototype.drawPolygonGeometry =
/** /**
* @param {?ol.style.Fill} fillStyle Fill style. * @param {ol.style.Fill} fillStyle Fill style.
* @param {?ol.style.Stroke} strokeStyle Stroke style. * @param {ol.style.Stroke} strokeStyle Stroke style.
*/ */
ol.render.IRender.prototype.setFillStrokeStyle = ol.render.IRender.prototype.setFillStrokeStyle =
function(fillStyle, strokeStyle) { function(fillStyle, strokeStyle) {
@@ -75,7 +75,7 @@ ol.render.IRender.prototype.setFillStrokeStyle =
/** /**
* @param {?ol.style.Image} imageStyle Image style. * @param {ol.style.Image} imageStyle Image style.
*/ */
ol.render.IRender.prototype.setImageStyle = function(imageStyle) { ol.render.IRender.prototype.setImageStyle = function(imageStyle) {
}; };

View File

@@ -11,8 +11,8 @@ goog.require('ol.style');
/** /**
* @param {number} radius Radius. * @param {number} radius Radius.
* @param {?ol.style.Fill} fillStyle Fill style. * @param {ol.style.Fill} fillStyle Fill style.
* @param {?ol.style.Stroke} strokeStyle Stroke style. * @param {ol.style.Stroke} strokeStyle Stroke style.
* @return {ol.style.Image} Image. * @return {ol.style.Image} Image.
*/ */
ol.shape.renderCircle = function(radius, fillStyle, strokeStyle) { ol.shape.renderCircle = function(radius, fillStyle, strokeStyle) {

View File

@@ -12,20 +12,30 @@ goog.require('goog.functions');
/** /**
* @typedef {{color: string}} * @typedef {{color: string}|null|undefined}
*/ */
ol.style.Fill; ol.style.Fill;
/** /**
* @param {?ol.style.Fill} fillStyle1 Fill style 1. * @param {ol.style.Fill} fillStyle1 Fill style 1.
* @param {?ol.style.Fill} fillStyle2 Fill style 2. * @param {ol.style.Fill} fillStyle2 Fill style 2.
* @return {boolean} Equals. * @return {boolean} Equals.
*/ */
ol.style.fill.equals = function(fillStyle1, fillStyle2) { ol.style.fill.equals = function(fillStyle1, fillStyle2) {
return fillStyle1 === fillStyle2 || ( if (goog.isDefAndNotNull(fillStyle1)) {
!goog.isNull(fillStyle1) && !goog.isNull(fillStyle2) && if (goog.isDefAndNotNull(fillStyle2)) {
fillStyle1.color == fillStyle2.color); return fillStyle1 === fillStyle2 || fillStyle1.color == fillStyle2.color;
} else {
return false;
}
} else {
if (goog.isDefAndNotNull(fillStyle2)) {
return false;
} else {
return true;
}
}
}; };
@@ -34,35 +44,46 @@ ol.style.fill.equals = function(fillStyle1, fillStyle2) {
* image: (HTMLCanvasElement|HTMLVideoElement|Image), * image: (HTMLCanvasElement|HTMLVideoElement|Image),
* rotation: number, * rotation: number,
* snapToPixel: (boolean|undefined), * snapToPixel: (boolean|undefined),
* subtractViewRotation: boolean}} * subtractViewRotation: boolean}|null|undefined}
*/ */
ol.style.Image; ol.style.Image;
/** /**
* @typedef {{color: string, * @typedef {{color: string,
* width: number}} * width: number}|null|undefined}
*/ */
ol.style.Stroke; ol.style.Stroke;
/** /**
* @param {?ol.style.Stroke} strokeStyle1 Stroke style 1. * @param {ol.style.Stroke} strokeStyle1 Stroke style 1.
* @param {?ol.style.Stroke} strokeStyle2 Stroke style 2. * @param {ol.style.Stroke} strokeStyle2 Stroke style 2.
* @return {boolean} Equals. * @return {boolean} Equals.
*/ */
ol.style.stroke.equals = function(strokeStyle1, strokeStyle2) { ol.style.stroke.equals = function(strokeStyle1, strokeStyle2) {
return strokeStyle1 === strokeStyle2 || ( if (goog.isDefAndNotNull(strokeStyle1)) {
!goog.isNull(strokeStyle1) && !goog.isNull(strokeStyle2) && if (goog.isDefAndNotNull(strokeStyle2)) {
strokeStyle1.color == strokeStyle2.color && return strokeStyle1 === strokeStyle2 ||
(strokeStyle1.color == strokeStyle2.color &&
strokeStyle1.width == strokeStyle2.width); strokeStyle1.width == strokeStyle2.width);
} else {
return false;
}
} else {
if (goog.isDefAndNotNull(strokeStyle2)) {
return false;
} else {
return true;
}
}
}; };
/** /**
* @typedef {{fill: ?ol.style.Fill, * @typedef {{fill: ol.style.Fill,
* image: ?ol.style.Image, * image: ol.style.Image,
* stroke: ?ol.style.Stroke, * stroke: ol.style.Stroke,
* zIndex: (number|undefined)}} * zIndex: (number|undefined)}}
*/ */
ol.style.Style; ol.style.Style;