-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e052dbb
commit c5bd2c3
Showing
6 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//! A [Color Table]( | ||
//! https://docs.microsoft.com/en-us/typography/opentype/spec/colr) implementation. | ||
|
||
use crate::cpal::{self, Color}; | ||
use crate::parser::{FromData, LazyArray16, Offset, Offset32, Stream}; | ||
use crate::GlyphId; | ||
|
||
/// A [base glyph](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#baseglyph-and-layer-records). | ||
#[derive(Clone, Copy, Debug)] | ||
struct BaseGlyphRecord { | ||
glyph_id: GlyphId, | ||
first_layer_index: u16, | ||
num_layers: u16, | ||
} | ||
|
||
impl FromData for BaseGlyphRecord { | ||
const SIZE: usize = 6; | ||
|
||
fn parse(data: &[u8]) -> Option<Self> { | ||
let mut s = Stream::new(data); | ||
Some(Self { | ||
glyph_id: s.read::<GlyphId>()?, | ||
first_layer_index: s.read::<u16>()?, | ||
num_layers: s.read::<u16>()?, | ||
}) | ||
} | ||
} | ||
|
||
/// A [layer](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#baseglyph-and-layer-records). | ||
#[derive(Clone, Copy, Debug)] | ||
struct LayerRecord { | ||
glyph_id: GlyphId, | ||
palette_index: u16, | ||
} | ||
|
||
impl FromData for LayerRecord { | ||
const SIZE: usize = 4; | ||
|
||
fn parse(data: &[u8]) -> Option<Self> { | ||
let mut s = Stream::new(data); | ||
Some(Self { | ||
glyph_id: s.read::<GlyphId>()?, | ||
palette_index: s.read::<u16>()?, | ||
}) | ||
} | ||
} | ||
|
||
/// A trait for color glyph painting. | ||
pub trait ColorGlyphPainter { | ||
/// Paints an outline glyph in the given color. | ||
fn glyph(&mut self, id: GlyphId, color: Color); | ||
} | ||
|
||
/// A [Color Table]( | ||
/// https://docs.microsoft.com/en-us/typography/opentype/spec/colr). | ||
/// | ||
/// Currently, only version 0 is supported. | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct Table<'a> { | ||
palettes: cpal::Table<'a>, | ||
base_glyphs: LazyArray16<'a, BaseGlyphRecord>, | ||
layers: LazyArray16<'a, LayerRecord>, | ||
} | ||
|
||
impl<'a> Table<'a> { | ||
/// Parses a table from raw data. | ||
pub fn parse(palettes: cpal::Table<'a>, data: &'a [u8]) -> Option<Self> { | ||
let mut s = Stream::new(data); | ||
|
||
let version = s.read::<u16>()?; | ||
if version > 1 { | ||
return None; | ||
} | ||
|
||
let num_base_glyphs = s.read::<u16>()?; | ||
let base_glyphs_offset = s.read::<Offset32>()?; | ||
let layers_offset = s.read::<Offset32>()?; | ||
let num_layers = s.read::<u16>()?; | ||
|
||
let base_glyphs = Stream::new_at(data, base_glyphs_offset.to_usize())? | ||
.read_array16::<BaseGlyphRecord>(num_base_glyphs)?; | ||
|
||
let layers = Stream::new_at(data, layers_offset.to_usize())? | ||
.read_array16::<LayerRecord>(num_layers)?; | ||
|
||
Some(Self { | ||
palettes, | ||
base_glyphs, | ||
layers, | ||
}) | ||
} | ||
|
||
/// Whether the table contains a definition for the given glyph. | ||
pub fn contains(&self, glyph_id: GlyphId) -> bool { | ||
self.base_glyphs | ||
.binary_search_by(|base| base.glyph_id.cmp(&glyph_id)) | ||
.is_some() | ||
} | ||
|
||
/// Paints the color glyph. | ||
pub fn paint( | ||
&self, | ||
glyph_id: GlyphId, | ||
palette: u16, | ||
painter: &mut dyn ColorGlyphPainter, | ||
) -> Option<()> { | ||
let (_, base) = self | ||
.base_glyphs | ||
.binary_search_by(|base| base.glyph_id.cmp(&glyph_id))?; | ||
|
||
let start = base.first_layer_index; | ||
let end = start.checked_add(base.num_layers)?; | ||
let layers = self.layers.slice(start..end)?; | ||
|
||
for layer in layers { | ||
let color = self.palettes.get(palette, layer.palette_index)?; | ||
painter.glyph(layer.glyph_id, color); | ||
} | ||
|
||
Some(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//! A [Color Palette Table]( | ||
//! https://docs.microsoft.com/en-us/typography/opentype/spec/cpal) implementation. | ||
|
||
use crate::parser::{FromData, LazyArray16, Offset, Offset32, Stream}; | ||
|
||
/// A [Color Palette Table]( | ||
/// https://docs.microsoft.com/en-us/typography/opentype/spec/cpal). | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct Table<'a> { | ||
color_indices: LazyArray16<'a, u16>, | ||
colors: LazyArray16<'a, Color>, | ||
} | ||
|
||
impl<'a> Table<'a> { | ||
/// Parses a table from raw data. | ||
pub fn parse(data: &'a [u8]) -> Option<Self> { | ||
let mut s = Stream::new(data); | ||
|
||
let version = s.read::<u16>()?; | ||
if version > 1 { | ||
return None; | ||
} | ||
|
||
s.skip::<u16>(); // number of palette entries | ||
|
||
let num_palettes = s.read::<u16>()?; | ||
let num_colors = s.read::<u16>()?; | ||
let color_records_offset = s.read::<Offset32>()?; | ||
let color_indices = s.read_array16::<u16>(num_palettes)?; | ||
|
||
let colors = Stream::new_at(data, color_records_offset.to_usize())? | ||
.read_array16::<Color>(num_colors)?; | ||
|
||
Some(Self { | ||
color_indices, | ||
colors, | ||
}) | ||
} | ||
|
||
/// Returns the color at the given index into the given palette. | ||
pub fn get(&self, palette: u16, palette_entry: u16) -> Option<Color> { | ||
let index = self | ||
.color_indices | ||
.get(palette)? | ||
.checked_add(palette_entry)?; | ||
self.colors.get(index) | ||
} | ||
} | ||
|
||
/// A BGRA color in sRGB. | ||
#[allow(missing_docs)] | ||
#[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
pub struct Color { | ||
pub blue: u8, | ||
pub green: u8, | ||
pub red: u8, | ||
pub alpha: u8, | ||
} | ||
|
||
impl FromData for Color { | ||
const SIZE: usize = 4; | ||
|
||
fn parse(data: &[u8]) -> Option<Self> { | ||
let mut s = Stream::new(data); | ||
Some(Self { | ||
blue: s.read::<u8>()?, | ||
green: s.read::<u8>()?, | ||
red: s.read::<u8>()?, | ||
alpha: s.read::<u8>()?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters