use base64::Engine;
use super::preludes::*;
pub fn as_svg_id(b: &[u8], prefix: &'static str) -> String {
let rev_zero = b.iter().rev().skip_while(|&&b| b == 0).count();
let id = &b[..rev_zero];
let id = base64::engine::general_purpose::STANDARD_NO_PAD.encode(id);
[prefix, &id].join("")
}
pub type SpanId = u64;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct DefId(pub u64);
impl DefId {
#[comemo::memoize]
pub fn as_svg_id(self, prefix: &'static str) -> String {
as_svg_id(self.0.to_le_bytes().as_slice(), prefix)
}
}
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct AbsoluteRef {
pub fingerprint: Fingerprint,
pub id: DefId,
}
impl fmt::Debug for AbsoluteRef {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"<AbsRef: {}{}>",
self.fingerprint.as_svg_id(""),
self.id.0
)
}
}
impl Hash for AbsoluteRef {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.fingerprint.hash(state);
}
}
impl AbsoluteRef {
#[inline]
pub fn as_svg_id(&self, prefix: &'static str) -> String {
self.fingerprint.as_svg_id(prefix)
}
#[inline]
pub fn as_unstable_svg_id(&self, prefix: &'static str) -> String {
self.id.as_svg_id(prefix)
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct FontRef {
pub hash: u32,
pub idx: u32,
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct GlyphRef {
pub font_hash: u32,
pub glyph_idx: u32,
}
impl GlyphRef {
#[comemo::memoize]
pub fn as_svg_id(&self, prefix: &'static str) -> String {
let t = ((self.font_hash as u64) | ((self.glyph_idx as u64) << 32)).to_le_bytes();
as_svg_id(&t.as_slice()[..6], prefix)
}
}