Skip to content

Commit

Permalink
Add adapter for cosmic.
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinMReppert committed Dec 1, 2024
1 parent 410b947 commit 0e78992
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions examples/scenes/src/cosmic_text_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ use unicode_segmentation::UnicodeSegmentation;

use std::cmp;
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::Arc;

pub struct CosmicData {

}
pub struct CosmicData {}

impl TestScene for CosmicTextSceneState {
fn render(&mut self, scene: &mut Scene, _scene_params: &mut SceneParams) {
Expand Down Expand Up @@ -81,6 +80,23 @@ impl Default for CosmicTextSceneState {
}
}

struct CosmicFontBlobAdapter {
font: Arc<cosmic_text::Font>,
}

/// Adapter to allow `cosmic_text::Font` to be used as a Blob.
impl CosmicFontBlobAdapter {
fn new(font: Arc<cosmic_text::Font>) -> Self {
Self { font }
}
}

impl AsRef<[u8]> for CosmicFontBlobAdapter {
fn as_ref(&self) -> &[u8] {
self.font.data()
}
}

impl CosmicTextSceneState {
pub fn new() -> Self {
let mut font_system = FontSystem::new();
Expand All @@ -95,9 +111,9 @@ impl CosmicTextSceneState {

for (font_id, index) in font_faces {
if let Some(font) = font_system.get_font(font_id) {
let resource = Arc::new(font.data().to_vec());
let font_blob = Blob::new(resource);
Blob::as_ref(&font_blob);
// For now use an adapter, to avoid cloning the entire font data.
// For alternatives, see https://github.com/linebender/vello/pull/739#discussion_r1858293718
let font_blob = Blob::new(Arc::new(CosmicFontBlobAdapter::new(font)));
let vello_font = Font::new(font_blob, index);
vello_fonts.insert(font_id, vello_font);
}
Expand Down Expand Up @@ -334,7 +350,9 @@ fn create_glyphs(
}

if let Some((last_font, last_glyph_color)) = last_font {
if last_font != glyph.font_id || last_glyph_color.components != glyph_color.components {
if last_font != glyph.font_id
|| last_glyph_color.components != glyph_color.components
{
buffer_line.glyph_runs.push(BufferGlyphRun {
font: last_font,
glyphs: current_glyphs,
Expand Down

0 comments on commit 0e78992

Please sign in to comment.