Skip to content

Commit

Permalink
Merge pull request #495 from CryZe/custom-text-engine
Browse files Browse the repository at this point in the history
Allow Customizing the Text Engine
  • Loading branch information
CryZe authored Dec 28, 2021
2 parents 6f1c467 + 45b9201 commit 85a4a34
Show file tree
Hide file tree
Showing 30 changed files with 1,407 additions and 958 deletions.
27 changes: 13 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ include = [
"/LICENSE-MIT",
"/README.md",
]
edition = "2018"
edition = "2021"
resolver = "2"

[package.metadata.docs.rs]
Expand Down Expand Up @@ -63,15 +63,20 @@ serde_json = { version = "1.0.8", optional = true }
utf-8 = { version = "0.7.4", optional = true }

# Rendering
ahash = { version = "0.7.0", default-features = false, optional = true }
# Currently doesn't require any additional dependencies.

# Path-based Text Engine
rustybuzz = { version = "0.4.0", optional = true }
ttf-parser = { version = "0.12.0", optional = true }
ttf-parser = { version = "0.12.3", optional = true }

# Font Loading
font-kit = { version = "0.10.0", optional = true }

# Software Rendering
tiny-skia = { version = "0.6.0", default-features = false, features = ["std", "simd"], optional = true }
tiny-skia = { version = "0.6.0", default-features = false, features = [
"std",
"simd",
], optional = true }

