Make code prettier
This updates ESLint and our shared eslint-config-openlayers to use Prettier. Most formatting changes were automatically applied with this:
npm run lint -- --fix
A few manual changes were required:
* In `examples/offscreen-canvas.js`, the `//eslint-disable-line` comment needed to be moved to the appropriate line to disable the error about the `'worker-loader!./offscreen-canvas.worker.js'` import.
* In `examples/webpack/exapmle-builder.js`, spaces could not be added after a couple `function`s for some reason. While editing this, I reworked `ExampleBuilder` to be a class.
* In `src/ol/format/WMSGetFeatureInfo.js`, the `// @ts-ignore` comment needed to be moved down one line so it applied to the `parsersNS` argument.
This commit is contained in:
@@ -1,15 +1,29 @@
|
||||
/**
|
||||
* @module ol/render/canvas/TextBuilder
|
||||
*/
|
||||
import {getUid} from '../../util.js';
|
||||
import CanvasBuilder from './Builder.js';
|
||||
import CanvasInstruction from './Instruction.js';
|
||||
import GeometryType from '../../geom/GeometryType.js';
|
||||
import TextPlacement from '../../style/TextPlacement.js';
|
||||
import {asColorLike} from '../../colorlike.js';
|
||||
import {
|
||||
defaultFillStyle,
|
||||
defaultFont,
|
||||
defaultLineCap,
|
||||
defaultLineDash,
|
||||
defaultLineDashOffset,
|
||||
defaultLineJoin,
|
||||
defaultLineWidth,
|
||||
defaultMiterLimit,
|
||||
defaultPadding,
|
||||
defaultStrokeStyle,
|
||||
defaultTextAlign,
|
||||
defaultTextBaseline,
|
||||
registerFont,
|
||||
} from '../canvas.js';
|
||||
import {getUid} from '../../util.js';
|
||||
import {intersects} from '../../extent.js';
|
||||
import {matchingChunk} from '../../geom/flat/straightchunk.js';
|
||||
import GeometryType from '../../geom/GeometryType.js';
|
||||
import {defaultTextAlign, defaultPadding, defaultLineCap, defaultLineDashOffset, defaultLineDash, defaultLineJoin, defaultFillStyle, registerFont, defaultFont, defaultLineWidth, defaultMiterLimit, defaultStrokeStyle, defaultTextBaseline} from '../canvas.js';
|
||||
import CanvasInstruction from './Instruction.js';
|
||||
import CanvasBuilder from './Builder.js';
|
||||
import TextPlacement from '../../style/TextPlacement.js';
|
||||
/**
|
||||
* @const
|
||||
* @enum {number}
|
||||
@@ -25,10 +39,9 @@ export const TEXT_ALIGN = {
|
||||
'hanging': 0.2,
|
||||
'alphabetic': 0.8,
|
||||
'ideographic': 0.8,
|
||||
'bottom': 1
|
||||
'bottom': 1,
|
||||
};
|
||||
|
||||
|
||||
class CanvasTextBuilder extends CanvasBuilder {
|
||||
/**
|
||||
* @param {number} tolerance Tolerance.
|
||||
@@ -176,7 +189,9 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
} else if (geometryType == GeometryType.MULTI_LINE_STRING) {
|
||||
ends = /** @type {import("../../geom/MultiLineString.js").default} */ (geometry).getEnds();
|
||||
} else if (geometryType == GeometryType.POLYGON) {
|
||||
ends = /** @type {import("../../geom/Polygon.js").default} */ (geometry).getEnds().slice(0, 1);
|
||||
ends = /** @type {import("../../geom/Polygon.js").default} */ (geometry)
|
||||
.getEnds()
|
||||
.slice(0, 1);
|
||||
} else if (geometryType == GeometryType.MULTI_POLYGON) {
|
||||
const endss = /** @type {import("../../geom/MultiPolygon.js").default} */ (geometry).getEndss();
|
||||
ends = [];
|
||||
@@ -190,7 +205,13 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
let flatEnd;
|
||||
for (let o = 0, oo = ends.length; o < oo; ++o) {
|
||||
if (textAlign == undefined) {
|
||||
const range = matchingChunk(textState.maxAngle, flatCoordinates, flatOffset, ends[o], stride);
|
||||
const range = matchingChunk(
|
||||
textState.maxAngle,
|
||||
flatCoordinates,
|
||||
flatOffset,
|
||||
ends[o],
|
||||
stride
|
||||
);
|
||||
flatOffset = range[0];
|
||||
flatEnd = range[1];
|
||||
} else {
|
||||
@@ -201,16 +222,16 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
}
|
||||
end = this.coordinates.length;
|
||||
flatOffset = ends[o];
|
||||
const declutterGroup = this.declutterGroups_ ?
|
||||
(o === 0 ? this.declutterGroups_[0] : [].concat(this.declutterGroups_[0])) :
|
||||
null;
|
||||
const declutterGroup = this.declutterGroups_
|
||||
? o === 0
|
||||
? this.declutterGroups_[0]
|
||||
: [].concat(this.declutterGroups_[0])
|
||||
: null;
|
||||
this.drawChars_(begin, end, declutterGroup);
|
||||
begin = end;
|
||||
}
|
||||
this.endGeometry(feature);
|
||||
|
||||
} else {
|
||||
|
||||
let geometryWidths = null;
|
||||
if (!textState.overflow) {
|
||||
geometryWidths = [];
|
||||
@@ -254,12 +275,22 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
break;
|
||||
default:
|
||||
}
|
||||
end = this.appendFlatCoordinates(flatCoordinates, 0, end, stride, false, false);
|
||||
end = this.appendFlatCoordinates(
|
||||
flatCoordinates,
|
||||
0,
|
||||
end,
|
||||
stride,
|
||||
false,
|
||||
false
|
||||
);
|
||||
|
||||
this.saveTextStates_();
|
||||
|
||||
if (textState.backgroundFill || textState.backgroundStroke) {
|
||||
this.setFillStrokeStyle(textState.backgroundFill, textState.backgroundStroke);
|
||||
this.setFillStrokeStyle(
|
||||
textState.backgroundFill,
|
||||
textState.backgroundStroke
|
||||
);
|
||||
if (textState.backgroundFill) {
|
||||
this.updateFillStyle(this.state, this.createFill);
|
||||
this.hitDetectionInstructions.push(this.createFill(this.state));
|
||||
@@ -276,24 +307,63 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
// For clarity, we pass NaN for offsetX, offsetY, width and height, which will be computed at
|
||||
// render time.
|
||||
const pixelRatio = this.pixelRatio;
|
||||
this.instructions.push([CanvasInstruction.DRAW_IMAGE, begin, end,
|
||||
null, NaN, NaN, this.declutterGroups_, NaN, 1, 0, 0,
|
||||
this.textRotateWithView_, this.textRotation_, 1, NaN,
|
||||
textState.padding == defaultPadding ?
|
||||
defaultPadding : textState.padding.map(function(p) {
|
||||
return p * pixelRatio;
|
||||
}),
|
||||
!!textState.backgroundFill, !!textState.backgroundStroke,
|
||||
this.text_, this.textKey_, this.strokeKey_, this.fillKey_,
|
||||
this.textOffsetX_, this.textOffsetY_, geometryWidths
|
||||
this.instructions.push([
|
||||
CanvasInstruction.DRAW_IMAGE,
|
||||
begin,
|
||||
end,
|
||||
null,
|
||||
NaN,
|
||||
NaN,
|
||||
this.declutterGroups_,
|
||||
NaN,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
this.textRotateWithView_,
|
||||
this.textRotation_,
|
||||
1,
|
||||
NaN,
|
||||
textState.padding == defaultPadding
|
||||
? defaultPadding
|
||||
: textState.padding.map(function (p) {
|
||||
return p * pixelRatio;
|
||||
}),
|
||||
!!textState.backgroundFill,
|
||||
!!textState.backgroundStroke,
|
||||
this.text_,
|
||||
this.textKey_,
|
||||
this.strokeKey_,
|
||||
this.fillKey_,
|
||||
this.textOffsetX_,
|
||||
this.textOffsetY_,
|
||||
geometryWidths,
|
||||
]);
|
||||
this.hitDetectionInstructions.push([CanvasInstruction.DRAW_IMAGE, begin, end,
|
||||
null, NaN, NaN, this.declutterGroups_, NaN, 1, 0, 0,
|
||||
this.textRotateWithView_, this.textRotation_, 1 / this.pixelRatio, NaN,
|
||||
this.hitDetectionInstructions.push([
|
||||
CanvasInstruction.DRAW_IMAGE,
|
||||
begin,
|
||||
end,
|
||||
null,
|
||||
NaN,
|
||||
NaN,
|
||||
this.declutterGroups_,
|
||||
NaN,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
this.textRotateWithView_,
|
||||
this.textRotation_,
|
||||
1 / this.pixelRatio,
|
||||
NaN,
|
||||
textState.padding,
|
||||
!!textState.backgroundFill, !!textState.backgroundStroke,
|
||||
this.text_, this.textKey_, this.strokeKey_, this.fillKey_,
|
||||
this.textOffsetX_, this.textOffsetY_, geometryWidths
|
||||
!!textState.backgroundFill,
|
||||
!!textState.backgroundStroke,
|
||||
this.text_,
|
||||
this.textKey_,
|
||||
this.strokeKey_,
|
||||
this.fillKey_,
|
||||
this.textOffsetX_,
|
||||
this.textOffsetY_,
|
||||
geometryWidths,
|
||||
]);
|
||||
|
||||
this.endGeometry(feature);
|
||||
@@ -318,7 +388,7 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
lineWidth: strokeState.lineWidth,
|
||||
lineJoin: strokeState.lineJoin,
|
||||
miterLimit: strokeState.miterLimit,
|
||||
lineDash: strokeState.lineDash
|
||||
lineDash: strokeState.lineDash,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -328,14 +398,14 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
font: textState.font,
|
||||
textAlign: textState.textAlign || defaultTextAlign,
|
||||
textBaseline: textState.textBaseline || defaultTextBaseline,
|
||||
scale: textState.scale
|
||||
scale: textState.scale,
|
||||
};
|
||||
}
|
||||
const fillKey = this.fillKey_;
|
||||
if (fillState) {
|
||||
if (!(fillKey in this.fillStates)) {
|
||||
this.fillStates[fillKey] = {
|
||||
fillStyle: fillState.fillStyle
|
||||
fillStyle: fillState.fillStyle,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -356,26 +426,49 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
const fillKey = this.fillKey_;
|
||||
this.saveTextStates_();
|
||||
|
||||
|
||||
const pixelRatio = this.pixelRatio;
|
||||
const baseline = TEXT_ALIGN[textState.textBaseline];
|
||||
|
||||
const offsetY = this.textOffsetY_ * pixelRatio;
|
||||
const text = this.text_;
|
||||
const textScale = textState.scale;
|
||||
const strokeWidth = strokeState ? strokeState.lineWidth * textScale / 2 : 0;
|
||||
const strokeWidth = strokeState
|
||||
? (strokeState.lineWidth * textScale) / 2
|
||||
: 0;
|
||||
|
||||
this.instructions.push([CanvasInstruction.DRAW_CHARS,
|
||||
begin, end, baseline, declutterGroup,
|
||||
textState.overflow, fillKey, textState.maxAngle,
|
||||
this.instructions.push([
|
||||
CanvasInstruction.DRAW_CHARS,
|
||||
begin,
|
||||
end,
|
||||
baseline,
|
||||
declutterGroup,
|
||||
textState.overflow,
|
||||
fillKey,
|
||||
textState.maxAngle,
|
||||
pixelRatio,
|
||||
offsetY, strokeKey, strokeWidth * pixelRatio, text, textKey, 1
|
||||
]);
|
||||
this.hitDetectionInstructions.push([CanvasInstruction.DRAW_CHARS,
|
||||
begin, end, baseline, declutterGroup,
|
||||
textState.overflow, fillKey, textState.maxAngle,
|
||||
offsetY,
|
||||
strokeKey,
|
||||
strokeWidth * pixelRatio,
|
||||
text,
|
||||
textKey,
|
||||
1,
|
||||
offsetY, strokeKey, strokeWidth, text, textKey, 1 / pixelRatio
|
||||
]);
|
||||
this.hitDetectionInstructions.push([
|
||||
CanvasInstruction.DRAW_CHARS,
|
||||
begin,
|
||||
end,
|
||||
baseline,
|
||||
declutterGroup,
|
||||
textState.overflow,
|
||||
fillKey,
|
||||
textState.maxAngle,
|
||||
1,
|
||||
offsetY,
|
||||
strokeKey,
|
||||
strokeWidth,
|
||||
text,
|
||||
textKey,
|
||||
1 / pixelRatio,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -401,7 +494,8 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
this.textFillState_ = fillState;
|
||||
}
|
||||
fillState.fillStyle = asColorLike(
|
||||
textFillStyle.getColor() || defaultFillStyle);
|
||||
textFillStyle.getColor() || defaultFillStyle
|
||||
);
|
||||
}
|
||||
|
||||
const textStrokeStyle = textStyle.getStroke();
|
||||
@@ -421,14 +515,15 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
strokeState.lineCap = textStrokeStyle.getLineCap() || defaultLineCap;
|
||||
strokeState.lineDash = lineDash ? lineDash.slice() : defaultLineDash;
|
||||
strokeState.lineDashOffset =
|
||||
lineDashOffset === undefined ? defaultLineDashOffset : lineDashOffset;
|
||||
lineDashOffset === undefined ? defaultLineDashOffset : lineDashOffset;
|
||||
strokeState.lineJoin = textStrokeStyle.getLineJoin() || defaultLineJoin;
|
||||
strokeState.lineWidth =
|
||||
lineWidth === undefined ? defaultLineWidth : lineWidth;
|
||||
lineWidth === undefined ? defaultLineWidth : lineWidth;
|
||||
strokeState.miterLimit =
|
||||
miterLimit === undefined ? defaultMiterLimit : miterLimit;
|
||||
miterLimit === undefined ? defaultMiterLimit : miterLimit;
|
||||
strokeState.strokeStyle = asColorLike(
|
||||
textStrokeStyle.getColor() || defaultStrokeStyle);
|
||||
textStrokeStyle.getColor() || defaultStrokeStyle
|
||||
);
|
||||
}
|
||||
|
||||
textState = this.textState_;
|
||||
@@ -440,7 +535,8 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
textState.maxAngle = textStyle.getMaxAngle();
|
||||
textState.placement = textStyle.getPlacement();
|
||||
textState.textAlign = textStyle.getTextAlign();
|
||||
textState.textBaseline = textStyle.getTextBaseline() || defaultTextBaseline;
|
||||
textState.textBaseline =
|
||||
textStyle.getTextBaseline() || defaultTextBaseline;
|
||||
textState.backgroundFill = textStyle.getBackgroundFill();
|
||||
textState.backgroundStroke = textStyle.getBackgroundStroke();
|
||||
textState.padding = textStyle.getPadding() || defaultPadding;
|
||||
@@ -453,21 +549,36 @@ class CanvasTextBuilder extends CanvasBuilder {
|
||||
this.text_ = textStyle.getText() || '';
|
||||
this.textOffsetX_ = textOffsetX === undefined ? 0 : textOffsetX;
|
||||
this.textOffsetY_ = textOffsetY === undefined ? 0 : textOffsetY;
|
||||
this.textRotateWithView_ = textRotateWithView === undefined ? false : textRotateWithView;
|
||||
this.textRotateWithView_ =
|
||||
textRotateWithView === undefined ? false : textRotateWithView;
|
||||
this.textRotation_ = textRotation === undefined ? 0 : textRotation;
|
||||
|
||||
this.strokeKey_ = strokeState ?
|
||||
(typeof strokeState.strokeStyle == 'string' ? strokeState.strokeStyle : getUid(strokeState.strokeStyle)) +
|
||||
strokeState.lineCap + strokeState.lineDashOffset + '|' + strokeState.lineWidth +
|
||||
strokeState.lineJoin + strokeState.miterLimit + '[' + strokeState.lineDash.join() + ']' :
|
||||
'';
|
||||
this.textKey_ = textState.font + textState.scale + (textState.textAlign || '?') + (textState.textBaseline || '?');
|
||||
this.fillKey_ = fillState ?
|
||||
(typeof fillState.fillStyle == 'string' ? fillState.fillStyle : ('|' + getUid(fillState.fillStyle))) :
|
||||
'';
|
||||
this.strokeKey_ = strokeState
|
||||
? (typeof strokeState.strokeStyle == 'string'
|
||||
? strokeState.strokeStyle
|
||||
: getUid(strokeState.strokeStyle)) +
|
||||
strokeState.lineCap +
|
||||
strokeState.lineDashOffset +
|
||||
'|' +
|
||||
strokeState.lineWidth +
|
||||
strokeState.lineJoin +
|
||||
strokeState.miterLimit +
|
||||
'[' +
|
||||
strokeState.lineDash.join() +
|
||||
']'
|
||||
: '';
|
||||
this.textKey_ =
|
||||
textState.font +
|
||||
textState.scale +
|
||||
(textState.textAlign || '?') +
|
||||
(textState.textBaseline || '?');
|
||||
this.fillKey_ = fillState
|
||||
? typeof fillState.fillStyle == 'string'
|
||||
? fillState.fillStyle
|
||||
: '|' + getUid(fillState.fillStyle)
|
||||
: '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default CanvasTextBuilder;
|
||||
|
||||
Reference in New Issue
Block a user