Skip to content

Lazy load fonts in a FontGroup - #20021

Merged
bors-servo merged 2 commits into
servo:masterfrom
jonleighton:lazy-font-group
Feb 22, 2018
Merged

bors-servo merged 2 commits into
servo:masterfrom
jonleighton:lazy-font-group

Conversation

@jonleighton

@jonleighton jonleighton commented Feb 11, 2018 •

Copy link
Copy Markdown
Contributor

The first commit message explains this so I'll just copy it here:


This is a step towards fixing #17267. To fix that, we need to be able to
try various different fallback fonts in turn, which would become
unweildy with the prior eager-loading strategy.

Prior to this change, FontGroup loaded up all Font instances, including
the fallback font, before any of them were checked for the presence of
the glyphs we're trying to render.

So for the following CSS:

font-family: Helvetica, Arial;

The FontGroup would contain a Font instance for Helvetica, and a Font
instance for Arial, and a Font instance for the fallback font.

It may be that Helvetica contains glyphs for every character in the
document, and therefore Arial and the fallback font are not needed at
all.

This change makes the strategy lazy, so that we'll only create a Font
for Arial if we cannot find a glyph within Helvetica. I've also
substantially refactored the existing code in the process and added
some documentation along the way.


I've added some tests in the second commit, but it required quite a bit of gymnastics to make it possible to write such a test. I'm not sure if the added complexity to the production code is worth it?

On the other hand, having this infrastructure in place may be useful for testing future changes in this area, and also possibly brings us a step closer to extracting a library as discussed in #4901. (What I mean by that is: it reduces coupling between FontCacheThread and FontContext -- the latter would have a place in such a library, the former wouldn't.)


This change is Reviewable

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @emilio: components/layout/text.rs, components/layout/inline.rs, components/layout/context.rs

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Feb 11, 2018
@emilio

emilio commented Feb 12, 2018

Copy link
Copy Markdown
Member

r? @glennw

@glennw

glennw commented Feb 13, 2018

Copy link
Copy Markdown
Member

Reviewed 7 of 10 files at r1, 2 of 7 files at r2.
Review status: 9 of 14 files reviewed at latest revision, 1 unresolved discussion.


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

