[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>
This commit is contained in:
Kevin Lubick 2021-02-23 08:29:04 -05:00
parent 58605b0466
commit 3dbca47568
9 changed files with 14 additions and 84 deletions

View File

@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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

View File

@ -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

View File

@ -322,8 +322,6 @@ function fontTests(CK: CanvasKit, face?: Typeface, paint?: Paint) {
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);

View File

@ -1515,21 +1515,6 @@ export interface Font extends EmbindObject<Font> {
*/
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

View File

@ -1144,31 +1144,6 @@ EMSCRIPTEN_BINDINGS(Skia) {
.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)

View File

@ -335,7 +335,6 @@ var CanvasKit = {
getSize: function() {},
getSkewX: function() {},
getTypeface: function() {},
measureText: function() {},
setHinting: function() {},
setLinearMetrics: function() {},
setScaleX: function() {},
@ -348,13 +347,11 @@ var CanvasKit = {
getGlyphBounds: function() {},
getGlyphIDs: function() {},
getGlyphWidths: function() {},
getWidths: function() {},
},
// private API (from C++ bindings)
_getGlyphIDs: function() {},
_getGlyphWidthBounds: function() {},
_getWidths: function() {},
},
FontMgr: {

View File

@ -88,37 +88,6 @@ CanvasKit._extraInitializations.push(function() {
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 @@ CanvasKit._extraInitializations.push(function() {
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);

View File

@ -284,15 +284,16 @@ describe('Font Behavior', () => {
// 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();
}

View File

@ -5,7 +5,7 @@ describe('CanvasKit\'s Matrix Helpers', () => {
});
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);