Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ members = [
"examples/neon-triangle",
"examples/neon-triangle/editor/editor-bridge",
"examples/arm",
"examples/text-width",

# maybe clean this up?
# "fframes-lyon-renderer",
Expand Down
Binary file modified e2e/snapshots/captions/1.5s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified e2e/snapshots/captions/5.2s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified e2e/snapshots/captions/7s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added e2e/snapshots/text-width/0.5s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions examples/text-width/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "text-width-example"
version = "1.4.1"
edition = "2024"
publish = false

[dependencies]
fframes = { workspace = true, features = ["cli", "compile-time-svgtree"] }

[features]
default = ["skia"]
skia = ["dep:fframes_skia_renderer"]

[target.'cfg(target_os = "macos")'.dependencies]
fframes_skia_renderer = { workspace = true, optional = true, features = ["cli", "metal"] }

[target.'cfg(not(any(target_os = "macos", target_arch = "wasm32")))'.dependencies]
fframes_skia_renderer = { workspace = true, optional = true, features = ["cli", "vulkan"] }

[target.'cfg(not(any(windows, target_arch = "wasm32")))'.dependencies]
fframes = { workspace = true, features = ["h264", "libav-agree-gpl", "build-portable"] }

[lints]
workspace = true
Binary file not shown.
Binary file not shown.
117 changes: 117 additions & 0 deletions examples/text-width/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! Boxes sized by `text_width`, a box filled by `text_fit` and a paragraph wrapped by
//! `text_break_lines`: each box should hug its glyphs, and no wrapped line should cross the
//! paragraph's right edge. The right column covers a variable font at two weights and Arabic
//! laid out with a fallback font inside Latin text.
use fframes::{
AudioMap, BreakLinesOpts, Duration, FFramesContext, FontQuery, Frame, Svgr, TextAlign,
TextOverflow, Video, include_media_dir, svgr,
};

include_media_dir!(pub struct InterMedia, "examples/arm/media");
mod own {
// Sofia Sans Semi Condensed (variable weight) and Noto Sans Arabic cut down to "سلام".
fframes::include_media_dir!(pub struct TextWidthMedia, "examples/text-width/media");
}
pub use own::TextWidthMedia;

const INTER: &str = "Inter 18pt";
const SOFIA: &str = "Sofia Sans Semi Condensed";
const PARAGRAPH_WIDTH: usize = 760;
const FIT_WIDTH: usize = 550;

pub struct TextWidthVideo;

fn font(family: &str, size: usize, weight: u16) -> FontQuery<'_> {
FontQuery {
family,
size,
weight,
..Default::default()
}
}

fn measured<'a>(
frame: &mut Frame,
ctx: &FFramesContext<'a, '_>,
font: FontQuery<'a>,
text: &'a str,
(x, y): (f64, f64),
) -> Svgr<'a> {
let width = frame.text_width(ctx, font, text).unwrap_or(0);
let size = font.size as f64;
svgr!(<g>
<rect x={x} y={y - size * 0.8} width={width} height={size} fill="#1f3a5f" />
<text x={x} y={y} font-family={font.family} font-weight={font.weight} font-size={size} fill="#f4f7fb">{text}</text>
</g>)
}

impl Video for TextWidthVideo {
const FPS: usize = 30;
const WIDTH: usize = 1920;
const HEIGHT: usize = 1080;

fn duration(&self) -> Duration<'_> {
Duration::Seconds(1.0)
}

fn audio(&self) -> AudioMap<'_> {
AudioMap::none()
}

fn render_frame<'a>(&'a self, mut frame: Frame, ctx: &FFramesContext<'a, '_>) -> Svgr<'a> {
let rows = [
(font(INTER, 100, 800), "AVAVAVAV WAVE", (120.0, 260.0)),
(
font(INTER, 46, 800),
"δεν είναι ανατολική ρωμαϊκή αυτοκρατορία",
(120.0, 420.0),
),
(
font(INTER, 46, 800),
"The Byzantine Greeks call Eastern Rome",
(120.0, 520.0),
),
(font(SOFIA, 64, 100), "Hamburgefonts WAVE", (1040.0, 660.0)),
(font(SOFIA, 64, 900), "Hamburgefonts WAVE", (1040.0, 760.0)),
(font(INTER, 64, 800), "AV سلام WAVE", (1040.0, 860.0)),
]
.into_iter()
.map(|(font, text, at)| measured(&mut frame, ctx, font, text, at))
.collect::<Vec<_>>();
let fitted = frame
.text_fit(
ctx,
font(INTER, 64, 800),
"Eastern Rome, Byzantium",
FIT_WIDTH,
TextOverflow::Ellipsis,
)
.unwrap_or_default()
.into_owned();
let paragraph = frame.text_break_lines(
ctx,
"Η Ανατολική Ρωμαϊκή Αυτοκρατορία ονομάστηκε αργότερα Βυζάντιο. Later historians named the Eastern Roman Empire Byzantium.",
BreakLinesOpts {
font: font(INTER, 40, 800),
width: PARAGRAPH_WIDTH,
line_height: 1.25,
x: 120,
y: 660,
align: TextAlign::Left,
fill: "#f4f7fb",
..Default::default()
},
);
svgr!(
<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080">
<rect width="1920" height="1080" fill="#0c1423" />
<text x="120" y="120" font-family={INTER} font-weight="800" font-size="40" fill="#91a3b9">"text_width, text_fit and text_break_lines"</text>
{rows}
<rect x="1040" y="209" width={FIT_WIDTH} height="64" fill="none" stroke="#e76f51" stroke-width="2" />
<text x="1040" y="260" font-family={INTER} font-weight="800" font-size="64" fill="#f4f7fb">{fitted}</text>
<path d={format!("M {} 600 V 1000", 120 + PARAGRAPH_WIDTH)} stroke="#e76f51" stroke-width="2" />
{paragraph}
</svg>
)
}
}
25 changes: 25 additions & 0 deletions examples/text-width/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use fframes::{CombinedMediaProvider, MediaProvider, RenderOptions, cli};
use std::process::ExitCode;
use text_width_example::{InterMedia, TextWidthMedia, TextWidthVideo};

fn main() -> ExitCode {
let (inter, own) = match (InterMedia::new(), TextWidthMedia::new()) {
(Ok(inter), Ok(own)) => (inter, own),
(Err(error), _) | (_, Err(error)) => {
eprintln!("{error}");
return ExitCode::FAILURE;
}
};
let media = CombinedMediaProvider::from([&inter as &dyn MediaProvider, &own]);
let video = TextWidthVideo;
let runner = cli::new(
&video,
RenderOptions {
media: Some(&media),
..Default::default()
},
);
#[cfg(feature = "skia")]
let runner = runner.renderers(fframes_skia_renderer::cli::SkiaRenderers::default());
runner.run()
}
2 changes: 2 additions & 0 deletions fframes-editor-controller/src/wasm_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ impl<TVideo: Video, TMedia: StaticMediaProvider<'static> + 'static> WasmEditor<T
pub fn ingest_font(&self, slice: &[u8]) -> JsValue {
let mut fonts = self.fonts.lock().unwrap();
let face_info = fonts.insert_font(slice.to_vec().into(), None);
// Cache keys name the font, not its bytes, so a replaced font would keep old widths.
self.break_lines_cache.clear();

serde_wasm_bindgen::to_value(&face_info).unwrap()
}
Expand Down
22 changes: 22 additions & 0 deletions fframes-editor-controller/src/wasm_font_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ impl StaticFontFace {
pub struct WasmFontSource {
pub static_fonts: Vec<StaticFontFace>,
pub data: HashMap<FaceInfo, Cow<'static, [u8]>>,
/// The same fonts for measuring text the way the native renderer lays it out.
pub fontdb: fframes::usvgr::fontdb::Database,
/// Faces each font added to `fontdb`, replaced when the font is inserted again.
face_ids: HashMap<FaceInfo, Vec<fframes::usvgr::fontdb::ID>>,
}

impl WasmFontSource {
pub fn new() -> Self {
Self {
static_fonts: vec![],
data: HashMap::new(),
fontdb: fframes::usvgr::fontdb::Database::new(),
face_ids: HashMap::new(),
}
}

Expand Down Expand Up @@ -81,6 +87,18 @@ impl WasmFontSource {
})
}

// Static fonts are shared with fontdb, not copied.
let shared: Arc<dyn AsRef<[u8]> + Send + Sync> = match &data {
Cow::Borrowed(bytes) => Arc::new(*bytes),
Cow::Owned(bytes) => Arc::new(bytes.clone()),
};
for id in self.face_ids.remove(&face_info).into_iter().flatten() {
self.fontdb.remove_face(id);
}
let ids = self
.fontdb
.load_font_source(fframes::usvgr::fontdb::Source::Binary(shared));
self.face_ids.insert(face_info.clone(), ids.to_vec());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
self.data.insert(face_info.clone(), data);
Some(face_info)
}
Expand Down Expand Up @@ -137,6 +155,10 @@ impl<'a> fframes::FontSource<'a> for WasmFontSource {
}))
}

fn measure_text(&self, font: &fframes::FontQuery, text: &str) -> Option<f32> {
fframes::measure_text_width(&self.fontdb, font, text)
}

fn add_font(&mut self, _filename: String, _font_data: Arc<dyn AsRef<[u8]> + Sync + Send>) {
unimplemented!(
"Adding fonts for wasm font source must be done through WasmFontSource::insert_font api"
Expand Down
6 changes: 6 additions & 0 deletions fframes/src/font_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,10 @@ pub trait FontSource<'a>: Sync + Send + std::fmt::Debug {
font_style: FontStyle,
font_stretch: FontStretch,
) -> Option<Box<dyn FontFace<'a> + 'a>>;

/// Width of `text` in pixels as the renderer lays it out, see
/// [`crate::measure_text_width`]. `None` falls back to per-character advances.
fn measure_text(&self, _font: &crate::FontQuery, _text: &str) -> Option<f32> {
None
}
}
Loading