This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 27638 - FEATURE REQUEST: Selectively enable metrics in measureText using optional second argument
Summary: FEATURE REQUEST: Selectively enable metrics in measureText using optional sec...
Status: NEW
Alias: None
Product: HTML WG
Classification: Unclassified
Component: HTML Canvas 2D Context (show other bugs)
Version: unspecified
Hardware: All All
: P2 enhancement
Target Milestone: ---
Assignee: Jay Munro
QA Contact: HTML WG Bugzilla archive list
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-17 12:34 UTC by Moti Zilberman
Modified: 2014-12-17 12:34 UTC (History)
3 users (show)

See Also:


Attachments

Description Moti Zilberman 2014-12-17 12:34:31 UTC
Hello,

I am the author of https://github.com/motiz88/canvas-text-metrics-polyfill .

I'd like to make the case for a small enhancement following up on the resolution of https://www.w3.org/Bugs/Public/show_bug.cgi?id=7798 , relating to the properties added to TextMetrics ( https://html.spec.whatwg.org/#drawing-text-to-the-bitmap ).

Background: Browser support for this useful and simple API is still surprisingly scarce (to my knowledge only Chrome Canary has it, behind an experimental flag, with a buggy implementation), so I have written a polyfill (which I believe is the first conforming to this API) based mainly on off-screen drawing and scanning. This approach is rather slow in the general case, but results in a robust and (at least, largely) conforming polyfill.

My suggested enhancement below, already implemented in my polyfill, would enable such polyfills (and possibly native implementations?) to be lighter and faster, in cases where e.g. only one or two metrics are needed out of the whole set. The availability of a fully conforming and performant polyfill may finally spur adoption of this API by developers and browser makers.

<suggestion>

interface CanvasRenderingContext2D {
...
TextMetrics measureText(DOMString text, optional CanvasMeasureTextOptions options);
...
};

interface CanvasMeasureTextOptions {
  // x-direction
  attribute boolean width; // advance width
  attribute boolean actualBoundingBoxLeft;
  attribute boolean actualBoundingBoxRight;

  // y-direction
  attribute boolean fontBoundingBoxAscent;
  attribute boolean fontBoundingBoxDescent;
  attribute boolean actualBoundingBoxAscent;
  attribute boolean actualBoundingBoxDescent;
  attribute boolean emHeightAscent;
  attribute boolean emHeightDescent;
  attribute boolean hangingBaseline;
  attribute boolean alphabeticBaseline;
  attribute boolean ideographicBaseline;
};

Augmented semantics of measureText():

If the options argument is missing or null, measureText() behaves as currently specified, and must return a TextMetrics object with valid values set on all its specified properties.

If there is a non-null options argument, then the returned TextMetrics object is only required to contain the set of properties whose names coincide with the names of properties on the options object which are both (1) set, and (2) equal to true. The TextMetrics object MAY additionally contain some properties it is not required to, in which case their values are not defined.

</suggestion>

I would appreciate any feedback on this.

Thanks,

Moti Zilberman