summaryrefslogtreecommitdiff
path: root/crates/typst-pdf/src/gradient.rs
diff options
context:
space:
mode:
authorFlorent Michel <56166507+FlorentCLMichel@users.noreply.github.com>2024-07-22 16:05:22 +0100
committerGitHub <noreply@github.com>2024-07-22 15:05:22 +0000
commitc4dd6fa06227b8c4a04cf488f371f52d5379ef9b (patch)
treea5025863899d9d0d045f4b36083f1f972ca46cbb /crates/typst-pdf/src/gradient.rs
parent1d74c8e8bfe47723f62eb0dbb3852c07a43be5fd (diff)
Adjust the number of color components written to the pdf according to the color space (#4568)
Diffstat (limited to 'crates/typst-pdf/src/gradient.rs')
-rw-r--r--crates/typst-pdf/src/gradient.rs35
1 files changed, 18 insertions, 17 deletions
diff --git a/crates/typst-pdf/src/gradient.rs b/crates/typst-pdf/src/gradient.rs
index de77df33..5f85f9ca 100644
--- a/crates/typst-pdf/src/gradient.rs
+++ b/crates/typst-pdf/src/gradient.rs
@@ -145,10 +145,9 @@ pub fn write_gradients(
.bits_per_component(16)
.bits_per_flag(8)
.shading_type(StreamShadingType::CoonsPatch)
- .decode([
- 0.0, 1.0, 0.0, 1.0, range[0], range[1], range[2], range[3],
- range[4], range[5],
- ])
+ .decode(
+ [0.0, 1.0, 0.0, 1.0].into_iter().chain(range.iter().copied()),
+ )
.anti_alias(gradient.anti_alias())
.filter(Filter::FlateDecode);
@@ -216,7 +215,7 @@ fn shading_function(
chunk
.stitching_function(function)
.domain([0.0, 1.0])
- .range(color_space.range())
+ .range(color_space.range().iter().copied())
.functions(functions)
.bounds(bounds)
.encode(encode);
@@ -235,7 +234,7 @@ fn single_gradient(
let reference = chunk.alloc();
chunk
.exponential_function(reference)
- .range(color_space.range())
+ .range(color_space.range().iter().copied())
.c0(color_space.convert(first_color))
.c1(color_space.convert(second_color))
.domain([0.0, 1.0])
@@ -344,13 +343,13 @@ fn register_gradient(
/// Structure:
/// - flag: `u8`
/// - points: `[u16; 24]`
-/// - colors: `[u16; 12]`
+/// - colors: `[u16; 4*N]` (N = number of components)
fn write_patch(
target: &mut Vec<u8>,
t: f32,
t1: f32,
- c0: [u16; 3],
- c1: [u16; 3],
+ c0: &[u16],
+ c1: &[u16],
angle: Angle,
) {
let theta = -TAU * t + angle.to_rad() as f32 + PI;
@@ -390,11 +389,13 @@ fn write_patch(
p1, p1, p2, p2, cp1, cp2, p3, p3, p1, p1, p1, p1,
]));
- let colors =
- [c0.map(u16::to_be), c0.map(u16::to_be), c1.map(u16::to_be), c1.map(u16::to_be)];
-
// Push the colors.
- target.extend_from_slice(bytemuck::cast_slice(&colors));
+ let colors = [c0, c0, c1, c1]
+ .into_iter()
+ .flat_map(|c| c.iter().copied().map(u16::to_be_bytes))
+ .flatten();
+
+ target.extend(colors);
}
fn control_point(c: Point, r: f32, angle_start: f32, angle_end: f32) -> (Point, Point) {
@@ -452,8 +453,8 @@ fn compute_vertex_stream(gradient: &Gradient, aspect_ratio: Ratio) -> Arc<Vec<u8
&mut vertices,
t0.get() as f32,
t1.get() as f32,
- encode_space.convert(c0),
- encode_space.convert(c1),
+ &encode_space.convert(c0),
+ &encode_space.convert(c1),
angle,
);
continue;
@@ -483,8 +484,8 @@ fn compute_vertex_stream(gradient: &Gradient, aspect_ratio: Ratio) -> Arc<Vec<u8
&mut vertices,
t_x as f32,
t_next as f32,
- encode_space.convert(c),
- encode_space.convert(c_next),
+ &encode_space.convert(c),
+ &encode_space.convert(c_next),
angle,
);