ol/geom/flat/textpath export
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
* @module ol/geom/flat/textpath
|
* @module ol/geom/flat/textpath
|
||||||
*/
|
*/
|
||||||
import {lerp} from '../../math.js';
|
import {lerp} from '../../math.js';
|
||||||
const _ol_geom_flat_textpath_ = {};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,7 +17,7 @@ const _ol_geom_flat_textpath_ = {};
|
|||||||
* @return {Array.<Array.<*>>} The result array of null if `maxAngle` was
|
* @return {Array.<Array.<*>>} The result array of null if `maxAngle` was
|
||||||
* exceeded. Entries of the array are x, y, anchorX, angle, chunk.
|
* exceeded. Entries of the array are x, y, anchorX, angle, chunk.
|
||||||
*/
|
*/
|
||||||
_ol_geom_flat_textpath_.lineString = function(
|
export function drawTextOnPath(
|
||||||
flatCoordinates, offset, end, stride, text, measure, startM, maxAngle) {
|
flatCoordinates, offset, end, stride, text, measure, startM, maxAngle) {
|
||||||
const result = [];
|
const result = [];
|
||||||
|
|
||||||
@@ -90,5 +89,4 @@ _ol_geom_flat_textpath_.lineString = function(
|
|||||||
startM += charLength;
|
startM += charLength;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
}
|
||||||
export default _ol_geom_flat_textpath_;
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import Relationship from '../../extent/Relationship.js';
|
|||||||
import GeometryType from '../../geom/GeometryType.js';
|
import GeometryType from '../../geom/GeometryType.js';
|
||||||
import {inflateCoordinates, inflateCoordinatesArray, inflateMultiCoordinatesArray} from '../../geom/flat/inflate.js';
|
import {inflateCoordinates, inflateCoordinatesArray, inflateMultiCoordinatesArray} from '../../geom/flat/inflate.js';
|
||||||
import {lineStringLength} from '../../geom/flat/length.js';
|
import {lineStringLength} from '../../geom/flat/length.js';
|
||||||
import _ol_geom_flat_textpath_ from '../../geom/flat/textpath.js';
|
import {drawTextOnPath} from '../../geom/flat/textpath.js';
|
||||||
import _ol_geom_flat_transform_ from '../../geom/flat/transform.js';
|
import _ol_geom_flat_transform_ from '../../geom/flat/transform.js';
|
||||||
import {CANVAS_LINE_DASH} from '../../has.js';
|
import {CANVAS_LINE_DASH} from '../../has.js';
|
||||||
import {isEmpty} from '../../obj.js';
|
import {isEmpty} from '../../obj.js';
|
||||||
@@ -715,7 +715,7 @@ CanvasReplay.prototype.replay_ = function(
|
|||||||
if (overflow || textLength <= pathLength) {
|
if (overflow || textLength <= pathLength) {
|
||||||
const textAlign = /** @type {ol.render.canvas.TextReplay} */ (this).textStates[textKey].textAlign;
|
const textAlign = /** @type {ol.render.canvas.TextReplay} */ (this).textStates[textKey].textAlign;
|
||||||
const startM = (pathLength - textLength) * _ol_render_replay_.TEXT_ALIGN[textAlign];
|
const startM = (pathLength - textLength) * _ol_render_replay_.TEXT_ALIGN[textAlign];
|
||||||
const parts = _ol_geom_flat_textpath_.lineString(
|
const parts = drawTextOnPath(
|
||||||
pixelCoordinates, begin, end, 2, text, measure, startM, maxAngle);
|
pixelCoordinates, begin, end, 2, text, measure, startM, maxAngle);
|
||||||
if (parts) {
|
if (parts) {
|
||||||
let c, cc, chars, label, part;
|
let c, cc, chars, label, part;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import _ol_geom_flat_textpath_ from '../../../../../src/ol/geom/flat/textpath.js';
|
import {drawTextOnPath} from '../../../../../src/ol/geom/flat/textpath.js';
|
||||||
import {lineStringLength} from '../../../../../src/ol/geom/flat/length.js';
|
import {lineStringLength} from '../../../../../src/ol/geom/flat/length.js';
|
||||||
|
|
||||||
describe('textpath', function() {
|
describe('textpath', function() {
|
||||||
@@ -16,27 +16,27 @@ describe('textpath', function() {
|
|||||||
|
|
||||||
it('center-aligns text on a horizontal line', function() {
|
it('center-aligns text on a horizontal line', function() {
|
||||||
const startM = 50 - 15;
|
const startM = 50 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
horizontal, 0, horizontal.length, 2, 'foo', measure, startM, Infinity);
|
horizontal, 0, horizontal.length, 2, 'foo', measure, startM, Infinity);
|
||||||
expect(instructions).to.eql([[40, 0, 5, 0, 'foo']]);
|
expect(instructions).to.eql([[40, 0, 5, 0, 'foo']]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('left-aligns text on a horizontal line', function() {
|
it('left-aligns text on a horizontal line', function() {
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
horizontal, 0, horizontal.length, 2, 'foo', measure, 0, Infinity);
|
horizontal, 0, horizontal.length, 2, 'foo', measure, 0, Infinity);
|
||||||
expect(instructions).to.eql([[5, 0, 5, 0, 'foo']]);
|
expect(instructions).to.eql([[5, 0, 5, 0, 'foo']]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('right-aligns text on a horizontal line', function() {
|
it('right-aligns text on a horizontal line', function() {
|
||||||
const startM = 100 - 30;
|
const startM = 100 - 30;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
horizontal, 0, horizontal.length, 2, 'foo', measure, startM, Infinity);
|
horizontal, 0, horizontal.length, 2, 'foo', measure, startM, Infinity);
|
||||||
expect(instructions).to.eql([[75, 0, 5, 0, 'foo']]);
|
expect(instructions).to.eql([[75, 0, 5, 0, 'foo']]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('draws text on a vertical line', function() {
|
it('draws text on a vertical line', function() {
|
||||||
const startM = 50 - 15;
|
const startM = 50 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
vertical, 0, vertical.length, 2, 'foo', measure, startM, Infinity);
|
vertical, 0, vertical.length, 2, 'foo', measure, startM, Infinity);
|
||||||
const a = 90 * Math.PI / 180;
|
const a = 90 * Math.PI / 180;
|
||||||
expect(instructions).to.eql([[0, 40, 5, a, 'foo']]);
|
expect(instructions).to.eql([[0, 40, 5, a, 'foo']]);
|
||||||
@@ -44,7 +44,7 @@ describe('textpath', function() {
|
|||||||
|
|
||||||
it('draws text on a diagonal line', function() {
|
it('draws text on a diagonal line', function() {
|
||||||
const startM = Math.sqrt(2) * 50 - 15;
|
const startM = Math.sqrt(2) * 50 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
diagonal, 0, diagonal.length, 2, 'foo', measure, startM, Infinity);
|
diagonal, 0, diagonal.length, 2, 'foo', measure, startM, Infinity);
|
||||||
expect(instructions[0][3]).to.be(45 * Math.PI / 180);
|
expect(instructions[0][3]).to.be(45 * Math.PI / 180);
|
||||||
expect(instructions.length).to.be(1);
|
expect(instructions.length).to.be(1);
|
||||||
@@ -52,7 +52,7 @@ describe('textpath', function() {
|
|||||||
|
|
||||||
it('draws reverse text on a diagonal line', function() {
|
it('draws reverse text on a diagonal line', function() {
|
||||||
const startM = Math.sqrt(2) * 50 - 15;
|
const startM = Math.sqrt(2) * 50 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
reverse, 0, reverse.length, 2, 'foo', measure, startM, Infinity);
|
reverse, 0, reverse.length, 2, 'foo', measure, startM, Infinity);
|
||||||
expect(instructions[0][3]).to.be(-45 * Math.PI / 180);
|
expect(instructions[0][3]).to.be(-45 * Math.PI / 180);
|
||||||
expect(instructions.length).to.be(1);
|
expect(instructions.length).to.be(1);
|
||||||
@@ -60,7 +60,7 @@ describe('textpath', function() {
|
|||||||
|
|
||||||
it('renders long text with extrapolation', function() {
|
it('renders long text with extrapolation', function() {
|
||||||
const startM = 50 - 75;
|
const startM = 50 - 75;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
horizontal, 0, horizontal.length, 2, 'foo-foo-foo-foo', measure, startM, Infinity);
|
horizontal, 0, horizontal.length, 2, 'foo-foo-foo-foo', measure, startM, Infinity);
|
||||||
expect(instructions[0]).to.eql([-20, 0, 5, 0, 'foo-foo-foo-foo']);
|
expect(instructions[0]).to.eql([-20, 0, 5, 0, 'foo-foo-foo-foo']);
|
||||||
expect(instructions.length).to.be(1);
|
expect(instructions.length).to.be(1);
|
||||||
@@ -69,7 +69,7 @@ describe('textpath', function() {
|
|||||||
it('renders angled text', function() {
|
it('renders angled text', function() {
|
||||||
const length = lineStringLength(angled, 0, angled.length, 2);
|
const length = lineStringLength(angled, 0, angled.length, 2);
|
||||||
const startM = length / 2 - 15;
|
const startM = length / 2 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
angled, 0, angled.length, 2, 'foo', measure, startM, Infinity);
|
angled, 0, angled.length, 2, 'foo', measure, startM, Infinity);
|
||||||
expect(instructions[0][3]).to.eql(45 * Math.PI / 180);
|
expect(instructions[0][3]).to.eql(45 * Math.PI / 180);
|
||||||
expect(instructions[0][4]).to.be('fo');
|
expect(instructions[0][4]).to.be('fo');
|
||||||
@@ -80,7 +80,7 @@ describe('textpath', function() {
|
|||||||
it('respects maxAngle', function() {
|
it('respects maxAngle', function() {
|
||||||
const length = lineStringLength(angled, 0, angled.length, 2);
|
const length = lineStringLength(angled, 0, angled.length, 2);
|
||||||
const startM = length / 2 - 15;
|
const startM = length / 2 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
angled, 0, angled.length, 2, 'foo', measure, startM, Math.PI / 4);
|
angled, 0, angled.length, 2, 'foo', measure, startM, Math.PI / 4);
|
||||||
expect(instructions).to.be(null);
|
expect(instructions).to.be(null);
|
||||||
});
|
});
|
||||||
@@ -88,7 +88,7 @@ describe('textpath', function() {
|
|||||||
it('uses the smallest angle for maxAngleDelta', function() {
|
it('uses the smallest angle for maxAngleDelta', function() {
|
||||||
const length = lineStringLength(reverseangled, 0, reverseangled.length, 2);
|
const length = lineStringLength(reverseangled, 0, reverseangled.length, 2);
|
||||||
const startM = length / 2 - 15;
|
const startM = length / 2 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
reverseangled, 0, reverseangled.length, 2, 'foo', measure, startM, Math.PI);
|
reverseangled, 0, reverseangled.length, 2, 'foo', measure, startM, Math.PI);
|
||||||
expect(instructions).to.not.be(undefined);
|
expect(instructions).to.not.be(undefined);
|
||||||
});
|
});
|
||||||
@@ -96,7 +96,7 @@ describe('textpath', function() {
|
|||||||
it('respects the offset option', function() {
|
it('respects the offset option', function() {
|
||||||
const length = lineStringLength(angled, 2, angled.length, 2);
|
const length = lineStringLength(angled, 2, angled.length, 2);
|
||||||
const startM = length / 2 - 15;
|
const startM = length / 2 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
angled, 2, angled.length, 2, 'foo', measure, startM, Infinity);
|
angled, 2, angled.length, 2, 'foo', measure, startM, Infinity);
|
||||||
expect(instructions[0][3]).to.be(-45 * Math.PI / 180);
|
expect(instructions[0][3]).to.be(-45 * Math.PI / 180);
|
||||||
expect(instructions.length).to.be(1);
|
expect(instructions.length).to.be(1);
|
||||||
@@ -105,7 +105,7 @@ describe('textpath', function() {
|
|||||||
it('respects the end option', function() {
|
it('respects the end option', function() {
|
||||||
const length = lineStringLength(angled, 0, 4, 2);
|
const length = lineStringLength(angled, 0, 4, 2);
|
||||||
const startM = length / 2 - 15;
|
const startM = length / 2 - 15;
|
||||||
const instructions = _ol_geom_flat_textpath_.lineString(
|
const instructions = drawTextOnPath(
|
||||||
angled, 0, 4, 2, 'foo', measure, startM, Infinity);
|
angled, 0, 4, 2, 'foo', measure, startM, Infinity);
|
||||||
expect(instructions[0][3]).to.be(45 * Math.PI / 180);
|
expect(instructions[0][3]).to.be(45 * Math.PI / 180);
|
||||||
expect(instructions.length).to.be(1);
|
expect(instructions.length).to.be(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user