Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: x/image/font: add sTypoAscent and sTypoDescent #68394

Open
zhuowenchen opened this issue Jul 12, 2024 · 13 comments
Open

proposal: x/image/font: add sTypoAscent and sTypoDescent #68394

zhuowenchen opened this issue Jul 12, 2024 · 13 comments
Labels
Milestone

Comments

@zhuowenchen
Copy link

Proposal Details

Now we will parse the ascender and descender in the hhea table of the font file, but this is not enough. According to the protocol description, the ascender and descender in the hhea table are applied to the apple platform, while the applications developed based on the windows platform require the ascender and descender to parse the sTypoAscender and sTypoDescender in the os2 table. (https://learn.microsoft.com/en-us/typography/opentype/spec/hhea)
image
However, our library does not provide such support. Can the sTypoAscender and sTypoDescender in the os2 table be added to the parse in the sfnt/sfnt.go file of golang.org/x/image/font/sfnt/?
The structure of the os2 table is described in this document, which contains the required sTypoAscender and sTypoDescender.(https://learn.microsoft.com/da-dk/typography/opentype/spec/os2)

@gopherbot gopherbot added this to the Proposal milestone Jul 12, 2024
@zhuowenchen
Copy link
Author

We can obtain sTypoAscent and sTypoDescent as follows.And exponse them to golang.org/x/images/font/font.go.
image

@ianlancetaylor ianlancetaylor changed the title proposal: golang.org/x/image/font: add sTypoAscent and sTypoDescent proposal: x/image/font: add sTypoAscent and sTypoDescent Jul 12, 2024
@ianlancetaylor
Copy link
Contributor

CC @nigeltao

@nigeltao
Copy link
Contributor

What does FreeType do when you ask it for a font's ascent?

@zhuowenchen
Copy link
Author

oh,I gave it a try and it would return the same value to me.It seems to be working correctly.

@zhuowenchen
Copy link
Author

But my point is not to indicate that there is a problem with obtaining ascent. I mean that in some occasions we may need sTypoAscent.Should we provide an API to obtain sTypoAscender?

@zhuowenchen
Copy link
Author

I tried to view the FreeType source code, and it seems that they have parsed sTypoAscender.
image

@nigeltao
Copy link
Contributor

Sorry for not being clearer. FreeType parses os2.sTypoAscender (https://github.com/freetype/freetype/blob/1452355de99d22b0cba6ccc17bf5cdf3682ef61e/src/sfnt/ttload.c#L1193) but it also parses hhea.Ascender (https://github.com/freetype/freetype/blob/1452355de99d22b0cba6ccc17bf5cdf3682ef61e/src/sfnt/ttmtx.c#L143).

My question is, as it parses both, which one does FreeType prefer when you ask it for a font's ascent?

@zhuowenchen
Copy link
Author

zhuowenchen commented Jul 25, 2024

I tried it. The FreeType will return the value which is parsed from hhea.Ascender when i ask it for a font's ascent.

@zhuowenchen
Copy link
Author

zhuowenchen commented Jul 25, 2024

Now, x/image/font return the same value. There is no problem in this logic. I mean sometimes we need the sTypoAscender, but now x/image/font can not support that. Maybe we could provide a new field which is named sTypoAscender in class Face or we could provide other ways to get sTypoAscender?

@zhuowenchen
Copy link
Author

What do you think of this proposal? If you are busy here, or let me submit the code and you help review it. I really want to be a code contributor, haha🤣

@nigeltao
Copy link
Contributor

Maybe we could provide a new field which is named sTypoAscender in class Face

font.Face and font.Metrics aren't specific to TrueType / OpenType. I'd rather not add something TTF-specific like sTypoAscender to that more-general font package. Maybe the sfnt package is a better place to contain that change, but I'm hesitant to provide two APIs for ascent before better understanding the motivation.

Can you elaborate on what you're eventually trying to do such that using OS_2.sTypoAscender works for you but using hhea.ascent does not?

Can you link to an example ttf file that demonstrates this?

@zhuowenchen
Copy link
Author

zhuowenchen commented Jul 30, 2024

Can you elaborate on what you're eventually trying to do such that using OS_2.sTypoAscender works for you but using hhea.ascent does not?

Sorry,I miss the message for a few day.
Now , I have 2 services , which is called A and the other is called B (calling them A and B is just for easy understanding).Service A is written in C ,it uses OS_2.sTypoAscender to render the font. The OS_2.sTypoAscender is got by this method https://github.com/nothings/stb/blob/master/stb_truetype.h#L2641.
However, service B is written in Go, so it cannot parse os_2.sTypoAscender now. If hhea.Ascender is used, it will cause the size of the rendered font to be different from that rendered by service A.The difference between them will cause some errors.

@zhuowenchen
Copy link
Author

zhuowenchen commented Jul 30, 2024

Can you link to an example ttf file that demonstrates this?

站酷庆科黄油体.ttf.zip
Let's take this file as an example. If we parse hhea.ascender, we will get 854. But if we parse os2.sTypoAscender, we will get 750. This gap will cause a difference in the size of the font we render.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants