modeling/src/text/vectorText.js

  1. const vectorChar = require('./vectorChar')
  2. const vectorParams = require('./vectorParams')
  3. // translate text line
  4. const translateLine = (options, line) => {
  5. const { x, y } = Object.assign({ x: 0, y: 0 }, options || {})
  6. const segments = line.segments
  7. let segment = null
  8. let point = null
  9. for (let i = 0, il = segments.length; i < il; i++) {
  10. segment = segments[i]
  11. for (let j = 0, jl = segment.length; j < jl; j++) {
  12. point = segment[j]
  13. segment[j] = [point[0] + x, point[1] + y]
  14. }
  15. }
  16. return line
  17. }
  18. /**
  19. * Construct an array of character segments from a ascii string whose characters code is between 31 and 127,
  20. * if one character is not supported it is replaced by a question mark.
  21. * @param {Object|String} [options] - options for construction or ascii string
  22. * @param {Float} [options.xOffset=0] - x offset
  23. * @param {Float} [options.yOffset=0] - y offset
  24. * @param {Float} [options.height=21] - font size (uppercase height)
  25. * @param {Float} [options.lineSpacing=1.4] - line spacing expressed as a percentage of font size
  26. * @param {Float} [options.letterSpacing=1] - extra letter spacing expressed as a percentage of font size
  27. * @param {String} [options.align='left'] - multi-line text alignment: left, center, right
  28. * @param {Float} [options.extrudeOffset=0] - width of the extrusion that will be applied (manually) after the creation of the character
  29. * @param {String} [options.input='?'] - ascii string (ignored/overwrited if provided as seconds parameter)
  30. * @param {String} [text='?'] - ascii string
  31. * @returns {Array} characters segments [[[x, y], ...], ...]
  32. * @alias module:modeling/text.vectorText
  33. *
  34. * @example
  35. * let textSegments = vectorText()
  36. * let textSegments = vectorText('OpenJSCAD')
  37. * let textSegments = vectorText({ yOffset: -50 }, 'OpenJSCAD')
  38. * let textSegments = vectorText({ yOffset: -80, input: 'OpenJSCAD' })
  39. */
  40. const vectorText = (options, text) => {
  41. const {
  42. xOffset, yOffset, input, font, height, align, extrudeOffset, lineSpacing, letterSpacing
  43. } = vectorParams(options, text)
  44. let [x, y] = [xOffset, yOffset]
  45. let i, il, char, vect, width, diff
  46. let line = { width: 0, segments: [] }
  47. const lines = []
  48. let output = []
  49. let maxWidth = 0
  50. const lineStart = x
  51. const pushLine = () => {
  52. lines.push(line)
  53. maxWidth = Math.max(maxWidth, line.width)
  54. line = { width: 0, segments: [] }
  55. }
  56. for (i = 0, il = input.length; i < il; i++) {
  57. char = input[i]
  58. vect = vectorChar({ xOffset: x, yOffset: y, font, height, extrudeOffset }, char)
  59. if (char === '\n') {
  60. x = lineStart
  61. y -= vect.height * lineSpacing
  62. pushLine()
  63. continue
  64. }
  65. width = vect.width * letterSpacing
  66. line.width += width
  67. x += width
  68. if (char !== ' ') {
  69. line.segments = line.segments.concat(vect.segments)
  70. }
  71. }
  72. if (line.segments.length) {
  73. pushLine()
  74. }
  75. for (i = 0, il = lines.length; i < il; i++) {
  76. line = lines[i]
  77. if (maxWidth > line.width) {
  78. diff = maxWidth - line.width
  79. if (align === 'right') {
  80. line = translateLine({ x: diff }, line)
  81. } else if (align === 'center') {
  82. line = translateLine({ x: diff / 2 }, line)
  83. }
  84. }
  85. output = output.concat(line.segments)
  86. }
  87. return output
  88. }
  89. module.exports = vectorText