From 26fd6e8a2bc0595597eb55a18c2f15afa13c4d86 Mon Sep 17 00:00:00 2001 From: Bruno Alves de Faria Date: Tue, 28 Nov 2017 19:08:28 -0200 Subject: [PATCH] Creating cornerstone method to get the bounding box --- Packages/ohif-core/client/lib/cornerstone.js | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Packages/ohif-core/client/lib/cornerstone.js b/Packages/ohif-core/client/lib/cornerstone.js index 55d566ef2..06c0ef924 100644 --- a/Packages/ohif-core/client/lib/cornerstone.js +++ b/Packages/ohif-core/client/lib/cornerstone.js @@ -3,6 +3,53 @@ import { _ } from 'meteor/underscore'; OHIF.cornerstone = {}; +OHIF.cornerstone.getBoundingBox = (context, textLines, x, y, options) => { + if (Object.prototype.toString.call(textLines) !== '[object Array]') { + textLines = [textLines]; + } + + const padding = 5; + const font = cornerstoneTools.textStyle.getFont(); + const fontSize = cornerstoneTools.textStyle.getFontSize(); + + context.save(); + context.font = font; + context.textBaseline = 'top'; + + // Find the longest text width in the array of text data + let maxWidth = 0; + + textLines.forEach(text => { + // Get the text width in the current font + const width = context.measureText(text).width; + + // Find the maximum with for all the text rows; + maxWidth = Math.max(maxWidth, width); + }); + + // Calculate the bounding box for this text box + const boundingBox = { + width: maxWidth + (padding * 2), + height: padding + textLines.length * (fontSize + padding) + }; + + if (options && options.centering && options.centering.x === true) { + x -= boundingBox.width / 2; + } + + if (options && options.centering && options.centering.y === true) { + y -= boundingBox.height / 2; + } + + boundingBox.left = x; + boundingBox.top = y; + + context.restore(); + + // Return the bounding box so it can be used for pointNearHandle + return boundingBox; +}; + OHIF.cornerstone.pixelToPage = (element, position) => { const enabledElement = cornerstone.getEnabledElement(element); const result = {