Skip to content

Commit

Permalink
fix: add back width and height props and missing props of TextMetrics (
Browse files Browse the repository at this point in the history
…#26)

* fix width and height
* fix measureText
  • Loading branch information
DjDeveloperr authored Jan 19, 2022
1 parent c104264 commit ac6f50d
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,7 @@ export var CanvasKitInit = (function () {
k.me();
});
};

Object.defineProperty(this, "currentTransform", {
enumerable: !0,
get: function () {
Expand Down Expand Up @@ -2956,12 +2957,42 @@ export var CanvasKitInit = (function () {
this.lineTo = function (k, p) {
R(this.Td, k, p);
};
this.measureText = function (k) {
k = this.ne.getGlyphIDs(k);
k = this.ne.getGlyphWidths(k);
let p = 0;
for (const z of k) p += z;
return { width: p };
this.measureText = function (txt) {
const glyphWidths = this.ne.getGlyphWidths(txt);
let width = 0;
for (const w of glyphWidths) {
width += w;
}
let glyphBounds = this.ne
.getGlyphBounds(txt)
.reduce((all, one, i) => {
const ch = Math.floor(i / 4);
all[ch] = [].concat(all[ch] || [], one);
return all;
}, []);
let actualBoundingBoxAscent = Math.abs(
glyphBounds.map((e) => e[1]).reduce((p, a) => p + a, 0),
) / glyphBounds.length;
let actualBoundingBoxDescent = Math.abs(
glyphBounds.map((e) => e[0]).reduce((p, a) => p + a, 0),
) / glyphBounds.length;
let actualBoundingBoxLeft = Math.abs(
glyphBounds.map((e) => e[3]).reduce((p, a) => p + a, 0),
) / glyphBounds.length;
// let actualBoundingBoxRight = Math.abs(
// glyphBounds.map((e) => e[2]).reduce((p, a) => p + a, 0),
// ) / glyphBounds.length;
const metrics = this.ne.getMetrics();
const res = {
width,
actualBoundingBoxAscent,
actualBoundingBoxDescent,
actualBoundingBoxLeft,
actualBoundingBoxRight: width + actualBoundingBoxLeft,
fontBoundingBoxAscent: Math.abs(metrics.ascent),
fontBoundingBoxDescent: Math.abs(metrics.descent),
};
return res;
};
this.moveTo = function (k, p) {
var z = this.Td;
Expand Down Expand Up @@ -3216,6 +3247,12 @@ export var CanvasKitInit = (function () {
function y(G) {
this.cf = G;
this.Md = new r(G.getCanvas());
Object.defineProperty(this, "width", {
value: this.cf.Af,
});
Object.defineProperty(this, "height", {
value: this.cf.xf,
});
this.Qe = [];
this.decodeImage = function (k) {
k = a.MakeImageFromEncoded(k);
Expand Down

0 comments on commit ac6f50d

Please sign in to comment.