impl PartialEq for FontHandle {
    fn eq(&self, other: &FontHandle) -> bool {

I'm always slightly uneasy about implementing PartialEq, since it's easy to add fields to the struct at a later date, and not realize that they may need to be included in the PartialEq implementation. Perhaps we could add a comment at the struct definition warning people to update the PartialEq implementation if fields are added to it?


Comments from Reviewable

@glennw

glennw commented Feb 13, 2018

Copy link
Copy Markdown
Member

Partial review only so far - I'll work through the rest of this today.

@glennw

glennw commented Feb 13, 2018

Copy link
Copy Markdown
Member

Reviewed 2 of 7 files at r2.
Review status: 11 of 14 files reviewed at latest revision, 3 unresolved discussions.


components/layout/text.rs, line 139 at r2 (raw file):

    /// be adjusted.
    fn flush_clump_to_list(&mut self,
                           mut font_context: &mut LayoutFontContext,

Why does this need to be mut when it is a mutable reference?


components/layout/text.rs, line 326 at r2 (raw file):

                // If no font is found (including fallbacks), there's no way we can render.
                let font = run_info.font.expect("No font found for text run!");

Do we ever expect a situation where this could be hit? Should this be a panic or just skip drawing and log an error / warning?


Comments from Reviewable

@glennw

glennw commented Feb 14, 2018

Copy link
Copy Markdown
Member

Reviewed 3 of 7 files at r2.
Review status: all files reviewed at latest revision, 4 unresolved discussions.


components/gfx/tests/font_context.rs, line 58 at r2 (raw file):

    fn add_face(family: &mut FontTemplates, name: &str) {
        let mut path: PathBuf = [

Is it a good idea to rely on files existing in WPT? I'm not sure - jdm will know whether that's fine.


Comments from Reviewable

@glennw

glennw commented Feb 14, 2018

Copy link
Copy Markdown
Member

This generally looks OK to me (minor comments from reviewable above). However, I haven't delved into the servo layout or text code for a long time now, so it'd be good for someone else to take a look at this and see if there are any obvious issues - perhaps @pcwalton or @mbrubeck ?

@jonleighton

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

I'm always slightly uneasy about implementing PartialEq, since it's easy to add fields to the struct at a later date, and not realize that they may need to be included in the PartialEq implementation. Perhaps we could add a comment at the struct definition warning people to update the PartialEq implementation if fields are added to it?

I think that's a sensible concern. I did something different than you suggested though: I added methods so that we can get at the underlying font identifier (an Atom), since that's what we're really trying to compare. WDYT?

Why does this need to be mut when it is a mutable reference?

Without it I get this error:

error[E0596]: cannot borrow immutable argument `font_context` as mutable
   --> components/layout/text.rs:218:79
    |
140 |                            font_context: &mut FontContext,
    |                            ------------ consider changing this to `mut font_context`
...
218 |                     let font = font_group.borrow_mut().find_by_codepoint(&mut font_context, character);
    |                                                                               ^^^^^^^^^^^^ cannot borrow mutably

I believe it is because this method passes the &mut font_context to more than one callee.

Do we ever expect a situation where this could be hit? Should this be a panic or just skip drawing and log an error / warning?

The current code already panics in the same situation which is why I haven't handled this different. We'd basically only hit this if we cannot find any font (including fallbacks) for the text run, even one which doesn't contain a glyph for the character we're trying to render. So I think it's a pretty rare case and would probably require a rather strange underlying system.

Is it a good idea to rely on files existing in WPT? I'm not sure - jdm will know whether that's fine.

I can't see those files changing, but I agree this does feel a bit weird. We could alternatively just copy those files into components/gfx/tests/support. Do you have any thoughts @jdm? If the files did ever get (re)moved, the test would start to fail, so it's not like we wouldn't notice.

@jdm

jdm commented Feb 14, 2018

Copy link
Copy Markdown
Member

Given that the WPT files could be renamed, moved, removed, etc. as part of an automated sync process that I'm trying to keep as hands-off as possible, I would prefer to duplicate any files that are relied upon by our unit tests, rather than hardcoding paths to files we don't completely control.

@glennw

glennw commented Feb 18, 2018

Copy link
Copy Markdown
Member

@mbrubeck or @pcwalton are you able to take a quick look over this too?

@jdm jdm assigned mbrubeck and unassigned glennw Feb 20, 2018

@mbrubeck mbrubeck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code changes look good to me. The test infrastructure looks fine too; it's not very heavyweight and the decoupling is very nice!

@jonleighton

Copy link
Copy Markdown
Contributor Author

@glennw @mbrubeck great, thanks. could one of you give it the "r+" please?

@jdm

jdm commented Feb 22, 2018

Copy link
Copy Markdown
Member

@bors-servo r=mbrubeck,glennw

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 31bacab has been approved by mbrubeck,glennw

@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 Feb 22, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 31bacab with merge 93bf259...

bors-servo pushed a commit that referenced this pull request Feb 22, 2018
 Lazy load fonts in a FontGroup

The first commit message explains this so I'll just copy it here:

---

This is a step towards fixing #17267. To fix that, we need to be able to
try various different fallback fonts in turn, which would become
unweildy with the prior eager-loading strategy.

Prior to this change, FontGroup loaded up all Font instances, including
the fallback font, before any of them were checked for the presence of
the glyphs we're trying to render.

So for the following CSS:

    font-family: Helvetica, Arial;

The FontGroup would contain a Font instance for Helvetica, and a Font
instance for Arial, and a Font instance for the fallback font.

It may be that Helvetica contains glyphs for every character in the
document, and therefore Arial and the fallback font are not needed at
all.

This change makes the strategy lazy, so that we'll only create a Font
for Arial if we cannot find a glyph within Helvetica. I've also
substantially refactored the existing code in the process and added
some documentation along the way.

---

I've added some tests in the second commit, but it required quite a bit of gymnastics to make it possible to write such a test. I'm not sure if the added complexity to the production code is worth it?

On the other hand, having this infrastructure in place may be useful for testing future changes in this area, and also possibly brings us a step closer to extracting a library as discussed in #4901. (What I mean by that is: it reduces coupling between `FontCacheThread` and `FontContext` -- the latter would have a place in such a library, the former wouldn't.)

<!-- 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/20021)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - mac-rel-wpt3

@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 Feb 22, 2018
@jdm

jdm commented Feb 22, 2018

Copy link
Copy Markdown
Member

Tests failed on all platforms.

This is a step towards fixing servo#17267. To fix that, we need to be able to
try various different fallback fonts in turn, which would become
unweildy with the prior eager-loading strategy.

Prior to this change, FontGroup loaded up all Font instances, including
the fallback font, before any of them were checked for the presence of
the glyphs we're trying to render.

So for the following CSS:

    font-family: Helvetica, Arial;

The FontGroup would contain a Font instance for Helvetica, and a Font
instance for Arial, and a Font instance for the fallback font.

It may be that Helvetica contains glyphs for every character in the
document, and therefore Arial and the fallback font are not needed at
all.

This change makes the strategy lazy, so that we'll only create a Font
for Arial if we cannot find a glyph within Helvetica. I've also
substantially refactored the existing code in the process and added
some documentation along the way.
Unfortunately, this required quite a bit of changes to the non-test
code. That's because FontContext depends on a FontCacheThread, which in
turn depends on a CoreResourceThread and therefore lots of other data
structures.

It seemed like it would be very difficult to instantiate a FontContext
as it was, and even if we could it seems like overkill to have all these
data structures present for a relatively focused test.

Therefore, I created a FontSource trait which represents the interface
which FontContext uses to talk to FontCacheThread. FontCacheThread then
implements FontSource. Then, in the test, we can create a dummy
implementation of FontSource rather than using FontCacheThread.

This actually has the advantage that we can make our dummy
implementation behave in certain specific way which are useful for
testing, for example it can count the number of times
find_font_template() is called, which helps us verify that
caching/lazy-loading is working as intended.
@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 Feb 22, 2018
@jonleighton

Copy link
Copy Markdown
Contributor Author

@jdm should be fixed now

@jdm

jdm commented Feb 22, 2018

Copy link
Copy Markdown
Member

@bors-servo r=mbrubeck,glennw

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit e4acb3f has been approved by mbrubeck,glennw

@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 Feb 22, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit e4acb3f with merge f48dce1...

bors-servo pushed a commit that referenced this pull request Feb 22, 2018
 Lazy load fonts in a FontGroup

The first commit message explains this so I'll just copy it here:

---

This is a step towards fixing #17267. To fix that, we need to be able to
try various different fallback fonts in turn, which would become
unweildy with the prior eager-loading strategy.

Prior to this change, FontGroup loaded up all Font instances, including
the fallback font, before any of them were checked for the presence of
the glyphs we're trying to render.

So for the following CSS:

    font-family: Helvetica, Arial;

The FontGroup would contain a Font instance for Helvetica, and a Font
instance for Arial, and a Font instance for the fallback font.

It may be that Helvetica contains glyphs for every character in the
document, and therefore Arial and the fallback font are not needed at
all.

This change makes the strategy lazy, so that we'll only create a Font
for Arial if we cannot find a glyph within Helvetica. I've also
substantially refactored the existing code in the process and added
some documentation along the way.

---

I've added some tests in the second commit, but it required quite a bit of gymnastics to make it possible to write such a test. I'm not sure if the added complexity to the production code is worth it?

On the other hand, having this infrastructure in place may be useful for testing future changes in this area, and also possibly brings us a step closer to extracting a library as discussed in #4901. (What I mean by that is: it reduces coupling between `FontCacheThread` and `FontContext` -- the latter would have a place in such a library, the former wouldn't.)

<!-- 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/20021)
<!-- Reviewable:end -->
@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: mbrubeck,glennw
Pushing f48dce1 to master...

@bors-servo
bors-servo merged commit e4acb3f into servo:master Feb 22, 2018
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 22, 2018
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.

8 participants