Repository navigation
Lazy load fonts in a FontGroup - #20021
Conversation
|
Heads up! This PR modifies the following files:
|
|
r? @glennw |
debd8bf to
c69c3ca
Compare
|
Reviewed 7 of 10 files at r1, 2 of 7 files at r2. components/gfx/platform/freetype/font.rs, line 77 at r1 (raw file):
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 |
|
Partial review only so far - I'll work through the rest of this today. |
|
Reviewed 2 of 7 files at r2. components/layout/text.rs, line 139 at r2 (raw file):
Why does this need to be mut when it is a mutable reference? components/layout/text.rs, line 326 at r2 (raw file):
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 |
|
Reviewed 3 of 7 files at r2. components/gfx/tests/font_context.rs, line 58 at r2 (raw file):
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 |
c69c3ca to
af31d70
Compare
|
Thanks for the review!
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
Without it I get this error: I believe it is because this method passes the
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.
I can't see those files changing, but I agree this does feel a bit weird. We could alternatively just copy those files into |
|
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. |
af31d70 to
31bacab
Compare
mbrubeck
left a comment
There was a problem hiding this comment.
The code changes look good to me. The test infrastructure looks fine too; it's not very heavyweight and the decoupling is very nice!
|
@bors-servo r=mbrubeck,glennw |
|
📌 Commit 31bacab has been approved by |
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 -->
|
💔 Test failed - mac-rel-wpt3 |
|
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.
31bacab to
e4acb3f
Compare
|
@jdm should be fixed now |
|
@bors-servo r=mbrubeck,glennw |
|
📌 Commit e4acb3f has been approved by |
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 -->
|
☀️ 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 |
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:
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
FontCacheThreadandFontContext-- the latter would have a place in such a library, the former wouldn't.)This change is