mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +08:00
measureText fallback
This commit is contained in:
parent
cb69ff9223
commit
bb5275f9d1
@ -3,6 +3,10 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
This project adheres to [Semantic Versioning](http://semver.org/).
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
### 9.3.13 (2024-07-05)
|
||||||
|
|
||||||
|
- Fallback for `Konva.Text.measureSize()` when 2d context doesn't return full data
|
||||||
|
|
||||||
### 9.3.12 (2024-06-20)
|
### 9.3.12 (2024-06-20)
|
||||||
|
|
||||||
- Fix stopped transforming when it was triggered by multi-touch
|
- Fix stopped transforming when it was triggered by multi-touch
|
||||||
|
@ -405,21 +405,30 @@ export class Text extends Shape<TextConfig> {
|
|||||||
|
|
||||||
metrics = _context.measureText(text);
|
metrics = _context.measureText(text);
|
||||||
_context.restore();
|
_context.restore();
|
||||||
|
|
||||||
|
// Scale the fallback values based on the provided fontSize compared to the sample size (100 in your new case)
|
||||||
|
const scaleFactor = fontSize / 100;
|
||||||
|
|
||||||
|
// Note, fallback values are from chrome browser with 100px font size and font-family "Arial"
|
||||||
return {
|
return {
|
||||||
// copy all text metrics data:
|
actualBoundingBoxAscent:
|
||||||
actualBoundingBoxAscent: metrics.actualBoundingBoxAscent,
|
metrics.actualBoundingBoxAscent ?? 71.58203125 * scaleFactor,
|
||||||
actualBoundingBoxDescent: metrics.actualBoundingBoxDescent,
|
actualBoundingBoxDescent: metrics.actualBoundingBoxDescent ?? 0, // Remains zero as there is no descent in the provided metrics
|
||||||
actualBoundingBoxLeft: metrics.actualBoundingBoxLeft,
|
actualBoundingBoxLeft:
|
||||||
actualBoundingBoxRight: metrics.actualBoundingBoxRight,
|
metrics.actualBoundingBoxLeft ?? -7.421875 * scaleFactor,
|
||||||
alphabeticBaseline: metrics.alphabeticBaseline,
|
actualBoundingBoxRight:
|
||||||
emHeightAscent: metrics.emHeightAscent,
|
metrics.actualBoundingBoxRight ?? 75.732421875 * scaleFactor,
|
||||||
emHeightDescent: metrics.emHeightDescent,
|
alphabeticBaseline: metrics.alphabeticBaseline ?? 0, // Remains zero as it's typically relative to the baseline itself
|
||||||
fontBoundingBoxAscent: metrics.fontBoundingBoxAscent,
|
emHeightAscent: metrics.emHeightAscent ?? 100 * scaleFactor,
|
||||||
fontBoundingBoxDescent: metrics.fontBoundingBoxDescent,
|
emHeightDescent: metrics.emHeightDescent ?? -20 * scaleFactor,
|
||||||
hangingBaseline: metrics.hangingBaseline,
|
fontBoundingBoxAscent: metrics.fontBoundingBoxAscent ?? 91 * scaleFactor,
|
||||||
ideographicBaseline: metrics.ideographicBaseline,
|
fontBoundingBoxDescent:
|
||||||
|
metrics.fontBoundingBoxDescent ?? 21 * scaleFactor,
|
||||||
|
hangingBaseline:
|
||||||
|
metrics.hangingBaseline ?? 72.80000305175781 * scaleFactor,
|
||||||
|
ideographicBaseline: metrics.ideographicBaseline ?? -21 * scaleFactor,
|
||||||
width: metrics.width,
|
width: metrics.width,
|
||||||
height: fontSize,
|
height: fontSize, // Typically set to the font size
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
_getContextFont() {
|
_getContextFont() {
|
||||||
|
@ -1674,8 +1674,7 @@ describe('Text', function () {
|
|||||||
assert.equal(layer.getContext().getTrace(), trace);
|
assert.equal(layer.getContext().getTrace(), trace);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('sets rtl text direction', function () {
|
||||||
it('sets rtl text direction', function () {
|
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
var layer = new Konva.Layer();
|
var layer = new Konva.Layer();
|
||||||
|
|
||||||
@ -1713,4 +1712,22 @@ describe('Text', function () {
|
|||||||
|
|
||||||
assert.equal(layer.getContext().getTrace(), trace);
|
assert.equal(layer.getContext().getTrace(), trace);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('try fixed render', () => {
|
||||||
|
Konva._fixTextRendering = true;
|
||||||
|
var stage = addStage();
|
||||||
|
var layer = new Konva.Layer();
|
||||||
|
|
||||||
|
stage.add(layer);
|
||||||
|
var text = new Konva.Text({ text: 'hello', fontSize: 100 });
|
||||||
|
|
||||||
|
layer.add(text);
|
||||||
|
layer.draw();
|
||||||
|
Konva._fixTextRendering = false;
|
||||||
|
|
||||||
|
const trace =
|
||||||
|
'clearRect(0,0,578,200);clearRect(0,0,578,200);save();transform(1,0,0,1,0,0);font=normal normal 100px Arial;textBaseline=alphabetic;textAlign=left;translate(0,0);save();fillStyle=black;fillText(hello,0,85);restore();restore();';
|
||||||
|
|
||||||
|
assert.equal(layer.getContext().getTrace(), trace);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user