2019-07-19 08:32:41 +00:00
|
|
|
use crate::backend::render::{RenderBackend, ShapeHandle};
|
2020-06-10 23:31:51 +00:00
|
|
|
use crate::html::TextSpan;
|
2019-10-08 02:27:31 +00:00
|
|
|
use crate::prelude::*;
|
2020-01-19 23:17:19 +00:00
|
|
|
use crate::transform::Transform;
|
2019-12-17 05:21:59 +00:00
|
|
|
use gc_arena::{Collect, Gc, MutationContext};
|
2019-05-04 18:45:11 +00:00
|
|
|
|
2021-02-02 00:41:17 +00:00
|
|
|
pub use swf::TextGridFit;
|
|
|
|
|
2020-05-29 03:38:19 +00:00
|
|
|
/// Certain Flash routines measure text by rounding down to the nearest whole pixel.
|
2020-06-02 02:49:33 +00:00
|
|
|
pub fn round_down_to_pixel(t: Twips) -> Twips {
|
2020-05-28 05:05:44 +00:00
|
|
|
Twips::from_pixels(t.to_pixels().floor())
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:48:51 +00:00
|
|
|
type Error = Box<dyn std::error::Error>;
|
2019-05-04 18:45:11 +00:00
|
|
|
|
2020-06-10 23:31:51 +00:00
|
|
|
/// Parameters necessary to evaluate a font.
|
|
|
|
#[derive(Copy, Clone, Debug, Collect)]
|
|
|
|
#[collect(require_static)]
|
|
|
|
pub struct EvalParameters {
|
|
|
|
/// The height of each glyph, equivalent to a font size.
|
|
|
|
height: Twips,
|
|
|
|
|
|
|
|
/// Additional letter spacing to be added to or removed from each glyph
|
|
|
|
/// after normal or kerned glyph advances are applied.
|
|
|
|
letter_spacing: Twips,
|
|
|
|
|
|
|
|
/// Whether or not to allow use of font-provided kerning metrics.
|
|
|
|
///
|
|
|
|
/// Fonts can optionally add or remove additional spacing between specific
|
|
|
|
/// pairs of letters, separate from the ordinary width between glyphs. This
|
|
|
|
/// parameter allows enabling or disabling that feature.
|
|
|
|
kerning: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EvalParameters {
|
|
|
|
/// Construct eval parameters from their individual parts.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn from_parts(height: Twips, letter_spacing: Twips, kerning: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
height,
|
|
|
|
letter_spacing,
|
|
|
|
kerning,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the formatting on a text span over to font evaluation
|
|
|
|
/// parameters.
|
|
|
|
pub fn from_span(span: &TextSpan) -> Self {
|
|
|
|
Self {
|
|
|
|
height: Twips::from_pixels(span.size),
|
|
|
|
letter_spacing: Twips::from_pixels(span.letter_spacing),
|
|
|
|
kerning: span.kerning,
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 01:34:18 +00:00
|
|
|
|
|
|
|
/// Get the height the font would be evaluated at.
|
|
|
|
pub fn height(&self) -> Twips {
|
|
|
|
self.height
|
|
|
|
}
|
2020-06-10 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:21:59 +00:00
|
|
|
#[derive(Debug, Clone, Collect, Copy)]
|
|
|
|
#[collect(no_drop)]
|
|
|
|
pub struct Font<'gc>(Gc<'gc, FontData>);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Collect)]
|
|
|
|
#[collect(require_static)]
|
|
|
|
struct FontData {
|
2019-10-08 02:27:31 +00:00
|
|
|
/// The list of glyphs defined in the font.
|
|
|
|
/// Used directly by `DefineText` tags.
|
|
|
|
glyphs: Vec<Glyph>,
|
|
|
|
|
|
|
|
/// A map from a Unicode code point to glyph in the `glyphs` array.
|
|
|
|
/// Used by `DefineEditText` tags.
|
|
|
|
code_point_to_glyph: fnv::FnvHashMap<u16, usize>,
|
2019-10-07 18:39:44 +00:00
|
|
|
|
|
|
|
/// The scaling applied to the font height to render at the proper size.
|
|
|
|
/// This depends on the DefineFont tag version.
|
|
|
|
scale: f32,
|
2019-10-08 02:27:31 +00:00
|
|
|
|
|
|
|
/// Kerning infomration.
|
|
|
|
/// Maps from a pair of unicode code points to horizontal offset value.
|
|
|
|
kerning_pairs: fnv::FnvHashMap<(u16, u16), Twips>,
|
2020-05-19 02:12:08 +00:00
|
|
|
|
2020-05-29 03:38:19 +00:00
|
|
|
/// The distance from the top of each glyph to the baseline of the font, in
|
|
|
|
/// EM-square coordinates.
|
|
|
|
ascent: u16,
|
|
|
|
|
|
|
|
/// The distance from the baseline of the font to the bottom of each glyph,
|
|
|
|
/// in EM-square coordinates.
|
|
|
|
descent: u16,
|
|
|
|
|
|
|
|
/// The distance between the bottom of any one glyph and the top of
|
|
|
|
/// another, in EM-square coordinates.
|
|
|
|
leading: i16,
|
|
|
|
|
2020-05-19 02:12:08 +00:00
|
|
|
/// The identity of the font.
|
|
|
|
descriptor: FontDescriptor,
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:21:59 +00:00
|
|
|
impl<'gc> Font<'gc> {
|
|
|
|
pub fn from_swf_tag(
|
|
|
|
gc_context: MutationContext<'gc, '_>,
|
|
|
|
renderer: &mut dyn RenderBackend,
|
|
|
|
tag: &swf::Font,
|
2021-01-20 20:46:22 +00:00
|
|
|
encoding: &'static swf::Encoding,
|
2019-12-17 05:21:59 +00:00
|
|
|
) -> Result<Font<'gc>, Error> {
|
2019-05-04 18:45:11 +00:00
|
|
|
let mut glyphs = vec![];
|
2019-10-08 02:27:31 +00:00
|
|
|
let mut code_point_to_glyph = fnv::FnvHashMap::default();
|
|
|
|
for swf_glyph in &tag.glyphs {
|
|
|
|
let glyph = Glyph {
|
2020-09-14 20:49:08 +00:00
|
|
|
shape_handle: renderer.register_glyph_shape(swf_glyph),
|
2019-10-08 02:27:31 +00:00
|
|
|
advance: swf_glyph.advance.unwrap_or(0),
|
2020-09-14 20:49:08 +00:00
|
|
|
shape: crate::shape_utils::swf_glyph_to_shape(swf_glyph),
|
2019-10-08 02:27:31 +00:00
|
|
|
};
|
|
|
|
let index = glyphs.len();
|
|
|
|
glyphs.push(glyph);
|
|
|
|
code_point_to_glyph.insert(swf_glyph.code, index);
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
2019-10-08 02:27:31 +00:00
|
|
|
let kerning_pairs: fnv::FnvHashMap<(u16, u16), Twips> = if let Some(layout) = &tag.layout {
|
|
|
|
layout
|
|
|
|
.kerning
|
|
|
|
.iter()
|
|
|
|
.map(|kerning| ((kerning.left_code, kerning.right_code), kerning.adjustment))
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
fnv::FnvHashMap::default()
|
|
|
|
};
|
2020-05-19 02:12:08 +00:00
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
let descriptor = FontDescriptor::from_swf_tag(tag, encoding);
|
2020-05-29 03:38:19 +00:00
|
|
|
let (ascent, descent, leading) = if let Some(layout) = &tag.layout {
|
|
|
|
(layout.ascent, layout.descent, layout.leading)
|
|
|
|
} else {
|
|
|
|
(0, 0, 0)
|
|
|
|
};
|
2020-05-19 02:12:08 +00:00
|
|
|
|
2019-12-17 05:21:59 +00:00
|
|
|
Ok(Font(Gc::allocate(
|
|
|
|
gc_context,
|
|
|
|
FontData {
|
|
|
|
glyphs,
|
|
|
|
code_point_to_glyph,
|
2019-10-07 18:39:44 +00:00
|
|
|
|
2019-12-17 05:21:59 +00:00
|
|
|
/// DefineFont3 stores coordinates at 20x the scale of DefineFont1/2.
|
|
|
|
/// (SWF19 p.164)
|
|
|
|
scale: if tag.version >= 3 { 20480.0 } else { 1024.0 },
|
|
|
|
kerning_pairs,
|
2020-05-29 03:38:19 +00:00
|
|
|
ascent,
|
|
|
|
descent,
|
|
|
|
leading,
|
2020-05-19 02:12:08 +00:00
|
|
|
descriptor,
|
2019-12-17 05:21:59 +00:00
|
|
|
},
|
|
|
|
)))
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 04:02:09 +00:00
|
|
|
/// Returns whether this font contains glyph shapes.
|
|
|
|
/// If not, this font should be rendered as a device font.
|
2019-12-17 05:21:59 +00:00
|
|
|
pub fn has_glyphs(self) -> bool {
|
|
|
|
!self.0.glyphs.is_empty()
|
2019-10-08 04:02:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 02:27:31 +00:00
|
|
|
/// Returns a glyph entry by index.
|
|
|
|
/// Used by `Text` display objects.
|
2019-12-17 05:21:59 +00:00
|
|
|
pub fn get_glyph(self, i: usize) -> Option<Glyph> {
|
|
|
|
self.0.glyphs.get(i).cloned()
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
2019-10-07 18:39:44 +00:00
|
|
|
|
2019-10-08 02:27:31 +00:00
|
|
|
/// Returns a glyph entry by character.
|
|
|
|
/// Used by `EditText` display objects.
|
2019-12-17 05:21:59 +00:00
|
|
|
pub fn get_glyph_for_char(self, c: char) -> Option<Glyph> {
|
2019-10-08 02:27:31 +00:00
|
|
|
// TODO: Properly handle UTF-16/out-of-bounds code points.
|
|
|
|
let code_point = c as u16;
|
2019-12-17 05:21:59 +00:00
|
|
|
if let Some(index) = self.0.code_point_to_glyph.get(&code_point) {
|
2019-10-08 02:27:31 +00:00
|
|
|
self.get_glyph(*index)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a pair of characters, applies the offset that should be applied
|
|
|
|
/// to the advance value between these two characters.
|
|
|
|
/// Returns 0 twips if no kerning offset exists between these two characters.
|
2019-12-17 05:21:59 +00:00
|
|
|
pub fn get_kerning_offset(self, left: char, right: char) -> Twips {
|
2019-10-08 02:27:31 +00:00
|
|
|
// TODO: Properly handle UTF-16/out-of-bounds code points.
|
|
|
|
let left_code_point = left as u16;
|
|
|
|
let right_code_point = right as u16;
|
2019-12-17 05:21:59 +00:00
|
|
|
self.0
|
|
|
|
.kerning_pairs
|
2019-10-08 02:27:31 +00:00
|
|
|
.get(&(left_code_point, right_code_point))
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2020-05-29 03:38:19 +00:00
|
|
|
/// Return the leading for this font at a given height.
|
|
|
|
pub fn get_leading_for_height(self, height: Twips) -> Twips {
|
|
|
|
let scale = height.get() as f32 / self.scale();
|
|
|
|
|
|
|
|
Twips::new((self.0.leading as f32 * scale) as i32)
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:34:18 +00:00
|
|
|
/// Get the baseline from the top of the glyph at a given height.
|
|
|
|
pub fn get_baseline_for_height(self, height: Twips) -> Twips {
|
|
|
|
let scale = height.get() as f32 / self.scale();
|
|
|
|
|
|
|
|
Twips::new((self.0.ascent as f32 * scale) as i32)
|
|
|
|
}
|
|
|
|
|
2019-10-08 02:27:31 +00:00
|
|
|
/// Returns whether this font contains kerning information.
|
2019-12-17 05:21:59 +00:00
|
|
|
pub fn has_kerning_info(self) -> bool {
|
|
|
|
!self.0.kerning_pairs.is_empty()
|
2019-10-08 02:27:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:21:59 +00:00
|
|
|
pub fn scale(self) -> f32 {
|
|
|
|
self.0.scale
|
2019-10-07 18:39:44 +00:00
|
|
|
}
|
2020-01-19 23:17:19 +00:00
|
|
|
|
|
|
|
/// Evaluate this font against a particular string on a glyph-by-glyph
|
|
|
|
/// basis.
|
|
|
|
///
|
|
|
|
/// This function takes the text string to evaluate against, the base
|
|
|
|
/// transform to start from, the height of each glyph, and produces a list
|
|
|
|
/// of transforms and glyphs which will be consumed by the `glyph_func`
|
|
|
|
/// closure. This corresponds to the series of drawing operations necessary
|
|
|
|
/// to render the text on a single horizontal line.
|
|
|
|
pub fn evaluate<FGlyph>(
|
|
|
|
self,
|
|
|
|
text: &str,
|
|
|
|
mut transform: Transform,
|
2020-06-10 23:31:51 +00:00
|
|
|
params: EvalParameters,
|
2020-01-19 23:17:19 +00:00
|
|
|
mut glyph_func: FGlyph,
|
|
|
|
) where
|
2020-10-31 20:42:03 +00:00
|
|
|
FGlyph: FnMut(usize, &Transform, &Glyph, Twips, Twips),
|
2020-01-19 23:17:19 +00:00
|
|
|
{
|
2020-06-10 23:31:51 +00:00
|
|
|
transform.matrix.ty += params.height;
|
|
|
|
let scale = params.height.get() as f32 / self.scale();
|
2020-01-19 23:17:19 +00:00
|
|
|
|
|
|
|
transform.matrix.a = scale;
|
|
|
|
transform.matrix.d = scale;
|
2020-12-15 21:38:48 +00:00
|
|
|
let mut char_indices = text.char_indices().peekable();
|
2020-01-19 23:17:19 +00:00
|
|
|
let has_kerning_info = self.has_kerning_info();
|
2020-10-31 20:42:03 +00:00
|
|
|
let mut x = Twips::zero();
|
2020-12-15 21:38:48 +00:00
|
|
|
while let Some((pos, c)) = char_indices.next() {
|
2020-02-04 00:32:05 +00:00
|
|
|
if let Some(glyph) = self.get_glyph_for_char(c) {
|
2020-02-18 19:39:53 +00:00
|
|
|
let mut advance = Twips::new(glyph.advance);
|
2020-06-10 23:31:51 +00:00
|
|
|
if has_kerning_info && params.kerning {
|
2020-12-15 21:38:48 +00:00
|
|
|
let next_char = char_indices.peek().cloned().unwrap_or((0, '\0')).1;
|
|
|
|
advance += self.get_kerning_offset(c, next_char);
|
2020-01-19 23:17:19 +00:00
|
|
|
}
|
2020-06-04 03:39:34 +00:00
|
|
|
let twips_advance =
|
2020-06-10 23:31:51 +00:00
|
|
|
Twips::new((advance.get() as f32 * scale) as i32) + params.letter_spacing;
|
2020-05-26 03:50:08 +00:00
|
|
|
|
2020-10-31 20:42:03 +00:00
|
|
|
glyph_func(pos, &transform, &glyph, twips_advance, x);
|
2020-05-26 03:50:08 +00:00
|
|
|
|
|
|
|
// Step horizontally.
|
|
|
|
transform.matrix.tx += twips_advance;
|
2020-10-31 20:42:03 +00:00
|
|
|
x += twips_advance;
|
2020-01-19 23:17:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-20 03:02:21 +00:00
|
|
|
|
|
|
|
/// Measure a particular string's metrics (width and height).
|
2020-06-10 02:55:04 +00:00
|
|
|
///
|
|
|
|
/// The `round` flag causes the returned coordinates to be rounded down to
|
|
|
|
/// the nearest pixel.
|
2020-06-10 23:31:51 +00:00
|
|
|
pub fn measure(self, text: &str, params: EvalParameters, round: bool) -> (Twips, Twips) {
|
2020-02-18 19:39:53 +00:00
|
|
|
let mut size = (Twips::new(0), Twips::new(0));
|
2020-01-20 03:02:21 +00:00
|
|
|
|
2020-05-26 03:50:08 +00:00
|
|
|
self.evaluate(
|
|
|
|
text,
|
|
|
|
Default::default(),
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-10-31 20:42:03 +00:00
|
|
|
|_pos, transform, _glyph, advance, _x| {
|
2020-05-26 03:50:08 +00:00
|
|
|
let tx = transform.matrix.tx;
|
|
|
|
let ty = transform.matrix.ty;
|
2020-06-10 02:55:04 +00:00
|
|
|
|
|
|
|
if round {
|
|
|
|
size.0 = std::cmp::max(size.0, round_down_to_pixel(tx + advance));
|
|
|
|
size.1 = std::cmp::max(size.1, round_down_to_pixel(ty));
|
|
|
|
} else {
|
|
|
|
size.0 = std::cmp::max(size.0, tx + advance);
|
|
|
|
size.1 = std::cmp::max(size.1, ty);
|
|
|
|
}
|
2020-05-26 03:50:08 +00:00
|
|
|
},
|
|
|
|
);
|
2020-01-20 03:02:21 +00:00
|
|
|
|
|
|
|
size
|
|
|
|
}
|
2020-01-21 00:23:00 +00:00
|
|
|
|
2020-05-21 03:11:44 +00:00
|
|
|
/// Given a line of text, find the first breakpoint within the text.
|
2020-01-21 00:23:00 +00:00
|
|
|
///
|
|
|
|
/// This function assumes only `" "` is valid whitespace to split words on,
|
2020-05-21 03:11:44 +00:00
|
|
|
/// and will not attempt to break words that are longer than `width`, nor
|
|
|
|
/// will it break at newlines.
|
2020-03-13 05:10:52 +00:00
|
|
|
///
|
2020-05-21 03:11:44 +00:00
|
|
|
/// The given `offset` determines the start of the initial line, while the
|
2020-05-26 03:17:44 +00:00
|
|
|
/// `width` indicates how long the line is supposed to be. Be careful to
|
|
|
|
/// note that it is possible for this function to return `0`; that
|
|
|
|
/// indicates that the string itself cannot fit on the line and should
|
|
|
|
/// break onto the next one.
|
2020-05-21 03:11:44 +00:00
|
|
|
///
|
|
|
|
/// This function yields `None` if the line is not broken.
|
2020-03-13 05:10:52 +00:00
|
|
|
///
|
|
|
|
/// TODO: This function and, more generally, this entire file will need to
|
|
|
|
/// be internationalized to implement AS3 `flash.text.engine`.
|
2020-05-21 03:11:44 +00:00
|
|
|
pub fn wrap_line(
|
2020-03-13 05:10:52 +00:00
|
|
|
self,
|
|
|
|
text: &str,
|
2020-06-10 23:31:51 +00:00
|
|
|
params: EvalParameters,
|
2020-03-13 05:10:52 +00:00
|
|
|
width: Twips,
|
|
|
|
offset: Twips,
|
2020-06-07 01:59:44 +00:00
|
|
|
mut is_start_of_line: bool,
|
2020-05-21 03:11:44 +00:00
|
|
|
) -> Option<usize> {
|
2020-05-26 03:17:44 +00:00
|
|
|
let mut remaining_width = width - offset;
|
|
|
|
if remaining_width < Twips::from_pixels(0.0) {
|
|
|
|
return Some(0);
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:51:48 +00:00
|
|
|
let mut line_end = 0;
|
2020-01-21 00:23:00 +00:00
|
|
|
|
|
|
|
for word in text.split(' ') {
|
2020-01-21 19:33:17 +00:00
|
|
|
let word_start = word.as_ptr() as usize - text.as_ptr() as usize;
|
2020-05-21 03:11:44 +00:00
|
|
|
let word_end = word_start + word.len();
|
2020-01-21 00:23:00 +00:00
|
|
|
|
2020-06-02 23:04:17 +00:00
|
|
|
let measure = self.measure(
|
2020-12-15 21:38:48 +00:00
|
|
|
// +1 is fine because ' ' is 1 byte
|
2020-06-02 23:04:17 +00:00
|
|
|
text.get(word_start..word_end + 1).unwrap_or(word),
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-06-27 00:56:21 +00:00
|
|
|
false,
|
2020-06-02 23:04:17 +00:00
|
|
|
);
|
|
|
|
|
2020-06-07 01:59:44 +00:00
|
|
|
if is_start_of_line && measure.0 > remaining_width {
|
2020-01-21 00:23:00 +00:00
|
|
|
//Failsafe for if we get a word wider than the field.
|
2020-06-07 01:59:44 +00:00
|
|
|
let mut last_passing_breakpoint = (Twips::new(0), Twips::new(0));
|
2020-12-15 21:38:48 +00:00
|
|
|
|
|
|
|
let cur_slice = &text[word_start..];
|
|
|
|
let mut char_iter = cur_slice.char_indices();
|
|
|
|
let mut prev_char_index = word_start;
|
|
|
|
let mut prev_frag_end = 0;
|
|
|
|
|
|
|
|
char_iter.next(); // No need to check cur_slice[0..0]
|
2020-06-07 01:59:44 +00:00
|
|
|
while last_passing_breakpoint.0 < remaining_width {
|
2020-12-15 21:38:48 +00:00
|
|
|
prev_char_index = word_start + prev_frag_end;
|
|
|
|
|
|
|
|
if let Some((frag_end, _)) = char_iter.next() {
|
|
|
|
last_passing_breakpoint =
|
|
|
|
self.measure(&cur_slice[..frag_end], params, false);
|
|
|
|
|
|
|
|
prev_frag_end = frag_end;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2020-01-21 00:23:00 +00:00
|
|
|
}
|
2020-06-07 01:59:44 +00:00
|
|
|
|
2020-12-15 21:38:48 +00:00
|
|
|
return Some(prev_char_index);
|
2020-05-26 03:17:44 +00:00
|
|
|
} else if measure.0 > remaining_width {
|
|
|
|
//The word is wider than our remaining width, return the end of
|
|
|
|
//the line.
|
|
|
|
return Some(line_end);
|
2020-01-21 00:23:00 +00:00
|
|
|
} else {
|
2020-05-26 03:17:44 +00:00
|
|
|
//Space remains for our current word, move up the word pointer.
|
2020-06-11 22:51:48 +00:00
|
|
|
line_end = word_end;
|
|
|
|
is_start_of_line = is_start_of_line && text[0..line_end].trim().is_empty();
|
2020-05-26 01:25:22 +00:00
|
|
|
|
2020-05-26 03:17:44 +00:00
|
|
|
//If the additional space were to cause an overflow, then
|
|
|
|
//return now.
|
2020-06-02 23:04:17 +00:00
|
|
|
remaining_width -= measure.0;
|
2020-05-26 03:17:44 +00:00
|
|
|
if remaining_width < Twips::from_pixels(0.0) {
|
|
|
|
return Some(word_end);
|
|
|
|
}
|
2020-01-21 00:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 03:11:44 +00:00
|
|
|
None
|
2020-01-21 00:23:00 +00:00
|
|
|
}
|
2020-05-19 02:12:08 +00:00
|
|
|
|
|
|
|
pub fn descriptor(self) -> FontDescriptor {
|
|
|
|
self.0.descriptor.clone()
|
|
|
|
}
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
2019-10-08 02:27:31 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Glyph {
|
2020-09-14 20:49:08 +00:00
|
|
|
pub shape_handle: ShapeHandle,
|
|
|
|
pub shape: swf::Shape,
|
2019-10-08 02:27:31 +00:00
|
|
|
pub advance: i16,
|
|
|
|
}
|
2020-05-19 02:12:08 +00:00
|
|
|
|
|
|
|
/// Structure which identifies a particular font by name and properties.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Collect)]
|
|
|
|
#[collect(require_static)]
|
|
|
|
pub struct FontDescriptor {
|
|
|
|
name: String,
|
|
|
|
is_bold: bool,
|
|
|
|
is_italic: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FontDescriptor {
|
|
|
|
/// Obtain a font descriptor from a SWF font tag.
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn from_swf_tag(val: &swf::Font, encoding: &'static swf::Encoding) -> Self {
|
|
|
|
let name = val.name.to_string_lossy(encoding);
|
2020-06-11 02:42:17 +00:00
|
|
|
|
2020-05-19 02:12:08 +00:00
|
|
|
Self {
|
2020-06-11 02:42:17 +00:00
|
|
|
name,
|
2020-05-19 02:12:08 +00:00
|
|
|
is_bold: val.is_bold,
|
|
|
|
is_italic: val.is_italic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtain a font descriptor from a name/bold/italic triplet.
|
|
|
|
pub fn from_parts(name: &str, is_bold: bool, is_italic: bool) -> Self {
|
2020-06-11 02:42:17 +00:00
|
|
|
let mut name = name.to_string();
|
|
|
|
|
|
|
|
if let Some(first_null) = name.find('\0') {
|
|
|
|
name.truncate(first_null);
|
|
|
|
};
|
|
|
|
|
2020-05-19 02:12:08 +00:00
|
|
|
Self {
|
2020-06-11 02:42:17 +00:00
|
|
|
name,
|
2020-05-19 02:12:08 +00:00
|
|
|
is_bold,
|
|
|
|
is_italic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the name of the font class this descriptor references.
|
|
|
|
pub fn class(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the boldness of the described font.
|
|
|
|
pub fn bold(&self) -> bool {
|
|
|
|
self.is_bold
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the italic-ness of the described font.
|
|
|
|
pub fn italic(&self) -> bool {
|
|
|
|
self.is_italic
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 20:38:54 +00:00
|
|
|
|
2021-02-02 00:41:17 +00:00
|
|
|
/// The text rendering engine that a text field should use.
|
|
|
|
/// This is controlled by the "Anti-alias" setting in the Flash IDE.
|
|
|
|
/// Using "Anti-alias for readibility" switches to the "Advanced" text
|
|
|
|
/// rendering engine.
|
|
|
|
#[derive(Debug, PartialEq, Clone, Collect)]
|
|
|
|
#[collect(require_static)]
|
|
|
|
pub enum TextRenderSettings {
|
|
|
|
/// This text should render with the standard rendering engine.
|
|
|
|
/// Set via "Anti-alias for animation" in the Flash IDE.
|
|
|
|
Default,
|
|
|
|
|
|
|
|
/// This text should render with the advanced rendering engine.
|
|
|
|
/// Set via "Anti-alias for readibility" in the Flash IDE.
|
|
|
|
/// The parameters are set via the CSMTextSettings SWF tag.
|
|
|
|
/// Ruffle does not support this currently, but this also affects
|
|
|
|
/// hit-testing behavior.
|
|
|
|
Advanced {
|
|
|
|
grid_fit: TextGridFit,
|
|
|
|
thickness: f32,
|
|
|
|
sharpness: f32,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextRenderSettings {
|
|
|
|
pub fn is_advanced(&self) -> bool {
|
|
|
|
matches!(self, TextRenderSettings::Advanced { .. })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TextRenderSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
TextRenderSettings::Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<swf::CsmTextSettings> for TextRenderSettings {
|
|
|
|
fn from(settings: swf::CsmTextSettings) -> Self {
|
|
|
|
if settings.use_advanced_rendering {
|
|
|
|
TextRenderSettings::Advanced {
|
|
|
|
grid_fit: settings.grid_fit,
|
|
|
|
thickness: settings.thickness,
|
|
|
|
sharpness: settings.sharpness,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TextRenderSettings::Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:38:54 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::backend::render::{NullRenderer, RenderBackend};
|
2020-06-10 23:31:51 +00:00
|
|
|
use crate::font::{EvalParameters, Font};
|
2020-05-23 20:38:54 +00:00
|
|
|
use crate::player::{Player, DEVICE_FONT_TAG};
|
|
|
|
use gc_arena::{rootless_arena, MutationContext};
|
2020-07-23 03:18:30 +00:00
|
|
|
use std::ops::DerefMut;
|
2020-05-23 20:38:54 +00:00
|
|
|
use swf::Twips;
|
|
|
|
|
|
|
|
fn with_device_font<F>(callback: F)
|
|
|
|
where
|
|
|
|
F: for<'gc> FnOnce(MutationContext<'gc, '_>, Font<'gc>),
|
|
|
|
{
|
|
|
|
rootless_arena(|mc| {
|
|
|
|
let mut renderer: Box<dyn RenderBackend> = Box::new(NullRenderer::new());
|
2020-07-23 03:18:30 +00:00
|
|
|
let device_font =
|
|
|
|
Player::load_device_font(mc, DEVICE_FONT_TAG, renderer.deref_mut()).unwrap();
|
2020-05-23 20:38:54 +00:00
|
|
|
|
|
|
|
callback(mc, device_font);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wrap_line_no_breakpoint() {
|
|
|
|
with_device_font(|_mc, df| {
|
2020-06-10 23:31:51 +00:00
|
|
|
let params =
|
|
|
|
EvalParameters::from_parts(Twips::from_pixels(12.0), Twips::from_pixels(0.0), true);
|
2020-05-23 20:38:54 +00:00
|
|
|
let string = "abcdefghijklmnopqrstuv";
|
|
|
|
let breakpoint = df.wrap_line(
|
|
|
|
&string,
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(200.0),
|
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(None, breakpoint);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wrap_line_breakpoint_every_word() {
|
|
|
|
with_device_font(|_mc, df| {
|
2020-06-10 23:31:51 +00:00
|
|
|
let params =
|
|
|
|
EvalParameters::from_parts(Twips::from_pixels(12.0), Twips::from_pixels(0.0), true);
|
2020-05-23 20:38:54 +00:00
|
|
|
let string = "abcd efgh ijkl mnop";
|
|
|
|
let mut last_bp = 0;
|
|
|
|
let breakpoint = df.wrap_line(
|
|
|
|
&string,
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-05-26 03:50:08 +00:00
|
|
|
Twips::from_pixels(35.0),
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(Some(4), breakpoint);
|
|
|
|
|
|
|
|
last_bp += breakpoint.unwrap() + 1;
|
|
|
|
|
|
|
|
let breakpoint2 = df.wrap_line(
|
|
|
|
&string[last_bp..],
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-05-26 03:50:08 +00:00
|
|
|
Twips::from_pixels(35.0),
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(Some(4), breakpoint2);
|
|
|
|
|
|
|
|
last_bp += breakpoint2.unwrap() + 1;
|
|
|
|
|
|
|
|
let breakpoint3 = df.wrap_line(
|
|
|
|
&string[last_bp..],
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-05-26 03:50:08 +00:00
|
|
|
Twips::from_pixels(35.0),
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(Some(4), breakpoint3);
|
|
|
|
|
|
|
|
last_bp += breakpoint3.unwrap() + 1;
|
|
|
|
|
|
|
|
let breakpoint4 = df.wrap_line(
|
|
|
|
&string[last_bp..],
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-05-26 03:50:08 +00:00
|
|
|
Twips::from_pixels(35.0),
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(None, breakpoint4);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-26 03:17:44 +00:00
|
|
|
#[test]
|
|
|
|
fn wrap_line_breakpoint_no_room() {
|
|
|
|
with_device_font(|_mc, df| {
|
2020-06-10 23:31:51 +00:00
|
|
|
let params =
|
|
|
|
EvalParameters::from_parts(Twips::from_pixels(12.0), Twips::from_pixels(0.0), true);
|
2020-05-26 03:17:44 +00:00
|
|
|
let string = "abcd efgh ijkl mnop";
|
|
|
|
let breakpoint = df.wrap_line(
|
|
|
|
&string,
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-05-26 03:17:44 +00:00
|
|
|
Twips::from_pixels(30.0),
|
|
|
|
Twips::from_pixels(29.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
false,
|
2020-05-26 03:17:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(Some(0), breakpoint);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:38:54 +00:00
|
|
|
#[test]
|
|
|
|
fn wrap_line_breakpoint_irregular_sized_words() {
|
|
|
|
with_device_font(|_mc, df| {
|
2020-06-10 23:31:51 +00:00
|
|
|
let params =
|
|
|
|
EvalParameters::from_parts(Twips::from_pixels(12.0), Twips::from_pixels(0.0), true);
|
2020-06-02 23:04:17 +00:00
|
|
|
let string = "abcdi j kl mnop q rstuv";
|
2020-05-23 20:38:54 +00:00
|
|
|
let mut last_bp = 0;
|
|
|
|
let breakpoint = df.wrap_line(
|
|
|
|
&string,
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-06-07 01:59:44 +00:00
|
|
|
Twips::from_pixels(37.0),
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
2020-06-02 23:04:17 +00:00
|
|
|
assert_eq!(Some(5), breakpoint);
|
2020-05-23 20:38:54 +00:00
|
|
|
|
|
|
|
last_bp += breakpoint.unwrap() + 1;
|
|
|
|
|
|
|
|
let breakpoint2 = df.wrap_line(
|
|
|
|
&string[last_bp..],
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-06-07 01:59:44 +00:00
|
|
|
Twips::from_pixels(37.0),
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
2020-05-26 01:25:22 +00:00
|
|
|
assert_eq!(Some(4), breakpoint2);
|
2020-05-23 20:38:54 +00:00
|
|
|
|
|
|
|
last_bp += breakpoint2.unwrap() + 1;
|
|
|
|
|
|
|
|
let breakpoint3 = df.wrap_line(
|
|
|
|
&string[last_bp..],
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-06-07 01:59:44 +00:00
|
|
|
Twips::from_pixels(37.0),
|
2020-05-23 20:38:54 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-23 20:38:54 +00:00
|
|
|
);
|
|
|
|
|
2020-05-26 01:25:22 +00:00
|
|
|
assert_eq!(Some(4), breakpoint3);
|
|
|
|
|
|
|
|
last_bp += breakpoint3.unwrap() + 1;
|
|
|
|
|
|
|
|
let breakpoint4 = df.wrap_line(
|
|
|
|
&string[last_bp..],
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-06-07 01:59:44 +00:00
|
|
|
Twips::from_pixels(37.0),
|
2020-05-26 01:25:22 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-26 01:25:22 +00:00
|
|
|
);
|
|
|
|
|
2020-05-26 03:50:08 +00:00
|
|
|
assert_eq!(Some(1), breakpoint4);
|
|
|
|
|
|
|
|
last_bp += breakpoint4.unwrap() + 1;
|
|
|
|
|
|
|
|
let breakpoint5 = df.wrap_line(
|
|
|
|
&string[last_bp..],
|
2020-06-10 23:31:51 +00:00
|
|
|
params,
|
2020-06-07 01:59:44 +00:00
|
|
|
Twips::from_pixels(37.0),
|
2020-05-26 03:50:08 +00:00
|
|
|
Twips::from_pixels(0.0),
|
2020-06-07 01:59:44 +00:00
|
|
|
true,
|
2020-05-26 03:50:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(None, breakpoint5);
|
2020-05-23 20:38:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|