-
Notifications
You must be signed in to change notification settings - Fork 15
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
Showing
18 changed files
with
337 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//! Generates a [cpal](https://learn.microsoft.com/en-us/typography/opentype/spec/cpal) table. | ||
use crate::{ | ||
error::Error, | ||
orchestration::{AnyWorkId, BeWork, Context, WorkId}, | ||
}; | ||
use fontdrasil::orchestration::{Access, Work}; | ||
use fontir::orchestration::WorkId as FeWorkId; | ||
use log::debug; | ||
use write_fonts::{tables::cpal::ColorRecord, tables::cpal::Cpal, NullableOffsetMarker}; | ||
|
||
#[derive(Debug)] | ||
struct CpalWork {} | ||
|
||
pub fn create_cpal_work() -> Box<BeWork> { | ||
Box::new(CpalWork {}) | ||
} | ||
|
||
impl Work<Context, AnyWorkId, Error> for CpalWork { | ||
fn id(&self) -> AnyWorkId { | ||
WorkId::Cpal.into() | ||
} | ||
|
||
fn read_access(&self) -> Access<AnyWorkId> { | ||
Access::Variant(AnyWorkId::Fe(FeWorkId::ColorPalettes)) | ||
} | ||
|
||
/// Generate [cpal](https://learn.microsoft.com/en-us/typography/opentype/spec/cpal) | ||
fn exec(&self, context: &Context) -> Result<(), Error> { | ||
let colors = context.ir.colors.try_get(); | ||
// Guard clause: no colors | ||
if !colors | ||
.as_ref() | ||
.map(|c| c.palettes.iter().any(|p| !p.is_empty())) | ||
.unwrap_or_default() | ||
{ | ||
debug!("Skip cpal; no colors"); | ||
return Ok(()); | ||
} | ||
let colors = colors.unwrap(); | ||
let sz0 = colors.palettes[0].len(); | ||
if colors.palettes.iter().any(|p| p.len() != sz0) { | ||
return Err(Error::InconsistentPaletteLength( | ||
colors.palettes.iter().map(Vec::len).collect(), | ||
)); | ||
} | ||
|
||
let color_records = colors | ||
.palettes | ||
.iter() | ||
.flat_map(|p| p.iter()) | ||
.map(|c| ColorRecord { | ||
red: c.r, | ||
green: c.g, | ||
blue: c.b, | ||
alpha: c.a, | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
if color_records.len() > u16::MAX.into() { | ||
return Err(Error::OutOfBounds { | ||
what: "Too many CPAL colorRecords".to_string(), | ||
value: format!("{}", color_records.len()), | ||
}); | ||
} | ||
|
||
let entries_per_palette = colors.palettes[0].len(); | ||
debug!( | ||
"CPAL has {} color records in {} palette(s) of {}", | ||
color_records.len(), | ||
colors.palettes.len(), | ||
entries_per_palette | ||
); | ||
context.cpal.set(Cpal { | ||
num_palette_entries: entries_per_palette as u16, | ||
num_palettes: colors.palettes.len() as u16, | ||
num_color_records: color_records.len() as u16, | ||
color_records_array: NullableOffsetMarker::new(Some(color_records)), | ||
color_record_indices: (0..colors.palettes.len()) | ||
.map(|i| (i * entries_per_palette) as u16) | ||
.collect(), | ||
..Default::default() | ||
}); | ||
|
||
Ok(()) | ||
} | ||
} |
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
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
Oops, something went wrong.