remove SendableTextRun & remove @mut Font From TextRun&Shaper(Font),Font.shaper,

fontfamily,fontgroup,render_context.font_ctx
This commit is contained in:
patrick kim 2013-12-05 21:14:58 +09:00
parent c60ab361a5
commit 964b5e9d9a
13 changed files with 146 additions and 113 deletions

View file

@ -18,7 +18,7 @@ use color::Color;
use servo_util::geometry::Au; use servo_util::geometry::Au;
use style::computed_values::border_style; use style::computed_values::border_style;
use render_context::RenderContext; use render_context::RenderContext;
use text::SendableTextRun; use text::TextRun;
use std::cast::transmute_region; use std::cast::transmute_region;
use geom::{Point2D, Rect, Size2D, SideOffsets2D}; use geom::{Point2D, Rect, Size2D, SideOffsets2D};
@ -47,7 +47,7 @@ impl<E> DisplayList<E> {
} }
/// Draws the display list into the given render context. /// Draws the display list into the given render context.
pub fn draw_into_context(&self, render_context: &RenderContext) { pub fn draw_into_context(&self, render_context: &mut RenderContext) {
debug!("Beginning display list."); debug!("Beginning display list.");
for item in self.list.iter() { for item in self.list.iter() {
// FIXME(Issue #150): crashes // FIXME(Issue #150): crashes
@ -87,7 +87,7 @@ pub struct SolidColorDisplayItem<E> {
/// Renders text. /// Renders text.
pub struct TextDisplayItem<E> { pub struct TextDisplayItem<E> {
base: BaseDisplayItem<E>, base: BaseDisplayItem<E>,
text_run: ~SendableTextRun, text_run: ~TextRun,
range: Range, range: Range,
color: Color, color: Color,
} }
@ -120,7 +120,7 @@ pub struct ClipDisplayItem<E> {
impl<E> DisplayItem<E> { impl<E> DisplayItem<E> {
/// Renders this display item into the given render context. /// Renders this display item into the given render context.
fn draw_into_context(&self, render_context: &RenderContext) { fn draw_into_context(&self, render_context: &mut RenderContext) {
match *self { match *self {
SolidColorDisplayItemClass(ref solid_color) => { SolidColorDisplayItemClass(ref solid_color) => {
render_context.draw_solid_color(&solid_color.base.bounds, solid_color.color) render_context.draw_solid_color(&solid_color.base.bounds, solid_color.color)
@ -142,14 +142,13 @@ impl<E> DisplayItem<E> {
debug!("Drawing text at {:?}.", text.base.bounds); debug!("Drawing text at {:?}.", text.base.bounds);
// FIXME(pcwalton): Allocating? Why? // FIXME(pcwalton): Allocating? Why?
let new_run = @text.text_run.deserialize(render_context.font_ctx); let font = render_context.font_ctx.get_font_by_descriptor(&text.text_run.font_descriptor).unwrap();
let font = new_run.font;
let origin = text.base.bounds.origin; let origin = text.base.bounds.origin;
let baseline_origin = Point2D(origin.x, origin.y + font.metrics.ascent); let baseline_origin = Point2D(origin.x, origin.y + font.metrics.ascent);
font.draw_text_into_context(render_context, font.draw_text_into_context(render_context,
new_run, &text.text_run,
&text.range, &text.range,
baseline_origin, baseline_origin,
text.color); text.color);
@ -160,18 +159,18 @@ impl<E> DisplayItem<E> {
let strikeout_size = font.metrics.strikeout_size; let strikeout_size = font.metrics.strikeout_size;
let strikeout_offset = font.metrics.strikeout_offset; let strikeout_offset = font.metrics.strikeout_offset;
if new_run.decoration.underline { if text.text_run.decoration.underline {
let underline_y = baseline_origin.y - underline_offset; let underline_y = baseline_origin.y - underline_offset;
let underline_bounds = Rect(Point2D(baseline_origin.x, underline_y), let underline_bounds = Rect(Point2D(baseline_origin.x, underline_y),
Size2D(width, underline_size)); Size2D(width, underline_size));
render_context.draw_solid_color(&underline_bounds, text.color); render_context.draw_solid_color(&underline_bounds, text.color);
} }
if new_run.decoration.overline { if text.text_run.decoration.overline {
let overline_bounds = Rect(Point2D(baseline_origin.x, origin.y), let overline_bounds = Rect(Point2D(baseline_origin.x, origin.y),
Size2D(width, underline_size)); Size2D(width, underline_size));
render_context.draw_solid_color(&overline_bounds, text.color); render_context.draw_solid_color(&overline_bounds, text.color);
} }
if new_run.decoration.line_through { if text.text_run.decoration.line_through {
let strikeout_y = baseline_origin.y - strikeout_offset; let strikeout_y = baseline_origin.y - strikeout_offset;
let strikeout_bounds = Rect(Point2D(baseline_origin.x, strikeout_y), let strikeout_bounds = Rect(Point2D(baseline_origin.x, strikeout_y),
Size2D(width, strikeout_size)); Size2D(width, strikeout_size));

View file

@ -75,6 +75,7 @@ pub trait FontTableMethods {
fn with_buffer(&self, &fn(*u8, uint)); fn with_buffer(&self, &fn(*u8, uint));
} }
#[deriving(Clone)]
pub struct FontMetrics { pub struct FontMetrics {
underline_size: Au, underline_size: Au,
underline_offset: Au, underline_offset: Au,
@ -234,7 +235,7 @@ and the renderer can use it to render text.
pub struct Font { pub struct Font {
priv handle: FontHandle, priv handle: FontHandle,
priv azure_font: Option<ScaledFont>, priv azure_font: Option<ScaledFont>,
priv shaper: Option<@Shaper>, priv shaper: Option<Shaper>,
style: UsedFontStyle, style: UsedFontStyle,
metrics: FontMetrics, metrics: FontMetrics,
backend: BackendType, backend: BackendType,
@ -243,7 +244,7 @@ pub struct Font {
glyph_advance_cache: HashCache<u32, FractionalPixel>, glyph_advance_cache: HashCache<u32, FractionalPixel>,
} }
impl Font { impl<'self> Font {
pub fn new_from_buffer(ctx: &FontContext, pub fn new_from_buffer(ctx: &FontContext,
buffer: ~[u8], buffer: ~[u8],
style: &SpecifiedFontStyle, style: &SpecifiedFontStyle,
@ -256,7 +257,7 @@ impl Font {
} else { } else {
return Err(handle.unwrap_err()); return Err(handle.unwrap_err());
}; };
let metrics = handle.get_metrics(); let metrics = handle.get_metrics();
// TODO(Issue #179): convert between specified and used font style here? // TODO(Issue #179): convert between specified and used font style here?
@ -304,16 +305,20 @@ impl Font {
return Ok(Font::new_from_adopted_handle(fctx, styled_handle, style, backend, profiler_chan)); return Ok(Font::new_from_adopted_handle(fctx, styled_handle, style, backend, profiler_chan));
} }
fn get_shaper(@mut self) -> @Shaper { fn make_shaper(&'self mut self) -> &'self Shaper {
// fast path: already created a shaper // fast path: already created a shaper
match self.shaper { match self.shaper {
Some(shaper) => { return shaper; }, Some(ref shaper) => {
let s: &'self Shaper = shaper;
return s;
},
None => {} None => {}
} }
let shaper = @Shaper::new(self); let shaper = Shaper::new(self);
self.shaper = Some(shaper); self.shaper = Some(shaper);
shaper let s:&'self Shaper = self.shaper.get_ref();
s
} }
pub fn get_table_for_tag(&self, tag: FontTableTag) -> Option<FontTable> { pub fn get_table_for_tag(&self, tag: FontTableTag) -> Option<FontTable> {
@ -369,7 +374,7 @@ impl Font {
#[fixed_stack_segment] #[fixed_stack_segment]
pub fn draw_text_into_context(&mut self, pub fn draw_text_into_context(&mut self,
rctx: &RenderContext, rctx: &RenderContext,
run: &TextRun, run: &~TextRun,
range: &Range, range: &Range,
baseline_origin: Point2D<Au>, baseline_origin: Point2D<Au>,
color: Color) { color: Color) {
@ -454,11 +459,13 @@ impl Font {
RunMetrics::new(advance, self.metrics.ascent, self.metrics.descent) RunMetrics::new(advance, self.metrics.ascent, self.metrics.descent)
} }
pub fn shape_text(@mut self, text: ~str, is_whitespace: bool) -> Arc<GlyphStore> { pub fn shape_text(&mut self, text: ~str, is_whitespace: bool) -> Arc<GlyphStore> {
let shaper = self.get_shaper();
//FIXME (ksh8281)
self.make_shaper();
do self.shape_cache.find_or_create(&text) |txt| { do self.shape_cache.find_or_create(&text) |txt| {
let mut glyphs = GlyphStore::new(text.char_len(), is_whitespace); let mut glyphs = GlyphStore::new(text.char_len(), is_whitespace);
shaper.shape_text(*txt, &mut glyphs); self.shaper.get_ref().shape_text(*txt, &mut glyphs);
Arc::new(glyphs) Arc::new(glyphs)
} }
} }

View file

@ -130,24 +130,31 @@ impl<'self> FontContext {
let family_name = family.trim(); let family_name = family.trim();
let transformed_family_name = self.transform_family(family_name); let transformed_family_name = self.transform_family(family_name);
debug!("(create font group) transformed family is `{:s}`", transformed_family_name); debug!("(create font group) transformed family is `{:s}`", transformed_family_name);
let mut found = false;
let result = match self.font_list { let result = match self.font_list {
Some(ref fl) => fl.find_font_in_family(transformed_family_name, style), Some(ref mut fl) => {
let font_in_family = fl.find_font_in_family(&transformed_family_name, style);
if font_in_family.is_some() {
let font_entry = font_in_family.unwrap();
let font_id =
SelectorPlatformIdentifier(font_entry.handle.face_identifier());
let font_desc = FontDescriptor::new((*style).clone(), font_id);
Some(font_desc)
} else {
None
}
}
None => None, None => None,
}; };
let mut found = false; if result.is_some() {
for font_entry in result.iter() {
found = true; found = true;
let instance = self.get_font_by_descriptor(&result.unwrap());
let font_id =
SelectorPlatformIdentifier(font_entry.handle.face_identifier());
let font_desc = FontDescriptor::new((*style).clone(), font_id);
let instance = self.get_font_by_descriptor(&font_desc);
for font in instance.iter() { fonts.push(*font); } for font in instance.iter() { fonts.push(*font); }
}; }
if !found { if !found {
debug!("(create font group) didn't find `{:s}`", transformed_family_name); debug!("(create font group) didn't find `{:s}`", transformed_family_name);
@ -156,7 +163,6 @@ impl<'self> FontContext {
if fonts.len() == 0 { if fonts.len() == 0 {
let last_resort = FontList::get_last_resort_font_families(); let last_resort = FontList::get_last_resort_font_families();
for family in last_resort.iter() { for family in last_resort.iter() {
let result = match self.font_list { let result = match self.font_list {
Some(ref fl) => fl.find_font_in_family(*family, style), Some(ref fl) => fl.find_font_in_family(*family, style),
@ -167,7 +173,6 @@ impl<'self> FontContext {
let font_id = let font_id =
SelectorPlatformIdentifier(font_entry.handle.face_identifier()); SelectorPlatformIdentifier(font_entry.handle.face_identifier());
let font_desc = FontDescriptor::new((*style).clone(), font_id); let font_desc = FontDescriptor::new((*style).clone(), font_id);
let instance = self.get_font_by_descriptor(&font_desc); let instance = self.get_font_by_descriptor(&font_desc);
for font in instance.iter() { for font in instance.iter() {
@ -183,7 +188,7 @@ impl<'self> FontContext {
debug!("(create font group) --- finished ---"); debug!("(create font group) --- finished ---");
@FontGroup::new(style.families.to_managed(), &used_style, fonts) FontGroup::new(style.families.to_owned(), &used_style, fonts)
} }
fn create_font_instance(&self, desc: &FontDescriptor) -> Result<@mut Font, ()> { fn create_font_instance(&self, desc: &FontDescriptor) -> Result<@mut Font, ()> {

View file

@ -13,11 +13,11 @@ use servo_util::time::ProfilerChan;
use std::hashmap::HashMap; use std::hashmap::HashMap;
pub type FontFamilyMap = HashMap<~str, @mut FontFamily>; pub type FontFamilyMap = HashMap<~str, FontFamily>;
trait FontListHandleMethods { trait FontListHandleMethods {
fn get_available_families(&self, fctx: &FontContextHandle) -> FontFamilyMap; fn get_available_families(&self, fctx: &FontContextHandle) -> FontFamilyMap;
fn load_variations_for_family(&self, family: @mut FontFamily); fn load_variations_for_family(&self, family: &mut FontFamily);
fn get_last_resort_font_families() -> ~[~str]; fn get_last_resort_font_families() -> ~[~str];
} }
@ -28,7 +28,7 @@ pub struct FontList {
prof_chan: ProfilerChan, prof_chan: ProfilerChan,
} }
impl FontList { impl<'self> FontList {
pub fn new(fctx: &FontContextHandle, pub fn new(fctx: &FontContextHandle,
prof_chan: ProfilerChan) prof_chan: ProfilerChan)
-> FontList { -> FontList {
@ -52,41 +52,46 @@ impl FontList {
} }
} }
pub fn find_font_in_family(&self, pub fn find_font_in_family(&'self mut self,
family_name: &str, family_name: &~str,
style: &SpecifiedFontStyle) -> Option<@FontEntry> { style: &SpecifiedFontStyle) -> Option<&'self FontEntry> {
let family = self.find_family(family_name);
// TODO(Issue #192: handle generic font families, like 'serif' and 'sans-serif'.
// if such family exists, try to match style to a font
let mut result: Option<@FontEntry> = None;
for fam in family.iter() {
result = fam.find_font_for_style(&self.handle, style);
}
let decision = if result.is_some() {
"Found"
} else {
"Couldn't find"
};
debug!("FontList: {:s} font face in family[{:s}] matching style", decision, family_name);
result
}
fn find_family(&self, family_name: &str) -> Option<@mut FontFamily> {
// look up canonical name
let family = self.family_map.find_equiv(&family_name);
let decision = if family.is_some() { "Found" } else { "Couldn't find" };
debug!("FontList: {:s} font family with name={:s}", decision, family_name);
// TODO(Issue #188): look up localized font family names if canonical name not found // TODO(Issue #188): look up localized font family names if canonical name not found
family.map(|f| *f) // look up canonical name
} if self.family_map.contains_key(family_name) {
//FIXME call twice!(ksh8281)
debug!("FontList: {:s} font family with name={:s}", "Found", family_name.to_str());
let s: &'self mut FontFamily = self.family_map.get_mut(family_name);
// TODO(Issue #192: handle generic font families, like 'serif' and 'sans-serif'.
// if such family exists, try to match style to a font
let result = s.find_font_for_style(&mut self.handle, style);
if result.is_some() {
return result;
}
None
}
else {
debug!("FontList: {:s} font family with name={:s}", "Couldn't find", family_name.to_str());
None
}
}
/*
fn find_family(&'self mut self, family_name: &~str) -> Option<&'self mut FontFamily> {
// TODO(Issue #188): look up localized font family names if canonical name not found
// look up canonical name
if self.family_map.contains_key(family_name) {
//FIXME call twice!(ksh8281)
debug!("FontList: {:s} font family with name={:s}", "Found", family_name.to_str());
let s: &'self mut FontFamily = self.family_map.get_mut(family_name);
Some(s)
}
else {
debug!("FontList: {:s} font family with name={:s}", "Couldn't find", family_name.to_str());
None
}
}
*/
pub fn get_last_resort_font_families() -> ~[~str] { pub fn get_last_resort_font_families() -> ~[~str] {
let last_resort = FontListHandle::get_last_resort_font_families(); let last_resort = FontListHandle::get_last_resort_font_families();
last_resort last_resort
@ -94,12 +99,12 @@ impl FontList {
} }
// Holds a specific font family, and the various // Holds a specific font family, and the various
pub struct FontFamily { pub struct FontFamily<'self> {
family_name: ~str, family_name: ~str,
entries: ~[@FontEntry], entries: ~[FontEntry],
} }
impl FontFamily { impl<'self> FontFamily {
pub fn new(family_name: &str) -> FontFamily { pub fn new(family_name: &str) -> FontFamily {
FontFamily { FontFamily {
family_name: family_name.to_str(), family_name: family_name.to_str(),
@ -107,7 +112,7 @@ impl FontFamily {
} }
} }
fn load_family_variations(@mut self, list: &FontListHandle) { fn load_family_variations(&mut self, list: &FontListHandle) {
if self.entries.len() > 0 { if self.entries.len() > 0 {
return return
} }
@ -115,8 +120,8 @@ impl FontFamily {
assert!(self.entries.len() > 0) assert!(self.entries.len() > 0)
} }
pub fn find_font_for_style(@mut self, list: &FontListHandle, style: &SpecifiedFontStyle) pub fn find_font_for_style(&'self mut self, list: &FontListHandle, style: &SpecifiedFontStyle)
-> Option<@FontEntry> { -> Option<&'self FontEntry> {
self.load_family_variations(list); self.load_family_variations(list);
// TODO(Issue #189): optimize lookup for // TODO(Issue #189): optimize lookup for
@ -129,7 +134,7 @@ impl FontFamily {
if (style.weight.is_bold() == entry.is_bold()) && if (style.weight.is_bold() == entry.is_bold()) &&
(style.italic == entry.is_italic()) { (style.italic == entry.is_italic()) {
return Some(*entry); return Some(entry);
} }
} }

View file

@ -53,7 +53,7 @@ impl FontListHandle {
while FcPatternGetString(*font, FC_FAMILY, v, &family) == FcResultMatch { while FcPatternGetString(*font, FC_FAMILY, v, &family) == FcResultMatch {
let family_name = str::raw::from_c_str(family as *c_char); let family_name = str::raw::from_c_str(family as *c_char);
debug!("Creating new FontFamily for family: {:s}", family_name); debug!("Creating new FontFamily for family: {:s}", family_name);
let new_family = @mut FontFamily::new(family_name); let new_family = FontFamily::new(family_name);
family_map.insert(family_name, new_family); family_map.insert(family_name, new_family);
v += 1; v += 1;
} }
@ -64,7 +64,7 @@ impl FontListHandle {
} }
#[fixed_stack_segment] #[fixed_stack_segment]
pub fn load_variations_for_family(&self, family: @mut FontFamily) { pub fn load_variations_for_family(&self, family: &mut FontFamily) {
debug!("getting variations for {:?}", family); debug!("getting variations for {:?}", family);
unsafe { unsafe {
let config = FcConfigGetCurrent(); let config = FcConfigGetCurrent();
@ -120,7 +120,7 @@ impl FontListHandle {
let font_handle = font_handle.unwrap(); let font_handle = font_handle.unwrap();
debug!("Creating new FontEntry for face: {:s}", font_handle.face_name()); debug!("Creating new FontEntry for face: {:s}", font_handle.face_name());
let entry = @FontEntry::new(font_handle); let entry = FontEntry::new(font_handle);
family.entries.push(entry); family.entries.push(entry);
} }

View file

@ -24,7 +24,7 @@ use std::libc::size_t;
pub struct RenderContext<'self> { pub struct RenderContext<'self> {
draw_target: &'self DrawTarget, draw_target: &'self DrawTarget,
font_ctx: @mut FontContext, font_ctx: &'self mut ~FontContext,
opts: &'self Opts, opts: &'self Opts,
/// The rectangle that this context encompasses in page coordinates. /// The rectangle that this context encompasses in page coordinates.
page_rect: Rect<f32>, page_rect: Rect<f32>,

View file

@ -97,7 +97,7 @@ pub struct RenderTask<C,T> {
port: Port<Msg<T>>, port: Port<Msg<T>>,
compositor: C, compositor: C,
constellation_chan: ConstellationChan, constellation_chan: ConstellationChan,
font_ctx: @mut FontContext, font_ctx: ~FontContext,
opts: Opts, opts: Opts,
/// A channel to the profiler. /// A channel to the profiler.
@ -150,7 +150,7 @@ impl<C: RenderListener + Send,T:Send+Freeze> RenderTask<C,T> {
port: port, port: port,
compositor: compositor, compositor: compositor,
constellation_chan: constellation_chan, constellation_chan: constellation_chan,
font_ctx: @mut FontContext::new(opts.render_backend.clone(), font_ctx: ~FontContext::new(opts.render_backend.clone(),
false, false,
profiler_chan.clone()), profiler_chan.clone()),
opts: opts, opts: opts,
@ -266,9 +266,9 @@ impl<C: RenderListener + Send,T:Send+Freeze> RenderTask<C,T> {
{ {
// Build the render context. // Build the render context.
let ctx = RenderContext { let mut ctx = RenderContext {
draw_target: &draw_target, draw_target: &draw_target,
font_ctx: self.font_ctx, font_ctx: &mut self.font_ctx,
opts: &self.opts, opts: &self.opts,
page_rect: tile.page_rect, page_rect: tile.page_rect,
screen_rect: tile.screen_rect, screen_rect: tile.screen_rect,
@ -287,7 +287,7 @@ impl<C: RenderListener + Send,T:Send+Freeze> RenderTask<C,T> {
// Draw the display list. // Draw the display list.
do profile(time::RenderingDrawingCategory, self.profiler_chan.clone()) { do profile(time::RenderingDrawingCategory, self.profiler_chan.clone()) {
render_layer.display_list.get().draw_into_context(&ctx); render_layer.display_list.get().draw_into_context(&mut ctx);
ctx.draw_target.flush(); ctx.draw_target.flush();
} }
} }

View file

@ -9,7 +9,6 @@ Note that you still must define each of the files as a module in
servo.rc. This is not ideal and may be changed in the future. */ servo.rc. This is not ideal and may be changed in the future. */
pub use text::shaping::Shaper; pub use text::shaping::Shaper;
pub use text::text_run::SendableTextRun;
pub use text::text_run::TextRun; pub use text::text_run::TextRun;
pub mod glyph; pub mod glyph;

View file

@ -21,7 +21,7 @@ pub trait ShaperMethods {
// TODO(Issue #163): this is a workaround for static methods and // TODO(Issue #163): this is a workaround for static methods and
// typedefs not working well together. It should be removed. // typedefs not working well together. It should be removed.
pub impl Shaper { pub impl Shaper {
pub fn new(font: @mut Font) -> Shaper { pub fn new(font: &mut Font) -> Shaper {
harfbuzz::shaper::HarfbuzzShaper::new(font) harfbuzz::shaper::HarfbuzzShaper::new(font)
} }
} }

View file

@ -136,7 +136,6 @@ impl ShapedGlyphData {
} }
pub struct Shaper { pub struct Shaper {
font: @mut Font,
priv hb_face: *hb_face_t, priv hb_face: *hb_face_t,
priv hb_font: *hb_font_t, priv hb_font: *hb_font_t,
priv hb_funcs: *hb_font_funcs_t, priv hb_funcs: *hb_font_funcs_t,
@ -161,13 +160,10 @@ impl Drop for Shaper {
impl Shaper { impl Shaper {
#[fixed_stack_segment] #[fixed_stack_segment]
pub fn new(font: @mut Font) -> Shaper { pub fn new(font: &mut Font) -> Shaper {
unsafe { unsafe {
// Indirection for Rust Issue #6248, dynamic freeze scope artifically extended // Indirection for Rust Issue #6248, dynamic freeze scope artifically extended
let font_ptr = { let font_ptr = font as *mut Font;
let borrowed_font= &mut *font;
borrowed_font as *mut Font
};
let hb_face: *hb_face_t = hb_face_create_for_tables(get_font_table_func, let hb_face: *hb_face_t = hb_face_create_for_tables(get_font_table_func,
font_ptr as *c_void, font_ptr as *c_void,
None); None);
@ -190,7 +186,6 @@ impl Shaper {
hb_font_set_funcs(hb_font, hb_funcs, font_ptr as *c_void, None); hb_font_set_funcs(hb_font, hb_funcs, font_ptr as *c_void, None);
Shaper { Shaper {
font: font,
hb_face: hb_face, hb_face: hb_face,
hb_font: hb_font, hb_font: hb_font,
hb_funcs: hb_funcs, hb_funcs: hb_funcs,

View file

@ -7,22 +7,25 @@ use std::vec::VecIterator;
use font_context::FontContext; use font_context::FontContext;
use servo_util::geometry::Au; use servo_util::geometry::Au;
use text::glyph::GlyphStore; use text::glyph::GlyphStore;
use font::{Font, FontDescriptor, RunMetrics}; use font::{Font, FontDescriptor, RunMetrics, FontStyle, FontMetrics};
use servo_util::range::Range; use servo_util::range::Range;
use extra::arc::Arc; use extra::arc::Arc;
use style::computed_values::text_decoration; use style::computed_values::text_decoration;
/// A text run. /// A text run.
#[deriving(Clone)]
pub struct TextRun { pub struct TextRun {
text: Arc<~str>, text: Arc<~str>,
font: @mut Font, font_descriptor: FontDescriptor,
font_metrics: FontMetrics,
font_style: FontStyle,
decoration: text_decoration::T, decoration: text_decoration::T,
glyphs: Arc<~[Arc<GlyphStore>]>, glyphs: Arc<~[Arc<GlyphStore>]>,
} }
/// The same as a text run, but with a font descriptor instead of a font. This makes them thread /// The same as a text run, but with a font descriptor instead of a font. This makes them thread
/// safe. /// safe.
pub struct SendableTextRun { /*pub struct SendableTextRun {
text: Arc<~str>, text: Arc<~str>,
font: FontDescriptor, font: FontDescriptor,
decoration: text_decoration::T, decoration: text_decoration::T,
@ -44,7 +47,7 @@ impl SendableTextRun {
} }
} }
} }
*/
pub struct SliceIterator<'self> { pub struct SliceIterator<'self> {
priv glyph_iter: VecIterator<'self, Arc<GlyphStore>>, priv glyph_iter: VecIterator<'self, Arc<GlyphStore>>,
priv range: Range, priv range: Range,
@ -120,12 +123,14 @@ impl<'self> Iterator<Range> for LineIterator<'self> {
} }
impl<'self> TextRun { impl<'self> TextRun {
pub fn new(font: @mut Font, text: ~str, decoration: text_decoration::T) -> TextRun { pub fn new(font: &mut Font, text: ~str, decoration: text_decoration::T) -> TextRun {
let glyphs = TextRun::break_and_shape(font, text); let glyphs = TextRun::break_and_shape(font, text);
let run = TextRun { let run = TextRun {
text: Arc::new(text), text: Arc::new(text),
font: font, font_style: font.style.clone(),
font_metrics: font.metrics.clone(),
font_descriptor: font.get_descriptor(),
decoration: decoration, decoration: decoration,
glyphs: Arc::new(glyphs), glyphs: Arc::new(glyphs),
}; };
@ -133,10 +138,9 @@ impl<'self> TextRun {
} }
pub fn teardown(&self) { pub fn teardown(&self) {
self.font.teardown();
} }
pub fn break_and_shape(font: @mut Font, text: &str) -> ~[Arc<GlyphStore>] { pub fn break_and_shape(font: &mut Font, text: &str) -> ~[Arc<GlyphStore>] {
// TODO(Issue #230): do a better job. See Gecko's LineBreaker. // TODO(Issue #230): do a better job. See Gecko's LineBreaker.
let mut glyphs = ~[]; let mut glyphs = ~[];
@ -190,7 +194,7 @@ impl<'self> TextRun {
glyphs glyphs
} }
/*
pub fn serialize(&self) -> SendableTextRun { pub fn serialize(&self) -> SendableTextRun {
SendableTextRun { SendableTextRun {
text: self.text.clone(), text: self.text.clone(),
@ -199,7 +203,7 @@ impl<'self> TextRun {
glyphs: self.glyphs.clone(), glyphs: self.glyphs.clone(),
} }
} }
*/
pub fn char_len(&self) -> uint { pub fn char_len(&self) -> uint {
do self.glyphs.get().iter().fold(0u) |len, slice_glyphs| { do self.glyphs.get().iter().fold(0u) |len, slice_glyphs| {
len + slice_glyphs.get().char_len() len + slice_glyphs.get().char_len()
@ -218,19 +222,30 @@ impl<'self> TextRun {
} }
pub fn metrics_for_range(&self, range: &Range) -> RunMetrics { pub fn metrics_for_range(&self, range: &Range) -> RunMetrics {
self.font.measure_text(self, range) // TODO(Issue #199): alter advance direction for RTL
// TODO(Issue #98): using inter-char and inter-word spacing settings when measuring text
let mut advance = Au(0);
for (glyphs, _offset, slice_range) in self.iter_slices_for_range(range) {
for (_i, glyph) in glyphs.iter_glyphs_for_char_range(&slice_range) {
advance = advance + glyph.advance();
}
}
RunMetrics::new(advance, self.font_metrics.ascent, self.font_metrics.descent)
} }
pub fn metrics_for_slice(&self, glyphs: &GlyphStore, slice_range: &Range) -> RunMetrics { pub fn metrics_for_slice(&self, glyphs: &GlyphStore, slice_range: &Range) -> RunMetrics {
self.font.measure_text_for_slice(glyphs, slice_range) let mut advance = Au(0);
for (_i, glyph) in glyphs.iter_glyphs_for_char_range(slice_range) {
advance = advance + glyph.advance();
}
RunMetrics::new(advance, self.font_metrics.ascent, self.font_metrics.descent)
} }
pub fn min_width_for_range(&self, range: &Range) -> Au { pub fn min_width_for_range(&self, range: &Range) -> Au {
let mut max_piece_width = Au(0); let mut max_piece_width = Au(0);
debug!("iterating outer range {:?}", range); debug!("iterating outer range {:?}", range);
for (glyphs, offset, slice_range) in self.iter_slices_for_range(range) { for (glyphs, offset, slice_range) in self.iter_slices_for_range(range) {
debug!("iterated on {:?}[{:?}]", offset, slice_range); debug!("iterated on {:?}[{:?}]", offset, slice_range);
let metrics = self.font.measure_text_for_slice(glyphs, &slice_range); let metrics = self.metrics_for_range(&slice_range);
max_piece_width = Au::max(max_piece_width, metrics.advance_width); max_piece_width = Au::max(max_piece_width, metrics.advance_width);
} }
max_piece_width max_piece_width

View file

@ -630,13 +630,21 @@ impl Box {
// Create the text box. // Create the text box.
do list.with_mut_ref |list| { do list.with_mut_ref |list| {
// FIXME(pcwalton): Allocation? Why?!
let run = ~TextRun {
text: text_box.run.text.clone(),
font_descriptor: text_box.run.font_descriptor.clone(),
font_metrics: text_box.run.font_metrics.clone(),
font_style: text_box.run.font_style.clone(),
decoration: text_box.run.decoration.clone(),
glyphs: text_box.run.glyphs.clone()
};
let text_display_item = ~TextDisplayItem { let text_display_item = ~TextDisplayItem {
base: BaseDisplayItem { base: BaseDisplayItem {
bounds: absolute_box_bounds, bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(&self), extra: ExtraDisplayListData::new(&self),
}, },
// FIXME(pcwalton): Allocation? Why?! text_run: run,
text_run: ~text_box.run.serialize(),
range: text_box.range, range: text_box.range,
color: color, color: color,
}; };
@ -947,7 +955,7 @@ impl Box {
self.position.mutate().ptr.size.width = image_width self.position.mutate().ptr.size.width = image_width
} }
ScannedTextBox(_) => { ScannedTextBox(_) => {
// Scanned text boxes will have already had their widths assigned by this point.
} }
UnscannedTextBox(_) => fail!("Unscanned text boxes should have been scanned by now!"), UnscannedTextBox(_) => fail!("Unscanned text boxes should have been scanned by now!"),
} }

View file

@ -711,7 +711,7 @@ impl Flow for InlineFlow {
// Find the top and bottom of the content area. // Find the top and bottom of the content area.
// Those are used in text-top and text-bottom value of 'vertical-align' // Those are used in text-top and text-bottom value of 'vertical-align'
let text_ascent = text_box.run.font.metrics.ascent; let text_ascent = text_box.run.font_metrics.ascent;
// Offset from the top of the box is 1/2 of the leading + ascent // Offset from the top of the box is 1/2 of the leading + ascent
let text_offset = text_ascent + (line_height - em_size).scale_by(0.5); let text_offset = text_ascent + (line_height - em_size).scale_by(0.5);