# Networking
splits-io-api = { version = "0.2.0", optional = true }
Expand Down Expand Up @@ -127,16 +132,10 @@ more-image-formats = [
"image/webp",
]
image-shrinking = ["std", "bytemuck", "more-image-formats"]
rendering = [
"ahash",
"bytemuck/derive",
"more-image-formats",
"rustybuzz",
"std",
"ttf-parser",
]
font-loading = ["std", "rendering", "font-kit"]
software-rendering = ["rendering", "tiny-skia"]
rendering = ["bytemuck/derive", "more-image-formats"]
path-based-text-engine = ["std", "rendering", "rustybuzz", "ttf-parser"]
font-loading = ["std", "path-based-text-engine", "font-kit"]
software-rendering = ["path-based-text-engine", "tiny-skia"]
wasm-web = [
"js-sys",
"livesplit-hotkey/wasm-web",
Expand Down
42 changes: 39 additions & 3 deletions benches/scene_management.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use livesplit_core::rendering::SharedOwnership;

cfg_if::cfg_if! {
if #[cfg(feature = "rendering")] {
use criterion::{criterion_group, criterion_main, Criterion};
use livesplit_core::{
layout::{self, Layout},
rendering::{PathBuilder, ResourceAllocator, SceneManager},
rendering::{PathBuilder, ResourceAllocator, SceneManager, Label, FontKind},
run::parser::livesplit,
settings::Font,
Run, Segment, TimeSpan, Timer, TimingMethod,
};
use std::{fs::File, io::BufReader};
Expand All @@ -14,26 +17,59 @@ cfg_if::cfg_if! {

struct Dummy;

impl PathBuilder<Dummy> for Dummy {
impl PathBuilder for Dummy {
type Path = ();

fn move_to(&mut self, _: f32, _: f32) {}
fn line_to(&mut self, _: f32, _: f32) {}
fn quad_to(&mut self, _: f32, _: f32, _: f32, _: f32) {}
fn curve_to(&mut self, _: f32, _: f32, _: f32, _: f32, _: f32, _: f32) {}
fn close(&mut self) {}
fn finish(self, _: &mut Dummy) -> Self::Path {}
fn finish(self) -> Self::Path {}
}

impl ResourceAllocator for Dummy {
type PathBuilder = Dummy;
type Path = ();
type Image = ();
type Font = ();
type Label = Dummy;

fn path_builder(&mut self) -> Self::PathBuilder {
Dummy
}
fn create_image(&mut self, _: u32, _: u32, _: &[u8]) -> Self::Image {}
fn create_font(&mut self, _: Option<&Font>, _: FontKind) -> Self::Font {}
fn create_label(
&mut self,
_: &str,
_: &mut Self::Font,
_: Option<f32>,
) -> Self::Label {
Dummy
}
fn update_label(
&mut self,
_: &mut Self::Label,
_: &str,
_: &mut Self::Font,
_: Option<f32>,
) {}
}

impl Label for Dummy {
fn width(&self, _: f32) -> f32 {
0.0
}
fn width_without_max_width(&self, _: f32) -> f32 {
0.0
}
}

impl SharedOwnership for Dummy {
fn share(&self) -> Self {
Dummy
}
}

fn default(c: &mut Criterion) {
Expand Down
16 changes: 9 additions & 7 deletions src/layout/component.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use super::{ComponentSettings, ComponentState, GeneralSettings};
use crate::component::{
blank_space, current_comparison, current_pace, delta, detailed_timer, graph, pb_chance,
possible_time_save, previous_segment, segment_time, separator, splits, sum_of_best, text,
timer, title, total_playtime,
use crate::{
component::{
blank_space, current_comparison, current_pace, delta, detailed_timer, graph, pb_chance,
possible_time_save, previous_segment, segment_time, separator, splits, sum_of_best, text,
timer, title, total_playtime,
},
platform::prelude::*,
settings::{SettingsDescription, Value},
timing::Snapshot,
};
use crate::platform::prelude::*;
use crate::settings::{SettingsDescription, Value};
use crate::timing::Snapshot;
use alloc::borrow::Cow;

/// A Component provides information about a run in a way that is easy to
Expand Down
36 changes: 18 additions & 18 deletions src/rendering/component/detailed_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@ use crate::{
rendering::{
component::timer,
consts::{vertical_padding, BOTH_PADDINGS, PADDING},
font::Label,
font::CachedLabel,
icon::Icon,
resource::ResourceAllocator,
scene::Layer,
solid, RenderContext,
},
};

pub struct Cache<I> {
pub struct Cache<I, L> {
icon: Option<Icon<I>>,
timer: timer::Cache<I>,
segment_timer: timer::Cache<I>,
segment_name: Label,
comparison1_name: Label,
comparison2_name: Label,
comparison1_time: Label,
comparison2_time: Label,
timer: timer::Cache<I, L>,
segment_timer: timer::Cache<I, L>,
segment_name: CachedLabel<L>,
comparison1_name: CachedLabel<L>,
comparison2_name: CachedLabel<L>,
comparison1_time: CachedLabel<L>,
comparison2_time: CachedLabel<L>,
}

impl<I> Cache<I> {
impl<I, L> Cache<I, L> {
pub const fn new() -> Self {
Self {
icon: None,
timer: timer::Cache::new(),
segment_timer: timer::Cache::new(),
segment_name: Label::new(),
comparison1_name: Label::new(),
comparison2_name: Label::new(),
comparison1_time: Label::new(),
comparison2_time: Label::new(),
segment_name: CachedLabel::new(),
comparison1_name: CachedLabel::new(),
comparison2_name: CachedLabel::new(),
comparison1_time: CachedLabel::new(),
comparison2_time: CachedLabel::new(),
}
}
}

pub(in crate::rendering) fn render<B: ResourceAllocator>(
cache: &mut Cache<B::Image>,
context: &mut RenderContext<'_, B>,
pub(in crate::rendering) fn render<A: ResourceAllocator>(
cache: &mut Cache<A::Image, A::Label>,
context: &mut RenderContext<'_, A>,
[width, height]: [f32; 2],
component: &State,
layout_state: &LayoutState,
Expand Down
6 changes: 3 additions & 3 deletions src/rendering/component/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub(in crate::rendering) fn render(
builder.line_to(width * p2.x, p2.y);
builder.line_to(width * p2.x, component.middle);
builder.close();
let partial_fill_path = builder.finish(&mut context.handles);
let partial_fill_path = builder.finish();
context.top_layer_path(partial_fill_path, component.partial_fill_color);

component.points.len() - 1
Expand All @@ -71,7 +71,7 @@ pub(in crate::rendering) fn render(
}
builder.line_to(width * component.points[len - 1].x, component.middle);
builder.close();
let fill_path = builder.finish(&mut context.handles);
let fill_path = builder.finish();
context.top_layer_path(fill_path, component.complete_fill_color);

for points in component.points.windows(2) {
Expand All @@ -85,7 +85,7 @@ pub(in crate::rendering) fn render(
component.graph_lines_color
};

let line_path = builder.finish(&mut context.handles);
let line_path = builder.finish();
context.top_layer_stroke_path(line_path, color, LINE_WIDTH);
}

Expand Down
20 changes: 10 additions & 10 deletions src/rendering/component/key_value.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
use std::marker::PhantomData;
use core::marker::PhantomData;

use crate::{
component::key_value::State,
layout::{LayoutDirection, LayoutState},
rendering::{
font::{AbbreviatedLabel, Label},
font::{AbbreviatedLabel, CachedLabel},
resource::ResourceAllocator,
RenderContext,
},
};

pub struct Cache<I> {
key: AbbreviatedLabel,
value: Label,
pub struct Cache<I, L> {
key: AbbreviatedLabel<L>,
value: CachedLabel<L>,
_image: PhantomData<I>,
}

impl<I> Cache<I> {
impl<I, L> Cache<I, L> {
pub const fn new() -> Self {
Self {
key: AbbreviatedLabel::new(),
value: Label::new(),
value: CachedLabel::new(),
_image: PhantomData,
}
}
}

pub(in crate::rendering) fn render<B: ResourceAllocator>(
cache: &mut Cache<B::Image>,
context: &mut RenderContext<'_, B>,
pub(in crate::rendering) fn render<A: ResourceAllocator>(
cache: &mut Cache<A::Image, A::Label>,
context: &mut RenderContext<'_, A>,
dim: [f32; 2],
component: &State,
layout_state: &LayoutState,
Expand Down
20 changes: 10 additions & 10 deletions src/rendering/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ pub mod text;
pub mod timer;
pub mod title;

pub enum Cache<I> {
pub enum Cache<I, L> {
Empty,
DetailedTimer(detailed_timer::Cache<I>),
KeyValue(key_value::Cache<I>),
Splits(splits::Cache<I>),
Text(text::Cache<I>),
Timer(timer::Cache<I>),
Title(title::Cache<I>),
DetailedTimer(detailed_timer::Cache<I, L>),
KeyValue(key_value::Cache<I, L>),
Splits(splits::Cache<I, L>),
Text(text::Cache<I, L>),
Timer(timer::Cache<I, L>),
Title(title::Cache<I, L>),
}

macro_rules! accessors {
($($variant:ident $module:ident),*) => {
$(
fn $module(&mut self) -> &mut $module::Cache<I> {
fn $module(&mut self) -> &mut $module::Cache<I, L> {
match self {
Self::$variant(c) => c,
_ => {
Expand All @@ -42,7 +42,7 @@ macro_rules! accessors {
};
}

impl<I> Cache<I> {
impl<I, L> Cache<I, L> {
pub const fn new(component: &ComponentState) -> Self {
match component {
ComponentState::DetailedTimer(_) => Self::DetailedTimer(detailed_timer::Cache::new()),
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn height(component: &ComponentState) -> f32 {
}

pub(super) fn render<A: ResourceAllocator>(
cache: &mut Cache<A::Image>,
cache: &mut Cache<A::Image, A::Label>,
context: &mut RenderContext<'_, A>,
component: &ComponentState,
state: &LayoutState,
Expand Down
Loading

0 comments on commit 85a4a34

Please sign in to comment.