Skip to content

Font fallback - #20506

Merged
bors-servo merged 5 commits into
servo:masterfrom
jonleighton:font-fallback
May 19, 2018
Merged

bors-servo merged 5 commits into
servo:masterfrom
jonleighton:font-fallback

Conversation

@jonleighton

@jonleighton jonleighton commented Apr 3, 2018 •

Copy link
Copy Markdown
Contributor

This implements more complete support for font fallback, see #17267.

r? @glennw @mbrubeck


This change is Reviewable

@highfive

highfive commented Apr 3, 2018

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @emilio: components/layout/text.rs

@highfive

highfive commented Apr 3, 2018

Copy link
Copy Markdown

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Apr 3, 2018
@jdm

jdm commented Apr 3, 2018

Copy link
Copy Markdown
Member

This looks amazing! \o/

@glennw

glennw commented Apr 3, 2018

Copy link
Copy Markdown
Member

Reviewed 17 of 23 files at r1.
Review status: 17 of 23 files reviewed at latest revision, all discussions resolved, some commit checks failed.


components/gfx/platform/freetype/font.rs, line 102 at r1 (raw file):

            FT_New_Memory_Face(lib, bytes.as_ptr(), bytes.len() as FT_Long, face_index, &mut face)
        } else {
            let filename = CString::new(&*template.identifier).expect("filename contains NUL byte!");

One of the reasons for making FT always load via memory here was so that the layout threads would never block on a synchronous file load. The idea was that the font cache thread would responsible for providing these. I'm not sure if that's really relevant or feasible, but that was the original idea, I think.


components/gfx/text/util.rs, line 156 at r1 (raw file):

    // https://en.wikipedia.org/wiki/Plane_(Unicode)#Supplementary_Ideographic_Plane
    if unicode_plane(codepoint) == 2 {

nit: could just be unicode_plane(codepoint) == 2 and remove the true / false here, unless there's more conditions to come in the future.


Comments from Reviewable

@glennw

glennw commented Apr 3, 2018

Copy link
Copy Markdown
Member

Partial review only so far - I'll try to work through the trickier bits today or tomorrow. Will need @mbrubeck to take a look at some of the text files that I'm not familiar with too.

@glennw

glennw commented Apr 4, 2018

Copy link
Copy Markdown
Member

Reviewed 6 of 23 files at r1.
Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


components/layout/text.rs, line 218 at r1 (raw file):

                let (mut start_position, mut end_position) = (0, 0);
                for (byte_index, character) in text.char_indices() {
                    if !character.is_control() {

Should get @mbrubeck to check this.


Comments from Reviewable

@glennw

glennw commented Apr 4, 2018

Copy link
Copy Markdown
Member

Reviewed the remaining files, they look good to me. I think it'd be worth getting @mbrubeck to do a quick pass over this too, as I'm not very up to date on text / font handling in Servo. My main question is related to the thread the font load occurs on (mentioned above).

@jonleighton

Copy link
Copy Markdown
Contributor Author

One of the reasons for making FT always load via memory here was so that the layout threads would never block on a synchronous file load. The idea was that the font cache thread would responsible for providing these. I'm not sure if that's really relevant or feasible, but that was the original idea, I think.

Thanks for the explanation. I note that the windows and macos implementations end up calling into platform APIs in this situation, do you know of calling those APIs results in any disk I/O?

Without this change I observed a big slowdown on Linux where the bytes of all the fallback fonts would be passed over the ipc-channel. The slowdown was mostly due to the CPU cost of serialization and deserialization. Performance was a lot better when I tried the serde_bytes crate, so that could be an option here (actually I think it is worth using serde_bytes regardless, but it may make performance acceptable if we want to keep all the I/O in the font cache thread).

One thing it is worth pointing out, is that by the time a layout thread does the file read, the file has already been read by the font cache thread. Therefore, we might hope that the data is already in an OS cache and it might be that no actual disk reads need to occur. I don't know enough about how this works to make any firm statements about that though. Another thing to say is that I'm testing this on a laptop with an SSD hard drive, so I'm probably less likely to notice disk I/O slowdowns.

Do you have any suggestions about how I could/should investigate this further so we can decide what the best approach is?

@jonleighton

Copy link
Copy Markdown
Contributor Author

I should add: when I looked into the bytes-over-IPC thing, I realised that the font code had originally been developed before a multiprocess architecture was implemented. Therefore, at the time, those bytes were not getting serialized or sent to a different process. Then, the multiprocess architecture came along and converted everything to IPC channels. I thought that perhaps it wasn't intentionally decided that the bytes should go over IPC, but instead just a consequence of how the code had evolved. It just seemed quite surprising to me to do this, but it might be that it's actually fine if we use serde_bytes.

@glennw

glennw commented Apr 5, 2018

Copy link
Copy Markdown
Member

You're exactly right - the original font code was built before any consideration for multi-process / ipc-channel, and serializing the bytes over the ipc channel was a side effect of that.

I think it's fine to move the file read into the font contexts at this stage - this is certainly a big improvement on what we have now.

We can re-consider the design of how / where we fetch the bytes as a separate issue for later - it's a big topic by itself.

There's no other blockers from me, but it would be good to get @mbrubeck to do a quick pass over this before merging - he has much more experience with the Servo text / layout code than me.

@mbrubeck

mbrubeck commented Apr 5, 2018

Copy link
Copy Markdown
Contributor

@bors-servo r+

Looks good to me. Thanks for this!

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit e15d3e1 has been approved by mbrubeck

@highfive highfive assigned mbrubeck and unassigned glennw Apr 5, 2018
@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Apr 5, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit e15d3e1 with merge b278ee4...

bors-servo pushed a commit that referenced this pull request Apr 5, 2018
Font fallback

This implements more complete support for font fallback, see #17267.

r? @glennw @mbrubeck

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20506)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - mac-dev-unit

@highfive highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Apr 5, 2018
@mbrubeck

mbrubeck commented Apr 5, 2018

Copy link
Copy Markdown
Contributor
error: unused import: `std::borrow::ToOwned`
 --> components/gfx/platform/macos/font_list.rs:6:5
  |
6 | use std::borrow::ToOwned;
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `-D unused-imports` implied by `-D warnings`

@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-tests-failed The changes caused existing tests to fail. labels Apr 6, 2018
@jonleighton

Copy link
Copy Markdown
Contributor Author

nit: could just be unicode_plane(codepoint) == 2 and remove the true / false here, unless there's more conditions to come in the future.

Done

Should get @mbrubeck to check this.

This wasn't actually 100% necessary for the fallback to work, though it's useful in terms of not doing unnecessary fallbacks for non-printing characters. I've split it out into a separate commit which gives a better explanation of what it does.

We can re-consider the design of how / where we fetch the bytes as a separate issue for later - it's a big topic by itself.

I've added a comment to the code linking to this discussion.

error: unused import: std::borrow::ToOwned

Fixed

@mbrubeck

mbrubeck commented Apr 6, 2018

Copy link
Copy Markdown
Contributor

@bors-servo r+

@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - android

@jdm

jdm commented May 18, 2018

Copy link
Copy Markdown
Member

@bors-servo try

@Eijebong

Copy link
Copy Markdown
Contributor

@bors-servo retry

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Trying commit be48d84 with merge 5d363e1...

@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - mac-rel-wpt1

Prior to this change, if none of the fonts specified in CSS contained a
glyph for a codepoint, we tried only one fallback font. If that font
didn't contain the glyph, we'd give up.

With this change, we try multiple fonts in turn. The font names we try
differ across each platform, and based on the codepoint we're trying to
match. The current implementation is heavily inspired by the analogous
code in Gecko, but I've used to ucd lib to make it more readable,
whereas Gecko matches raw unicode ranges.

This fixes some of the issues reported in servo#17267, although colour emoji
support is not implemented.

== Notes on changes to WPT metadata ==

=== css/css-text/i18n/css3-text-line-break-opclns-* ===

A bunch of these have started failing on macos when they previously
passed.

These tests check that the browser automatically inserts line breaks
near certain characters that are classified as "opening and closing
punctuation". The idea is that if we have e.g. an opening parenthesis,
it does not make sense for it to appear at the end of a line box; it
should "stick" to the next character and go into the next line box.

Before this change, a lot of these codepoints rendered as a missing
glyph on Mac and Linux. In some cases, that meant that the test was
passing.

After this change, a bunch of these codepoints are now rendering glyphs
on Mac (but not Linux). In some cases, the test should continue to pass
where it previously did when rendering with the missing glyph.

However, it seems this has also exposed a layout bug. The "ref" div in
these tests contains a <br> element, and it seems that this, combined
with these punctuation characters, makes the spacing between glyphs ever
so slightly different to the "test" div. (Speculation: might be
something to do with shaping?)

Therefore I've had to mark a bunch of these tests failing on mac.

=== css/css-text/i18n/css3-text-line-break-baspglwj-* ===

Some of these previously passed on Mac due to a missing glyph. Now that
we're rendering the correct glyph, they are failing.

=== css/css-text/word-break/word-break-normal-bo-000.html ===

The characters now render correctly on Mac, and the test is passing. But
we do not find a suitable fallback font on Linux, so it is still failing
on that platform.

=== css/css-text/word-break/word-break-break-all-007.html ===

This was previously passing on Mac, but only because missing character
glyphs were rendered. Now that a fallback font is able to be found, it
(correctly) fails.

=== mozilla/tests/css/font_fallback_* ===

These are new tests added in this commit. 01 and 02 are marked failing
on Linux because the builders don't have the appropriate fonts installed
(that will be a follow-up).

Fix build errors from rebase

FontTemplateDescriptor can no longer just derive(Hash). We need to
implement it on each component part, because the components now
generally wrap floats, which do not impl Hash because of NaN. However in
this case we know that we won't have a NaN, so it is safe to manually
impl Hash.
We can encounter control characters here, for example when processing a
<pre> element which contains newlines. Control characters are inherently
non-printing, therefore if we try to call find_by_codepoint for these
characters we will end up triggering an unnecessary font fallback
search.
FontTemplateData gets passed over IPC during the communication between
FontContext and FontCacheThread. Serializing and deserializing these
bytes is expensive, so this change ensures that we only do that when the
bytes can't be read from disk. A similar strategy is already used on
macos and windows.

The performance problem was particularly noticeable after implenting
font fallback, where the content process would potentially work through
a list of fonts, trying to find one which contains a certain glyph. That
could result in lots of font bytes going over IPC.
Before this change, if we needed to create a Font which we've already
created, but at a new size, then we'd fetch the FontTemplateInfo again.
If the bytes of the font are held in memory, then this could be
expensive as we need to pass those bytes over IPC.
@jonleighton

Copy link
Copy Markdown
Contributor Author

@bors-servo try

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Trying commit 3025a26 with merge 30212d9...

@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-rel-wpt

@jonleighton

Copy link
Copy Markdown
Contributor Author

@bors-servo retry

Reason: seems like a random crash:

{"status": "CRASH", "group": "default", "message": null, "stack": null, "subtest": null, "test": "/html/browsers/history/the-location-interface/location-protocol-setter.html", "line": 96789, "action": "test_result", "expected": "OK"}

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Trying commit 3025a26 with merge 1b56bf5...

@jdm

jdm commented May 19, 2018

Copy link
Copy Markdown
Member

Rather than immediately retrying, it's best to rely on the grid to see what other test results are reported. In particular, retrying doesn't cancel any ongoing test runs so it also delays the rest of the queue.

@jonleighton

Copy link
Copy Markdown
Contributor Author

Ok, noted, I didn't realise that the previous build hadn't completely finished.

@jonleighton

Copy link
Copy Markdown
Contributor Author

Ok, the retry worked! (Maybe it didn't report here as I accidentally closed and reopened the PR...)

Can I get a merge please? 🙏 😄

@emilio

emilio commented May 19, 2018

Copy link
Copy Markdown
Member

Homu is stuck on this PR, just trying to un-stick it.

@bors-servo r-

@emilio

emilio commented May 19, 2018

Copy link
Copy Markdown
Member

@bors-servo r=emilio,mbrubeck

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 3025a26 has been approved by emilio,mbrubeck

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 3025a26 with merge 824f403...

@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - mac-rel-wpt4

@jonleighton

Copy link
Copy Markdown
Contributor Author

o_O we managed to crash rustc:

rustc(40975,0x700000e13000) malloc: *** error for object 0x1: pointer being freed was not allocated

@jdm

jdm commented May 19, 2018

Copy link
Copy Markdown
Member

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 3025a26 with merge 77dcc67...

@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev
Approved by: emilio,mbrubeck
Pushing 77dcc67 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants