mirror of
https://github.com/servo/servo.git
synced 2025-06-08 00:23:30 +00:00
Basic implementation of canvas.fillText
This commit is contained in:
parent
726f7d7209
commit
f161ab8e57
2 changed files with 61 additions and 4 deletions
|
@ -264,6 +264,15 @@ pub trait GenericDrawTarget {
|
||||||
operator: CompositionOp,
|
operator: CompositionOp,
|
||||||
);
|
);
|
||||||
fn fill(&mut self, path: &Path, pattern: Pattern, draw_options: &DrawOptions);
|
fn fill(&mut self, path: &Path, pattern: Pattern, draw_options: &DrawOptions);
|
||||||
|
fn fill_text(
|
||||||
|
&mut self,
|
||||||
|
text: String,
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
max_width: Option<f64>,
|
||||||
|
pattern: Pattern,
|
||||||
|
draw_options: &DrawOptions,
|
||||||
|
);
|
||||||
fn fill_rect(&mut self, rect: &Rect<f32>, pattern: Pattern, draw_options: Option<&DrawOptions>);
|
fn fill_rect(&mut self, rect: &Rect<f32>, pattern: Pattern, draw_options: Option<&DrawOptions>);
|
||||||
fn get_format(&self) -> SurfaceFormat;
|
fn get_format(&self) -> SurfaceFormat;
|
||||||
fn get_size(&self) -> Size2D<i32>;
|
fn get_size(&self) -> Size2D<i32>;
|
||||||
|
@ -456,10 +465,29 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fill_text(&self, text: String, x: f64, y: f64, max_width: Option<f64>) {
|
pub fn fill_text(&mut self, text: String, x: f64, y: f64, max_width: Option<f64>) {
|
||||||
error!(
|
// 1. If maxWidth was provided but is less than or equal to zero or equal to NaN,
|
||||||
"Unimplemented canvas2d.fillText. Values received: {}, {}, {}, {:?}.",
|
// then return an empty array.
|
||||||
text, x, y, max_width
|
if max_width.map_or(false, |max_width| max_width <= 0.) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Replace all ASCII whitespace in text with U+0020 SPACE characters.
|
||||||
|
let text = text
|
||||||
|
.chars()
|
||||||
|
.map(|c| match c {
|
||||||
|
' ' | '\t' | '\n' | '\r' | '\x0C' => '\x20',
|
||||||
|
_ => c,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
self.draw_target.fill_text(
|
||||||
|
text,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
max_width,
|
||||||
|
self.state.fill_style.clone(),
|
||||||
|
&self.state.draw_options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,9 @@ use canvas_traits::canvas::*;
|
||||||
use cssparser::RGBA;
|
use cssparser::RGBA;
|
||||||
use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D};
|
use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D};
|
||||||
use euclid::Angle;
|
use euclid::Angle;
|
||||||
|
use font_kit::family_name::FamilyName;
|
||||||
|
use font_kit::properties::Properties;
|
||||||
|
use font_kit::source::SystemSource;
|
||||||
use lyon_geom::Arc;
|
use lyon_geom::Arc;
|
||||||
use raqote::PathOp;
|
use raqote::PathOp;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
@ -513,6 +516,32 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fill_text(
|
||||||
|
&mut self,
|
||||||
|
text: String,
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
max_width: Option<f64>,
|
||||||
|
pattern: canvas_data::Pattern,
|
||||||
|
draw_options: &DrawOptions,
|
||||||
|
) {
|
||||||
|
let font = SystemSource::new()
|
||||||
|
.select_best_match(&[FamilyName::SansSerif], &Properties::new())
|
||||||
|
.unwrap()
|
||||||
|
.load()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
self.draw_text(
|
||||||
|
&font,
|
||||||
|
24.,
|
||||||
|
&text,
|
||||||
|
Point2D::new(x as f32, y as f32),
|
||||||
|
&pattern.source(),
|
||||||
|
draw_options.as_raqote(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn fill_rect(
|
fn fill_rect(
|
||||||
&mut self,
|
&mut self,
|
||||||
rect: &Rect<f32>,
|
rect: &Rect<f32>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue