Skip to content

Commit

Permalink
Merge branch 'master' into colr-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Oct 15, 2023
2 parents 2cb01f2 + cbe2d73 commit 75cf9d0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- `svg::SvgDocumentsList` returns `svg::SvgDocument` and not just `&[u8]` now.
Thanks to [wjian23](https://github.com/wjian23).
- `Face::set_variation` allows duplicated axes now.

## [0.19.2] - 2023-09-13
### Added
Expand Down
8 changes: 4 additions & 4 deletions examples/font2svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ impl<'a> ttf::colr::Painter<'a> for GlyphPainter<'a> {

fn paint_foreground(&mut self) {
// The caller must provide this color. We simply fallback to black.
self.paint_color(ttf::colr::BgraColor::new(0, 0, 0, 255));
self.paint_color(ttf::RgbaColor::new(0, 0, 0, 255));
}

fn paint_color(&mut self, color: ttf::colr::BgraColor) {
fn paint_color(&mut self, color: ttf::RgbaColor) {
self.svg.start_element("path");
self.svg.write_color_attribute("fill", color);
let opacity = f32::from(color.alpha) / 255.0;
Expand Down Expand Up @@ -536,13 +536,13 @@ fn color_glyph(
}

trait XmlWriterExt {
fn write_color_attribute(&mut self, name: &str, ts: ttf::colr::BgraColor);
fn write_color_attribute(&mut self, name: &str, ts: ttf::RgbaColor);
fn write_transform_attribute(&mut self, name: &str, ts: ttf::Transform);
fn write_spread_method_attribute(&mut self, method: ttf::colr::GradientExtend);
}

impl XmlWriterExt for xmlwriter::XmlWriter {
fn write_color_attribute(&mut self, name: &str, color: ttf::colr::BgraColor) {
fn write_color_attribute(&mut self, name: &str, color: ttf::RgbaColor) {
self.write_attribute_fmt(
name,
format_args!("rgb({}, {}, {})", color.red, color.green, color.blue),
Expand Down
46 changes: 34 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,33 @@ impl core::fmt::Debug for Transform {
}
}

/// A RGBA color in the sRGB color space.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct RgbaColor {
pub red: u8,
pub green: u8,
pub blue: u8,
pub alpha: u8,
}

impl RgbaColor {
/// Creates a new `RgbaColor`.
#[inline]
pub fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self {
blue,
green,
red,
alpha,
}
}

pub(crate) fn apply_alpha(&mut self, alpha: f32) {
self.alpha = (((f32::from(self.alpha) / 255.0) * alpha) * 255.0) as u8;
}
}

/// A trait for glyph outline construction.
pub trait OutlineBuilder {
/// Appends a MoveTo segment.
Expand Down Expand Up @@ -2176,21 +2203,16 @@ impl<'a> Face<'a> {
return None;
}

let v = self
.variation_axes()
.into_iter()
.enumerate()
.find(|(_, a)| a.tag == axis);
if let Some((idx, a)) = v {
if idx >= MAX_VAR_COORDS {
return None;
}

self.coordinates.data[idx] = a.normalized_value(value);
} else {
if usize::from(self.variation_axes().len()) >= MAX_VAR_COORDS {
return None;
}

for (i, var_axis) in self.variation_axes().into_iter().enumerate() {
if var_axis.tag == axis {
self.coordinates.data[i] = var_axis.normalized_value(value);
}
}

// TODO: optimize
if let Some(avar) = self.tables.avar {
// Ignore error.
Expand Down
22 changes: 9 additions & 13 deletions src/tables/colr.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
//! A [Color Table](
//! https://docs.microsoft.com/en-us/typography/opentype/spec/colr) implementation.

use crate::parser::{
FromData, LazyArray16, LazyArray32, Offset, Offset24, Offset32, Stream, F2DOT14,
};
use crate::GlyphId;
use crate::{cpal, Fixed, Transform};

pub use cpal::BgraColor;
use crate::parser::{FromData, LazyArray16, Offset, Offset24, Offset32, Stream, F2DOT14};
use crate::{cpal, Fixed, LazyArray32, Transform};
use crate::{GlyphId, RgbaColor};

/// A [base glyph](
/// https://learn.microsoft.com/en-us/typography/opentype/spec/colr#baseglyph-and-layer-records).
Expand Down Expand Up @@ -139,7 +135,7 @@ impl ColorLine<'_> {
#[derive(Clone, Copy, Debug)]
pub struct ColorStop {
pub stop_offset: f32,
pub color: BgraColor,
pub color: RgbaColor,
}

#[derive(Clone)]
Expand Down Expand Up @@ -350,12 +346,12 @@ impl FromData for CompositeMode {
///
/// See [COLR](https://learn.microsoft.com/en-us/typography/opentype/spec/colr) for details.
pub trait Painter<'a> {
/// Paints an outline glyph using the given color.
/// Outlines a glyph and stores it until the next paint command.
fn outline(&mut self, glyph_id: GlyphId);

/// Paints an outline glyph using the application provided text foreground color.
/// Paints the current glyph outline using the application provided text foreground color.
fn paint_foreground(&mut self);
fn paint_color(&mut self, color: BgraColor);
/// Paints the current glyph outline using the provided color.
fn paint_color(&mut self, color: RgbaColor);
fn paint_linear_gradient(&mut self, gradient: LinearGradient<'a>);
fn paint_radial_gradient(&mut self, gradient: RadialGradient<'a>);
fn paint_sweep_gradient(&mut self, gradient: SweepGradient<'a>);
Expand Down Expand Up @@ -397,7 +393,7 @@ impl<'a> Table<'a> {
let mut s = Stream::new(data);

let version = s.read::<u16>()?;
if version > 1 {
if version != 0 {
return None;
}

Expand Down
33 changes: 12 additions & 21 deletions src/tables/cpal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use core::num::NonZeroU16;

use crate::parser::{FromData, LazyArray16, Offset, Offset32, Stream};
use crate::RgbaColor;

/// A [Color Palette Table](
/// https://docs.microsoft.com/en-us/typography/opentype/spec/cpal).
Expand All @@ -19,7 +20,7 @@ impl<'a> Table<'a> {
let mut s = Stream::new(data);

let version = s.read::<u16>()?;
if version > 1 {
if version != 0 {
return None;
}

Expand Down Expand Up @@ -50,37 +51,27 @@ impl<'a> Table<'a> {
}

/// Returns the color at the given index into the given palette.
pub fn get(&self, palette_index: u16, palette_entry: u16) -> Option<BgraColor> {
pub fn get(&self, palette_index: u16, palette_entry: u16) -> Option<RgbaColor> {
let index = self
.color_indices
.get(palette_index)?
.checked_add(palette_entry)?;
self.colors.get(index)
self.colors.get(index).map(|c| c.to_rgba())
}
}

/// A BGRA color in the sRGB color space.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct BgraColor {
pub blue: u8,
pub green: u8,
pub red: u8,
pub alpha: u8,
struct BgraColor {
blue: u8,
green: u8,
red: u8,
alpha: u8,
}

impl BgraColor {
pub fn new(blue: u8, green: u8, red: u8, alpha: u8) -> Self {
Self {
blue,
green,
red,
alpha,
}
}

pub(crate) fn apply_alpha(&mut self, alpha: f32) {
self.alpha = (((f32::from(self.alpha) / 255.0) * alpha) * 255.0) as u8;
#[inline]
fn to_rgba(self) -> RgbaColor {
RgbaColor::new(self.red, self.green, self.blue, self.alpha)
}
}

Expand Down
13 changes: 6 additions & 7 deletions tests/tables/colr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{convert, Unit::*};
use ttf_parser::colr::{self, Painter};
use ttf_parser::cpal::{self, BgraColor};
use ttf_parser::GlyphId;
use ttf_parser::{cpal, GlyphId, RgbaColor};

#[test]
fn basic() {
Expand Down Expand Up @@ -39,9 +38,9 @@ fn basic() {
colr.paint(GlyphId(id), 0, &mut painter).map(|_| painter.0)
};

let a = BgraColor { blue: 10, green: 15, red: 20, alpha: 25 };
let b = BgraColor { blue: 30, green: 35, red: 40, alpha: 45 };
let c = BgraColor { blue: 50, green: 55, red: 60, alpha: 65 };
let a = RgbaColor::new(20, 15, 10, 25);
let b = RgbaColor::new(40, 35, 30, 45);
let c = RgbaColor::new(60, 55, 50, 65);

assert_eq!(cpal.get(0, 0), Some(a));
assert_eq!(cpal.get(0, 1), Some(b));
Expand Down Expand Up @@ -85,7 +84,7 @@ fn basic() {
enum Command {
Outline(u16),
Foreground,
PaintColor(BgraColor),
PaintColor(RgbaColor),
}

struct VecPainter(Vec<Command>);
Expand All @@ -99,7 +98,7 @@ impl Painter<'_> for VecPainter {
self.0.push(Command::Foreground);
}

fn paint_color(&mut self, color: BgraColor) {
fn paint_color(&mut self, color: RgbaColor) {
self.0.push(Command::PaintColor(color));
}

Expand Down

0 comments on commit 75cf9d0

Please sign in to comment.