mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Format script component
This commit is contained in:
parent
2ca7a13473
commit
c37a345dc9
357 changed files with 25485 additions and 18076 deletions
|
@ -61,9 +61,11 @@ pub struct HTMLCanvasElement {
|
|||
}
|
||||
|
||||
impl HTMLCanvasElement {
|
||||
fn new_inherited(local_name: LocalName,
|
||||
prefix: Option<Prefix>,
|
||||
document: &Document) -> HTMLCanvasElement {
|
||||
fn new_inherited(
|
||||
local_name: LocalName,
|
||||
prefix: Option<Prefix>,
|
||||
document: &Document,
|
||||
) -> HTMLCanvasElement {
|
||||
HTMLCanvasElement {
|
||||
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
|
||||
context: DomRefCell::new(None),
|
||||
|
@ -71,12 +73,18 @@ impl HTMLCanvasElement {
|
|||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
pub fn new(local_name: LocalName,
|
||||
prefix: Option<Prefix>,
|
||||
document: &Document) -> DomRoot<HTMLCanvasElement> {
|
||||
Node::reflect_node(Box::new(HTMLCanvasElement::new_inherited(local_name, prefix, document)),
|
||||
document,
|
||||
HTMLCanvasElementBinding::Wrap)
|
||||
pub fn new(
|
||||
local_name: LocalName,
|
||||
prefix: Option<Prefix>,
|
||||
document: &Document,
|
||||
) -> DomRoot<HTMLCanvasElement> {
|
||||
Node::reflect_node(
|
||||
Box::new(HTMLCanvasElement::new_inherited(
|
||||
local_name, prefix, document,
|
||||
)),
|
||||
document,
|
||||
HTMLCanvasElementBinding::Wrap,
|
||||
)
|
||||
}
|
||||
|
||||
fn recreate_contexts(&self) {
|
||||
|
@ -124,13 +132,15 @@ impl LayoutHTMLCanvasElementHelpers for LayoutDom<HTMLCanvasElement> {
|
|||
Some(&CanvasContext::WebGL2(ref context)) => {
|
||||
context.to_layout().canvas_data_source()
|
||||
},
|
||||
None => {
|
||||
HTMLCanvasDataSource::Image(None)
|
||||
}
|
||||
None => HTMLCanvasDataSource::Image(None),
|
||||
};
|
||||
|
||||
let width_attr = canvas.upcast::<Element>().get_attr_for_layout(&ns!(), &local_name!("width"));
|
||||
let height_attr = canvas.upcast::<Element>().get_attr_for_layout(&ns!(), &local_name!("height"));
|
||||
let width_attr = canvas
|
||||
.upcast::<Element>()
|
||||
.get_attr_for_layout(&ns!(), &local_name!("width"));
|
||||
let height_attr = canvas
|
||||
.upcast::<Element>()
|
||||
.get_attr_for_layout(&ns!(), &local_name!("height"));
|
||||
HTMLCanvasData {
|
||||
source: source,
|
||||
width: width_attr.map_or(DEFAULT_WIDTH, |val| val.as_uint()),
|
||||
|
@ -164,7 +174,8 @@ impl LayoutHTMLCanvasElementHelpers for LayoutDom<HTMLCanvasElement> {
|
|||
fn get_canvas_id_for_layout(&self) -> CanvasId {
|
||||
unsafe {
|
||||
let canvas = &*self.unsafe_get();
|
||||
if let &Some(CanvasContext::Context2d(ref context)) = canvas.context.borrow_for_layout() {
|
||||
if let &Some(CanvasContext::Context2d(ref context)) = canvas.context.borrow_for_layout()
|
||||
{
|
||||
context.to_layout().get_canvas_id()
|
||||
} else {
|
||||
CanvasId(0)
|
||||
|
@ -173,7 +184,6 @@ impl LayoutHTMLCanvasElementHelpers for LayoutDom<HTMLCanvasElement> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl HTMLCanvasElement {
|
||||
pub fn context(&self) -> Option<Ref<CanvasContext>> {
|
||||
ref_filter_map::ref_filter_map(self.context.borrow(), |ctx| ctx.as_ref())
|
||||
|
@ -220,7 +230,7 @@ impl HTMLCanvasElement {
|
|||
options: HandleValue,
|
||||
) -> Option<DomRoot<WebGL2RenderingContext>> {
|
||||
if !PREFS.is_webgl2_enabled() {
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
if let Some(ctx) = self.context() {
|
||||
return match *ctx {
|
||||
|
@ -241,7 +251,7 @@ impl HTMLCanvasElement {
|
|||
match *self.context.borrow() {
|
||||
Some(CanvasContext::WebGL(ref context)) => Some(DomRoot::from_ref(&*context)),
|
||||
Some(CanvasContext::WebGL2(ref context)) => Some(context.base_context()),
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -255,11 +265,11 @@ impl HTMLCanvasElement {
|
|||
Ok(ConversionResult::Failure(ref error)) => {
|
||||
throw_type_error(cx, &error);
|
||||
None
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
debug!("Unexpected error on conversion of WebGLContextAttributes");
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -271,13 +281,17 @@ impl HTMLCanvasElement {
|
|||
let size = self.get_size();
|
||||
|
||||
if size.width == 0 || size.height == 0 {
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
|
||||
let data = match self.context.borrow().as_ref() {
|
||||
Some(&CanvasContext::Context2d(ref context)) => {
|
||||
let (sender, receiver) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
|
||||
let msg = CanvasMsg::FromScript(FromScriptMsg::SendPixels(sender), context.get_canvas_id());
|
||||
let (sender, receiver) =
|
||||
ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
|
||||
let msg = CanvasMsg::FromScript(
|
||||
FromScriptMsg::SendPixels(sender),
|
||||
context.get_canvas_id(),
|
||||
);
|
||||
context.get_ipc_renderer().send(msg).unwrap();
|
||||
|
||||
receiver.recv().unwrap()?.into()
|
||||
|
@ -319,19 +333,16 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
|||
options: HandleValue,
|
||||
) -> Option<RenderingContext> {
|
||||
match &*id {
|
||||
"2d" => {
|
||||
self.get_or_init_2d_context()
|
||||
.map(RenderingContext::CanvasRenderingContext2D)
|
||||
}
|
||||
"webgl" | "experimental-webgl" => {
|
||||
self.get_or_init_webgl_context(cx, options)
|
||||
.map(RenderingContext::WebGLRenderingContext)
|
||||
}
|
||||
"webgl2" | "experimental-webgl2" => {
|
||||
self.get_or_init_webgl2_context(cx, options)
|
||||
.map(RenderingContext::WebGL2RenderingContext)
|
||||
}
|
||||
_ => None
|
||||
"2d" => self
|
||||
.get_or_init_2d_context()
|
||||
.map(RenderingContext::CanvasRenderingContext2D),
|
||||
"webgl" | "experimental-webgl" => self
|
||||
.get_or_init_webgl_context(cx, options)
|
||||
.map(RenderingContext::WebGLRenderingContext),
|
||||
"webgl2" | "experimental-webgl2" => self
|
||||
.get_or_init_webgl2_context(cx, options)
|
||||
.map(RenderingContext::WebGL2RenderingContext),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -358,27 +369,31 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
|||
// Step 3.
|
||||
let raw_data = match *self.context.borrow() {
|
||||
Some(CanvasContext::Context2d(ref context)) => {
|
||||
let image_data = context.GetImageData(Finite::wrap(0f64), Finite::wrap(0f64),
|
||||
Finite::wrap(self.Width() as f64),
|
||||
Finite::wrap(self.Height() as f64))?;
|
||||
let image_data = context.GetImageData(
|
||||
Finite::wrap(0f64),
|
||||
Finite::wrap(0f64),
|
||||
Finite::wrap(self.Width() as f64),
|
||||
Finite::wrap(self.Height() as f64),
|
||||
)?;
|
||||
image_data.get_data_array()
|
||||
}
|
||||
},
|
||||
Some(CanvasContext::WebGL(ref context)) => {
|
||||
match context.get_image_data(self.Width(), self.Height()) {
|
||||
Some(data) => data,
|
||||
None => return Ok(USVString("data:,".into())),
|
||||
}
|
||||
}
|
||||
Some(CanvasContext::WebGL2(ref context)) => {
|
||||
match context.base_context().get_image_data(self.Width(), self.Height()) {
|
||||
Some(data) => data,
|
||||
None => return Ok(USVString("data:,".into())),
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(CanvasContext::WebGL2(ref context)) => match context
|
||||
.base_context()
|
||||
.get_image_data(self.Width(), self.Height())
|
||||
{
|
||||
Some(data) => data,
|
||||
None => return Ok(USVString("data:,".into())),
|
||||
},
|
||||
None => {
|
||||
// Each pixel is fully-transparent black.
|
||||
vec![0; (self.Width() * self.Height() * 4) as usize]
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// Only handle image/png for now.
|
||||
|
@ -387,7 +402,9 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
|||
let mut encoded = Vec::new();
|
||||
{
|
||||
let encoder: PNGEncoder<&mut Vec<u8>> = PNGEncoder::new(&mut encoded);
|
||||
encoder.encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8)).unwrap();
|
||||
encoder
|
||||
.encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let encoded = base64::encode(&encoded);
|
||||
|
@ -412,7 +429,10 @@ impl VirtualMethods for HTMLCanvasElement {
|
|||
match name {
|
||||
&local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
|
||||
&local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
|
||||
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
|
||||
_ => self
|
||||
.super_type()
|
||||
.unwrap()
|
||||
.parse_plain_attribute(name, value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -438,13 +458,15 @@ pub mod utils {
|
|||
|
||||
pub fn request_image_from_cache(window: &Window, url: ServoUrl) -> ImageResponse {
|
||||
let image_cache = window.image_cache();
|
||||
let response =
|
||||
image_cache.find_image_or_metadata(url.into(),
|
||||
UsePlaceholder::No,
|
||||
CanRequestImages::No);
|
||||
let response = image_cache.find_image_or_metadata(
|
||||
url.into(),
|
||||
UsePlaceholder::No,
|
||||
CanRequestImages::No,
|
||||
);
|
||||
match response {
|
||||
Ok(ImageOrMetadataAvailable::ImageAvailable(image, url)) =>
|
||||
ImageResponse::Loaded(image, url),
|
||||
Ok(ImageOrMetadataAvailable::ImageAvailable(image, url)) => {
|
||||
ImageResponse::Loaded(image, url)
|
||||
},
|
||||
_ => ImageResponse::None,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue