Skip to content
Draft
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
17 changes: 11 additions & 6 deletions Cargo.lock

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

6 changes: 4 additions & 2 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 Expand Up @@ -105,8 +106,9 @@ float_cmp = "allow"
# into the parent directory of fframes (as a sibling) and uncomment the following lines:
# svgr = { path = "../svgr/crates/svgr" }
# usvgr = { path = "../svgr/crates/usvgr" }
svgr = "0.50.3"
usvgr = "0.50.3"
# Until https://github.com/dmtrKovalenko/svgr/pull/26 is released:
svgr = { git = "https://github.com/silouanwright/svgr", branch = "feat/shaped-text-measure" }
usvgr = { git = "https://github.com/silouanwright/svgr", branch = "feat/shaped-text-measure" }
# ffmpeg-sys-next with prebuilt FFmpeg binaries: https://github.com/dmtrKovalenko/rust-ffmpeg-sys
ffmpeg-sys-fframes = "9.0.0"
wasm-bindgen = "0.2.100"
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 modified e2e/snapshots/fframes-intro/outroscene_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());
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 fontdb(&self) -> Option<&fframes::usvgr::fontdb::Database> {
Some(&self.fontdb)
}

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
7 changes: 7 additions & 0 deletions fframes-skia-renderer/src/instant_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ impl<TBackend: SkiaBackend> SkiaFFramesRenderer<'_, TBackend> {
fframes_ctx.video_decoders.clone(),
);

if native_ctx.converter_cache.usvgr_shape_cache.is_none() {
native_ctx.converter_cache.usvgr_shape_cache = fframes_ctx
.break_lines_cache
.as_ref()
.map(TextCache::shapes);
}

let tree = fframes::render_frame_guarded(video, fframe, &ctx)?.into_svg_tree(
&fframes_ctx.usvg_options,
&mut native_ctx.converter_cache,
Expand Down
3 changes: 2 additions & 1 deletion fframes-skia-renderer/src/skia_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ fn generate_frames<'a, 'media: 'a, TVideo: Video + Sync + Send>(
#[cfg(feature = "debug")] metrics: Arc<crate::metrics::ThreadMetrics>,
) -> FFramesRendererResult<()> {
let break_lines_cache = TextCache::new(10);
let mut converter_cache = usvgr::Cache::new_with_text_cache(text_cache_capacity);
let mut converter_cache =
TextCache::converter_cache(break_lines_cache.as_ref(), text_cache_capacity);
// x2 because sometimes we might need to decode 2 frames at once
let video_decoders_worker = VideoDecodersWorker::new(queue_size * 2);

Expand Down
4 changes: 2 additions & 2 deletions fframes/src/captions/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl Captions {
fn prepare(
&self,
track: &CaptionTrack,
_frame: &mut Frame,
frame: &mut Frame,
ctx: &FFramesContext<'_, '_>,
) -> Result<PreparedTrack, CaptionError> {
if self.font_size == 0 || !self.line_height.is_finite() || self.line_height < 1.0 {
Expand Down Expand Up @@ -402,7 +402,7 @@ impl Captions {
CaptionError::invalid("layout.font", "caption layout needs a font source")
})?;
let measure = |text: &str| {
crate::text_get_width_impl(self.font_query(), text, font_source)
crate::text_get_width_impl(self.font_query(), text, font_source, frame.text_shapes())
.map(|width| width as f64)
.ok_or_else(|| {
CaptionError::invalid(
Expand Down
7 changes: 7 additions & 0 deletions fframes/src/font_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ pub trait FontSource<'a>: Sync + Send + std::fmt::Debug {
font_style: FontStyle,
font_stretch: FontStretch,
) -> Option<Box<dyn FontFace<'a> + 'a>>;

/// The database the renderer takes these fonts from. With it text is measured the way the
/// renderer lays it out (kerning, fallback fonts, font variations); `None` falls back to
/// per-character advances.
fn fontdb(&self) -> Option<&usvgr::fontdb::Database> {
None
}
}
Loading
Loading