Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/preview/umbra/0.1.1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.ttf
.DS_Store
OFL.txt
.vscode
7 changes: 7 additions & 0 deletions packages/preview/umbra/0.1.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 David Armstrong

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 changes: 47 additions & 0 deletions packages/preview/umbra/0.1.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Umbra

Umbra is a library for drawing basic gradient shadows in [typst](https://typst.app). It currently provides only one function for drawing a shadow along one edge of a path.

## Examples
<!-- img width is set so the table gets evenly spaced by GitHubs css -->
### Basic Shadow
<a href="gallery/basic.typ">
<img src="gallery/basic.png" height="250px">
</a>

### Neumorphism
<a href="gallery/neumorphism.typ">
<img src="gallery/neumorphism.png" height="250px">
</a>

### Torn Paper
<a href="gallery/torn-paper.typ">
<img src="gallery/torn-paper.png" height="250">
</a>

*Click on the example image to jump to the code.*

## Usage

The following code creates a very basic square shadow:
```
#import "@preview/umbra:0.1.1": shadow-path

#shadow-path((10%, 10%), (10%, 90%), (90%, 90%), (90%, 10%), closed: true)
```

The function syntax is similar to the normal path syntax. The following arguments were added:

* `shadow-radius` (default `0.5cm`): The shadow size in the direction normal to the edge
* `shadow-stops` (default `(gray, white)`): The colours to be used in the shadow, passed directly to `gradient`
* `correction` (default `5deg`): A small correction factor to be added to round shadows at corners. Otherwise, there will be a small gap between the two shadows

### Vertex Order

The order of the vertices defines the direction of the shadow. If the shadow is the wrong way around, just reverse the vertices.

### Transparency

This package is designed in such a way that it should support transparency in the gradients (i.e. corners define shadows using a path which approximates the arc, instead of an entire circle). However, typst doesn't currently support transparency in gradients. ([issue](https://github.com/typst/typst/issues/2546)).

In addition, the aforementioned correction factor would likely cause issues with transparent gradients.
Binary file added packages/preview/umbra/0.1.1/gallery/basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/preview/umbra/0.1.1/gallery/basic.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import "@preview/umbra:0.1.1": shadow-path

#set page(width: 15cm, height: 15cm, margin: 0.5cm)

#shadow-path((10%, 10%), (10%, 90%), (90%, 90%), (90%, 10%), closed: true)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions packages/preview/umbra/0.1.1/gallery/neumorphism.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#import "@preview/umbra:0.1.1": shadow-path

#let background-colour = color.rgb("#EFEEEE")
#let radius = 0.4cm

#set page(width: 15cm, height: 15cm, margin: 0.5cm, fill: background-colour)

#box(
{
place(shadow-path(
shadow-stops: (white.mix((background-colour, 50%)), background-colour,),
shadow-radius: radius,
(16%, 15%),
(15%, 16%),
(15%, 79%),
(16%, 80%),
(79%, 80%),
(80%, 79%),
(80%, 16%),
(79%, 15%),
closed: true,
))
place(
shadow-path(
shadow-stops: (color.rgb("#D1CDC7").mix((background-colour, 50%)), background-colour,),
shadow-radius: radius,
(21%, 20%),
(20%, 21%),
(20%, 84%),
(21%, 85%),
(84%, 85%),
(85%, 84%),
(85%, 20%),
(84%, 21%),
closed: true,
),
)
polygon(
fill: background-colour,
(16%, 15%),
(15%, 16%),
(15%, 84%),
(16%, 85%),
(85%, 84%),
(84%, 85%),
(85%, 16%),
(84%, 15%),
)
},
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions packages/preview/umbra/0.1.1/gallery/torn-paper.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#import "@preview/suiji:0.3.0": *

Check warning on line 1 in packages/preview/umbra/0.1.1/gallery/torn-paper.typ

View check run for this annotation

Typst package check / @preview/umbra:0.1.1

packages/preview/umbra/0.1.1/gallery/torn-paper.typ#L1

This import seems to use an older version of the package.
#import "@preview/umbra:0.1.1": shadow-path

#set page(width: 13cm, height: 8cm, margin: 0.0cm)

#let torn-paper(
tear-positions: (40%, 45%),
alignment: left,
paper-colour: rgb("ECEBE9"),
mult: 0.4%,
seed: auto,
approx-steps: 100,
) = {
assert(alignment in (left, right, top, bottom))
context {
let tear-positions = tear-positions
if type(tear-positions) != array {
tear-positions = (tear-positions, tear-positions)
} else if tear-positions.len() == 1 {
tear-positions = tear-positions * 2
}
let seed = seed
if seed == auto {
seed = counter(page).get().at(0)
counter(page).step()
}
let chunk_size = int(calc.round(approx-steps / (tear-positions.len() - 1)))
let steps = chunk_size * (tear-positions.len() - 1)
let rng = gen-rng-f(seed)
let edge0-vals
(rng, edge0-vals) = random-f(rng, size: (steps - 1))
edge0-vals = edge0-vals.fold((0%,), (acc, x) => {
acc.push(acc.last() + ((x * 2 - 1) * mult))
acc
})
edge0-vals = edge0-vals.chunks(chunk_size).zip(tear-positions, tear-positions.slice(1)).map(
((chunk, prev, next)) => {
chunk.enumerate().map(
((i, x)) => {
let r = i / (chunk.len() - 1)
assert(r <= 1)
prev + (next - prev) * r + x - chunk.first() * (1 - r) - chunk.last() * r
},
)
},
).flatten()
assert(edge0-vals.len() == steps)
assert(edge0-vals.first() == tear-positions.first())
assert(edge0-vals.last() == tear-positions.last())
edge0-vals = edge0-vals.enumerate().map(((i, x)) => {
(x, i * 100% / (steps - 1))
})

let edge1-vals
(rng, edge1-vals) = random-f(rng, size: steps)
edge1-vals = edge0-vals.rev().zip(edge1-vals).map((((x, y), r)) => (x + r * 0.2% + 2%, y))

let shadow-vals = edge1-vals
shadow-vals.insert(0, shadow-vals.first())

let edge2-vals = edge1-vals
edge2-vals.insert(0, (100%, 100%))
edge2-vals.push((100%, 0%))

edge1-vals = edge1-vals + edge0-vals

edge0-vals.insert(0, (0%, 0%))
edge0-vals.push((0%, 100%))

if alignment == right {
edge0-vals = edge0-vals.map(((x, y)) => (100% - x, y))
edge1-vals = edge1-vals.map(((x, y)) => (100% - x, y))
edge2-vals = edge2-vals.map(((x, y)) => (100% - x, y))
shadow-vals = shadow-vals.map(((x, y)) => (100% - x, y)).rev()
} else if alignment == top {
edge0-vals = edge0-vals.map(((x, y)) => (y, x))
edge1-vals = edge1-vals.map(((x, y)) => (y, x))
edge2-vals = edge2-vals.map(((x, y)) => (y, x))
shadow-vals = shadow-vals.map(((x, y)) => (y, x))
} else if alignment == bottom {
edge0-vals = edge0-vals.map(((x, y)) => (y, 100% - x))
edge1-vals = edge1-vals.map(((x, y)) => (y, 100% - x))
edge2-vals = edge2-vals.map(((x, y)) => (y, 100% - x))
shadow-vals = shadow-vals.map(((x, y)) => (y, 100% - x))
}

layout(
size => {
block(
width: 100%,
height: 100%,
{
polygon(fill: paper-colour, stroke: none, ..edge2-vals)
polygon(fill: none, stroke: none, ..edge0-vals)
place(
horizon + left,
polygon(fill: white.transparentize(50%), stroke: none, ..edge1-vals),
)
place(top + left, shadow-path(
..shadow-vals,
shadow-stops: (luma(210), paper-colour),
shadow-radius: 0.15cm,
))
},
)
},
)
}
}

#set text(size: 36pt, font: "Indie Flower")

#show regex("[a-zA-Z]"): (letter) => {
context{
let seed = counter("letters").get().at(0)
counter("letters").step()
let rng = gen-rng-f(seed)
let x
let y
(rng, (x, y)) = random-f(rng, size: 2)
box(move(dx: x * 0.1cm, dy: y * 0.2cm - 0.1cm, letter))
}
}

#place(horizon + left, [#h(0.8cm) at a quarter to twelve

#h(3.3cm) learn what

#h(7.7cm) maybe])

#place(torn-paper(
tear-positions: (100%, 95%, 86%, 78%, 70%, 50%, 30%, 0%),
alignment: right,
approx-steps: 250,
))
3 changes: 3 additions & 0 deletions packages/preview/umbra/0.1.1/src/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#let version = version((0, 1, 0))

#import "shadow-path.typ": shadow-path
Loading