diff --git a/lib/network/modules/components/shared/Label.js b/lib/network/modules/components/shared/Label.js index bf769a04e..841ef1df1 100644 --- a/lib/network/modules/components/shared/Label.js +++ b/lib/network/modules/components/shared/Label.js @@ -713,7 +713,7 @@ class Label { */ _processLabelText(ctx, selected, hover, inText) { let splitter = new LabelSplitter(ctx, this, selected, hover); - return splitter.process(inText); + return splitter.process(ctx, inText); } @@ -735,7 +735,7 @@ class Label { state.width = this.fontOptions.minWdt; } - this.size.labelHeight =state.height; + this.size.labelHeight = state.height; if ((this.fontOptions.minHgt > 0) && (state.height < this.fontOptions.minHgt)) { state.height = this.fontOptions.minHgt; } diff --git a/lib/network/modules/components/shared/LabelSplitter.js b/lib/network/modules/components/shared/LabelSplitter.js index 5ef0fc6d5..ecd2f9329 100644 --- a/lib/network/modules/components/shared/LabelSplitter.js +++ b/lib/network/modules/components/shared/LabelSplitter.js @@ -336,16 +336,23 @@ class LabelSplitter { * In order not to break existing functionality, for the time being this behaviour will * be retained in any code changes. * + * @param {CanvasRenderingContext2D} ctx * @param {string} text text to split * @returns {Array} */ - process(text) { + process(ctx, text) { if (!ComponentUtil.isValidLabel(text)) { return this.lines.finalize(); } var font = this.parent.fontOptions; + // Set the context's font to match the label's main font, + // otherwise measurements will be incorrect. + let fontString = ""; + fontString += font.size + "px " + font.face; + ctx.font = fontString.replace(/"/g, ""); + // Normalize the end-of-line's to a single representation - order important text = text.replace(/\r\n/g, '\n'); // Dos EOL's text = text.replace(/\r/g, '\n'); // Mac EOL's diff --git a/test/network/hoverLabelSplitterEdgeCase.html b/test/network/hoverLabelSplitterEdgeCase.html new file mode 100644 index 000000000..1f0896877 --- /dev/null +++ b/test/network/hoverLabelSplitterEdgeCase.html @@ -0,0 +1,93 @@ + + + + Label Splitter Text Measurement Test + + + + + + + + + +

Bug #3928 meant that nodes would change size after being hovered if they had a widthConstraint specified.

+

This was due to the context's font not being set to the correct value when the label was evaluated during edges update, so the line would be split based on incorrect measurements.

+ +
+ + + + \ No newline at end of file