mirror of
https://github.com/typst/typst
synced 2025-05-15 01:25:28 +08:00
Color fixes (#2262)
This commit is contained in:
parent
ca2312deec
commit
62f37ad4ab
@ -290,11 +290,7 @@ impl Show for RawElem {
|
|||||||
|
|
||||||
let theme = theme.as_deref().unwrap_or(&THEME);
|
let theme = theme.as_deref().unwrap_or(&THEME);
|
||||||
|
|
||||||
let foreground = theme
|
let foreground = theme.settings.foreground.unwrap_or(synt::Color::BLACK);
|
||||||
.settings
|
|
||||||
.foreground
|
|
||||||
.map(to_typst)
|
|
||||||
.map_or(Color::BLACK, Color::from);
|
|
||||||
|
|
||||||
let mut realized = if matches!(lang.as_deref(), Some("typ" | "typst" | "typc")) {
|
let mut realized = if matches!(lang.as_deref(), Some("typ" | "typst" | "typc")) {
|
||||||
let root = match lang.as_deref() {
|
let root = match lang.as_deref() {
|
||||||
@ -309,7 +305,7 @@ impl Show for RawElem {
|
|||||||
vec![],
|
vec![],
|
||||||
&highlighter,
|
&highlighter,
|
||||||
&mut |node, style| {
|
&mut |node, style| {
|
||||||
seq.push(styled(&text[node.range()], foreground.into(), style));
|
seq.push(styled(&text[node.range()], foreground, style));
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -334,7 +330,7 @@ impl Show for RawElem {
|
|||||||
for (style, piece) in
|
for (style, piece) in
|
||||||
highlighter.highlight_line(line, syntax_set).into_iter().flatten()
|
highlighter.highlight_line(line, syntax_set).into_iter().flatten()
|
||||||
{
|
{
|
||||||
seq.push(styled(piece, foreground.into(), style));
|
seq.push(styled(piece, foreground, style));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -432,12 +428,11 @@ fn highlight_themed<F>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Style a piece of text with a syntect style.
|
/// Style a piece of text with a syntect style.
|
||||||
fn styled(piece: &str, foreground: Paint, style: synt::Style) -> Content {
|
fn styled(piece: &str, foreground: synt::Color, style: synt::Style) -> Content {
|
||||||
let mut body = TextElem::packed(piece);
|
let mut body = TextElem::packed(piece);
|
||||||
|
|
||||||
let paint = to_typst(style.foreground).into();
|
if style.foreground != foreground {
|
||||||
if paint != foreground {
|
body = body.styled(TextElem::set_fill(to_typst(style.foreground).into()));
|
||||||
body = body.styled(TextElem::set_fill(paint));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if style.font_style.contains(synt::FontStyle::BOLD) {
|
if style.font_style.contains(synt::FontStyle::BOLD) {
|
||||||
|
@ -199,20 +199,13 @@ impl ColorSpaces {
|
|||||||
writer
|
writer
|
||||||
.icc_profile(srgb, &profile)
|
.icc_profile(srgb, &profile)
|
||||||
.n(3)
|
.n(3)
|
||||||
.range([0.0, 1.0, 0.0, 1.0, 0.0, 1.0])
|
.range([0.0, 1.0, 0.0, 1.0, 0.0, 1.0]);
|
||||||
.alternate()
|
|
||||||
.srgb();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the gray color space
|
// Write the gray color space
|
||||||
if let Some(gray) = self.d65_gray {
|
if let Some(gray) = self.d65_gray {
|
||||||
let profile = gray_icc();
|
let profile = gray_icc();
|
||||||
writer
|
writer.icc_profile(gray, &profile).n(1).range([0.0, 1.0]);
|
||||||
.icc_profile(gray, &profile)
|
|
||||||
.n(1)
|
|
||||||
.range([0.0, 1.0])
|
|
||||||
.alternate()
|
|
||||||
.d65_gray();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,10 @@ use image::{DynamicImage, GenericImageView, Rgba};
|
|||||||
use pdf_writer::{Filter, Finish};
|
use pdf_writer::{Filter, Finish};
|
||||||
|
|
||||||
use super::{deflate, PdfContext, RefExt};
|
use super::{deflate, PdfContext, RefExt};
|
||||||
use crate::image::{ImageKind, RasterFormat, RasterImage};
|
use crate::{
|
||||||
|
geom::ColorSpace,
|
||||||
|
image::{ImageKind, RasterFormat, RasterImage},
|
||||||
|
};
|
||||||
|
|
||||||
/// Embed all used images into the PDF.
|
/// Embed all used images into the PDF.
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
@ -33,9 +36,9 @@ pub fn write_images(ctx: &mut PdfContext) {
|
|||||||
if raster.icc().is_some() {
|
if raster.icc().is_some() {
|
||||||
space.icc_based(icc_ref);
|
space.icc_based(icc_ref);
|
||||||
} else if has_color {
|
} else if has_color {
|
||||||
space.device_rgb();
|
ctx.colors.write(ColorSpace::Srgb, space, &mut ctx.alloc);
|
||||||
} else {
|
} else {
|
||||||
space.device_gray();
|
ctx.colors.write(ColorSpace::D65Gray, space, &mut ctx.alloc);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a second gray-scale image containing the alpha values if
|
// Add a second gray-scale image containing the alpha values if
|
||||||
|
@ -907,8 +907,9 @@ impl Color {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the color's RGB(A) representation as an array of 8-bit values.
|
||||||
pub fn to_vec4_u8(&self) -> [u8; 4] {
|
pub fn to_vec4_u8(&self) -> [u8; 4] {
|
||||||
self.to_vec4().map(|x| (x * 255.0).round() as u8)
|
self.to_rgba().to_vec4().map(|x| (x * 255.0).round() as u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_space(self, space: ColorSpace) -> Self {
|
pub fn to_space(self, space: ColorSpace) -> Self {
|
||||||
@ -1113,7 +1114,9 @@ impl PartialEq for Color {
|
|||||||
// Lower precision for comparison to avoid rounding errors.
|
// Lower precision for comparison to avoid rounding errors.
|
||||||
// Keeps backward compatibility with previous versions of Typst.
|
// Keeps backward compatibility with previous versions of Typst.
|
||||||
(Self::Rgba(_), Self::Rgba(_)) => self.to_vec4_u8() == other.to_vec4_u8(),
|
(Self::Rgba(_), Self::Rgba(_)) => self.to_vec4_u8() == other.to_vec4_u8(),
|
||||||
(Self::Luma(a), Self::Luma(b)) => a == b,
|
(Self::Luma(a), Self::Luma(b)) => {
|
||||||
|
(a.luma * 255.0).round() as u8 == (b.luma * 255.0).round() as u8
|
||||||
|
}
|
||||||
(Self::Oklab(a), Self::Oklab(b)) => a == b,
|
(Self::Oklab(a), Self::Oklab(b)) => a == b,
|
||||||
(Self::LinearRgb(a), Self::LinearRgb(b)) => a == b,
|
(Self::LinearRgb(a), Self::LinearRgb(b)) => a == b,
|
||||||
(Self::Cmyk(a), Self::Cmyk(b)) => a == b,
|
(Self::Cmyk(a), Self::Cmyk(b)) => a == b,
|
||||||
|
BIN
tests/ref/bugs/raw-color-overwrite.png
Normal file
BIN
tests/ref/bugs/raw-color-overwrite.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
13
tests/typ/bugs/raw-color-overwrite.typ
Normal file
13
tests/typ/bugs/raw-color-overwrite.typ
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Test that the color of a raw block is not overwritten
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#show raw: set text(fill: blue)
|
||||||
|
|
||||||
|
`Hello, World!`
|
||||||
|
|
||||||
|
```rs
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, World!");
|
||||||
|
}
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user