Last active
December 17, 2015 22:19
-
-
Save jdm/5680884 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/components/servo-gfx/font.rs b/src/components/servo-gfx/font.rs | |
index 6405c0f..37a712d 100644 | |
--- a/src/components/servo-gfx/font.rs | |
+++ b/src/components/servo-gfx/font.rs | |
@@ -204,6 +204,7 @@ A font instance. Layout can use this to calculate glyph metrics | |
and the renderer can use it to render text. | |
*/ | |
pub struct Font { | |
+ priv context: *mut FontContext, | |
priv handle: FontHandle, | |
priv azure_font: Option<ScaledFont>, | |
priv shaper: Option<@Shaper>, | |
@@ -213,7 +214,7 @@ pub struct Font { | |
} | |
pub impl Font { | |
- fn new_from_buffer(ctx: &FontContext, | |
+ fn new_from_buffer(ctx: &mut FontContext, | |
buffer: ~[u8], | |
style: &SpecifiedFontStyle, | |
backend: BackendType) | |
@@ -229,6 +230,7 @@ pub impl Font { | |
// TODO(Issue #179): convert between specified and used font style here? | |
return Ok(@mut Font { | |
+ context: ptr::to_mut_unsafe_ptr(ctx), | |
handle: handle, | |
azure_font: None, | |
shaper: None, | |
@@ -238,11 +240,12 @@ pub impl Font { | |
}); | |
} | |
- fn new_from_adopted_handle(_fctx: &FontContext, handle: FontHandle, | |
+ fn new_from_adopted_handle(_fctx: &mut FontContext, handle: FontHandle, | |
style: &SpecifiedFontStyle, backend: BackendType) -> @mut Font { | |
let metrics = handle.get_metrics(); | |
@mut Font { | |
+ context: ptr::to_mut_unsafe_ptr(_fctx), | |
handle: handle, | |
azure_font: None, | |
shaper: None, | |
@@ -252,7 +255,7 @@ pub impl Font { | |
} | |
} | |
- fn new_from_existing_handle(fctx: &FontContext, handle: &FontHandle, | |
+ fn new_from_existing_handle(fctx: &mut FontContext, handle: &FontHandle, | |
style: &SpecifiedFontStyle, backend: BackendType) -> Result<@mut Font,()> { | |
// TODO(Issue #179): convert between specified and used font style here? | |
@@ -271,7 +274,9 @@ pub impl Font { | |
None => {} | |
} | |
- let shaper = @Shaper::new(self); | |
+ let context = unsafe { &mut *self.context }; | |
+ let shaper = context.get_shaper_for_font(self); | |
+ //let shaper = @Shaper::new(self); | |
self.shaper = Some(shaper); | |
shaper | |
} | |
diff --git a/src/components/servo-gfx/font_context.rs b/src/components/servo-gfx/font_context.rs | |
index 5a4a57b..87c1b63 100644 | |
--- a/src/components/servo-gfx/font_context.rs | |
+++ b/src/components/servo-gfx/font_context.rs | |
@@ -8,6 +8,7 @@ use font_list::FontList; | |
use servo_util::cache::Cache; | |
use servo_util::cache::MonoCache; | |
use servo_util::time::ProfilerChan; | |
+use text::Shaper; | |
use platform::font::FontHandle; | |
use platform::font_context::FontContextHandle; | |
@@ -36,6 +37,7 @@ pub trait FontContextHandleMethods { | |
#[allow(non_implicitly_copyable_typarams)] | |
pub struct FontContext { | |
instance_cache: MonoCache<FontDescriptor, @mut Font>, | |
+ shaper_cache: MonoCache<FontDescriptor, @Shaper>, | |
font_list: Option<FontList>, // only needed by layout | |
handle: FontContextHandle, | |
backend: BackendType, | |
@@ -65,6 +67,8 @@ pub impl<'self> FontContext { | |
// TODO(Rust #3902): remove extraneous type parameters once they are inferred correctly. | |
instance_cache: | |
Cache::new::<FontDescriptor,@mut Font,MonoCache<FontDescriptor,@mut Font>>(10), | |
+ shaper_cache: | |
+ Cache::new::<FontDescriptor,@Shaper,MonoCache<FontDescriptor,@Shaper>>(10), | |
font_list: font_list, | |
handle: handle, | |
backend: backend, | |
@@ -72,6 +76,18 @@ pub impl<'self> FontContext { | |
} | |
} | |
+ fn get_shaper_for_font(&mut self, font: @mut Font) -> @Shaper { | |
+ let desc = font.get_descriptor(); | |
+ match self.shaper_cache.find(&desc) { | |
+ Some(s) => s, | |
+ None => { | |
+ let shaper = @Shaper::new(font); | |
+ self.shaper_cache.insert(&desc, shaper); | |
+ shaper | |
+ } | |
+ } | |
+ } | |
+ | |
priv fn get_font_list(&'self self) -> &'self FontList { | |
self.font_list.get_ref() | |
} | |
@@ -118,16 +134,23 @@ pub impl<'self> FontContext { | |
let transformed_family_name = self.transform_family(family_name); | |
debug!("(create font group) transformed family is `%s`", transformed_family_name); | |
+ let mut entry = None; | |
+ let mut found = false; | |
+ { | |
let list = self.get_font_list(); | |
let result = list.find_font_in_family(transformed_family_name, style); | |
- let mut found = false; | |
for result.each |font_entry| { | |
found = true; | |
+ entry = Some(*font_entry); | |
// TODO(Issue #203): route this instantion through FontContext's Font instance cache. | |
+ } | |
+ } | |
+ | |
+ for entry.each |font_entry| { | |
let instance = Font::new_from_existing_handle(self, &font_entry.handle, style, self.backend); | |
do result::iter(&instance) |font: &@mut Font| { fonts.push(*font); } | |
- }; | |
+ } | |
if !found { | |
debug!("(create font group) didn't find `%s`", transformed_family_name); | |
@@ -143,7 +166,7 @@ pub impl<'self> FontContext { | |
@FontGroup::new(style.families.to_managed(), &used_style, fonts) | |
} | |
- priv fn create_font_instance(&self, desc: &FontDescriptor) -> Result<@mut Font, ()> { | |
+ priv fn create_font_instance(&mut self, desc: &FontDescriptor) -> Result<@mut Font, ()> { | |
return match &desc.selector { | |
// TODO(Issue #174): implement by-platform-name font selectors. | |
&SelectorPlatformIdentifier(ref identifier) => { | |
diff --git a/src/test/test_hammer_layout.js b/src/test/test_hammer_layout.js | |
index 8312d8a..8872b15 100644 | |
--- a/src/test/test_hammer_layout.js | |
+++ b/src/test/test_hammer_layout.js | |
@@ -1,7 +1,7 @@ | |
var divs = document.getElementsByTagName("div"); | |
var div = divs[0]; | |
-var count = 1000000; | |
+var count = 10000; | |
var start = new Date(); | |
for (var i = 0; i < count; i++) { | |
div.setAttribute('id', 'styled'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment