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 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};
@ -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
@ -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,
}
@ -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)
@ -142,14 +142,13 @@ 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 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.text_run,
&text.range,
baseline_origin,
text.color);
@ -160,18 +159,18 @@ impl<E> DisplayItem<E> {
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));

View file

@ -75,6 +75,7 @@ pub trait FontTableMethods {
fn with_buffer(&self, &fn(*u8, uint));
}
#[deriving(Clone)]
pub struct FontMetrics {
underline_size: Au,
underline_offset: Au,
@ -234,7 +235,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,
@ -243,7 +244,7 @@ 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,
@ -256,7 +257,7 @@ impl Font {
} else {
return Err(handle.unwrap_err());
};
let metrics = handle.get_metrics();
// 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));
}
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
let s:&'self Shaper = self.shaper.get_ref();
s
}
pub fn get_table_for_tag(&self, tag: FontTableTag) -> Option<FontTable> {
@ -369,7 +374,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) {
@ -454,11 +459,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)
}
}

View file

@ -130,24 +130,31 @@ impl<'self> FontContext {
let family_name = family.trim();
let transformed_family_name = self.transform_family(family_name);
debug!("(create font group) transformed family is `{:s}`", transformed_family_name);
let mut found = false;
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,
};
let mut found = false;
for font_entry in result.iter() {
if result.is_some() {
found = true;
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);
let instance = self.get_font_by_descriptor(&result.unwrap());
for font in instance.iter() { fonts.push(*font); }
};
}
if !found {
debug!("(create font group) didn't find `{:s}`", transformed_family_name);
@ -156,7 +163,6 @@ impl<'self> FontContext {
if fonts.len() == 0 {
let last_resort = FontList::get_last_resort_font_families();
for family in last_resort.iter() {
let result = match self.font_list {
Some(ref fl) => fl.find_font_in_family(*family, style),
@ -167,7 +173,6 @@ impl<'self> FontContext {
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() {
@ -183,7 +188,7 @@ impl<'self> FontContext {
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, ()> {

View file

@ -13,11 +13,11 @@ use servo_util::time::ProfilerChan;
use std::hashmap::HashMap;
pub type FontFamilyMap = HashMap<~str, @mut FontFamily>;
pub type FontFamilyMap = HashMap<~str, FontFamily>;
trait FontListHandleMethods {
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];
}
@ -28,7 +28,7 @@ pub struct FontList {
prof_chan: ProfilerChan,
}
impl FontList {
impl<'self> FontList {
pub fn new(fctx: &FontContextHandle,
prof_chan: ProfilerChan)
-> FontList {
@ -52,41 +52,46 @@ impl FontList {
}
}
pub fn find_font_in_family(&self,
family_name: &str,
style: &SpecifiedFontStyle) -> Option<@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);
pub fn find_font_in_family(&'self mut self,
family_name: &~str,
style: &SpecifiedFontStyle) -> Option<&'self FontEntry> {
// 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] {
let last_resort = FontListHandle::get_last_resort_font_families();
last_resort
@ -94,12 +99,12 @@ impl FontList {
}
// Holds a specific font family, and the various
pub struct FontFamily {
pub struct FontFamily<'self> {
family_name: ~str,
entries: ~[@FontEntry],
entries: ~[FontEntry],
}
impl FontFamily {
impl<'self> FontFamily {
pub fn new(family_name: &str) -> FontFamily {
FontFamily {
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 {
return
}
@ -115,8 +120,8 @@ impl FontFamily {
assert!(self.entries.len() > 0)
}
pub fn find_font_for_style(@mut self, list: &FontListHandle, style: &SpecifiedFontStyle)
-> Option<@FontEntry> {
pub fn find_font_for_style(&'self mut self, list: &FontListHandle, style: &SpecifiedFontStyle)
-> Option<&'self FontEntry> {
self.load_family_variations(list);
// TODO(Issue #189): optimize lookup for
@ -129,7 +134,7 @@ impl FontFamily {
if (style.weight.is_bold() == entry.is_bold()) &&
(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 {
let family_name = str::raw::from_c_str(family as *c_char);
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);
v += 1;
}
@ -64,7 +64,7 @@ impl FontListHandle {
}
#[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);
unsafe {
let config = FcConfigGetCurrent();
@ -120,7 +120,7 @@ impl FontListHandle {
let font_handle = font_handle.unwrap();
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);
}

View file

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

View file

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

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. */
pub use text::shaping::Shaper;
pub use text::text_run::SendableTextRun;
pub use text::text_run::TextRun;
pub mod glyph;

View file

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

View file

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

View file

@ -7,22 +7,25 @@ use std::vec::VecIterator;
use font_context::FontContext;
use servo_util::geometry::Au;
use text::glyph::GlyphStore;
use font::{Font, FontDescriptor, RunMetrics};
use font::{Font, FontDescriptor, RunMetrics, FontStyle, FontMetrics};
use servo_util::range::Range;
use extra::arc::Arc;
use style::computed_values::text_decoration;
/// A text run.
#[deriving(Clone)]
pub struct TextRun {
text: Arc<~str>,
font: @mut Font,
font_descriptor: FontDescriptor,
font_metrics: FontMetrics,
font_style: FontStyle,
decoration: text_decoration::T,
glyphs: Arc<~[Arc<GlyphStore>]>,
}
/// The same as a text run, but with a font descriptor instead of a font. This makes them thread
/// safe.
pub struct SendableTextRun {
/*pub struct SendableTextRun {
text: Arc<~str>,
font: FontDescriptor,
decoration: text_decoration::T,
@ -44,7 +47,7 @@ impl SendableTextRun {
}
}
}
*/
pub struct SliceIterator<'self> {
priv glyph_iter: VecIterator<'self, Arc<GlyphStore>>,
priv range: Range,
@ -120,12 +123,14 @@ impl<'self> Iterator<Range> for LineIterator<'self> {
}
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 run = TextRun {
text: Arc::new(text),
font: font,
font_style: font.style.clone(),
font_metrics: font.metrics.clone(),
font_descriptor: font.get_descriptor(),
decoration: decoration,
glyphs: Arc::new(glyphs),
};
@ -133,10 +138,9 @@ impl<'self> TextRun {
}
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.
let mut glyphs = ~[];
@ -190,7 +194,7 @@ impl<'self> TextRun {
glyphs
}
/*
pub fn serialize(&self) -> SendableTextRun {
SendableTextRun {
text: self.text.clone(),
@ -199,7 +203,7 @@ impl<'self> TextRun {
glyphs: self.glyphs.clone(),
}
}
*/
pub fn char_len(&self) -> uint {
do self.glyphs.get().iter().fold(0u) |len, slice_glyphs| {
len + slice_glyphs.get().char_len()
@ -218,19 +222,30 @@ impl<'self> TextRun {
}
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 {
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 {
let mut max_piece_width = Au(0);
debug!("iterating outer range {:?}", range);
for (glyphs, offset, slice_range) in self.iter_slices_for_range(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

View file

@ -630,13 +630,21 @@ impl Box {
// Create the text box.
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 {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(&self),
},
// FIXME(pcwalton): Allocation? Why?!
text_run: ~text_box.run.serialize(),
text_run: run,
range: text_box.range,
color: color,
};
@ -947,7 +955,7 @@ impl Box {
self.position.mutate().ptr.size.width = image_width
}
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!"),
}

View file

@ -711,7 +711,7 @@ impl Flow for InlineFlow {
// Find the top and bottom of the content area.
// 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
let text_offset = text_ascent + (line_height - em_size).scale_by(0.5);