Skip to content

Commit

Permalink
Merge pull request servo#1342 from ksh8281/remove_@_from_font_context
Browse files Browse the repository at this point in the history
Remove @ from font context, remove SendableTextRun
  • Loading branch information
larsbergstrom committed Dec 7, 2013
2 parents c5e5e5a + 86e2cac commit 403958a
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 214 deletions.
44 changes: 23 additions & 21 deletions src/components/gfx/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use color::Color;
use servo_util::geometry::Au;
use style::computed_values::border_style;
use render_context::RenderContext;
use text::SendableTextRun;
use text::TextRun;

use std::cast::transmute_region;
use geom::{Point2D, Rect, Size2D, SideOffsets2D};
Expand Down Expand Up @@ -47,7 +47,7 @@ impl<E> DisplayList<E> {
}

/// 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.");
for item in self.list.iter() {
// FIXME(Issue #150): crashes
Expand Down Expand Up @@ -87,7 +87,7 @@ pub struct SolidColorDisplayItem<E> {
/// Renders text.
pub struct TextDisplayItem<E> {
base: BaseDisplayItem<E>,
text_run: ~SendableTextRun,
text_run: ~TextRun,
range: Range,
color: Color,
}
Expand Down Expand Up @@ -120,7 +120,7 @@ pub struct ClipDisplayItem<E> {

impl<E> DisplayItem<E> {
/// 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 {
SolidColorDisplayItemClass(ref solid_color) => {
render_context.draw_solid_color(&solid_color.base.bounds, solid_color.color)
Expand All @@ -142,36 +142,38 @@ impl<E> DisplayItem<E> {
debug!("Drawing text at {:?}.", text.base.bounds);

// 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 font_metrics = font.with_borrow( |font| {
font.metrics.clone()
});
let origin = text.base.bounds.origin;
let baseline_origin = Point2D(origin.x, origin.y + font.metrics.ascent);

font.draw_text_into_context(render_context,
new_run,
&text.range,
baseline_origin,
text.color);

let baseline_origin = Point2D(origin.x, origin.y + font_metrics.ascent);
font.with_mut_borrow( |font| {
font.draw_text_into_context(render_context,
&text.text_run,
&text.range,
baseline_origin,
text.color);
});
let width = text.base.bounds.size.width;
let underline_size = font.metrics.underline_size;
let underline_offset = font.metrics.underline_offset;
let strikeout_size = font.metrics.strikeout_size;
let strikeout_offset = font.metrics.strikeout_offset;
let underline_size = font_metrics.underline_size;
let underline_offset = font_metrics.underline_offset;
let strikeout_size = font_metrics.strikeout_size;
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_bounds = Rect(Point2D(baseline_origin.x, underline_y),
Size2D(width, underline_size));
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),
Size2D(width, underline_size));
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_bounds = Rect(Point2D(baseline_origin.x, strikeout_y),
Size2D(width, strikeout_size));
Expand Down
53 changes: 31 additions & 22 deletions src/components/gfx/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::cast;
use std::ptr;
use std::str;
use std::vec;
use std::rc::RcMut;
use servo_util::cache::{Cache, HashCache};
use servo_util::range::Range;
use servo_util::time::ProfilerChan;
Expand Down Expand Up @@ -75,6 +76,7 @@ pub trait FontTableMethods {
fn with_buffer(&self, &fn(*u8, uint));
}

#[deriving(Clone)]
pub struct FontMetrics {
underline_size: Au,
underline_offset: Au,
Expand Down Expand Up @@ -171,15 +173,15 @@ pub enum FontSelector {
// The ordering of font instances is mainly decided by the CSS
// 'font-family' property. The last font is a system fallback font.
pub struct FontGroup {
families: @str,
families: ~str,
// style of the first western font in group, which is
// used for purposes of calculating text run metrics.
style: UsedFontStyle,
fonts: ~[@mut Font],
fonts: ~[RcMut<Font>]
}

impl FontGroup {
pub fn new(families: @str, style: &UsedFontStyle, fonts: ~[@mut Font]) -> FontGroup {
pub fn new(families: ~str, style: &UsedFontStyle, fonts: ~[RcMut<Font>]) -> FontGroup {
FontGroup {
families: families,
style: (*style).clone(),
Expand All @@ -195,7 +197,9 @@ impl FontGroup {
assert!(self.fonts.len() > 0);

// TODO(Issue #177): Actually fall back through the FontGroup when a font is unsuitable.
return TextRun::new(self.fonts[0], text, decoration);
self.fonts[0].with_mut_borrow(|font| {
TextRun::new(font, text.clone(), decoration)
})
}
}

Expand Down Expand Up @@ -234,7 +238,7 @@ and the renderer can use it to render text.
pub struct Font {
priv handle: FontHandle,
priv azure_font: Option<ScaledFont>,
priv shaper: Option<@Shaper>,
priv shaper: Option<Shaper>,
style: UsedFontStyle,
metrics: FontMetrics,
backend: BackendType,
Expand All @@ -243,24 +247,24 @@ pub struct Font {
glyph_advance_cache: HashCache<u32, FractionalPixel>,
}

impl Font {
impl<'self> Font {
pub fn new_from_buffer(ctx: &FontContext,
buffer: ~[u8],
style: &SpecifiedFontStyle,
backend: BackendType,
profiler_chan: ProfilerChan)
-> Result<@mut Font, ()> {
-> Result<RcMut<Font>, ()> {
let handle = FontHandleMethods::new_from_buffer(&ctx.handle, buffer, style);
let handle: FontHandle = if handle.is_ok() {
handle.unwrap()
} else {
return Err(handle.unwrap_err());
};

let metrics = handle.get_metrics();
// TODO(Issue #179): convert between specified and used font style here?

return Ok(@mut Font {
return Ok(RcMut::new(Font {
handle: handle,
azure_font: None,
shaper: None,
Expand All @@ -270,15 +274,15 @@ impl Font {
profiler_chan: profiler_chan,
shape_cache: HashCache::new(),
glyph_advance_cache: HashCache::new(),
});
}));
}

pub fn new_from_adopted_handle(_fctx: &FontContext, handle: FontHandle,
style: &SpecifiedFontStyle, backend: BackendType,
profiler_chan: ProfilerChan) -> @mut Font {
profiler_chan: ProfilerChan) -> Font {
let metrics = handle.get_metrics();

@mut Font {
Font {
handle: handle,
azure_font: None,
shaper: None,
Expand All @@ -293,27 +297,30 @@ impl Font {

pub fn new_from_existing_handle(fctx: &FontContext, handle: &FontHandle,
style: &SpecifiedFontStyle, backend: BackendType,
profiler_chan: ProfilerChan) -> Result<@mut Font,()> {
profiler_chan: ProfilerChan) -> Result<RcMut<Font>,()> {

// TODO(Issue #179): convert between specified and used font style here?
let styled_handle = match handle.clone_with_style(&fctx.handle, style) {
Ok(result) => result,
Err(()) => return Err(())
};

return Ok(Font::new_from_adopted_handle(fctx, styled_handle, style, backend, profiler_chan));
return Ok(RcMut::new(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
match self.shaper {
Some(shaper) => { return shaper; },
Some(ref shaper) => {
let s: &'self Shaper = shaper;
return s;
},
None => {}
}

let shaper = @Shaper::new(self);
let shaper = Shaper::new(self);
self.shaper = Some(shaper);
shaper
self.shaper.get_ref()
}

pub fn get_table_for_tag(&self, tag: FontTableTag) -> Option<FontTable> {
Expand Down Expand Up @@ -369,7 +376,7 @@ impl Font {
#[fixed_stack_segment]
pub fn draw_text_into_context(&mut self,
rctx: &RenderContext,
run: &TextRun,
run: &~TextRun,
range: &Range,
baseline_origin: Point2D<Au>,
color: Color) {
Expand Down Expand Up @@ -454,11 +461,13 @@ impl Font {
RunMetrics::new(advance, self.metrics.ascent, self.metrics.descent)
}

pub fn shape_text(@mut self, text: ~str, is_whitespace: bool) -> Arc<GlyphStore> {
let shaper = self.get_shaper();
pub fn shape_text(&mut self, text: ~str, is_whitespace: bool) -> Arc<GlyphStore> {

//FIXME (ksh8281)
self.make_shaper();
do self.shape_cache.find_or_create(&text) |txt| {
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)
}
}
Expand Down

0 comments on commit 403958a

Please sign in to comment.