Revert "auto merge of #1356 : ksh8281/servo/remove_@_in_LayoutTask.FontContext, r=pcwalton"

This reverts commit e8ffac13d7, reversing
changes made to db923feffe.

Reverting this change because FreeType is *not* thread safe. See the
documentation here:

http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html

"In multi-threaded applications, make sure that the same FT_Library
object or any of its children doesn't get accessed in parallel."

We will need to use a `MutexArc` instead.
This commit is contained in:
Patrick Walton 2013-12-09 19:39:40 -08:00
parent e8ffac13d7
commit 30bbaa49b7
13 changed files with 120 additions and 122 deletions

View file

@ -93,7 +93,7 @@ pub struct SolidColorDisplayItem<E> {
/// Renders text.
pub struct TextDisplayItem<E> {
base: BaseDisplayItem<E>,
text_run: Arc<~TextRun>,
text_run: ~TextRun,
range: Range,
color: Color,
}
@ -163,8 +163,7 @@ impl<E> DisplayItem<E> {
debug!("Drawing text at {:?}.", text.base.bounds);
// FIXME(pcwalton): Allocating? Why?
let text_run = text.text_run.get();
let font = render_context.font_ctx.get_font_by_descriptor(&text_run.font_descriptor).unwrap();
let font = render_context.font_ctx.get_font_by_descriptor(&text.text_run.font_descriptor).unwrap();
let font_metrics = font.with_borrow( |font| {
font.metrics.clone()
@ -173,7 +172,7 @@ impl<E> DisplayItem<E> {
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.get(),
&text.text_run,
&text.range,
baseline_origin,
text.color);
@ -184,18 +183,18 @@ impl<E> DisplayItem<E> {
let strikeout_size = font_metrics.strikeout_size;
let strikeout_offset = font_metrics.strikeout_offset;
if text_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 text_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 text_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

@ -31,6 +31,7 @@ pub fn dummy_style() -> FontStyle {
}
pub trait FontContextHandleMethods {
fn clone(&self) -> FontContextHandle;
fn create_font_from_identifier(&self, ~str, UsedFontStyle) -> Result<FontHandle, ()>;
}

View file

@ -8,6 +8,7 @@ use font::{CSSFontWeight, FontHandleMethods, FontMetrics, FontTableMethods};
use font::{FontTableTag, FractionalPixel, SpecifiedFontStyle, UsedFontStyle, FontWeight100};
use font::{FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600};
use font::{FontWeight700, FontWeight800, FontWeight900};
use font_context::FontContextHandleMethods;
use servo_util::geometry::Au;
use servo_util::geometry;
use platform::font_context::FontContextHandle;
@ -78,7 +79,7 @@ impl FontHandleMethods for FontHandle {
buf: ~[u8],
style: &SpecifiedFontStyle)
-> Result<FontHandle, ()> {
let ft_ctx: FT_Library = fctx.ctx.get().ctx;
let ft_ctx: FT_Library = fctx.ctx.ctx;
if ft_ctx.is_null() { return Err(()); }
let face_result = do buf.as_imm_buf |bytes: *u8, len: uint| {
@ -291,7 +292,7 @@ impl<'self> FontHandle {
pub fn new_from_file(fctx: &FontContextHandle, file: &str,
style: &SpecifiedFontStyle) -> Result<FontHandle, ()> {
unsafe {
let ft_ctx: FT_Library = fctx.ctx.get().ctx;
let ft_ctx: FT_Library = fctx.ctx.ctx;
if ft_ctx.is_null() { return Err(()); }
let mut face: FT_Face = ptr::null();
@ -319,7 +320,7 @@ impl<'self> FontHandle {
pub fn new_from_file_unstyled(fctx: &FontContextHandle, file: ~str)
-> Result<FontHandle, ()> {
unsafe {
let ft_ctx: FT_Library = fctx.ctx.get().ctx;
let ft_ctx: FT_Library = fctx.ctx.ctx;
if ft_ctx.is_null() { return Err(()); }
let mut face: FT_Face = ptr::null();

View file

@ -11,23 +11,30 @@ use freetype::freetype::{FTErrorMethods, FT_Library};
use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType};
use std::ptr;
use extra::arc::Arc;
#[deriving(Clone)]
struct FreeTypeLibraryHandle {
ctx: FT_Library,
}
#[deriving(Clone)]
// FIXME(ksh8281) this value have to use atomic operation for counting ref
static mut font_context_ref_count: uint = 0;
static mut ft_pointer: Option<FT_Library> = None;
pub struct FontContextHandle {
ctx: Arc<FreeTypeLibraryHandle>,
ctx: FreeTypeLibraryHandle,
}
impl Drop for FreeTypeLibraryHandle {
impl Drop for FontContextHandle {
#[fixed_stack_segment]
fn drop(&mut self) {
assert!(self.ctx.is_not_null());
unsafe { FT_Done_FreeType(self.ctx) };
assert!(self.ctx.ctx.is_not_null());
unsafe {
assert!(font_context_ref_count >= 1);
font_context_ref_count = font_context_ref_count - 1;
if font_context_ref_count == 0 {
FT_Done_FreeType(self.ctx.ctx);
}
}
}
}
@ -35,17 +42,38 @@ impl FontContextHandle {
#[fixed_stack_segment]
pub fn new() -> FontContextHandle {
unsafe {
let ctx: FT_Library = ptr::null();
let result = FT_Init_FreeType(&ctx);
if !result.succeeded() { fail!("Unable to initialize FreeType library"); }
FontContextHandle {
ctx: Arc::new(FreeTypeLibraryHandle { ctx: ctx }),
match ft_pointer {
Some(ref ctx) => {
font_context_ref_count = font_context_ref_count + 1;
FontContextHandle {
ctx: FreeTypeLibraryHandle { ctx: ctx.clone() },
}
},
None => {
let ctx: FT_Library = ptr::null();
let result = FT_Init_FreeType(ptr::to_unsafe_ptr(&ctx));
if !result.succeeded() { fail!("Unable to initialize FreeType library"); }
ft_pointer = Some(ctx);
font_context_ref_count = font_context_ref_count + 1;
FontContextHandle {
ctx: FreeTypeLibraryHandle { ctx: ctx },
}
}
}
}
}
}
impl FontContextHandleMethods for FontContextHandle {
fn clone(&self) -> FontContextHandle {
unsafe {
font_context_ref_count = font_context_ref_count + 1;
FontContextHandle {
ctx: self.ctx.clone()
}
}
}
fn create_font_from_identifier(&self, name: ~str, style: UsedFontStyle)
-> Result<FontHandle, ()> {
debug!("Creating font handle for {:s}", name);

View file

@ -19,6 +19,7 @@ use fontconfig::fontconfig::{
use font::{FontHandleMethods, UsedFontStyle};
use font_context::FontContextHandleMethods;
use font_list::{FontEntry, FontFamily, FontFamilyMap};
use platform::font::FontHandle;
use platform::font_context::FontContextHandle;

View file

@ -8,12 +8,10 @@ use platform::macos::font::FontHandle;
use core_text;
#[deriving(Clone)]
pub struct FontContextHandle {
ctx: ()
}
#[deriving(Clone)]
impl FontContextHandle {
// this is a placeholder until NSFontManager or whatever is bound in here.
pub fn new() -> FontContextHandle {
@ -22,6 +20,12 @@ impl FontContextHandle {
}
impl FontContextHandleMethods for FontContextHandle {
fn clone(&self) -> FontContextHandle {
FontContextHandle {
ctx: self.ctx
}
}
fn create_font_from_identifier(&self,
name: ~str,
style: UsedFontStyle)