Move measureTextWidths to render/canvas
This commit is contained in:
committed by
Guillaume Beraudo
parent
3170355b07
commit
3d203f990e
@@ -324,6 +324,25 @@ export function measureTextWidth(font, text) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} font Font to use for measuring.
|
||||||
|
* @param {Array<string>} lines Lines to measure.
|
||||||
|
* @param {Array<number>} widths Array will be populated with the widths of
|
||||||
|
* each line.
|
||||||
|
* @return {number} Width of the whole text.
|
||||||
|
*/
|
||||||
|
export function measureTextWidths(font, lines, widths) {
|
||||||
|
const numLines = lines.length;
|
||||||
|
let width = 0;
|
||||||
|
for (let i = 0; i < numLines; ++i) {
|
||||||
|
const currentWidth = measureTextWidth(font, lines[i]);
|
||||||
|
width = Math.max(width, currentWidth);
|
||||||
|
widths.push(currentWidth);
|
||||||
|
}
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {CanvasRenderingContext2D} context Context.
|
* @param {CanvasRenderingContext2D} context Context.
|
||||||
* @param {number} rotation Rotation.
|
* @param {number} rotation Rotation.
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import {
|
|||||||
/// Imports copied from TextReplay
|
/// Imports copied from TextReplay
|
||||||
import {asColorLike} from '../../colorlike.js';
|
import {asColorLike} from '../../colorlike.js';
|
||||||
import {createCanvasContext2D} from '../../dom.js';
|
import {createCanvasContext2D} from '../../dom.js';
|
||||||
import {labelCache, measureTextWidth, defaultTextAlign, measureTextHeight, defaultLineCap, defaultLineDashOffset, defaultLineDash, defaultLineJoin, defaultFillStyle, checkFont, defaultFont, defaultLineWidth, defaultMiterLimit, defaultStrokeStyle, defaultTextBaseline} from '../canvas.js';
|
import {labelCache, measureTextWidth, measureTextWidths, defaultTextAlign, measureTextHeight, defaultLineCap, defaultLineDashOffset, defaultLineDash, defaultLineJoin, defaultFillStyle, checkFont, defaultFont, defaultLineWidth, defaultMiterLimit, defaultStrokeStyle, defaultTextBaseline} from '../canvas.js';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1222,24 +1222,3 @@ class CanvasInstructionsExecutor {
|
|||||||
|
|
||||||
|
|
||||||
export default CanvasInstructionsExecutor;
|
export default CanvasInstructionsExecutor;
|
||||||
|
|
||||||
|
|
||||||
/////////////// Below is code copied from TextReplay /////////////////
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} font Font to use for measuring.
|
|
||||||
* @param {Array<string>} lines Lines to measure.
|
|
||||||
* @param {Array<number>} widths Array will be populated with the widths of
|
|
||||||
* each line.
|
|
||||||
* @return {number} Width of the whole text.
|
|
||||||
*/
|
|
||||||
export function measureTextWidths(font, lines, widths) {
|
|
||||||
const numLines = lines.length;
|
|
||||||
let width = 0;
|
|
||||||
for (let i = 0; i < numLines; ++i) {
|
|
||||||
const currentWidth = measureTextWidth(font, lines[i]);
|
|
||||||
width = Math.max(width, currentWidth);
|
|
||||||
widths.push(currentWidth);
|
|
||||||
}
|
|
||||||
return width;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {intersects} from '../../extent.js';
|
|||||||
import {matchingChunk} from '../../geom/flat/straightchunk.js';
|
import {matchingChunk} from '../../geom/flat/straightchunk.js';
|
||||||
import GeometryType from '../../geom/GeometryType.js';
|
import GeometryType from '../../geom/GeometryType.js';
|
||||||
import {CANVAS_LINE_DASH} from '../../has.js';
|
import {CANVAS_LINE_DASH} from '../../has.js';
|
||||||
import {labelCache, measureTextWidth, defaultTextAlign, measureTextHeight, defaultPadding, defaultLineCap, defaultLineDashOffset, defaultLineDash, defaultLineJoin, defaultFillStyle, checkFont, defaultFont, defaultLineWidth, defaultMiterLimit, defaultStrokeStyle, defaultTextBaseline} from '../canvas.js';
|
import {labelCache, measureTextWidth, measureTextWidths, defaultTextAlign, measureTextHeight, defaultPadding, defaultLineCap, defaultLineDashOffset, defaultLineDash, defaultLineJoin, defaultFillStyle, checkFont, defaultFont, defaultLineWidth, defaultMiterLimit, defaultStrokeStyle, defaultTextBaseline} from '../canvas.js';
|
||||||
import CanvasInstruction from './Instruction.js';
|
import CanvasInstruction from './Instruction.js';
|
||||||
import CanvasInstructionsBuilder from './InstructionsBuilder.js';
|
import CanvasInstructionsBuilder from './InstructionsBuilder.js';
|
||||||
import {TEXT_ALIGN} from '../replay.js';
|
import {TEXT_ALIGN} from '../replay.js';
|
||||||
@@ -528,23 +528,4 @@ class CanvasTextBuilder extends CanvasInstructionsBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} font Font to use for measuring.
|
|
||||||
* @param {Array<string>} lines Lines to measure.
|
|
||||||
* @param {Array<number>} widths Array will be populated with the widths of
|
|
||||||
* each line.
|
|
||||||
* @return {number} Width of the whole text.
|
|
||||||
*/
|
|
||||||
export function measureTextWidths(font, lines, widths) {
|
|
||||||
const numLines = lines.length;
|
|
||||||
let width = 0;
|
|
||||||
for (let i = 0; i < numLines; ++i) {
|
|
||||||
const currentWidth = measureTextWidth(font, lines[i]);
|
|
||||||
width = Math.max(width, currentWidth);
|
|
||||||
widths.push(currentWidth);
|
|
||||||
}
|
|
||||||
return width;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export default CanvasTextBuilder;
|
export default CanvasTextBuilder;
|
||||||
|
|||||||
Reference in New Issue
Block a user