mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Migrate fonts to be stored as resources (#4165)
* Good start
* Impl
* allow responses.add(async { Message::NoOp });
* Fix
* Cache font blob in text node
* Refactor font handling to use Font struct direcly
* Fix fmt
* Embedd Font Resources by default
* Review
* Review
* Cache font info based on ResourceHash
This commit is contained in:
@@ -6,7 +6,6 @@ use std::hash::{Hash, Hasher};
|
||||
use std::ptr::addr_of;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use text_nodes::FontCache;
|
||||
use vector_types::vector::style::RenderMode;
|
||||
|
||||
pub use graphene_resource as resource;
|
||||
@@ -126,8 +125,6 @@ impl GetEditorPreferences for DummyPreferences {
|
||||
}
|
||||
|
||||
pub struct EditorApi<Io> {
|
||||
/// Font data (for rendering text) made available to the graph through the `PlatformEditorApi`.
|
||||
pub font_cache: FontCache,
|
||||
/// Gives access to APIs like resources.
|
||||
pub application_io: Option<Arc<Io>>,
|
||||
pub node_graph_message_sender: Box<dyn NodeGraphUpdateSender + Send + Sync>,
|
||||
@@ -140,7 +137,6 @@ impl<Io> Eq for EditorApi<Io> {}
|
||||
impl<Io: Default> Default for EditorApi<Io> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
font_cache: FontCache::default(),
|
||||
application_io: None,
|
||||
node_graph_message_sender: Box::new(Logger),
|
||||
editor_preferences: Box::new(DummyPreferences),
|
||||
@@ -150,7 +146,6 @@ impl<Io: Default> Default for EditorApi<Io> {
|
||||
|
||||
impl<Io> Hash for EditorApi<Io> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.font_cache.hash(state);
|
||||
self.application_io.as_ref().map_or(0, |io| io as *const _ as usize).hash(state);
|
||||
(self.node_graph_message_sender.as_ref() as *const dyn NodeGraphUpdateSender).hash(state);
|
||||
(self.editor_preferences.as_ref() as *const dyn GetEditorPreferences).hash(state);
|
||||
@@ -165,8 +160,7 @@ impl<Io> core_types::graphene_hash::CacheHash for EditorApi<Io> {
|
||||
|
||||
impl<Io> PartialEq for EditorApi<Io> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.font_cache == other.font_cache
|
||||
&& self.application_io.as_ref().map_or(0, |io| addr_of!(io) as usize) == other.application_io.as_ref().map_or(0, |io| addr_of!(io) as usize)
|
||||
self.application_io.as_ref().map_or(0, |io| addr_of!(io) as usize) == other.application_io.as_ref().map_or(0, |io| addr_of!(io) as usize)
|
||||
&& std::ptr::eq(self.node_graph_message_sender.as_ref() as *const _, other.node_graph_message_sender.as_ref() as *const _)
|
||||
&& std::ptr::eq(self.editor_preferences.as_ref() as *const _, other.editor_preferences.as_ref() as *const _)
|
||||
}
|
||||
@@ -174,7 +168,7 @@ impl<Io> PartialEq for EditorApi<Io> {
|
||||
|
||||
impl<T> Debug for EditorApi<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("EditorApi").field("font_cache", &self.font_cache).finish()
|
||||
f.debug_struct("EditorApi").finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ impl Resource {
|
||||
pub fn hash(&self) -> ResourceHash {
|
||||
self.hash
|
||||
}
|
||||
|
||||
pub fn empty() -> Self {
|
||||
Self::new([])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Resource> for Arc<dyn AsRef<[u8]> + Send + Sync> {
|
||||
@@ -67,7 +71,7 @@ impl CacheHash for Resource {
|
||||
}
|
||||
|
||||
/// Blake3 content hash of a resource, represented as 32 bytes
|
||||
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, DynAny)]
|
||||
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord, DynAny)]
|
||||
pub struct ResourceHash([u8; 32]);
|
||||
|
||||
impl From<&[u8]> for ResourceHash {
|
||||
@@ -135,6 +139,12 @@ impl std::str::FromStr for ResourceHash {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ResourceHash {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(&String::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ResourceHashParseError {
|
||||
InvalidLength { found: usize },
|
||||
@@ -238,6 +248,18 @@ impl ResourceId {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for ResourceId {
|
||||
fn from(value: u64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResourceId> for u64 {
|
||||
fn from(id: ResourceId) -> Self {
|
||||
id.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ResourceId {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
|
||||
Reference in New Issue
Block a user