* Get the size of the tooltip
(tooltip, options)
| 146 | * Get the size of the tooltip |
| 147 | */ |
| 148 | function getTooltipSize(tooltip, options) { |
| 149 | const ctx = tooltip.chart.ctx; |
| 150 | const {body, footer, title} = tooltip; |
| 151 | const {boxWidth, boxHeight} = options; |
| 152 | const bodyFont = toFont(options.bodyFont); |
| 153 | const titleFont = toFont(options.titleFont); |
| 154 | const footerFont = toFont(options.footerFont); |
| 155 | const titleLineCount = title.length; |
| 156 | const footerLineCount = footer.length; |
| 157 | const bodyLineItemCount = body.length; |
| 158 | |
| 159 | const padding = toPadding(options.padding); |
| 160 | let height = padding.height; |
| 161 | let width = 0; |
| 162 | |
| 163 | // Count of all lines in the body |
| 164 | let combinedBodyLength = body.reduce((count, bodyItem) => count + bodyItem.before.length + bodyItem.lines.length + bodyItem.after.length, 0); |
| 165 | combinedBodyLength += tooltip.beforeBody.length + tooltip.afterBody.length; |
| 166 | |
| 167 | if (titleLineCount) { |
| 168 | height += titleLineCount * titleFont.lineHeight |
| 169 | + (titleLineCount - 1) * options.titleSpacing |
| 170 | + options.titleMarginBottom; |
| 171 | } |
| 172 | if (combinedBodyLength) { |
| 173 | // Body lines may include some extra height depending on boxHeight |
| 174 | const bodyLineHeight = options.displayColors ? Math.max(boxHeight, bodyFont.lineHeight) : bodyFont.lineHeight; |
| 175 | height += bodyLineItemCount * bodyLineHeight |
| 176 | + (combinedBodyLength - bodyLineItemCount) * bodyFont.lineHeight |
| 177 | + (combinedBodyLength - 1) * options.bodySpacing; |
| 178 | } |
| 179 | if (footerLineCount) { |
| 180 | height += options.footerMarginTop |
| 181 | + footerLineCount * footerFont.lineHeight |
| 182 | + (footerLineCount - 1) * options.footerSpacing; |
| 183 | } |
| 184 | |
| 185 | // Title width |
| 186 | let widthPadding = 0; |
| 187 | const maxLineWidth = function(line) { |
| 188 | width = Math.max(width, ctx.measureText(line).width + widthPadding); |
| 189 | }; |
| 190 | |
| 191 | ctx.save(); |
| 192 | |
| 193 | ctx.font = titleFont.string; |
| 194 | each(tooltip.title, maxLineWidth); |
| 195 | |
| 196 | // Body width |
| 197 | ctx.font = bodyFont.string; |
| 198 | each(tooltip.beforeBody.concat(tooltip.afterBody), maxLineWidth); |
| 199 | |
| 200 | // Body lines may include some extra width due to the color box |
| 201 | widthPadding = options.displayColors ? (boxWidth + 2 + options.boxPadding) : 0; |
| 202 | each(body, (bodyItem) => { |
| 203 | each(bodyItem.before, maxLineWidth); |
| 204 | each(bodyItem.lines, maxLineWidth); |
| 205 | each(bodyItem.after, maxLineWidth); |
no test coverage detected