Extend Text.js: add justification option

This commit is contained in:
Denis
2022-04-11 14:45:35 +02:00
parent 5651520dcd
commit 5a61bb51f5

View File

@@ -35,6 +35,10 @@ const DEFAULT_FILL_COLOR = '#333';
* @property {string} [textAlign] Text alignment. Possible values: 'left', 'right', 'center', 'end' or 'start'.
* Default is 'center' for `placement: 'point'`. For `placement: 'line'`, the default is to let the renderer choose a
* placement where `maxAngle` is not exceeded.
* @property {string} [justify] Text justification within the text box.
* If not set, text is justified towards the `textAlign` anchor.
* Otherwise, use options `'left'`, `'center'`, or `'right'` to justify the text within the text box.
* See: https://github.com/openlayers/openlayers/issues/13481
* @property {string} [textBaseline='middle'] Text base line. Possible values: 'bottom', 'top', 'middle', 'alphabetic',
* 'hanging', 'ideographic'.
* @property {import("./Fill.js").default} [fill] Fill style. If none is provided, we'll use a dark fill-style (#333).
@@ -101,6 +105,12 @@ class Text {
*/
this.textAlign_ = options.textAlign;
/**
* @private
* @type {string|undefined}
*/
this.justify_ = options.justify;
/**
* @private
* @type {string|undefined}
@@ -194,6 +204,7 @@ class Text {
scale: Array.isArray(scale) ? scale.slice() : scale,
text: this.getText(),
textAlign: this.getTextAlign(),
justify: this.getJustify(),
textBaseline: this.getTextBaseline(),
fill: this.getFill() ? this.getFill().clone() : undefined,
stroke: this.getStroke() ? this.getStroke().clone() : undefined,
@@ -334,6 +345,15 @@ class Text {
return this.textAlign_;
}
/**
* Get the justification.
* @return {string|undefined} Justification.
* @api
*/
getJustify() {
return this.justify_;
}
/**
* Get the text baseline.
* @return {string|undefined} Text baseline.
@@ -501,6 +521,16 @@ class Text {
this.textAlign_ = textAlign;
}
/**
* Set the justification.
*
* @param {string|undefined} justify Justification.
* @api
*/
setJustify(justify) {
this.justify_ = justify;
}
/**
* Set the text baseline.
*