|
| 1 | +import macro from 'vtk.js/Sources/macros'; |
| 2 | +import vtkPlaneSource from 'vtk.js/Sources/Filters/Sources/PlaneSource'; |
| 3 | +import vtkTexture from 'vtk.js/Sources/Rendering/Core/Texture'; |
| 4 | +import vtkActor2D from 'vtk.js/Sources/Rendering/Core/Actor2D'; |
| 5 | +import vtkMapper2D from 'vtk.js/Sources/Rendering/Core/Mapper2D'; |
| 6 | +import vtkTextProperty from 'vtk.js/Sources/Rendering/Core/TextProperty'; |
| 7 | +import ImageHelper from 'vtk.js/Sources/Common/Core/ImageHelper'; |
| 8 | +import * as vtkMath from 'vtk.js/Sources/Common/Core/Math'; |
| 9 | + |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | +// vtkTextActor methods |
| 12 | +// ---------------------------------------------------------------------------- |
| 13 | + |
| 14 | +function vtkTextActor(publicAPI, model) { |
| 15 | + // Set our className |
| 16 | + model.classHierarchy.push('vtkTextActor'); |
| 17 | + |
| 18 | + publicAPI.makeProperty = vtkTextProperty.newInstance; |
| 19 | + |
| 20 | + publicAPI.getProperty = () => { |
| 21 | + if (model.property === null) { |
| 22 | + model.property = publicAPI.makeProperty(); |
| 23 | + } |
| 24 | + return model.property; |
| 25 | + }; |
| 26 | + |
| 27 | + let texture; |
| 28 | + const mapper = vtkMapper2D.newInstance(); |
| 29 | + const plane = vtkPlaneSource.newInstance({ |
| 30 | + xResolution: 1, |
| 31 | + yResolution: 1, |
| 32 | + }); |
| 33 | + |
| 34 | + function createTextTexture(text) { |
| 35 | + const fontSizeScale = publicAPI.getProperty().getFontSizeScale(); |
| 36 | + const fontStyle = publicAPI.getProperty().getFontStyle(); |
| 37 | + const fontFamily = publicAPI.getProperty().getFontFamily(); |
| 38 | + const fontColor = publicAPI.getProperty().getFontColor(); |
| 39 | + const shadowColor = publicAPI.getProperty().getShadowColor(); |
| 40 | + const shadowOffset = publicAPI.getProperty().getShadowOffset(); |
| 41 | + const shadowBlur = publicAPI.getProperty().getShadowBlur(); |
| 42 | + const resolution = publicAPI.getProperty().getResolution(); |
| 43 | + const backgroundColor = publicAPI.getProperty().getBackgroundColor(); |
| 44 | + |
| 45 | + const dpr = Math.max(window.devicePixelRatio || 1, 1); |
| 46 | + const canvas = document.createElement('canvas'); |
| 47 | + const ctx = canvas.getContext('2d'); |
| 48 | + |
| 49 | + // Set the text properties to measure |
| 50 | + const textSize = fontSizeScale(resolution) * dpr; |
| 51 | + |
| 52 | + ctx.font = `${fontStyle} ${textSize}px "${fontFamily}"`; |
| 53 | + ctx.textBaseline = 'middle'; |
| 54 | + ctx.textAlign = 'center'; |
| 55 | + |
| 56 | + // Measure the text |
| 57 | + const metrics = ctx.measureText(text); |
| 58 | + const textWidth = metrics.width / dpr; |
| 59 | + |
| 60 | + const { |
| 61 | + actualBoundingBoxLeft, |
| 62 | + actualBoundingBoxRight, |
| 63 | + actualBoundingBoxAscent, |
| 64 | + actualBoundingBoxDescent, |
| 65 | + } = metrics; |
| 66 | + const hAdjustment = (actualBoundingBoxLeft - actualBoundingBoxRight) / 2; |
| 67 | + const vAdjustment = |
| 68 | + (actualBoundingBoxAscent - actualBoundingBoxDescent) / 2; |
| 69 | + |
| 70 | + const textHeight = textSize / dpr - vAdjustment; |
| 71 | + |
| 72 | + // Update canvas size to fit text |
| 73 | + const [width, height] = [textWidth * dpr, textHeight * dpr]; |
| 74 | + canvas.width = width; |
| 75 | + canvas.height = height; |
| 76 | + |
| 77 | + // vertical flip |
| 78 | + ctx.translate(0, height); |
| 79 | + ctx.scale(1, -1); |
| 80 | + |
| 81 | + // Clear the canvas |
| 82 | + ctx.clearRect(0, 0, width, height); |
| 83 | + |
| 84 | + if (backgroundColor) { |
| 85 | + ctx.fillStyle = vtkMath.floatRGB2HexCode(backgroundColor); |
| 86 | + ctx.fillRect(0, 0, width, height); |
| 87 | + } |
| 88 | + |
| 89 | + // Reset context after resize and prepare for rendering |
| 90 | + ctx.imageSmoothingEnabled = true; |
| 91 | + ctx.imageSmoothingQuality = 'high'; |
| 92 | + ctx.font = `${fontStyle} ${textSize}px "${fontFamily}"`; |
| 93 | + ctx.fillStyle = vtkMath.floatRGB2HexCode(fontColor); |
| 94 | + ctx.textBaseline = 'middle'; |
| 95 | + ctx.textAlign = 'center'; |
| 96 | + |
| 97 | + // set shadow |
| 98 | + ctx.shadowColor = shadowColor; |
| 99 | + ctx.shadowOffsetX = shadowOffset[0]; |
| 100 | + ctx.shadowOffsetY = shadowOffset[1]; |
| 101 | + ctx.shadowBlur = shadowBlur; |
| 102 | + |
| 103 | + ctx.fillText(text, width / 2 + hAdjustment, height / 2 + vAdjustment); |
| 104 | + |
| 105 | + const tex = vtkTexture.newInstance(); |
| 106 | + const vtkImage = ImageHelper.canvasToImageData(canvas); |
| 107 | + tex.setInputData(vtkImage, 0); |
| 108 | + |
| 109 | + // Update plane dimensions to match text size |
| 110 | + plane.set({ |
| 111 | + point1: [width, 0, 0], |
| 112 | + point2: [0, height, 0], |
| 113 | + }); |
| 114 | + |
| 115 | + return tex; |
| 116 | + } |
| 117 | + |
| 118 | + mapper.setInputConnection(plane.getOutputPort()); |
| 119 | + |
| 120 | + publicAPI.setMapper(mapper); |
| 121 | + |
| 122 | + publicAPI.setInput = (input) => { |
| 123 | + if (input !== model.input && input) { |
| 124 | + model.input = input; |
| 125 | + publicAPI.removeTexture(texture); |
| 126 | + texture = createTextTexture(model.input); |
| 127 | + publicAPI.addTexture(texture); |
| 128 | + } |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +// Default property values |
| 133 | +const DEFAULT_VALUES = { |
| 134 | + mapper: null, |
| 135 | + property: null, |
| 136 | +}; |
| 137 | + |
| 138 | +export function extend(publicAPI, model, initialValues = {}) { |
| 139 | + Object.assign(model, DEFAULT_VALUES, initialValues); |
| 140 | + |
| 141 | + // Inheritance |
| 142 | + vtkActor2D.extend(publicAPI, model, initialValues); |
| 143 | + |
| 144 | + // Build VTK API |
| 145 | + macro.setGet(publicAPI, model, ['input']); |
| 146 | + |
| 147 | + // Object methods |
| 148 | + vtkTextActor(publicAPI, model); |
| 149 | +} |
| 150 | + |
| 151 | +export const newInstance = macro.newInstance(extend, 'vtkTextActor'); |
| 152 | + |
| 153 | +export default { newInstance, extend }; |
0 commit comments