Skip to content

Commit c24fdf3

Browse files
committed
Document pathfinder_content more
1 parent 36c347a commit c24fdf3

File tree

8 files changed

+129
-7
lines changed

8 files changed

+129
-7
lines changed

content/src/clip.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Clips polygons.
12+
13+
#![allow(deprecated)]
14+
1115
use crate::outline::{Contour, ContourIterFlags, PointFlags, PushSegmentFlags};
1216
use crate::segment::{CubicSegment, Segment};
1317
use arrayvec::ArrayVec;
@@ -310,16 +314,25 @@ enum EdgeRelativeLocation {
310314

311315
// 3D quad clipping
312316

317+
/// Clips polygons in 3D homogeneous coordinates against the (-1, 1) normalized device coordinate
318+
/// cube.
319+
#[deprecated]
313320
pub struct PolygonClipper3D {
314321
subject: Vec<Vector4F>,
315322
}
316323

317324
impl PolygonClipper3D {
325+
/// Creates a new polygon clipper with the vertices of the given polygon-to-be-clipped, in 3D
326+
/// homogeneous coordinates.
327+
#[deprecated]
318328
#[inline]
319329
pub fn new(subject: Vec<Vector4F>) -> PolygonClipper3D {
320330
PolygonClipper3D { subject }
321331
}
322332

333+
/// Clips the subject polygon against the (-1, 1) normalized device coordinate cube and returns
334+
/// the resulting vertices, in 3D homogeneous coordinates.
335+
#[deprecated]
323336
pub fn clip(mut self) -> Vec<Vector4F> {
324337
// TODO(pcwalton): Fast path for completely contained polygon?
325338

content/src/dash.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,49 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Line dashing support.
11+
//! Transforms a stroke into a dashed stroke.
1212
1313
use crate::outline::{Contour, ContourIterFlags, Outline, PushSegmentFlags};
1414
use std::mem;
1515

1616
const EPSILON: f32 = 0.0001;
1717

18+
/// Transforms a stroke into a dashed stroke.
1819
pub struct OutlineDash<'a> {
1920
input: &'a Outline,
2021
output: Outline,
2122
state: DashState<'a>,
2223
}
2324

2425
impl<'a> OutlineDash<'a> {
26+
/// Creates a new outline dasher for the given stroke.
27+
///
28+
/// Arguments:
29+
///
30+
/// * `input`: The input stroke to be dashed. This must not yet been converted to a fill; i.e.
31+
/// it is assumed that the stroke-to-fill conversion happens *after* this dashing process.
32+
///
33+
/// * `dashes`: The list of dashes, specified as alternating pixel lengths of lines and gaps
34+
/// that describe the pattern. See
35+
/// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash.
36+
///
37+
/// * `offset`: The line dash offset, or "phase". See
38+
/// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset.
2539
#[inline]
2640
pub fn new(input: &'a Outline, dashes: &'a [f32], offset: f32) -> OutlineDash<'a> {
2741
OutlineDash { input, output: Outline::new(), state: DashState::new(dashes, offset) }
2842
}
2943

44+
/// Performs the dashing operation.
45+
///
46+
/// The results can be retrieved with the `into_outline()` method.
3047
pub fn dash(&mut self) {
3148
for contour in &self.input.contours {
3249
ContourDash::new(contour, &mut self.output, &mut self.state).dash()
3350
}
3451
}
3552

53+
/// Returns the resulting dashed outline.
3654
pub fn into_outline(mut self) -> Outline {
3755
if self.state.is_on() {
3856
self.output.push_contour(self.state.output);

content/src/effects.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@ use pathfinder_geometry::line_segment::LineSegment2F;
1515
use pathfinder_geometry::vector::Vector2F;
1616
use pathfinder_simd::default::F32x2;
1717

18-
/// This intentionally does not precisely match what Core Graphics does (a
19-
/// Lanczos function), because we don't want any ringing artefacts.
18+
/// A defringing kernel for LCD screens that approximates the macOS/iOS look.
19+
///
20+
/// This intentionally does not precisely match what Core Graphics does (a Lanczos function),
21+
/// because we don't want any ringing artefacts.
2022
pub static DEFRINGING_KERNEL_CORE_GRAPHICS: DefringingKernel =
2123
DefringingKernel([0.033165660, 0.102074051, 0.221434336, 0.286651906]);
24+
25+
/// A defringing kernel for LCD screens that approximates the FreeType look.
2226
pub static DEFRINGING_KERNEL_FREETYPE: DefringingKernel =
2327
DefringingKernel([0.0, 0.031372549, 0.301960784, 0.337254902]);
2428

29+
/// Stem darkening factors that approximate the macOS look.
30+
///
2531
/// Should match macOS 10.13 High Sierra.
2632
pub static STEM_DARKENING_FACTORS: [f32; 2] = [0.0121, 0.0121 * 1.25];
2733

34+
/// The maximum number of pixels we are willing to expand outlines by to match the macOS look.
35+
///
2836
/// Should match macOS 10.13 High Sierra.
2937
pub const MAX_STEM_DARKENING_AMOUNT: [f32; 2] = [0.3, 0.3];
3038

31-
/// This value is a subjective cutoff. Above this ppem value, no stem darkening is performed.
39+
/// A subjective cutoff. Above this ppem value, no stem darkening is performed.
3240
pub const MAX_STEM_DARKENING_PIXELS_PER_EM: f32 = 72.0;
3341

3442
/// The shader that should be used when compositing this layer onto its destination.

content/src/fill.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Fill rules.
11+
//! The fill rule, which determines how self-intersecting paths are filled.
1212
13+
/// The fill rule, which determines how self-intersecting paths are filled.
14+
///
15+
/// Paths that don't intersect themselves (and have no holes) are unaffected by the choice of fill
16+
/// rule.
1317
#[derive(Clone, Copy, PartialEq, Debug)]
1418
pub enum FillRule {
19+
/// The nonzero rule: https://en.wikipedia.org/wiki/Nonzero-rule
1520
Winding,
21+
/// The even-odd rule: https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
1622
EvenOdd,
1723
}

content/src/gradient.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Gradient effects that paths can be filled with.
12+
1113
use crate::util;
1214
use pathfinder_color::ColorU;
1315
use pathfinder_geometry::line_segment::LineSegment2F;

content/src/pattern.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Pattern {
3333
flags: PatternFlags,
3434
}
3535

36+
/// Where a raster image pattern comes from.
3637
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
3738
pub enum PatternSource {
3839
Image(Image),
@@ -42,9 +43,10 @@ pub enum PatternSource {
4243
}
4344
}
4445

45-
/// RGBA, non-premultiplied.
46+
/// A raster image, in 32-bit RGBA (8 bits per channel), non-premultiplied form.
4647
// FIXME(pcwalton): Hash the pixel contents so that we don't have to compare every pixel!
4748
// TODO(pcwalton): Should the pixels be premultiplied?
49+
// TODO(pcwalton): Color spaces.
4850
#[derive(Clone, PartialEq, Eq)]
4951
pub struct Image {
5052
size: Vector2I,
@@ -58,9 +60,15 @@ pub struct Image {
5860
pub struct ImageHash(pub u64);
5961

6062
bitflags! {
63+
/// Various flags that determine behavior of a pattern.
6164
pub struct PatternFlags: u8 {
65+
/// If set, the pattern repeats in the X direction. If unset, the base color is used.
6266
const REPEAT_X = 0x01;
67+
/// If set, the pattern repeats in the Y direction. If unset, the base color is used.
6368
const REPEAT_Y = 0x02;
69+
/// If set, nearest-neighbor interpolation is used when compositing this pattern (i.e. the
70+
/// image will be pixelated). If unset, bilinear interpolation is used when compositing
71+
/// this pattern (i.e. the image will be smooth).
6472
const NO_SMOOTHING = 0x04;
6573
}
6674
}
@@ -76,26 +84,37 @@ impl Pattern {
7684
}
7785
}
7886

87+
/// Creates a new pattern from the given image.
88+
///
89+
/// The transform is initialized to the identity transform. There is no filter.
7990
#[inline]
8091
pub fn from_image(image: Image) -> Pattern {
8192
Pattern::from_source(PatternSource::Image(image))
8293
}
8394

95+
/// Creates a new pattern from the given render target with the given size.
96+
///
97+
/// The transform is initialized to the identity transform. There is no filter.
8498
#[inline]
8599
pub fn from_render_target(id: RenderTargetId, size: Vector2I) -> Pattern {
86100
Pattern::from_source(PatternSource::RenderTarget { id, size })
87101
}
88102

103+
/// Returns the affine transform applied to this pattern.
89104
#[inline]
90105
pub fn transform(&self) -> Transform2F {
91106
self.transform
92107
}
93108

109+
/// Applies the given transform to this pattern.
110+
///
111+
/// The transform is applied after any existing transform.
94112
#[inline]
95113
pub fn apply_transform(&mut self, transform: Transform2F) {
96114
self.transform = transform * self.transform;
97115
}
98116

117+
/// Returns the underlying pixel size of this pattern, not taking transforms into account.
99118
#[inline]
100119
pub fn size(&self) -> Vector2I {
101120
match self.source {
@@ -104,58 +123,81 @@ impl Pattern {
104123
}
105124
}
106125

126+
/// Returns the filter attached to this pattern, if any.
107127
#[inline]
108128
pub fn filter(&self) -> Option<PatternFilter> {
109129
self.filter
110130
}
111131

132+
/// Applies a filter to this pattern, replacing any previous filter if any.
112133
#[inline]
113134
pub fn set_filter(&mut self, filter: Option<PatternFilter>) {
114135
self.filter = filter;
115136
}
116137

138+
/// Returns true if this pattern repeats in the X direction or false if the base color will be
139+
/// used when sampling beyond the coordinates of the image.
117140
#[inline]
118141
pub fn repeat_x(&self) -> bool {
119142
self.flags.contains(PatternFlags::REPEAT_X)
120143
}
121144

145+
/// Set to true if the pattern should repeat in the X direction or false if the base color
146+
/// should be used when sampling beyond the coordinates of the image.
122147
#[inline]
123148
pub fn set_repeat_x(&mut self, repeat_x: bool) {
124149
self.flags.set(PatternFlags::REPEAT_X, repeat_x);
125150
}
126151

152+
/// Returns true if this pattern repeats in the Y direction or false if the base color will be
153+
/// used when sampling beyond the coordinates of the image.
127154
#[inline]
128155
pub fn repeat_y(&self) -> bool {
129156
self.flags.contains(PatternFlags::REPEAT_Y)
130157
}
131158

159+
/// Set to true if the pattern should repeat in the Y direction or false if the base color
160+
/// should be used when sampling beyond the coordinates of the image.
132161
#[inline]
133162
pub fn set_repeat_y(&mut self, repeat_y: bool) {
134163
self.flags.set(PatternFlags::REPEAT_Y, repeat_y);
135164
}
136165

166+
/// Returns true if this pattern should use bilinear interpolation (i.e. the image will be
167+
/// smooth) when scaled or false if this pattern should use nearest-neighbor interpolation
168+
/// (i.e. the image will be pixelated).
137169
#[inline]
138170
pub fn smoothing_enabled(&self) -> bool {
139171
!self.flags.contains(PatternFlags::NO_SMOOTHING)
140172
}
141173

174+
/// Set to true if the pattern should use bilinear interpolation (i.e. should be smooth) when
175+
/// scaled or false if this pattern should use nearest-neighbor interpolation (i.e. should be
176+
/// pixelated).
142177
#[inline]
143178
pub fn set_smoothing_enabled(&mut self, enable: bool) {
144179
self.flags.set(PatternFlags::NO_SMOOTHING, !enable);
145180
}
146181

182+
/// Returns true if this pattern is obviously fully opaque.
183+
///
184+
/// This is a best-effort quick check, so it might return false even if the image is actually
185+
/// opaque.
147186
#[inline]
148187
pub fn is_opaque(&self) -> bool {
149188
self.source.is_opaque()
150189
}
151190

191+
/// Returns the underlying source of the pattern.
152192
#[inline]
153193
pub fn source(&self) -> &PatternSource {
154194
&self.source
155195
}
156196
}
157197

158198
impl Image {
199+
/// Creates a new image with the given device pixel size and pixel store, as 32-bit RGBA (8
200+
/// bits per channel), RGBA, linear color space, nonpremultiplied.
159201
#[inline]
160202
pub fn new(size: Vector2I, pixels: Arc<Vec<ColorU>>) -> Image {
161203
assert_eq!(size.x() as usize * size.y() as usize, pixels.len());
@@ -168,28 +210,37 @@ impl Image {
168210
Image { size, pixels, pixels_hash, is_opaque }
169211
}
170212

213+
/// A convenience function to create a new image with the given image from the `image` crate.
171214
#[cfg(feature = "pf-image")]
172215
pub fn from_image_buffer(image_buffer: RgbaImage) -> Image {
173216
let (width, height) = image_buffer.dimensions();
174217
let pixels = color::u8_vec_to_color_vec(image_buffer.into_raw());
175218
Image::new(vec2i(width as i32, height as i32), Arc::new(pixels))
176219
}
177220

221+
/// Returns the device pixel size of the image.
178222
#[inline]
179223
pub fn size(&self) -> Vector2I {
180224
self.size
181225
}
182226

227+
/// Returns the pixel buffer of this image as 32-bit RGBA (8 bits per channel), RGBA, linear
228+
/// color space, nonpremultiplied.
183229
#[inline]
184230
pub fn pixels(&self) -> &Arc<Vec<ColorU>> {
185231
&self.pixels
186232
}
187233

234+
/// Returns true if this image is obviously opaque.
235+
///
236+
/// This is a best-guess quick check, and as such it might return false even if the image is
237+
/// fully opaque.
188238
#[inline]
189239
pub fn is_opaque(&self) -> bool {
190240
self.is_opaque
191241
}
192242

243+
/// Returns a non-cryptographic hash of the image, which should be globally unique.
193244
#[inline]
194245
pub fn get_hash(&self) -> ImageHash {
195246
let mut hasher = DefaultHasher::new();
@@ -199,6 +250,10 @@ impl Image {
199250
}
200251

201252
impl PatternSource {
253+
/// Returns true if this pattern is obviously opaque.
254+
///
255+
/// This is a best-guess quick check, and as such it might return false even if the pattern is
256+
/// fully opaque.
202257
#[inline]
203258
pub fn is_opaque(&self) -> bool {
204259
match *self {

0 commit comments

Comments
 (0)