Re: Text ascent, descent and x-height

Thanks for the reply, Erik. I'm happy to see that a more advanced interface has already been discussed. I've written up two use-cases, the proposal (again), some requirements and an open question, which you can find in just a few lines. There are surely more open questions that can be asked, as well as more requirements that can be stated. Input on those fronts is of course more than welcome.

The general problem:
How can we correctly position a piece of text in SVG programatically?


Use-case 1:
Suppose we have an n by m rectangle and a piece of text s pixels high and t
pixels wide, such that t < n. We want to center the piece of text in the
rectangle programmatically by offsetting the text.

Determining the x-offset for the text is straightforward, using the existing
getComputedTextLength function.

For western locales, by default, the origin of a piece of text is on the text's
baseline. To correctly offset the text on the y-axis, we therefore need to know
at least the font's ascent height (from the top of my head):

  y-offset = ((m / 2) - (font-size / 2)) + ascent height


Use-case 2:
Suppose we want to rotate a piece of text around the center of its bounding
box, determining the center point programmatically. The syntax for rotation is

  rotate(angle x-offset y-offset)

The offsets are relative to the shape's origin, which in the case of text
(again, assuming a western locale) is on its baseline at x = 0.

Again, determining the required x-offset is straightforward, using
getComputedTextLength. For the y-offset, we need to know at least the descent
height:

  y-offset = 0 - ((font-size / 2) - descent height)


Proposal:
I propose to add the following methods to SVGTextContentElement, the latter of
which is not directly relevant for the use-cases I've mentioned, but may
undoubtedly be useful:

  float getComputedTextAscentHeight();
  float getComputedTextDescentHeight();
  float getComputedTextXHeight();


Requirements for the methods:

- They must provide the values for the text in the selected systemLanguage in a
  <switch> element.

- They must provide the values for the font that will be rendered. For
  example, if the specified font is not available on the client, the methods
  return the heights of the fall-back font instead.


Open questions:

- How would this work with top-to-bottom locales?



> On 11 Nov 2014, at 10:09, Erik Dahlström <ed@opera.com> wrote:
> 
> On Sat, 08 Nov 2014 00:05:00 +0100, Jurriën Stutterheim <j.stutterheim@cs.ru.nl> wrote:
> 
>> Hi all,
>> 
>> For my research I'm currently working on a high-level, compositional image API in the purely functional programming language Clean. We are using SVG as back-end, i.e., we are generating SVG from this high-level image description.
>> 
>> In order to successfully generate images containing text, we need access to font metrics. Currently, SVGTextContentElement already defines getComputedTextLength, amongst others. However, to accurately position text in an image, we also need access to a font's ascent, descent and x-height. While this information is already present in the form of an attribute on the font-face element, I would like to be able to access this information for any given text, even if I haven't manually defined the font in a font-face element.
> 
> Note that SVG fonts have been removed from SVG2.
> 
>> In particular, I would like to see the following additions to SVGTextContentElement:
>> 
>> interface SVGTextContentElement : SVGElement, SVGTests, SVGLangSpace, SVGExternalResourcesRequired, SVGStylable {
>> 
>>  (snip)
>> 
>>  float getComputedTextAscentHeight();
>>  float getComputedTextDecentHeight();
>>  float getComputedTextXHeight();
>> };
>> 
>> Would this be possible?
> 
> Yes, it's technically possible.
> 
>> What are the technical hurdles for such an API?
> 
> Font-switching or font fallbacks may affect the result, for example.
> 
>> Also, what is the formal procedure for proposing such API changes?
> 
> Typically, the first step would be to present your use-cases and requirements.
> 
> Access to more font metrics has been requested before, see
> https://www.w3.org/Graphics/SVG/WG/wiki/SVG2_Requirements_Mailing_List_Feedback#Consider_adding_advanced_font_metrics_interface (ISSUE-2379).
> 
> Cheers
> -- 
> Erik Dahlstrom, Web Technology Developer, Opera Software
> Co-Chair, W3C SVG Working Group
> 

Received on Tuesday, 11 November 2014 11:31:35 UTC