* Get the size of the tooltip
(tooltip, options)
| 182 | * Get the size of the tooltip |
| 183 | */ |
| 184 | function getTooltipSize(tooltip, options) { |
| 185 | const ctx = tooltip.chart.ctx; |
| 186 | const {body, footer, title} = tooltip; |
| 187 | const {boxWidth, boxHeight} = options; |
| 188 | const bodyFont = toFont(options.bodyFont); |
| 189 | const titleFont = toFont(options.titleFont); |
| 190 | const footerFont = toFont(options.footerFont); |
| 191 | const titleLineCount = title.length; |
| 192 | const footerLineCount = footer.length; |
| 193 | const bodyLineItemCount = body.length; |
| 194 | |
| 195 | const padding = toPadding(options.padding); |
| 196 | let height = padding.height; |
| 197 | let width = 0; |
| 198 | |
| 199 | // Count of all lines in the body |
| 200 | let combinedBodyLength = body.reduce((count, bodyItem) => count + bodyItem.before.length + bodyItem.lines.length + bodyItem.after.length, 0); |
| 201 | combinedBodyLength += tooltip.beforeBody.length + tooltip.afterBody.length; |
| 202 | |
| 203 | if (titleLineCount) { |
| 204 | height += titleLineCount * titleFont.lineHeight |
| 205 | + (titleLineCount - 1) * options.titleSpacing |
| 206 | + options.titleMarginBottom; |
| 207 | } |
| 208 | if (combinedBodyLength) { |
| 209 | // Body lines may include some extra height depending on boxHeight |
| 210 | const bodyLineHeight = options.displayColors ? Math.max(boxHeight, bodyFont.lineHeight) : bodyFont.lineHeight; |
| 211 | height += bodyLineItemCount * bodyLineHeight |
| 212 | + (combinedBodyLength - bodyLineItemCount) * bodyFont.lineHeight |
| 213 | + (combinedBodyLength - 1) * options.bodySpacing; |
| 214 | } |
| 215 | if (footerLineCount) { |
| 216 | height += options.footerMarginTop |
| 217 | + footerLineCount * footerFont.lineHeight |
| 218 | + (footerLineCount - 1) * options.footerSpacing; |
| 219 | } |
| 220 | |
| 221 | // Title width |
| 222 | let widthPadding = 0; |
| 223 | const maxLineWidth = function(line) { |
| 224 | width = Math.max(width, ctx.measureText(line).width + widthPadding); |
| 225 | }; |
| 226 | |
| 227 | ctx.save(); |
| 228 | |
| 229 | ctx.font = titleFont.string; |
| 230 | each(tooltip.title, maxLineWidth); |
| 231 | |
| 232 | // Body width |
| 233 | ctx.font = bodyFont.string; |
| 234 | each(tooltip.beforeBody.concat(tooltip.afterBody), maxLineWidth); |
| 235 | |
| 236 | // Body lines may include some extra width due to the color box |
| 237 | widthPadding = options.displayColors ? (boxWidth + 2 + options.boxPadding) : 0; |
| 238 | each(body, (bodyItem) => { |
| 239 | each(bodyItem.before, maxLineWidth); |
| 240 | each(bodyItem.lines, maxLineWidth); |
| 241 | each(bodyItem.after, maxLineWidth); |
no test coverage detected