[canvaskit] Remove Font.measureText and Font.getWidths
This had been previously deprecated.
Bug: skia:10717
Change-Id: Ic57ed835c13cfa7812099a3ef20ed7ff5aa62f7f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/371339
Reviewed-by: Nathaniel Nifong <nifong@google.com>
diff --git a/modules/canvaskit/CHANGELOG.md b/modules/canvaskit/CHANGELOG.md
index 8eecd8f..f3deeda 100644
--- a/modules/canvaskit/CHANGELOG.md
+++ b/modules/canvaskit/CHANGELOG.md
@@ -10,6 +10,11 @@
- The `ShapedText` type has been removed. Clients who want ShapedText should use the
Paragraph APIs.
+### Removed
+ - `Font.measureText`, which had been previously deprecated. Clients should use either
+ Paragraph APIs or `Font.getGlyphWidths` instead (the latter does no shaping).
+ - `Font.getWidths`, which had been previously deprecated. Clients should use `Font.getGlyphWidths`.
+
## [0.24.0] - 2021-02-18
### Added
diff --git a/modules/canvaskit/canvaskit/example.html b/modules/canvaskit/canvaskit/example.html
index d6ead9f..82a37a9 100644
--- a/modules/canvaskit/canvaskit/example.html
+++ b/modules/canvaskit/canvaskit/example.html
@@ -368,16 +368,15 @@
ctx.fillStyle = 'black';
ctx.font = '26px Bungee';
ctx.rotate(.1);
- let text = ctx.measureText('Awesome');
ctx.fillText('Awesome ', 25, 100);
- ctx.strokeText('Groovy!', 35+text.width, 100);
+ ctx.strokeText('Groovy!', 200, 100);
// Draw line under Awesome
ctx.strokeStyle = 'rgba(125,0,0,0.5)';
ctx.beginPath();
ctx.lineWidth = 6;
ctx.moveTo(25, 105);
- ctx.lineTo(25 + text.width, 105);
+ ctx.lineTo(200, 105);
ctx.stroke();
// squished vertically
diff --git a/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts b/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts
index 00585d1..b000ee4 100644
--- a/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts
+++ b/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts
@@ -322,8 +322,6 @@
font.getSize();
font.getSkewX();
font.getTypeface();
- const w2 = font.getWidths('abcdefg'); // $ExpectType number[]
- const w = font.measureText('abc'); // $ExpectType number
font.setEdging(CK.FontEdging.Alias);
font.setEmbeddedBitmaps(true);
font.setHinting(CK.FontHinting.Slight);
diff --git a/modules/canvaskit/canvaskit/types/index.d.ts b/modules/canvaskit/canvaskit/types/index.d.ts
index 62b70f6..6a1f609 100644
--- a/modules/canvaskit/canvaskit/types/index.d.ts
+++ b/modules/canvaskit/canvaskit/types/index.d.ts
@@ -1516,21 +1516,6 @@
getTypeface(): Typeface | null;
/**
- * Retrieves the advanceX measurements for each code point in str.
- * [deprecated] Use getGlyphIDs and getGlyphWidths instead.
- * @param str
- */
- getWidths(str: string): number[];
-
- /**
- * Retrieves the total advance with the given string.
- * If attempting to shape text to fit into a given width, using getGlyphIDs and getGlyphWidths
- * is probably easier / more efficient.
- * @param str
- */
- measureText(str: string): number;
-
- /**
* Requests, but does not require, that edge pixels draw opaque or with partial transparency.
* @param edging
*/
diff --git a/modules/canvaskit/canvaskit_bindings.cpp b/modules/canvaskit/canvaskit_bindings.cpp
index 86b6e3f..f30e16e 100644
--- a/modules/canvaskit/canvaskit_bindings.cpp
+++ b/modules/canvaskit/canvaskit_bindings.cpp
@@ -1144,31 +1144,6 @@
.function("getSize", &SkFont::getSize)
.function("getSkewX", &SkFont::getSkewX)
.function("getTypeface", &SkFont::getTypeface, allow_raw_pointers())
- .function("_getWidths", optional_override([](SkFont& self, uintptr_t /* char* */ sptr,
- size_t strLen, size_t expectedCodePoints,
- uintptr_t /* SkScalar* */ wptr) -> bool {
- char* str = reinterpret_cast<char*>(sptr);
- SkScalar* widths = reinterpret_cast<SkScalar*>(wptr);
-
- SkGlyphID* glyphStorage = new SkGlyphID[expectedCodePoints];
- int actualCodePoints = self.textToGlyphs(str, strLen, SkTextEncoding::kUTF8,
- glyphStorage, expectedCodePoints);
- if (actualCodePoints != expectedCodePoints) {
- SkDebugf("Actually %d glyphs, expected only %d\n",
- actualCodePoints, expectedCodePoints);
- return false;
- }
-
- self.getWidths(glyphStorage, actualCodePoints, widths);
- delete[] glyphStorage;
- return true;
- }))
- .function("measureText", optional_override([](SkFont& self, std::string text) {
- // TODO(kjlubick): Remove this API
- // Need to maybe add a helper in interface.js that supports UTF-8
- // Otherwise, go with std::wstring and set UTF-32 encoding.
- return self.measureText(text.c_str(), text.length(), SkTextEncoding::kUTF8);
- }))
.function("setEdging", &SkFont::setEdging)
.function("setEmbeddedBitmaps", &SkFont::setEmbeddedBitmaps)
.function("setHinting", &SkFont::setHinting)
diff --git a/modules/canvaskit/externs.js b/modules/canvaskit/externs.js
index af74c2f..96231d1 100644
--- a/modules/canvaskit/externs.js
+++ b/modules/canvaskit/externs.js
@@ -335,7 +335,6 @@
getSize: function() {},
getSkewX: function() {},
getTypeface: function() {},
- measureText: function() {},
setHinting: function() {},
setLinearMetrics: function() {},
setScaleX: function() {},
@@ -348,13 +347,11 @@
getGlyphBounds: function() {},
getGlyphIDs: function() {},
getGlyphWidths: function() {},
- getWidths: function() {},
},
// private API (from C++ bindings)
_getGlyphIDs: function() {},
_getGlyphWidthBounds: function() {},
- _getWidths: function() {},
},
FontMgr: {
diff --git a/modules/canvaskit/font.js b/modules/canvaskit/font.js
index 68f5523..f804a33 100644
--- a/modules/canvaskit/font.js
+++ b/modules/canvaskit/font.js
@@ -88,37 +88,6 @@
return rv;
};
- // Returns an array of the widths of the glyphs in this string.
- // TODO(kjlubick) Remove this API - getGlyphWidths is the better API.
- CanvasKit.Font.prototype.getWidths = function(str) {
- // add 1 for null terminator
- var codePoints = str.length + 1;
- // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
- // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
- // Add 1 for null terminator
- var strBytes = lengthBytesUTF8(str) + 1;
- var strPtr = CanvasKit._malloc(strBytes);
- stringToUTF8(str, strPtr, strBytes);
-
- var bytesPerFloat = 4;
- // allocate widths == numCodePoints
- var widthPtr = CanvasKit._malloc(codePoints * bytesPerFloat);
- if (!this._getWidths(strPtr, strBytes, codePoints, widthPtr)) {
- Debug('Could not compute widths');
- CanvasKit._free(strPtr);
- CanvasKit._free(widthPtr);
- return null;
- }
- // reminder, this shouldn't copy the data, just is a nice way to
- // wrap 4 bytes together into a float.
- var widths = new Float32Array(CanvasKit.HEAPU8.buffer, widthPtr, codePoints);
- // This copies the data so we can free the CanvasKit memory
- var retVal = Array.from(widths);
- CanvasKit._free(strPtr);
- CanvasKit._free(widthPtr);
- return retVal;
- };
-
// arguments should all be arrayBuffers or be an array of arrayBuffers.
CanvasKit.FontMgr.FromData = function() {
if (!arguments.length) {
@@ -184,7 +153,8 @@
initialOffset = 0;
}
- var widths = font.getWidths(str);
+ var ids = font.getGlyphIDs(str);
+ var widths = font.getGlyphWidths(ids);
var rsx = new CanvasKit.RSXFormBuilder();
var meas = new CanvasKit.ContourMeasureIter(path, false, 1);
diff --git a/modules/canvaskit/tests/font.spec.js b/modules/canvaskit/tests/font.spec.js
index 89a4ea4..53708f7 100644
--- a/modules/canvaskit/tests/font.spec.js
+++ b/modules/canvaskit/tests/font.spec.js
@@ -284,15 +284,16 @@
// just update them with the new values. For super-accurate readings, one could
// run a C++ snippet of code and compare the values, but that is likely unnecessary
// unless we suspect a bug with the bindings.
- const expectedSizes = [1178.71143, 458.64258, 50.450683];
+ const expectedSizes = [241.06299, 93.79883, 10.31787];
for (const idx in fontSizes) {
const font = new CanvasKit.Font(typeface, fontSizes[idx]);
font.setHinting(CanvasKit.FontHinting.None);
font.setLinearMetrics(true);
font.setSubpixel(true);
- const res = font.measureText('someText');
- expect(res).toBeCloseTo(expectedSizes[idx], 5);
+ const ids = font.getGlyphIDs('M');
+ const widths = font.getGlyphWidths(ids);
+ expect(widths[0]).toBeCloseTo(expectedSizes[idx], 5);
font.delete();
}
diff --git a/modules/canvaskit/tests/matrix.spec.js b/modules/canvaskit/tests/matrix.spec.js
index 28dac43..fa84b4d 100644
--- a/modules/canvaskit/tests/matrix.spec.js
+++ b/modules/canvaskit/tests/matrix.spec.js
@@ -5,7 +5,7 @@
});
const expectArrayCloseTo = (a, b, precision) => {
- precision = precision || 14 // digits of precision in base 10
+ precision = precision || 14; // digits of precision in base 10
expect(a.length).toEqual(b.length);
for (let i=0; i<a.length; i++) {
expect(a[i]).toBeCloseTo(b[i], precision);