const vectorParams = require('./vectorParams')
/**
* Represents a character as a list of segments
* @typedef {Object} VectorCharObject
* @property {Float} width - character width
* @property {Float} height - character height (uppercase)
* @property {Array} segments - character segments [[[x, y], ...], ...]
*/
/** Construct a {@link VectorCharObject} from a ascii character whose code is between 31 and 127,
* if the character is not supported it is replaced by a question mark.
* @param {Object|String} [options] - options for construction or ascii character
* @param {Float} [options.xOffset=0] - x offset
* @param {Float} [options.yOffset=0] - y offset
* @param {Float} [options.height=21] - font size (uppercase height)
* @param {Float} [options.extrudeOffset=0] - width of the extrusion that will be applied (manually) after the creation of the character
* @param {String} [options.input='?'] - ascii character (ignored/overwrited if provided as seconds parameter)
* @param {String} [char='?'] - ascii character
* @returns {VectorCharObject}
* @alias module:modeling/text.vectorChar
*
* @example
* let vectorCharObject = vectorChar()
* or
* let vectorCharObject = vectorChar('A')
* or
* let vectorCharObject = vectorChar({ xOffset: 57 }, 'C')
* or
* let vectorCharObject = vectorChar({ xOffset: 78, input: '!' })
*/
const vectorChar = (options, char) => {
const {
xOffset, yOffset, input, font, height, extrudeOffset
} = vectorParams(options, char)
let code = input.charCodeAt(0)
if (!code || !font[code]) {
code = 63 // 63 => ?
}
const glyph = [].concat(font[code])
const ratio = (height - extrudeOffset) / font.height
const extrudeYOffset = (extrudeOffset / 2)
const width = glyph.shift() * ratio
const segments = []
let polyline = []
for (let i = 0, il = glyph.length; i < il; i += 2) {
const gx = ratio * glyph[i] + xOffset
const gy = ratio * glyph[i + 1] + yOffset + extrudeYOffset
if (glyph[i] !== undefined) {
polyline.push([gx, gy])
continue
}
segments.push(polyline)
polyline = []
i--
}
if (polyline.length) {
segments.push(polyline)
}
return { width, height, segments }
}
module.exports = vectorChar