#let rtext(body)={ text(fill:maroon,body) } = 可视化库(Visualize) 绘图与数据可视化。 如果你想要创建更高级的图形或图表,也可以了解一下#link("https://github.com/cetz-package/cetz")[CeTZ包],以及适配你使用场景的#link("https://typst.app/universe")[更多专用包]。 === 易用性(Accessibility) Typst绘制的所有形状和路径都会自动标记为#link()[装饰元素],这样在导出PDF时,辅助技术(AT)将不会识别它们。但它们内部的内容(如果存在)仍然可以被访问。 如果你使用该类别中的函数创建带有语义含义的插图,请将其包裹在#link(
)[figure]函数调用内以保证可访问性。使用它的#link()[alt参数]来提供#link()[替代描述]。 == 圆(circle) 一个可附带可选内容的圆形。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst // Without content. #circle(radius: 25pt) // With content. #circle[ #set align(center + horizon) Automatically \ sized to fit. ] ``` ],[ // Without content. #circle(radius: 25pt) // With content. #circle[ #set align(center + horizon) Automatically \ sized to fit. ] ] ) === 参数(Parameters) ```typc circle( radius: length, width: auto relative, height: auto relative fraction, fill: none color gradient tiling, stroke: none auto length color gradient stroke tiling dictionary, inset: relative dictionary, outset: relative dictionary, body: none content, ) → content ``` - *radius*: length 该圆形的半径。该参数与宽度、高度互斥。默认值:```typc 0pt```。 - *width*: auto relative 圆形的宽度。该参数与半径、高度互斥。默认值:```typc auto```。 与半径不同,该值可以相对于父容器的宽度进行设置。 - *height*: auto relative fraction 圆形的高度。该属性与半径、宽度互斥。默认值:```typc auto```。 与半径不同,该值可以相对于父容器的高度进行设置。 - *fill*: none color gradient tiling 如何填充圆形。更多详情请参阅#link()[矩形的文档]。默认值:```typc none```。 - *stroke*: none auto length color gradient stroke tiling dictionary 如何绘制圆形轮廓。更多详情请参阅#link()[矩形的文档]。默认值:```typc auto```。 - *inset*: relative dictionary 圆形内容的内边距设置值。更多详情请参阅#link()[盒子]相关文档。默认值:```typc 0%+5pt```。 - *outset*: relative dictionary 在不影响布局的前提下,需要将圆形的尺寸扩大多少。更多信息请查看#link()[盒子]相关文档。默认值:```typc (:)```。 - *body*: none content 放置在圆形内的内容。圆形会自动扩大以适配该内容,并保持1:1的宽高比。默认值:```typc none```。 #let rainbow(content) = { set text(fill: gradient.linear(..color.map.coolwarm)) box(content) } == #rainbow[颜色(color)] 特定色彩空间中的一种颜色。 Typst 支持: -- 通过 #link()[rgb函数]使用 sRGB -- 通过 #link()[cmyk函数]使用设备 CMYK -- 通过 #link()[luma函数]使用 D65 灰度 -- 通过 #link()[oklab函数]使用 Oklab -- 通过 #link()[oklch函数]使用 Oklch -- 通过 #link()[color.linear-rgb函数]使用线性 RGB -- 通过 #link()[color.hsl函数]使用 HSL -- 通过 #link()[color.hsv函数]使用 HSV -- 通过 #link()[color.spot类型]使用由专色着色剂描述的色彩空间 除CMYK和专色油墨外,所有色彩空间都带有Alpha通道。 在本文档中,我们将可以相互混合的色彩称为印刷原色(目前指除专色以外的所有色彩)。在该术语里,“印刷原色”表示色调是在整个印刷过程中生成,而非预先制作。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #rect(fill: aqua) ``` ],[ #rect(fill: aqua) ] ) === 预定义的颜色(Predefined colors) Typst定义了以下内置颜色: #table( columns: (1fr,1fr), align: left, stroke: none, table.header([*Color*],[*Definition*]), table.hline(), [black],[luma(0)], [gray],[luma(170)], [silver],[luma(221)], [white],[luma(255)], [navy],[rgb(\"\#001f3f\")], [blue],[rgb(\"\#0074d9\")], [aqua],[rgb(\"\#7fdbff\")], [teal],[rgb(\"\#39cccc\")], [eastern],[rgb(\"\#239dad\")], [purple],[rgb(\"\#b10dc9\")], [fuchsia],[rgb(\"\#f012be\")], [maroon],[rgb(\"\#85144b\")], [red],[rgb(\"\#ff4136\")], [orange],[rgb(\"\#ff851b\")], [yellow],[rgb(\"\#ffdc00\")], [olive],[rgb(\"\#3d9970\")], [green],[rgb(\"\#2ecc40\")], [lime],[rgb(\"\#01ff70\")], ) 预定义颜色以及最重要的颜色构造函数在全局范围内和颜色类型的作用域内均可使用,因此你既可以写color.red,也可以直接写red。 #grid( columns: 9, gutter: 8pt, inset: 20pt, fill: (x,y)=> if (x==0 and y==0) {black} else if (x==1 and y==0) { gray } else if(x==2 and y==0){silver} else if(x==3 and y==0){white} else if(x==4 and y==0){navy} else if(x==5 and y==0){blue} else if(x==6 and y==0){aqua} else if(x==7 and y==0){teal} else if(x==8 and y==0){eastern} else if(x==0 and y==1){purple} else if(x==1 and y==1){fuchsia} else if(x==2 and y==1){maroon} else if(x==3 and y==1){red} else if(x==4 and y==1){orange} else if(x==5 and y==1){yellow} else if(x==6 and y==1){olive} else if(x==7 and y==1){green} else if(x==8 and y==1){lime} , align: center +horizon, [#text(fill: white,size: 10pt,"black")], [#text(fill: black,size: 10pt,"gray")], [#text(fill: black,size: 10pt,"silver")], [#text(fill: black,size: 10pt,"white")], [#text(fill: white,size: 10pt,"navy")], [#text(fill: white,size: 10pt,"blue")], [#text(fill: black,size: 10pt,"aqua")], [#text(fill: black,size: 10pt,"teal")], [#text(fill: black,size: 10pt,"eastern")], [#text(fill: white,size: 10pt,"purple")], [#text(fill: black,size: 10pt,"fhchsia")], [#text(fill: white,size: 10pt,"maroon")], [#text(fill: black,size: 10pt,"red")], [#text(fill: black,size: 10pt,"orange")], [#text(fill: black,size: 10pt,"yellow")], [#text(fill: black,size: 10pt,"olive")], [#text(fill: black,size: 10pt,"green")], [#text(fill: black,size: 10pt,"lime")], ) === 预定义颜色映射(Predefined color maps) Typst还内置了若干预设色板,可用于[渐变效果]。这些色板就是在color.map模块中定义的颜色数组。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #circle(fill: gradient.linear(..color.map.crest)) ``` ], [ #circle(fill: gradient.linear(..color.map.crest)) ] ) #table( columns: (0.5fr,2fr), stroke: none, align: left, table.header([*Map*],[*Details*]), table.hline(), [turbo],[一张在感知上均匀的彩虹色地图。阅读#link("https://ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html")[这篇博客文章]了解更多详情。], [cividis],[一张从蓝色到灰色再到黄色的颜色图,更多详情请参阅#link("https://bids.github.io/colormap/")[这篇博客文章]。], [rainbow],[该颜色映射通过完整的色谱循环。最佳使用方式是将插值颜色空间设置为HSL。彩虹渐变不适合用于数据可视化,因为它在感知上并不均匀,因此值之间的差异对于读者来说会变得模糊不清。它只应作为装饰用途。], [spectral],[红黄蓝三色映射。], [viridis],[从紫色到青色再到黄色的颜色映射。], [inferno],[从黑到红再到黄的颜色映射。], [magma],[从黑色到紫色再到黄色的颜色映射。], [plasma],[从紫色到粉色再到黄色的颜色映射。], [rocket],[从黑到红再到白的颜色映射。], [mako],[从黑色到青色再到白色的颜色映射。], [coolwarm],[从蓝色到白色再到红色的颜色映射,颜色过渡平滑。], [vlag],[由浅蓝到白色再到红色的颜色映射。], [icefire],[从浅青色到黑色再到橙色的颜色映射。], [flare],[从橙色到紫色的颜色映射,在感知上是均匀的。], [crest],[从浅绿色到蓝色的颜色映射。], ) 部分热门预设并未纳入,原因是它们不提供免费许可。还有一些预设(例如Jet)未被加入,是因为它们对色盲群体不友好。你可以随意使用或创建包含其他对你有用预设的软件包! #table( align: center, columns: 1, stroke: none, fill: luma(230), [ #image("images/7004.png") ] ) === 定义(Definitions) - *luma* 创建灰度颜色。 灰度颜色在内部由单一亮度分量表示。 也可以通过#link()[components]方法获取这些分量。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #for x in range(250, step: 50) { box(square(fill: luma(x))) } ``` ],[ #for x in range(250, step: 50) { box(square(fill: luma(x))) } ] ) ```typc color.luma( lightness: int ratio, alpha: ratio, color: color, ) → color ``` -- *lightness*: int or #rainbow[ratio] 明度分量。 -- *alpha*: ratio 透明度通道。 -- *color*: #rainbow[color] 或者:要转换为灰度的颜色。 如果指定了该参数,则不应设置亮度参数。 - *oklab* 创建一个#link("https://bottosson.github.io/posts/oklab/")[Oklab]颜色。 该色彩空间非常适合以下场景: -- 在保持感知色相不变的情况下进行饱和度调整等色彩处理 -- 生成具有均匀感知明度的灰度图像 -- 制作平滑且均匀的色彩过渡与渐变效果 线性Oklab颜色在内部由包含四个分量的数组表示: -- 亮度(#link()[比率]) -- a(#link()[浮点数]或#link()[比率]。比率以0.4为基准;即50%等于0.2) -- b(#link()[浮点数]或#link()[比率]。比率以0.4为基准;即50%等于0.2) -- 透明度(alpha)(#link()[比率]) 可通过components方法获取这些分量。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #square( fill: oklab(27%, 20%, -3%, 50%) ) ``` ],[ #square( fill: oklab(27%, 20%, -3%, 50%) ) ] ) ```typc color.oklab( lightness: ratio, a: float ratio, b: float ratio, alpha: ratio, color: color, ) → color ``` -- *lightness*: ratio 度量分量。 -- *a*: float ratio a(“绿/红”)分量。 -- *b*: float ratio b(蓝/黄)分量。 -- *alpha*: ratio 透明度通道。 -- *color*: #rainbow[color] 或者:要转换为Oklab的颜色。 如果指定此项,则不应提供各个分量。 - *oklch* 创建一个#link("https://bottosson.github.io/posts/oklab/")[Oklch]颜色。 该色彩空间非常适合以下使用场景: -- 涉及亮度、色度和色相的色彩处理 -- 生成具有均匀感知亮度的灰度图像 -- 制作平滑且均匀的色彩过渡与渐变 线性Oklch颜色在内部由包含四个分量的数组表示: -- 亮度(比率) -- 色度(浮点数或比率。比率相对于0.4;即50%等于0.2) -- 色相(角度) -- 透明度(比率) 可通过#link()[components]方法获取这些分量。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #square( fill: oklch(40%, 0.2, 160deg, 50%) ) ``` ],[ #square( fill: oklch(40%, 0.2, 160deg, 50%) ) ] ) ```typc color.oklch( lightness: ratio, chroma: float ratio, hue: angle, alpha: ratio, color: color, ) → color ``` -- *lightness*: ratio 亮度分量。 -- *chroma*: float ratio 色度分量。 -- *hue*: angle 色相分量。 -- *alpha*: ratio 透明度(比率) -- *color*: #rainbow[color] 或者:要转换为Oklch的颜色。 如果指定此项,则不应提供各个单独分量。 - *linear-rgb* 创建带有线性亮度的RGB(A)颜色。 该色彩空间与sRGB类似,区别在于颜色分量没有经过伽马校正。这便于执行混合、插值等色彩运算。不过,这类运算更推荐使用#link()[oklab函数]。 线性RGB(A)颜色在内部由包含四个分量的数组表示: -- 红色(#link()[比率]) -- 绿色(#link()[比率]) -- 蓝色(#link()[比率]) -- 透明度(#link()[比率]) 也可以通过#link()[components]方法获取这些分量。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #square(fill: color.linear-rgb( 30%, 50%, 10%, )) ``` ],[ #set align(horizon) #square(fill: color.linear-rgb( 30%, 50%, 10%, )) ] ) ```typc color.linear-rgb( red: int ratio, green: int ratio, blue: int ratio, alpha: int ratio, color: color, ) → color ``` -- *red*: int ratio 红色分量。 -- *green*: int ratio 绿色分量。 -- *blue*: int ratio 蓝色分量。 -- *alpha*: int ratio 透明度通道。 -- *color*: #rainbow[color] 或者:要转换为线性RGB(A)的颜色。 如果指定了此项,则不应提供各个分量。 - *rgb* 创建一个RGB(A)颜色。 该颜色在sRGB色彩空间中指定。 RGB(A)颜色在内部由包含四个分量的数组表示: -- 红色(比例值) -- 绿色(比例值) -- 蓝色(比例值) -- 透明度(比例值) 可通过#link()[components]方法获取这些分量。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #square(fill: rgb("#b1f2eb")) #square(fill: rgb(87, 127, 230)) #square(fill: rgb(25%, 13%, 65%)) ``` ],[ #set align(horizon) #square(fill: rgb("#b1f2eb")) #square(fill: rgb(87, 127, 230)) #square(fill: rgb(25%, 13%, 65%)) ] ) ```typc color.rgb( red: int ratio, green: int ratio, blue: int ratio, alpha: int ratio, hex: str, color: color, ) → color ``` -- *red*: int ratio 红色分量。 -- *green*: int ratio 绿色分量。 -- *blue*: int ratio 蓝色分量。 -- *alpha*: int ratio 透明度通道。 -- *hex*: str 或者:十六进制表示的颜色。 支持3位、4位、6位或8位十六进制数字,可选择带上前置井号。 如果指定该值,则不应再单独设置各颜色分量。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #text(16pt, rgb("#239dad"))[ *Typst* ] ``` ],[ #text(16pt, rgb("#239dad"))[ *Typst* ] ] ) -- *color*: #rainbow[color] 或者:要转换为线性RGB(A)的颜色。 如果指定了此项,则不应提供各个分量。 - *cmyk* 创建CMYK颜色。 如果你需要适配特定打印机,这个功能会很有用。转换成用于屏幕预览的RGB颜色,其效果可能和打印机实际输出的颜色存在差异。 CMYK颜色在内部由包含四个分量的数组表示: -- 青色(cyan )(比例值) -- 品红色(magenta)(比例值) -- 黄色(yellow)(比例值) -- 黑色(key)(比例值) 也可以通过#link()[components]方法获取这些分量。 请注意,启用PDF/A输出时,目前暂不支持CMYK颜色。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #square( fill: cmyk(27%, 0%, 3%, 5%) ) ``` ],[ #set align(horizon) #square( fill: cmyk(27%, 0%, 3%, 5%) ) ] ) ```typc color.cmyk( cyan: ratio, magenta: ratio, yellow: ratio, key: ratio, color: color, ) → color ``` -- *cyan*: ratio 青色分量。 -- *magenta*: ratio 品红色分量。 -- *yellow*: ratio 黄色分量。 -- *key*: ratio 黑色分量。 -- *color*: #rainbow[color] 或者:要转换为CMYK模式的颜色。 如果指定此项,则不应提供各个分量值。 - *hsl* 创建一个HSL颜色。 该色彩空间适合通过色相、饱和度和明度来指定颜色,也适用于色彩处理,例如在保持人眼感知色相不变的同时调整饱和度。 HSL颜色在内部由包含四个分量的数组表示: -- 色相(角度) -- 饱和度(比率) -- 亮度(比率) -- 透明度(比率) 可通过#link()[components]方法获取这些分量。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #square( fill: color.hsl(30deg, 50%, 60%) ) ``` ],[ #set align(horizon) #square( fill: color.hsl(30deg, 50%, 60%) ) ] ) ```typc color.hsl( hue: angle, saturation: int ratio, lightness: int ratio, alpha: int ratio, color: color, ) → color ``` -- *hue*: angle 色相角。 -- *saturation*: int ratio 饱和度分量。 -- *lightness*: int ratio 亮度分量。 -- *alpha*: int ratio 透明度通道。 -- *color*: #rainbow[color] 或者:要转换为HSL的颜色。 如果指定此项,则不应单独提供各分量。 - *hsv* 创建一个HSV颜色。 该色彩空间适用于通过色相、饱和度和明度来指定颜色。它也可用于色彩处理,例如在保持人眼感知色相不变的情况下调整饱和度。 HSV颜色在内部由包含四个分量的数组表示: -- 色相(角度) -- 饱和度(比率) -- 亮度(比率) -- 透明度(比率) 也可以通过#link()[components]方法获取这些分量。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #square( fill: color.hsv(30deg, 50%, 60%) ) ``` ],[ #set align(horizon) #square( fill: color.hsv(30deg, 50%, 60%) ) ] ) ```typc color.hsv( hue: angle, saturation: int ratio, value: int ratio, alpha: int ratio, color: color, ) → color ``` -- *hue*: angle 色相角。 -- *saturation*: int ratio 饱和度分量。 -- *value*: int ratio 亮度分量。 -- *alpha*: int ratio 透明度通道。 -- *color*: #rainbow[color] 或者:要转换为HSV的颜色。 如果指定此项,则不应单独提供各分量。 - *components* 提取此颜色的各分量。 该数组的大小与数值取决于色彩空间。你可以通过#link()[space]获取色彩空间。下表列出了各类色彩空间及其对应的分量: #table( columns: 5, align: left, stroke: none, table.header([*Color space*],[*C1*],[*C2*],[*C3*],[*C4*]), table.hline(), [#link()[luma]],[Lightness],[],[],[], [#link()[oklab]],[Lightness],[a],[b],[Alpha], [#link()[oklch]],[Lightness],[Chroma],[Hue],[Alpha], [#link()[linear-rgb]],[Red],[Green],[Blue],[Alpha], [#link()[rgb]],[Red],[Green],[Blue],[Alpha], [#link()[cmyk]],[Cyan],[Magenta],[Yellow],[Key], [#link()[hsl]],[Hue],[Saturation],[Lightness],[Alpha], [#link()[hsv]],[Hue],[Saturation],[Value],[Alpha], [#link()[spot]],[Tint],[],[], ) 各个独立值的含义与类型,请参阅对应色彩空间的文档。透明度(alpha)分量为可选项,仅当alpha参数为true时才会包含。返回数组的长度取决于分量数量以及是否包含透明度分量。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst // note that the alpha component is included by default #rgb(40%, 60%, 80%).components() ``` ], [ #set align(center) // note that the alpha component is included by default #rgb(40%, 60%, 80%).components() ] ) ```typc self.components(alpha: bool) → array ``` -- *alpha*: bool 是否包含透明(Alpha)通道。 - *space* 返回该颜色所属色彩空间的构造函数。 返回以下值之一: -- #link()[luma] -- #link()[oklab] -- #link()[oklch] -- #link()[linear-rgb] -- #link()[rgb] -- #link()[cmyk] -- #link()[hsl] -- #link()[hsv] #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #let color = cmyk(1%, 2%, 3%, 4%) #(color.space() == cmyk) ``` ],[ #set align(horizon) #let color = cmyk(1%, 2%, 3%, 4%) #(color.space() == cmyk) ] ) ```typc self.space() → spot any ``` - *to-hex* 返回该颜色的RGB(A)十六进制表示形式(例如 \#ffaa32 或 \#020304fe)。若透明度分量(\#020304fe中的最后两位)等于ff(255 / 100%),则省略该分量。 ```typc self.to-hex() → str ``` - *lighten* 将颜色按指定系数提亮。 ```typc self.lighten(factor: ratio) → color ``` -- *factor*: ratio 用于提亮颜色的因子 - *darken* 按照给定系数降低颜色亮度。 ```typc self.darken(factor: ratio) → color ``` -- *factor*: ratio 用于降色的因子 - *saturate* 按指定系数提升颜色的饱和度。 仅印刷四色颜色可调整饱和度。如果需要调整专色的饱和度,请先将其转换为印刷四色颜色。 ```typc self.saturate(factor: ratio) → color ``` -- *factor*: ratio 用于提升颜色饱和度的系数。 - *desaturate* 按指定系数降低颜色的饱和度。 仅印刷四色颜色可以降低饱和度。如果要降低专色的饱和度,请先将其转换为印刷四色颜色。 ```typc self.desaturate(factor: ratio) → color ``` -- *factor*: ratio ```typc self.saturate(factor: ratio) → color ``` -- *factor*: ratio 用于降低颜色饱和度的系数。 - *negate* 使用提供的颜色空间生成互补色。你可以将其理解为色轮上的对立色。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #square(fill: yellow) #square(fill: yellow.negate()) #square(fill: yellow.negate(space: rgb)) ``` ], [ #square(fill: yellow) #square(fill: yellow.negate()) #square(fill: yellow.negate(space: rgb)) ] ) ```typc self.negate(space: auto spot any) → color ``` -- *space*: auto spot any 用于转换的颜色空间。默认情况下,使用感知颜色空间。 - *rotate* 按指定角度旋转颜色的色相。 该函数仅适用于具有明确定义色相分量的颜色模型,即Oklch、HSL和HSV。 ```typc self.rotate( angle: angle, space: any, ) → color ``` -- *angle*: angle 色相旋转的角度。 -- *space*: any 用于旋转的色彩空间。默认情况下,该操作在感知色彩空间(oklch)中执行。 - *mix* 混合两种或多种颜色,生成新颜色。 在包含色相分量的色彩空间(HSL、HSV、Oklch)中,一次仅可混合两种颜色。在此类色彩空间中混合两种以上颜色将会报错! #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #set block(height: 20pt, width: 100%) #block(fill: red.mix(blue)) #block(fill: red.mix(blue, space: rgb)) #block(fill: color.mix(red, blue, white)) #block(fill: color.mix((red, 70%), (blue, 30%))) ``` ], [ #set block(height: 20pt, width: 100%) #block(fill: red.mix(blue)) #block(fill: red.mix(blue, space: rgb)) #block(fill: color.mix(red, blue, white)) #block(fill: color.mix((red, 70%), (blue, 30%))) ] ) ```typc color.mix( colors: ..color array, space: auto spot any, ) → color ``` -- *colors*: color array 颜色(可附带权重)以由颜色和权重(浮点数或比率)组成的二元组(长度为2的数组)形式指定。 权重之和不必为100%,权重是相对于所有权重总和的相对值。 -- *space*: auto spot any 用于混合的色彩空间。默认情况下,混合将在感知色彩空间(oklab)中进行;如果所有颜色都使用同一种专色油墨,则使用该油墨进行混合。 所有颜色都将转换为此色彩空间。 - *transparentize* 按给定系数使颜色变得更透明。 此方法与现有的alpha值相关。如果缩放值为正,则计算alpha - alpha \* 缩放值。负缩放值的行为类似于color.opacify(-缩放值)。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(220)} , [ ```typst #block(fill: red)[opaque] #block(fill: red.transparentize(50%))[half red] #block(fill: red.transparentize(75%))[quarter red] ``` ], [ #block(fill: red)[opaque] #block(fill: red.transparentize(50%))[half red] #block(fill: red.transparentize(75%))[quarter red] ] ) ```typc self.transparentize(scale: ratio) → color ``` -- *scale*: ratio 用于修改透明度(alpha)值的系数。 - *opacify* 按指定比例提高颜色的不透明度。 此方法基于现有的透明度(alpha)值进行计算。若比例值为正,则按 `alpha + scale - alpha * scale` 计算;比例值为负时,效果等同于 color.transparentize(-scale)。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(220)} , [ ```typst #let half-red = red.transparentize(50%) #block(fill: half-red.opacify(100%))[opaque] #block(fill: half-red.opacify(50%))[three quarters red] #block(fill: half-red.opacify(-50%))[one quarter red] ``` ], [ #set align(horizon) #let half-red = red.transparentize(50%) #block(fill: half-red.opacify(100%))[opaque] #block(fill: half-red.opacify(50%))[three quarters red] #block(fill: half-red.opacify(-50%))[one quarter red] ] ) ```typc self.opacify(scale: ratio) → color ``` -- *scale*: ratio 用于修改透明度(alpha)值的系数。 - *spot* 可用于生成专色的专色着色剂。 在专业印刷环境中,可使用专色来指定精确颜料。创建好专色着色剂后,你可以利用其淡色方法生成各类颜色。 *构造函数* 创建一种新的专色着色剂。 ```typc color.spot( name: one str, fallback: color, ) → spot ``` -- *name*: one str 需使用的专色着色剂名称。 生产过程中,该名称将由人工核验并匹配对应的着色剂,因此该取值必须无歧义。最好引用PANTONE、HKS、RAL、Toyo以及DIC等色卡体系中的颜色。 生产时此处的值区分大小写:“PANTONE 2221 C”与“PANTONE 2221 c”会被视作两种不同颜色。请确保命名规则统一,要么引用色卡体系,要么与印刷生产专员协商确定。 若该值设为“all”,且印刷包含多个印版,使用该着色剂时,指定的淡色会均匀施加到全部印版上。若选择none,则使用此颜色时不会添加任何着色剂。该特殊取值常用于表示裁切或上光工艺。务必与印刷生产方沟通确认! 我们不建议使用“青色”“品红色”“黄色”“关键色”“黑色”这些名称,或是它们的本地语言译名。根据打印机型号不同,这些名称有可能会被识别为CMYK印刷四色,也有可能不会。 #table( columns: (0.5fr,1.5fr), align: left, stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"all\"],[使用所有可用的色版,而不是仅使用特定着色剂。] ) -- *fallback*: color 如果指定的着色剂不可用,该如何渲染此颜色。 许多媒介(例如屏幕预览和家用打印机)无法使用这种特定专色着色剂。为了展示目标印刷效果的近似效果,会改用另一种可用的颜色。 *关于spot的定义* -- *tint* 使用该着色剂,按指定淡印比例创建专色。 淡印比例代表着色剂的使用占比。100%的淡印比例表示以最高浓度使用着色剂,而0%则代表不使用任何着色剂。 #table( columns: (1fr,1fr), align: (left,center), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let pantone = color.spot( "PANTONE 2221 C", rgb("#239dad") ) #square(fill: pantone.tint(100%)) #square(fill: pantone.tint(70%)) #square(fill: pantone.tint(40%)) ``` ],[ #set align(horizon) #let pantone = color.spot( "PANTONE 2221 C", rgb("#239dad") ) #square(fill: pantone.tint(100%)) #square(fill: pantone.tint(70%)) #square(fill: pantone.tint(40%)) ] ) ```typc self.tint(value: ratio) → color ``` -- *value*: ratio 着色百分比,范围在0%至100%之间。 == 曲线(curve) 由移动、线条和贝塞尔曲线段组成的曲线。 在任意时刻,都存在一个概念上的画笔或光标。 -- 移动元素:移动光标,但不进行绘制。 -- 直线/二次/三次元素:从光标位置向新位置绘制一段线条,可附带贝塞尔曲线控制点。 -- 闭合元素:绘制一条直线或平滑线条,回到曲线起点或上一个最近的移动段起点。 出于布局需要,曲线的边界框是一个紧贴的矩形,包含所有线段以及坐标点(#text(fill: red,"0pt, 0pt"))。 位置可以采用绝对方式指定(即相对于(#text(fill: red,"0pt, 0pt"))),也可以相对于当前画笔/光标位置,也就是上一段线段的终点位置。 贝塞尔曲线控制点可通过传入#text(fill: red,"none") 予以省略,或传入#text(fill: red,"auto"),由上一段线段自动镜像生成。 === 示例(Example) #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #curve( fill: blue.lighten(80%), stroke: blue, curve.move((0pt, 50pt)), curve.line((100pt, 50pt)), curve.cubic(none, (90pt, 0pt), (50pt, 0pt)), curve.close(), ) ``` ], [ #set align(horizon+center) #curve( fill: blue.lighten(80%), stroke: blue, curve.move((0pt, 50pt)), curve.line((100pt, 50pt)), curve.cubic(none, (90pt, 0pt), (50pt, 0pt)), curve.close(), ) ] ) === 参数(Parameters) ```typc curve( fill: none color gradient tiling, fill-rule: str, stroke: none auto length color gradient stroke tiling dictionary, components: ..content, ) → content ``` - *fill*: none color gradient tiling 如何填充曲线。默认值:```typc none```。 设置填充时,默认描边会消失。若要创建同时带有填充和描边的曲线,你需要对两者都进行设置。 - *fill-rule*: str 用于填充曲线的绘制规则。默认值:```typc none-zero```。 #table( columns: (1fr,1fr), align: (left,center), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst // We use `.with` to get a new // function that has the common // arguments pre-applied. #let star = curve.with( fill: red, curve.move((25pt, 0pt)), curve.line((10pt, 50pt)), curve.line((50pt, 20pt)), curve.line((0pt, 20pt)), curve.line((40pt, 50pt)), curve.close(), ) #star(fill-rule: "non-zero") #star(fill-rule: "even-odd") ``` ],[ #set align(horizon) #let star = curve.with( fill: red, curve.move((25pt, 0pt)), curve.line((10pt, 50pt)), curve.line((50pt, 20pt)), curve.line((0pt, 20pt)), curve.line((40pt, 50pt)), curve.close(), ) #star(fill-rule: "non-zero") #star(fill-rule: "even-odd") //#image("images/7023.png",width: auto) ] ) #table( columns: (0.5fr,1.5fr), align: left, stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"non-zero\"],[指定通过带符号边相交次数的非零和来计算“内部”。], [\"even-odd\"],[指定通过奇数次边相交来计算“内部”。], ) - *stroke*: none auto length color gradient stroke tiling dictionary 如何[绘制]曲线描边。默认值:```typc auto```。 可设置为none以关闭描边,或设置为auto:当且仅当未设置填充时,将使用1磅黑色描边。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #let down = curve.line((40pt, 40pt), relative: true) #let up = curve.line((40pt, -40pt), relative: true) #curve( stroke: 4pt + gradient.linear(red, blue), down, up, down, up, down, ) ``` ], [ #set align(horizon+center) #let down = curve.line((40pt, 40pt), relative: true) #let up = curve.line((40pt, -40pt), relative: true) #curve( stroke: 4pt + gradient.linear(red, blue), down, up, down, up, down, ) //#image("images/7024.png",width: 100%) ] ) - *components*: content 曲线的组成部分包括移动点、直线段和贝塞尔曲线段,并且形成闭合图形。 === 定义(Definitions) - *move* 新建一个曲线组件。 如果未传入 curve.move 参数,曲线将从坐标(0pt, 0pt)处开始绘制。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #curve( fill: blue.lighten(80%), fill-rule: "even-odd", stroke: blue, curve.line((50pt, 0pt)), curve.line((50pt, 50pt)), curve.line((0pt, 50pt)), curve.close(), curve.move((10pt, 10pt)), curve.line((40pt, 10pt)), curve.line((40pt, 40pt)), curve.line((10pt, 40pt)), curve.close(), ) ``` ],[ #set align(horizon) #curve( fill: blue.lighten(80%), fill-rule: "even-odd", stroke: blue, curve.line((50pt, 0pt)), curve.line((50pt, 50pt)), curve.line((0pt, 50pt)), curve.close(), curve.move((10pt, 10pt)), curve.line((40pt, 10pt)), curve.line((40pt, 40pt)), curve.line((10pt, 40pt)), curve.close(), ) //#image("images/7025.png",width: auto) ] ) ```typc curve.move( start: array, relative: bool, ) → content ``` -- *start*: array 新组件的起点。 -- *relative*: bool 坐标是否相对于上一个点。默认值:```typc false```。 - *line(直线)* 从当前点向后续点添加一条直线。 #table( columns: (1fr,1fr), align: (left,center), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #curve( stroke: blue, curve.line((50pt, 0pt)), curve.line((50pt, 50pt)), curve.line((100pt, 50pt)), curve.line((100pt, 0pt)), curve.line((150pt, 0pt)), ) ``` ],[ #set align(horizon) #image("images/7026.png",width: auto) ] ) ```typc curve.line( end: array, relative: bool, ) → content ``` -- *end*: array 线条终止的位置。 -- *relative*: bool 坐标是否相对于上一个点。默认值:```typc false```。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #curve( stroke: blue, curve.move((50pt,0pt)), curve.line((50pt, 0pt), relative: true), curve.line((0pt, 50pt), relative: true), curve.line((50pt, 0pt), relative: true), curve.line((0pt, -50pt), relative: true), curve.line((50pt, 0pt), relative: true), ) ``` ], [ #curve( stroke: blue, curve.move((50pt,0pt)), curve.line((50pt, 0pt), relative: true), curve.line((0pt, 50pt), relative: true), curve.line((50pt, 0pt), relative: true), curve.line((0pt, -50pt), relative: true), curve.line((50pt, 0pt), relative: true), ) ] ) - *quad(二次贝塞尔曲线)* 新增一条二次贝塞尔曲线片段,从最后一个点绘制至终点,以control作为控制点。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #curve( stroke: blue, curve.move((20pt,20pt)), curve.quad((50pt, 180pt),(300pt,80pt)), ) ``` ], [ #curve( stroke: blue, curve.move((20pt,20pt)), curve.quad((50pt, 180pt),(300pt,80pt)), ) ] ) ```typc curve.quad( control: none auto array, end: array, relative: bool, ) → content ``` -- *control*: none auto array 二次贝塞尔曲线的控制点。 (1) 若设为自动,且该线段紧随另一条二次贝塞尔曲线,则会镜像上一个控制点。 (2) 若设为无,控制点默认为终点,曲线将变为一条直线。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #curve( stroke: 2pt, curve.quad((20pt, 40pt), (40pt, 40pt), relative: true), curve.quad(auto, (40pt, -40pt), relative: true), ) ``` ], [ #curve( stroke: 2pt, curve.quad((20pt, 40pt), (40pt, 40pt), relative: true), curve.quad(auto, (40pt, -40pt), relative: true), ) ] ) -- *end*: array 该线段的终止点。 -- *relative*: bool 控制坐标与终点坐标是否相对于上一个点。默认值:```typc false```。 - *cubic(三次贝塞尔曲线)* 从最后一个点到终点添加一条三次贝塞尔曲线段,以起点控制点和终点控制点作为控制点。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst // Function to illustrate where the control points are. #let handle(start, end) = place( line(stroke: red, start: start, end: end) ) #handle((0pt, 80pt), (10pt, 20pt)) #handle((90pt, 60pt), (100pt, 0pt)) #curve( stroke: blue, curve.move((0pt, 80pt)), curve.cubic((10pt, 20pt), (90pt, 60pt), (100pt, 0pt)), ) ``` ], [ // Function to illustrate where the control points are. #let handle(start, end) = place( line(stroke: red, start: start, end: end) ) #handle((0pt, 80pt), (10pt, 20pt)) #handle((90pt, 60pt), (100pt, 0pt)) #curve( stroke: blue, curve.move((0pt, 80pt)), curve.cubic((10pt, 20pt), (90pt, 60pt), (100pt, 0pt)), ) ] ) ```typc curve.cubic( control-start: none auto array, control-end:none array, end: array, relative: bool, ) → content ``` -- *control-start*: none auto array 曲线段起点向外延伸的控制点。 (1) 若设为自动,且该元素紧跟另一个三次曲线元素,则上一个控制点会被镜像。用SVG术语来说,这会让三次曲线的行为类似S运算符,而非C运算符。 (2) 若设为无,则该曲线没有第一个控制点,或者等效地,控制点默认为曲线的起点。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #curve( stroke: blue, curve.move((0pt, 50pt)), // - No start control point // - End control point at `(20pt, 0pt)` // - End point at `(50pt, 0pt)` curve.cubic(none, (20pt, 0pt), (50pt, 0pt)), // - No start control point // - No end control point // - End point at `(50pt, 0pt)` curve.cubic(none, none, (100pt, 50pt)), ) #curve( stroke: blue, curve.move((0pt, 50pt)), curve.cubic(none, (20pt, 0pt), (50pt, 0pt)), // Passing `auto` instead of `none` means the start control point // mirrors the end control point of the previous curve. Mirror of // `(20pt, 0pt)` w.r.t `(50pt, 0pt)` is `(80pt, 0pt)`. curve.cubic(auto, none, (100pt, 50pt)), ) #curve( stroke: blue, curve.move((0pt, 50pt)), curve.cubic(none, (20pt, 0pt), (50pt, 0pt)), // `(80pt, 0pt)` is the same as `auto` in this case. curve.cubic((80pt, 0pt), none, (100pt, 50pt)), ) ``` ], [ #curve( stroke: blue, curve.move((0pt, 50pt)), // - No start control point // - End control point at `(20pt, 0pt)` // - End point at `(50pt, 0pt)` curve.cubic(none, (20pt, 0pt), (50pt, 0pt)), // - No start control point // - No end control point // - End point at `(50pt, 0pt)` curve.cubic(none, none, (100pt, 50pt)), ) #curve( stroke: blue, curve.move((0pt, 50pt)), curve.cubic(none, (20pt, 0pt), (50pt, 0pt)), // Passing `auto` instead of `none` means the start control point // mirrors the end control point of the previous curve. Mirror of // `(20pt, 0pt)` w.r.t `(50pt, 0pt)` is `(80pt, 0pt)`. curve.cubic(auto, none, (100pt, 50pt)), ) #curve( stroke: blue, curve.move((0pt, 50pt)), curve.cubic(none, (20pt, 0pt), (50pt, 0pt)), // `(80pt, 0pt)` is the same as `auto` in this case. curve.cubic((80pt, 0pt), none, (100pt, 50pt)), ) ] ) -- *control-end*:none array 曲线段终点处的控制点。 若设置为无,则该曲线没有终点控制点,等价于控制点默认为曲线的终点。 -- *end*: array 曲线段的终止点。 -- *relative*: bool 控制起点、控制终点以及结束坐标是否相对于前一个点。默认值:```typc false```。 - *close* 通过增加一条从最后一个点到曲线起点(或上一个curve.move的最后一个点)的线段来闭合曲线。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst // We define a function to show the same shape with // both closing modes. #let shape(mode: "smooth") = curve( fill: blue.lighten(80%), stroke: blue, curve.move((0pt, 50pt)), curve.line((100pt, 50pt)), curve.cubic(auto, (90pt, 0pt), (50pt, 0pt)), curve.close(mode: mode), ) #shape(mode: "smooth") #shape(mode: "straight") ``` ], [ // We define a function to show the same shape with // both closing modes. #let shape(mode: "smooth") = curve( fill: blue.lighten(80%), stroke: blue, curve.move((0pt, 50pt)), curve.line((100pt, 50pt)), curve.cubic(auto, (90pt, 0pt), (50pt, 0pt)), curve.close(mode: mode), ) #shape(mode: "smooth") #shape(mode: "straight") ] ) ```typc curve.close(mode: str) → content ``` -- *mode*: str 如何闭合曲线。默认值:```typc smooth```。 #table( columns: (0.5fr,2fr), align: left, stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"smooth\"],[采用平滑线段闭合曲线,该线段会考虑起点对侧的控制点。], [\"straight\"],[用直线闭合曲线。], ) == 椭圆(ellipse) 可附带可选内容的椭圆。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst // Without content. #ellipse(width: 35%, height: 30pt) // With content. #ellipse[ #set align(center) Automatically sized \ to fit the content. ] ``` ],[ // Without content. #ellipse(width: 35%, height: 30pt) // With content. #ellipse[ #set align(center) Automatically sized \ to fit the content. ] ] ) === 参数(Parameters) ```typc ellipse( width: auto relative, height: auto relative fraction, fill: none color gradient tiling, stroke: none auto length color gradient stroke tiling dictionary, inset: relative dictionary, outset: relative dictionary, body: none content, ) → content ``` - *width*: auto relative 椭圆相对于其父容器的宽度。默认值:```typc auto```。 - *height*: auto relative fraction 椭圆相对于其父容器的高度。默认值:```typc auto```。 - *fill*: none color gradient tiling 如何填充椭圆。更多详情请参阅矩形的相关文档。默认值:```typc none```。 - *stroke*: none auto length color gradient stroke tiling dictionary 如何绘制椭圆轮廓。更多详情请参阅矩形的文档。默认值:```typc auto```。 - *inset*: relative dictionary 椭圆内容应填充多少。更多详情请参阅#link()[盒子]的文档。默认值:```typc 0%+5pt```。 - *outset*: relative dictionary 在不影响布局的前提下,椭圆尺寸需要扩大多少。更多详情请参阅#link()[盒子]的文档说明。默认值:```typc (:)```。 - *body*: none content 放入椭圆内的内容。默认值:```typc none```。 当省略此项时,椭圆将采用默认尺寸,最大为45pt×30pt。 == 渐变(gradient) 颜色渐变。 Typst通过gradient.linear函数支持线性渐变,通过gradient.radial函数支持径向渐变,通过gradient.conic函数支持圆锥渐变。 渐变可用于以下目的: -- 作为填充来绘制形状的内部:```typc rect(fill: gradient.linear(..))``` -- 作为绘制形状轮廓的笔划:```typc rect(stroke: 1pt + gradient.linear(..))``` -- 作为文本填充:```typc set text(fill: gradient.linear(..))``` -- 作为一张颜色映射,你可以用以下方法采样:```typc gradient.linear(..).sample(50%)``` === 示例(Examples) #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #stack( dir: ltr, spacing: 1fr, square(fill: gradient.linear(..color.map.rainbow)), square(fill: gradient.radial(..color.map.rainbow)), square(fill: gradient.conic(..color.map.rainbow)), ) ``` ], [ #stack( dir: ltr, spacing: 1fr, square(fill: gradient.linear(..color.map.rainbow)), square(fill: gradient.radial(..color.map.rainbow)), square(fill: gradient.conic(..color.map.rainbow)), ) ] ) 文本同样支持渐变效果,但仅在将 relativeness属性设置为 #text(fill: red,"auto")(默认值)或 "#text(fill: red,"parent")" 时生效。如果想要实现逐词或逐字符的渐变,你可以手动将文字或字符放入#link()[容器]内,也可以通过#link()[show规则]来实现。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #set text(fill: gradient.linear(red, blue)) #let rainbow(content) = { set text(fill: gradient.linear(..color.map.rainbow)) box(content) } This is a gradient on text, but with a #rainbow[twist]! ``` ], [ #set text(fill: gradient.linear(red, blue)) #let rainbow(content) = { set text(fill: gradient.linear(..color.map.rainbow)) box(content) } This is a gradient on text, but with a #rainbow[twist]! ] ) === 色标(Stops) 渐变由一系列色标组成。每个色标都包含一种颜色和一个偏移量。偏移量可以是介于#rtext[0%]到#rtext[100%]之间的#link()[比例],或是#rtext[0]度到#rtext[360]度之间的角度。偏移量是相对位置,用于确定该色标在渐变中的位置。色标的颜色就是渐变在该位置处呈现的颜色。定义渐变时可以省略偏移量,这种情况下,Typst会均匀分布所有色标。 Typst预定义了若干颜色映射,可直接用作色标。更多信息请查阅#link()[颜色]相关文档。 === 相对性(Relativeness) 0%和100%色标停止点的位置取决于容器的尺寸。该容器可以是正在被绘制的图形本身,或是距离最近的外层容器。这一点由渐变构造函数的relative参数控制。默认情况下,渐变相对于正在绘制的图形;但如果渐变应用于文本,则渐变会相对于最近的父级容器。 Typst 按以下方式确定父级容器: -- 对于放置在文档根级/顶层的图形,其最近父级就是页面本身。 -- 对于其他图形,父级为包含该图形的最内层#link()[块]或#link()[盒子]。 === 色彩空间与插值(Color spaces and interpolation) 渐变可以在任意色彩空间中进行插值。默认情况下,渐变会在#link()[Oklab]色彩空间内插值,这是一个#link("https://programmingdesignsystems.com/color/perceptually-uniform-color-spaces/index.html")[感知均匀]的色彩空间。这代表人眼会觉得该渐变的色彩过渡平滑。这一点在数据可视化场景中尤其实用。 不过,你可以自行选择在任意受支持的色彩空间中对渐变进行插值,但需要注意:部分色彩空间并不适合用于色彩之间的感知插值。选择插值空间时,请参考下方表格。 #table( columns: (1fr,1fr), stroke: none, table.header([*Color space*],[*Perceptually uniform?*]), table.hline(), [#link()[Oklab]],[Yes], [#link()[Oklch]],[Yes], [#link()[sRGB]],[No], [#link()[linear-RGB]],[Yes], [#link()[CMYK]],[No], [#link()[Grayscale]],[Yes], [#link()[HSL]],[No], [#link()[HSV]],[No], ) #figure( image("images/7028.png") ) === 方向(Direction) 部分渐变对方向敏感。例如,线性渐变拥有一个角度,该角度决定它的方向。Typst 使用顺时针角度,0°代表从左到右,90°代表从上到下,180°代表从右到左,270°代表从下到上。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #stack( dir: ltr, spacing: 1fr, square(fill: gradient.linear(red, blue, angle: 0deg)), square(fill: gradient.linear(red, blue, angle: 90deg)), square(fill: gradient.linear(red, blue, angle: 180deg)), square(fill: gradient.linear(red, blue, angle: 270deg)), ) ``` ], [ #stack( dir: ltr, spacing: 1fr, square(fill: gradient.linear(red, blue, angle: 0deg)), square(fill: gradient.linear(red, blue, angle: 90deg)), square(fill: gradient.linear(red, blue, angle: 180deg)), square(fill: gradient.linear(red, blue, angle: 270deg)), ) ] ) === 文件大小说明(Note on file sizes) 渐变的数据量可能会相当大,尤其是在包含大量色标时。这是因为渐变是以颜色和偏移量列表的形式存储的,会占用大量空间。在导出SVG时,渐变会以#link()[color.rgb]颜色列表保存,还原原始色彩空间,并在中间添加经过数量优化的额外色标。在导出PDF时,对于#link()[color.oklab]、#link()[color.oklch]、#link()[color.hsv]、#link()[color.hsl]以及#link()[color.linear-rgb]色彩空间下的渐变,处理方式相同。这样就无需在PDF文件中对这些色彩空间进行编码,但会为渐变增加额外色标,进而增大文件体积。 === 定义(Definitions) - *linear* 创建一个新的线性渐变,其中颜色沿一条直线过渡。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #rect( width: 100%, height: 20pt, fill: gradient.linear( ..color.map.viridis, ), ) ``` ],[ #set align(horizon) #rect( width: 100%, height: 20pt, fill: gradient.linear( ..color.map.viridis, ), ) ] ) ```typc gradient.linear( stops: ..color array, space: auto spot any, relative: auto str, dir: direction, angle: angle, ) → gradient ``` -- *stops*: color array 渐变的#link()[色标]。 -- *space*: auto spot any 用于对渐变进行插值的色彩空间。默认值:```typc auto```。 默认使用名为 #link()[Oklab] 的感知均匀色彩空间。 -- *relative*: auto str 渐变的#link()[相对位置]。默认值:```typc auto```。 元素的父级是包含该元素的最内层#link()[盒子]或#link()[块];如果不存在,则为页面本身。 #table( columns: (1fr,2fr), stroke: none, table.header([*Variant*],[*PDetails*]), table.hline(), [\"self\"],[相对于其自身(自身包围盒)。], [\"parent\"],[相对于其父对象(父对象的边界框)。], ) -- *dir*: direction 渐变的方向。默认值:```typc ltr```。 -- *angle*: angle 渐变的角度。 - *radial* 创建一个新的径向渐变,颜色从中心点向外辐射。 该渐变由两个圆定义:焦点圆和终止圆。焦点圆是以焦点中心点为圆心、焦点半径为半径的圆,用于标记渐变的起始位置,该位置使用第一个色标对应的颜色。终止圆是以中心点为圆心、半径为半径的圆,用于标记渐变的结束位置,该位置使用最后一个色标对应的颜色。渐变将在这两个圆之间进行插值计算。 借助这四个参数(即起始圆的焦点,以及终止圆的圆心和半径),我们可以定义出属性比基础径向渐变更加丰富的渐变效果。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #stack( dir: ltr, spacing: 1fr, circle(fill: gradient.radial( ..color.map.viridis, )), circle(fill: gradient.radial( ..color.map.viridis, focal-center: (10%, 40%), focal-radius: 5%, )), ) ``` ],[ #stack( dir: ltr, spacing: 1fr, circle(fill: gradient.radial( ..color.map.viridis, )), circle(fill: gradient.radial( ..color.map.viridis, focal-center: (10%, 40%), focal-radius: 5%, )), ) ] ) ```typst gradient.radial( stops:..color array, space: auto spot any, relative: auto str, center: array, radius: ratio, focal-center: auto array, focal-radius: ratio, ) → gradient ``` -- *stops*: color array 渐变的#link()[色标]。 -- *space*: auto spot any 用于对渐变进行插值的色彩空间。默认值:```typc auto```。 默认使用名为#link()[Oklab]的感知均匀色彩空间。 -- *relative*: auto str 渐变的#link()[相对位置]。默认值:```typc auto```。 元素的父级是包含该元素的最内层#link()[盒子]或#link()[块];如果不存在,则为页面本身。 #table( columns: (1fr,2fr), stroke: none, table.header([*Variant*],[*PDetails*]), table.hline(), [\"self\"],[相对于其自身(自身包围盒)。], [\"parent\"],[相对于其父对象(父对象的边界框)。], ) -- *center*: array 渐变终止圆的圆心。默认值:```typc center```。 取值(#rtext[50%], #rtext[50%])代表终止圆在其容器内居中。 -- *radius*: ratio 渐变终止圆的半径。默认值:```typc 50%```。 默认值设为#rtext()[50%]。终止半径必须大于焦点半径。 -- *focal-center*: auto array 渐变焦点圆的中心。默认值:```typc auto```。 焦点中心必须位于终止圆内部。 (#rtext[50%], #rtext[50%]) 这个值代表焦点圆以其容器中心为圆心。 默认情况下,该值与最后一个圆的中心保持一致。 -- *focal-radius*: ratio 渐变焦点圆的半径。默认值:```typc 0%```。 焦点中心必须位于终止圆内部。 默认设置为#rtext[0%]。焦点半径必须小于终止半径。 - *conic* 创建新的圆锥渐变,其中颜色围绕中心点呈径向变化。 你可以通过 center 参数控制渐变的中心点。默认情况下,中心点为形状的中心。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #stack( dir: ltr, spacing: 1fr, circle(fill: gradient.conic( ..color.map.viridis, )), circle(fill: gradient.conic( ..color.map.viridis, center: (20%, 30%), )), ) ``` ],[ #stack( dir: ltr, spacing: 1fr, circle(fill: gradient.conic( ..color.map.viridis, )), circle(fill: gradient.conic( ..color.map.viridis, center: (20%, 30%), )), ) ] ) ```typc gradient.conic( stops: ..color array, angle: angle, space: auto spot any, relative: auto str, center: array, ) → gradient ``` -- *stops*: color array 渐变的#link()[色标]。 -- *angle*: angle 渐变的角度。默认值:```typc 0deg```。 -- *space*: auto spot any 用于对渐变进行插值的色彩空间。默认值:```typc auto```。 -- *relative*: auto str 渐变的#link()[相对位置]。默认值:```typc auto```。 元素的父级是包含该元素的最内层#link()[盒子]或#link()[块];如果不存在,则为页面本身。 #table( columns: (1fr,2fr), stroke: none, table.header([*Variant*],[*PDetails*]), table.hline(), [\"self\"],[相对于其自身(自身包围盒)。], [\"parent\"],[相对于其父对象(父对象的边界框)。], ) -- *center*: array 渐变终止圆的圆心。默认值:```typc (50%,50%)```。 取值(#rtext[50%], #rtext[50%])代表终止圆在其容器内居中。 - *sharp* 生成该渐变的锐化版本。 锐化渐变的颜色之间存在离散突变,而非平滑过渡。它们对于创建预设渐变的颜色列表尤其有用。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #set rect(width: 100%, height: 20pt) #let grad = gradient.linear(..color.map.rainbow) #rect(fill: grad) #rect(fill: grad.sharp(5)) #rect(fill: grad.sharp(5, smoothness: 20%)) ``` ], [ #set rect(width: 100%, height: 20pt) #let grad = gradient.linear(..color.map.rainbow) #rect(fill: grad) #rect(fill: grad.sharp(5)) #rect(fill: grad.sharp(5, smoothness: 20%)) ] ) ```typst self.sharp( steps: int, smoothness: ratio, ) → gradient ``` -- *steps*: int 渐变的色标数。 -- *smoothness*: ratio, 渐变的光滑度。默认值:```typc 0%```。 - *repeat* 将该渐变重复指定次数,可选择在每第二次重复时对其进行镜像翻转。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #circle( radius: 40pt, fill: gradient .radial(aqua, white) .repeat(4), ) ``` ],[ #circle( radius: 40pt, fill: gradient .radial(aqua, white) .repeat(4), ) ] ) ```typst self.repeat( repetitions: int, mirror: bool, ) → gradient ``` -- *repetitions*: int 渐变重复次数。 -- *mirror*: bool 是否在每第二次重复时对梯度进行镜像处理,即第一次实例(以及所有奇数次实例)保持不变。默认值:```typc false```。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #circle( radius: 40pt, fill: gradient .conic(green, black) .repeat(2, mirror: true) ) ``` ],[ #circle( radius: 40pt, fill: gradient .conic(green, black) .repeat(2, mirror: true) ) ] ) - *kind* 返回该渐变的类型。 ```typst self.kind() → function ``` - *stops* 返回此渐变的色标。 ```typst self.stops() → array ``` - *space* 返回此渐变的混合空间。 ```typst self.space() → spot any ``` - *relative* 返回此渐变的相对位置。 ```typst self.relative() → auto str ``` - *angle* 返回此渐变的角度。 如果该渐变既不是线性渐变也不是圆锥渐变,则返回空值。 ```typst self.angle() → none angle ``` - *center* 返回此渐变的中心点。 如果该渐变既不是径向渐变也不是圆锥渐变,则返回空值。 ```typst self.center() → none array ``` - *radius* 返回此渐变的半径。 如果该渐变不是径向渐变,则返回空。 ```typst self.radius() → none ratio ``` - *focal-center* 返回此渐变的焦点中心。 如果该渐变不是径向渐变,则返回空。 ```typst self.focal-center() → none array ``` - *focal-radius* 返回此渐变的焦点半径。 如果该渐变不是径向渐变,则返回空。 ```typst self.focal-radius() → none ratio ``` - *sample* 在指定位置对渐变进行采样。 该位置可以是渐变上的位置(0%到100%之间的比例值),也可以是一个角度。超出此范围的所有数值都将被限制在区间内。 ```typst self.sample(t: angle ratio) → color ``` - *samples* 一次性在多个位置对渐变进行采样,并以数组形式返回结果。 ```typst self.samples(ts: ..angle ratio) → array ``` -- *ts*: angle ratio 用于采样渐变的位置。 == 图像(image) 光栅图形或矢量图形。 你可以将图片放置在figure标签内,为其添加编号与图注。 和大多数元素一样,图片默认是块级元素,因此不会嵌入相邻段落。若要强制让图片变为行内元素,可将其放入容器中。 === 示例(Example) #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #figure( image("images/molecular.png", width: 80%), caption: [ A step in the molecular testing pipeline of our lab. ], ) ``` ],[ #figure( image("images/molecular.png", width: 80%), caption: [ A step in the molecular testing pipeline of our lab. ], ) ] ) === 参数(Parameters) ```typc image( source: str path bytes, format: auto str dictionary, width: auto relative, height: auto relative fraction, alt: none str, page: int, fit: str, scaling: auto str, icc: auto str path bytes, ) → content ``` - *source*: str path bytes 图像文件的路径,或是采用支持格式之一构成图像的原始字节数据。 字节可用于指定行优先、从左至右、从上到下格式的原始像素数据。 - *format*: auto str dictionary\ 图像的格式。默认值:```typc auto```。\ 默认情况下会自动检测格式。通常,仅当以原始字节作为源时才需要指定格式(即便在这种情况下,Typst 也会尝试自动识别格式,但并非总能成功)。\ 支持的格式有 "png"、"jpg"、"gif"、"svg"、"pdf"、"webp",以及原始像素数据。\ 请注意,将PDF文件作为图像使用时有多项限制: - 导出为PDF时,所使用的PDF图像文件版本必须等于或低于导出目标PDF版本。 - 在按特定PDF标准(如PDF/A-3或PDF/UA-1)导出时,目前不支持将PDF文件用作图像。这种情况下,你可以改用SVG嵌入矢量图像。 - 该图像文件不得设置密码保护。 - PDF图像内的标签不会被保留。你必须提供替代描述,保障图像的无障碍访问。\ 在将原始像素数据作为源提供时,你必须指定一个包含以下键的字典作为格式: - 编码(字符串):像素数据的编码格式。可选值如下: - "rgb8"(3个8位通道:红、绿、蓝) - "rgba8"(4个8位通道:红、绿、蓝、阿尔法) - "luma8"(1个8位通道) - "lumaa8"(2个8位通道:亮度和阿尔法) - 宽度(整数):图像的像素宽度。 - 高度(整数):图像的像素高度。 指定编码下,像素宽度乘以高度再乘以通道数的结果,必须与源数据一致。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #image( read( "images/tetrahedron.png", encoding: none, ), format: "png", width: 2cm, ) #image( bytes(range(16).map(x => x * 16)), format: ( encoding: "luma8", width: 4, height: 4, ), width: 2cm, ) ``` ],[ #image( read( "images/tetrahedron.png", encoding: none, ), format: "png", width: 2cm, ) #image( bytes(range(16).map(x => x * 16)), format: ( encoding: "luma8", width: 4, height: 4, ), width: 2cm, ) ] ) #table( columns: (0.5fr,2.5fr), stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"png\"],[适用于插图与透明图形的光栅格式。], [\"jpg\"],[适用于照片的有损光栅格式。], [\"gif\"],[通常用于短视频片段的光栅格式。Typst可以加载GIF文件,但加载后会变为静态图像。], [\"webp\"],[支持有损压缩与无损压缩的光栅格式。], [\"svg\"],[网页所用的矢量图形格式。], [\"pdf\"],[网页所用的矢量图形格式。], ) - *width*: auto relative 图像的宽度。默认值:```typc auto```。 - *height*: auto relative fraction 图像的高度。默认值:```typc auto```。 - *alt*: none str 图像的替代描述文本。默认值:```typc none```。 屏幕阅读器等辅助技术(AT)会使用该文本,向视障用户描述图像内容。 当图像放置在figure元素内时,请使用该参数描述图像,而不是figure元素的alt参数。唯一例外情况:图像与figure内其他内容构成单一语义单元。这种场景下,请使用figure元素的alt参数描述整体内容,不要使用本参数。 你可以在无障碍指南中学习如何编写优质的替代描述文本。 - *page*: int 应作为图片嵌入的页码。该属性仅对PDF文件生效。默认值:```typc 1```。 - *fit*: str 图像应如何自适应到指定区域(该区域由宽度和高度字段定义)。默认值:```typc cover```。 请注意,如果区域的宽高比与图像的宽高比一致,fit参数不会产生任何视觉上的变化。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #image("images/tiger1.png", width: 80%, fit: "cover") #image("images/tiger1.png", width: 80%, fit: "contain") #image("images/tiger1.png", width: 80%, fit: "stretch") ``` ], [ #set align(center) #image("images/tiger1.png", width: 80%, fit: "cover") #image("images/tiger1.png", width: 100%, fit: "contain") #image("images/tiger1.png", width: 100%, fit: "stretch") ] ) #table( columns: (0.5fr,2.5fr), stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"cover\"],[图片应完全铺满区域(仅通过横向或纵向裁剪图片来保持宽高比)。此为默认设置。], [\"contain\"],[图像应完整容纳在该区域内(保持宽高比;不裁剪图像;某一边尺寸可小于规定值)。], [\"stretch\"],[图像应进行拉伸以完全填满区域,即使这会造成图像变形(不保留宽高比,不对图像进行裁剪)。], ) - *scaling*: auto str 向查看者提示图像应如何缩放。默认值:```typc auto```。 设置为auto时,默认行为交由查看器决定。导出PNG时,Typst默认使用平滑缩放,与大多数PDF和SVG查看器一致。 注意:不同PDF查看器呈现的最终效果可能存在差异。 #table( columns: (0.5fr,2.5fr), stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"smooth\"],[采用双线性插值等平滑算法进行缩放。], [\"pixelated\"],[使用最近邻算法或类似算法进行缩放,以保留图像的像素风格。], ) - *icc*: auto str path bytes 图像的ICC配置文件。默认值:```typc auto```。 ICC配置文件规定如何解读图像中的颜色。设置为自动时,Typst将尝试从图像中提取ICC配置文件。 == 直线段(line) 从一点到另一点的线段。 === 示例(Example) #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #line(length: 100%) #line(end: (50%, 20%)) #line( length: 4cm, stroke: 2pt + maroon, ) ``` ],[ //#set page(height: 100pt) #line(length: 100%) #line(end: (50%, 20%)) #line( length: 4cm, stroke: 2pt + maroon, ) ] ) === 参数(Parameters) ```typc line( start: array, end: none array, length: relative, angle: angle, stroke: length color gradient stroke tiling dictionary, ) → content ``` - *start*: array 线段的起点。默认值:```typc (0%+0pt,0%+0pt)```。 必须是由恰好两个相对长度组成的数组。 - *end*: none array 线段终止的位置。默认值:```typc none```。 - *length*: relative 线段的长度。仅当end为#rtext[none]时,该参数才生效。默认值:```typc 0%+30pt```。 - *angle*: angle 直线偏离原点的角度。仅当end为#rtext[none]时该参数生效。默认值:```typc 0deg```。 - *stroke*: length color gradient stroke tiling dictionary 如何绘制线段。默认值:```typc 1pt+black```。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #set line(length: 100%) #stack( spacing: 1em, line(stroke: 2pt + red), line(stroke: (paint: blue, thickness: 4pt, cap: "round")), line(stroke: (paint: blue, thickness: 1pt, dash: "dashed")), line(stroke: (paint: blue, thickness: 1pt, dash: ("dot", 2pt, 4pt, 2pt))), ) ``` ], [ #set line(length: 100%) #stack( spacing: 1em, line(stroke: 2pt + red), line(stroke: (paint: blue, thickness: 4pt, cap: "round")), line(stroke: (paint: blue, thickness: 1pt, dash: "dashed")), line(stroke: (paint: blue, thickness: 1pt, dash: ("dot", 2pt, 4pt, 2pt))), ) ] ) == 多边形(polygon) 一个闭合多边形。 该多边形由顶点定义,并自动闭合。 === 示例(Example) #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #polygon( fill: blue.lighten(80%), stroke: blue, (20%, 0pt), (60%, 0pt), (80%, 2cm), (0%, 2cm), ) ``` ],[ #polygon( fill: blue.lighten(80%), stroke: blue, (20%, 0pt), (60%, 0pt), (80%, 2cm), (0%, 2cm), ) ] ) === 参数(Parameters) ```typc polygon( fill: none color gradient tiling, fill-rule: str, stroke: none auto length color gradient stroke tiling dictionary, vertices: ..array, ) → content ``` - *fill*: none color gradient tiling 如何填充多边形。默认值:```typc none```。 设置填充时,默认的描边会消失。要创建同时带有填充和描边的矩形,你需要对两者都进行配置。 - *fill-rule*: str 用于填充多边形的绘制规则。默认值:```typc none-zero```。 有关示例,请参阅#link()[曲线文档]。 #table( columns: (0.5fr,1.5fr), stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"non-zero\"],[指定“内部”通过有符号边相交的非零和计算得出。], [\"even-odd\"],[指定通过奇数次边交叉来判定“内部”。], ) - *stroke*: none auto length color gradient stroke tiling dictionary 如何绘制多边形描边。默认值:```typc auto```。 可设置为#rtext[none]以关闭描边,或设置为#rtext[auto];当且仅当未设置填充时,auto代表1磅黑色描边。 - *vertices*: array 多边形的顶点。每个点由包含两个相对长度的数组指定。 === 定义(Definitions) - *regular* 由尺寸和顶点数量定义的正多边形。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #polygon.regular( fill: blue.lighten(80%), stroke: blue, size: 50pt, vertices: 3, ) ``` ],[ #polygon.regular( fill: blue.lighten(80%), stroke: blue, size: 50pt, vertices: 3, ) ] ) ```typc polygon.regular( fill: none color gradient tiling, stroke: none auto length color gradient stroke tiling dictionary, size: length, vertices: int, ) → content ``` -- *fill*: none color gradient tiling 如何填充多边形。更多详情请参阅通用#link()[多边形的文档]。 -- *stroke*: none auto length color gradient stroke tiling dictionary 如何绘制多边形描边。更多详情请参阅通用#link()[多边形文档]。 -- *size*: length 正多边形#link("https://en.wikipedia.org/wiki/Circumcircle")[外接圆]的直径。默认值:```typc 1em```。 -- *vertices*: int 多边形的顶点数量。默认值:```typc 3```。 == 矩形(rect) 一个可附带可选内容的矩形。 === 示例(Example) #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst // Without content. #rect(width: 35%, height: 30pt) // With content. #rect[ Automatically sized \ to fit the content. ] ``` ],[ // Without content. #rect(width: 35%, height: 30pt) // With content. #rect[ Automatically sized \ to fit the content. ] ] ) === 参数(Parameters) ```typc rect( width: auto relative, height: auto relative fraction, fill: none color gradient tiling, stroke: none auto length color gradient stroke tiling dictionary, radius: relative dictionary, inset: relative dictionary, outset: relative dictionary, body: none content, ) → content ``` - *width*: auto relative 该矩形相对于其父容器的宽度。默认值:```typc auto```。 - *height*: auto relative fraction 该矩形相对于其父容器的高度。默认值:```typc auto```。 - *fill*: none color gradient tiling 如何填充矩形。默认值:```typc none```。 设置填充时,默认描边会消失。要创建同时带有填充和描边的矩形,必须对两者都进行设置。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #rect(fill: blue) ``` ],[ #rect(fill: blue) ] ) - *stroke*: none auto length color gradient stroke tiling dictionary\ 如何绘制矩形描边。默认值:```typc auto```。\ 可选设置如下: - none:不启用描边 - auto:仅当未设置填充时,自动使用1磅黑色描边 - 任意类型的描边 - 单独定义每条边描边的字典。该字典可包含以下优先级依次递减的键: - top:顶部笔画。 - right:右侧笔画。 - bottom:底部笔画。 - left:左侧笔画。 - x:左右笔画。 - y:上下笔画。 - 其余:除字典显式设置尺寸的边之外,其余各边的笔画。 所有关键词均为可选项;省略的关键词将使用其先前设置的值,若从未设置过则使用默认绘制。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #stack( dir: ltr, spacing: 1fr, rect(stroke: red), rect(stroke: 2pt), rect(stroke: 2pt + red), ) ``` ],[ #stack( dir: ltr, spacing: 1fr, rect(stroke: red), rect(stroke: 2pt), rect(stroke: 2pt + red), ) ] ) - *radius*: relative dictionary\ 矩形圆角的圆化程度,取值相对于宽、高最小值的二分之一。默认值:```typc (:)```。\ 可设置为: - 相对长度:统一圆角半径。 - 字典:使用字典时,可单独设置每条边的描边。字典可包含以下键,优先级顺序如下: - 左上角(top-left):左上角圆角半径。 - 右上角(top-right):右上角圆角半径。 - 右下角(bottom-right):右下角圆角半径。 - 左下角(bottom-left):左下角圆角半径。 - 左侧(left):左上角和左下角圆角半径。 - 顶部(top):左上角和右上角圆角半径。 - 右侧(right):右上角和右下角圆角半径。 - 底部(bottom):左下角和右下角圆角半径。 - 其余(rest):字典未显式设置尺寸的所有角的圆角半径。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #set rect(stroke: 4pt) #rect( radius: ( left: 5pt, top-right: 20pt, bottom-right: 10pt, ), stroke: ( left: red, top: yellow, right: green, bottom: blue, ), ) ``` ],[ #set rect(stroke: 4pt) #rect( radius: ( left: 5pt, top-right: 20pt, bottom-right: 10pt, ), stroke: ( left: red, top: yellow, right: green, bottom: blue, ), ) ] ) - *inset*: relative dictionary 矩形内容的内边距大小。更多详情请参阅#link()[盒子相关文档]。默认值:```typc 0%+5pt```。 - *outset*: relative dictionary 在不影响布局的前提下,矩形尺寸可以扩充多少。更多详情请参阅#link()[盒子相关文档]。默认值:```typc (:)```。 - *body*: none content 要放入矩形内的内容。默认值:```typc none```。 如果省略此项,矩形将采用默认尺寸,最大为45磅×30磅。 == 正方形(square) 一个可附带可选内容的正方形。 === 示例(Example) #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst // Without content. #square(size: 40pt) // With content. #square[ Automatically \ sized to fit. ] ``` ],[ // Without content. #square(size: 40pt) // With content. #square[ Automatically \ sized to fit. ] ] ) === 参数(Parameters) ```typc square( size: auto length, width: auto relative, height: auto relative fraction, fill: none color gradient tiling, stroke: none auto length color gradient stroke tiling dictionary, radius: relative dictionary, inset: relative dictionary, outset: relative dictionary, body: none content, ) → content ``` - *size*: auto length 正方形的边长。该参数与宽度、高度互斥。默认值:```typc auto```。 - *width*: auto relative 正方形的宽度。该属性与size和height互斥。默认值:```typc auto```。 与size不同,该值可以相对于父容器的宽度进行设置。 - *height*: auto relative fraction 该正方形的高度。此属性与size、width互斥。默认值:```typc auto```。 与size不同,该值可以相对于父容器的高度进行设置。 - *fill*: none color gradient tiling 如何填充正方形。更多详情请参阅#link()[矩形的文档]。默认值:```typc none```。 - *stroke*: none auto length color gradient stroke tiling 如何绘制正方形描边。更多详情请参阅#link()[矩形的文档]。默认值:```typc auto```。 - *radius*: relative dictionary 正方形边角的圆角程度。更多详情请参阅#link()[矩形相关文档]。默认值:```typc (:)```。 - *inset*: relative dictionary 正方形内容的内边距大小。更多详情请参阅#link()[盒子相关文档]。默认值:```typc 0%+5pt```。 - *outset*: relative dictionary 在不影响布局的前提下,正方形尺寸可以扩充多少。更多详情请参阅#link()[盒子相关文档]。默认值:```typc (:)```。 - *body*: none content 放置到方块内的内容。方块会自动扩展以适配该内容,并保持1:1的宽高比。默认值:```typc none```。 若省略此项,方块默认最大尺寸为#rtext[30pt]。 == 描边(stroke) 定义如何绘制一条线。 描边包含填充(纯色或渐变)、粗细、线帽、线连接、斜接限制以及虚线样式。所有这些属性值均为可选项,并带有合理的默认值。 === 示例(Example) #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #set line(length: 100%) #stack( spacing: 1em, line(stroke: 2pt + red), line(stroke: (paint: blue, thickness: 4pt, cap: "round")), line(stroke: (paint: blue, thickness: 1pt, dash: "dashed")), line(stroke: (paint: blue, thickness: 1pt, dash: ("dot", 2pt, 4pt, 2pt))), ) ``` ], [ #set line(length: 100%) #stack( spacing: 1em, line(stroke: 2pt + red), line(stroke: (paint: blue, thickness: 4pt, cap: "round")), line(stroke: (paint: blue, thickness: 1pt, dash: "dashed")), line(stroke: 2pt + gradient.linear(..color.map.rainbow)), ) ] ) === 简单描边(Simple strokes) 你可以通过颜色、线条粗细,或是两者组合来创建简单的实线描边。具体来说,在需要传入描边参数的位置,你可以传递以下任意一种值: -- 用于指定描边粗细的长度值。颜色会继承上层设置,默认为黑色。 -- 描边所使用的颜色。粗细会继承上层设置,默认为#rtext[1pt]。 -- 使用加号运算符组合颜色与粗细的描边,例如 #rtext[2pt +] red。 如果需要完全自定义,你也可以向任何需要描边参数的函数传入#link()[字典]或描边对象。字典的键可以包含构造函数的任意参数,如下所示。 === 字段(Fields) 在描边对象上,你可以访问构造函数中列出的任意字段。例如,(2pt + blue).thickness 的值为2pt。同时,stroke(red).cap 为自动,因为该属性未指定。设置为自动的字段会继承属性值。 === 构造函数(Constructor) 将值转换为描边,或使用给定参数构建描边。 请注意,在大多数情况下,你无需手动将值转换为描边即可使用,系统会自动完成转换。不过,该构造函数可用于确保一个值拥有笔触的全部字段。 ```typc stroke( paint: auto color gradient tiling, thickness:auto length, cap: auto str, join: auto str, dash: none auto str array dictionary, miter-limit: auto float, ) → stroke ``` - *paint*: auto color gradient tiling 用于描边的颜色或渐变。 如果设置为#rtext[auto],则该值会继承,默认为黑色。 - *thickness*: auto length 描边粗细。 若设置为自动,该值将继承,默认值为#rtext[1pt]。 - *cap*: auto str 描边端点的渲染方式。 若设置为#rtext[auto],则继承该属性值,默认值为\"butt\"。 #table( columns: (0.5fr,2fr), stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"butt\"],[方形描边端点,边线位于描边的终点位置。], [\"round\"],[以描边终点为中心的圆形描边端点。], [\"square\"],[以描边终点为中心的方形描边端点。], ) - *join*: auto str 急弯的渲染方式。 若设置为自动,该值将继承,默认值为“miter”。 #table( columns: (0.5fr,2fr), stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"miter\"],[线段以尖角相连。超过斜接限制的急剧弯折将改为斜切处理。], [\"round\"],[线段以圆角相连。], [\"bevel\"],[线段采用斜切方式连接(用直边对接相连线段的端头)。], ) - *dash*: none auto str array dictionary\ 要使用的虚线样式。可以是以下类型之一: - 下文列出的预定义样式之一。 - none,等同于预定义样式“solid”(实线)。 - 由线段与间隔交替长度组成的数组。你也可以使用字符串“dot”代表长度等于线条粗细的点。 - 包含array(与上面的数组相同)和phase(长度类型)键的字典,phase用于定义从样式的哪个位置开始绘制。 如果设置为#rtext[auto],则该值会继承,默认值为#rtext[none]。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #set line(length: 100%, stroke: 2pt) #stack( spacing: 1em, line(stroke: (dash: "dashed")), line(stroke: (dash: (10pt, 5pt, "dot", 5pt))), line(stroke: (dash: (array: (10pt, 5pt, "dot", 5pt), phase: 10pt))), ) ``` ], [ #set line(length: 100%, stroke: 2pt) #stack( spacing: 1em, line(stroke: (dash: "dashed")), line(stroke: (dash: (10pt, 5pt, "dot", 5pt))), line(stroke: (dash: (array: (10pt, 5pt, "dot", 5pt), phase: 10pt))), ) ] ) #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #let patterns = ( ("dotted", "dashed", "dash-dotted").map(d => ( "loosely-" + d, d, "densely-" + d, )) ).flatten() #let display(dash) = { set par(spacing: 0.3em) show "loosely": set text(eastern.darken(17%)) show "densely": set text(purple) let derived = dash.starts-with("loosely-") or dash.starts-with("densely-") if derived { dash } else { strong(dash) } rect( line(stroke: (dash: dash), length: 100%), stroke: none, fill: yellow.transparentize(80%), ) } 预定义模式的盒子: #table( columns: 3, stroke: none, align: (left, center, left), table.cell(colspan: 3, align: center, display("solid")), ..patterns.map(display), ) ``` ], [ #let patterns = ( ("dotted", "dashed", "dash-dotted").map(d => ( "loosely-" + d, d, "densely-" + d, )) ).flatten() #let display(dash) = { set par(spacing: 0.3em) show "loosely": set text(eastern.darken(17%)) show "densely": set text(purple) let derived = dash.starts-with("loosely-") or dash.starts-with("densely-") if derived { dash } else { strong(dash) } rect( line(stroke: (dash: dash), length: 100%), stroke: none, fill: yellow.transparentize(80%), ) } #table( columns: 3, stroke: none, align: (left, center, left), table.cell(colspan: 3, align: center, display("solid")), ..patterns.map(display), ) ] ) #table( columns: (1fr,1fr), stroke: none, table.header([*Variant*],[*Details*]), table.hline(), [\"solid\"],[()], [\"dotted\"],[(\"dot\", 2pt)], [\"densely-dotted\"],[(\"dot\", 1pt)], [\"loosely-dotted\"],[(\"dot\", 4pt)], [\"dashed\"],[(3pt, 3pt)], [\"densely-dashed\"],[(3pt, 2pt)], [\"loosely-dashed\"],[(3pt, 6pt)], [\"dash-dotted\"],[(3pt, 2pt, \"dot\", 2pt)], [\"densely-dash-dotted\"],[(3pt, 1pt, \"dot\", 1pt)], [\"loosely-dash-dotted\"],[(3pt, 4pt, \"dot\", 4pt)], ) - *miter-limit*: auto float 当突出的锐角弯折使用斜角连接而非斜接连接时对应的数值。数值越大,触发斜角连接前可支持的角度就越锐利。仅当连接方式为“miter(斜接)”时生效。 具体来说,斜接限制是拐角突出长度与描边厚度之间的最大比值。 若设置为#rtext[auto](自动),该值将继承上层属性,默认值为4.0。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #let items = ( curve.move((15pt, 0pt)), curve.line((0pt, 30pt)), curve.line((30pt, 30pt)), curve.line((10pt, 20pt)), ) #set curve(stroke: 6pt + blue) #stack( dir: ltr, spacing: 1cm, curve(stroke: (miter-limit: 1), ..items), curve(stroke: (miter-limit: 4), ..items), curve(stroke: (miter-limit: 5), ..items), ) ``` ], [ #let items = ( curve.move((15pt, 0pt)), curve.line((0pt, 30pt)), curve.line((30pt, 30pt)), curve.line((10pt, 20pt)), ) #set curve(stroke: 6pt + blue) #stack( dir: ltr, spacing: 1cm, curve(stroke: (miter-limit: 1), ..items), curve(stroke: (miter-limit: 4), ..items), curve(stroke: (miter-limit: 5), ..items), ) ] ) == 平铺(tiling) 重复平铺填充。 Typst支持最常见的平铺类型,这类平铺会以网格形式重复图案,铺满填充或描边元素的整个区域。图案由一个平铺#link()[尺寸]和用于定义每个单元格内容的主体部分构成。你还可以在平铺单元格之间添加水平或垂直#link()[间距],并调整平铺的起始#link()[偏移位置]。 === 示例 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #let pat = tiling(size: (30pt, 30pt), { place(line(start: (0%, 0%), end: (100%, 100%))) place(line(start: (0%, 100%), end: (100%, 0%))) }) #rect(fill: pat, width: 100%, height: 60pt, stroke: 1pt) ``` ], [ #let pat = tiling(size: (30pt, 30pt), { place(line(start: (0%, 0%), end: (100%, 100%))) place(line(start: (0%, 100%), end: (100%, 0%))) }) #rect(fill: pat, width: 100%, height: 60pt, stroke: 1pt) ] ) === 文本上的平铺(Tilings on text) 文本同样支持平铺,但仅在将#link()[相对设置]为#rtext()[auto](默认值)或"parent"时生效。如需实现逐词或逐字形平铺,你可以手动将文本中的词语或字符放入方框中,或是通过显示规则完成该操作。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #let pat = tiling( size: (30pt, 30pt), relative: "parent", square( size: 30pt, fill: gradient .conic(..color.map.rainbow), ) ) #set text(fill: pat) #lorem(10) ``` ],[ #let pat = tiling( size: (30pt, 30pt), relative: "parent", square( size: 30pt, fill: gradient .conic(..color.map.rainbow), ) ) #set text(fill: pat) #lorem(10) ] ) === 构造函数(Constructor) 构造一个新的平铺。 ```typc tiling( size: auto array, spacing: array, offset: array, relative: auto str, body: content, ) → tiling ``` - *size*: auto array 每个平铺单元的边界框,以(x, y)坐标对形式指定。默认值:```typc auto```。 若设置为auto,则平铺区域将采用布局内容的尺寸。 - *spacing*: array 将平铺单元之间的间距指定为(x, y)坐标对。默认值:```typc (0pt,0pt)```。 如果间距小于平铺单元尺寸,平铺图案会发生自重叠。如果间距大于单元尺寸,平铺图案之间会出现空隙。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #let pat = tiling( size: (30pt, 30pt), spacing: (10pt, 20pt), square(size: 30pt, fill: gradient.conic(..color.map.rainbow)), ) #rect( width: 100%, height: 80pt, fill: pat, stroke: (thickness: 1pt, dash: "dotted"), ) ``` ], [ #let pat = tiling( size: (30pt, 30pt), spacing: (10pt, 20pt), square(size: 30pt, fill: gradient.conic(..color.map.rainbow)), ) #rect( width: 100%, height: 80pt, fill: pat, stroke: (thickness: 1pt, dash: "dotted"), ) ] ) - *offset*: array 平移整个瓦片网格,不会改变瓦片大小或间距。默认值:```typc (0%+0pt,0%+0pt)```。 偏移量以(x, y)坐标对设定。x为正值时图案向右移动,y为正值时图案向下移动。相对值会基于瓦片尺寸加上间距进行换算。 请注意,该偏移产生的位移作用于瓦片本身;而内部内容的位移(例如通过place(dx: .., dy: ..)),当内容移动到瓦片边界框外时可能会发生裁切。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #set rect(width: 100%, height: 80pt, stroke: 1pt) #let pat = tiling( size: (20pt, 20pt), circle(radius: 10pt, fill: blue), ) #let pat-with-offset = tiling( size: (20pt, 20pt), offset: (50%, 50%), circle(radius: 10pt, fill: blue), ) #grid( columns: 2, column-gutter: 10pt, rect(fill: pat), rect(fill: pat-with-offset), ) ``` ], [ #set rect(width: 100%, height: 80pt, stroke: 1pt) #let pat = tiling( size: (20pt, 20pt), circle(radius: 10pt, fill: blue), ) #let pat-with-offset = tiling( size: (20pt, 20pt), offset: (50%, 50%), circle(radius: 10pt, fill: blue), ) #grid( columns: 2, column-gutter: 10pt, rect(fill: pat), rect(fill: pat-with-offset), ) ] ) - *relative*: auto str 确定平铺图案相对于哪个元素的边界框进行绘制。默认值:```typc auto```。 默认情况下,平铺图案相对于其绘制的形状(自身)进行绘制;当平铺应用于文本时除外,此时平铺图案相对于最近的祖先容器(父级)绘制。 元素的父级是包含该元素的最内层框或块;若不存在,则父级为页面本身。 #table( columns: 1fr, align: left, stroke: none, fill: (x,y)=> if (y==0) {white} else if (y==1) {luma(240)} , [ ```typst #let pat = tiling( size: (20pt, 20pt), spacing: (5pt, 5pt), relative: "self", circle(radius: 10pt, fill: teal), ) #let pat-with-parent = tiling( size: (20pt, 20pt), spacing: (5pt, 5pt), relative: "parent", circle(radius: 10pt, fill: teal), ) #set raw(lang: "typc") #table( columns: (1fr, 1fr, 1fr), rows: (auto, 80pt), table.header(`"self"`, `"parent"`, `"parent"`), // This one is local to the cell itself. table.cell(fill: pat, none), // These two are both page-relative, so the // pattern is continous. table.cell(fill: pat-with-parent, none), table.cell(fill: pat-with-parent, none), ) ``` ], [ #let pat = tiling( size: (20pt, 20pt), spacing: (5pt, 5pt), relative: "self", circle(radius: 10pt, fill: teal), ) #let pat-with-parent = tiling( size: (20pt, 20pt), spacing: (5pt, 5pt), relative: "parent", circle(radius: 10pt, fill: teal), ) #set raw(lang: "typc") #table( columns: (1fr, 1fr, 1fr), rows: (auto, 80pt), table.header(`"self"`, `"parent"`, `"parent"`), // This one is local to the cell itself. table.cell(fill: pat, none), // These two are both page-relative, so the // pattern is continous. table.cell(fill: pat-with-parent, none), table.cell(fill: pat-with-parent, none), ) ] ) - *body*: content 每个平铺单元的内容。