refactored notes rendering
This commit is contained in:
		| @@ -1,14 +1,51 @@ | ||||
| #import "/src/cetz.typ": draw | ||||
| #import "/src/cetz.typ": draw, styles | ||||
|  | ||||
| #import "/src/consts.typ": * | ||||
| #import "/src/core/utils.typ": get-ctx, set-ctx, expand-parent-group | ||||
| #import "/src/core/utils.typ": get-ctx, is-elmt, set-ctx, expand-parent-group, normalize-measure | ||||
|  | ||||
| #let note-default-style = ( | ||||
|   shape: "default", | ||||
|   fill: rgb("#FEFFDD"), | ||||
|   stroke: black + .5pt | ||||
| ) | ||||
|  | ||||
| #let resolve-style(ctx, note) = { | ||||
|   return styles.resolve( | ||||
|     ctx.style, | ||||
|     merge: note.style, | ||||
|     root: "note", | ||||
|     base: note-default-style | ||||
|   ) | ||||
| } | ||||
|  | ||||
| #let pre-resolve-styles() = get-ctx(ctx => { | ||||
|   let ctx = ctx | ||||
|   let notes = ctx.setup.notes | ||||
|  | ||||
|   for (i, elmt) in ctx.setup.elements.enumerate() { | ||||
|     if type(elmt) == function { | ||||
|       ctx = elmt(ctx).ctx | ||||
|     } else if is-elmt(elmt) { | ||||
|       if elmt.type == "note" { | ||||
|         let style = resolve-style(ctx, elmt) | ||||
|         notes.at(elmt.id).insert("resolved-style", style) | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   set-ctx(c => { | ||||
|     c.setup.notes = notes | ||||
|     return c | ||||
|   }) | ||||
| }) | ||||
|  | ||||
| #let get-size(note) = { | ||||
|   let PAD = if note.shape == "hex" {NOTE-HEX-PAD} else {NOTE-PAD} | ||||
|   let style = note.resolved-style | ||||
|   let PAD = if style.shape == "hex" {NOTE-HEX-PAD} else {NOTE-PAD} | ||||
|   let m = measure(box(note.content)) | ||||
|   let w = m.width / 1pt + PAD.last() * 2 | ||||
|   let h = m.height / 1pt + PAD.first() * 2 | ||||
|   if note.shape == "default" { | ||||
|   if style.shape == "default" { | ||||
|     w += NOTE-CORNER-SIZE | ||||
|   } | ||||
|   return ( | ||||
| @@ -31,14 +68,15 @@ | ||||
| } | ||||
|  | ||||
| #let get-box(note) = { | ||||
|   let PAD = if note.shape == "hex" {NOTE-HEX-PAD} else {NOTE-PAD} | ||||
|   let style = note.resolved-style | ||||
|   let PAD = if style.shape == "hex" {NOTE-HEX-PAD} else {NOTE-PAD} | ||||
|   let inset = ( | ||||
|     left: PAD.last() * 1pt, | ||||
|     right: PAD.last() * 1pt, | ||||
|     top: PAD.first() * 1pt, | ||||
|     bottom: PAD.first() * 1pt, | ||||
|   ) | ||||
|   if note.shape == "default" { | ||||
|   if style.shape == "default" { | ||||
|     inset.right += NOTE-CORNER-SIZE * 1pt | ||||
|   } | ||||
|   if note.side == "left" { | ||||
| @@ -62,18 +100,21 @@ | ||||
|   } | ||||
|  | ||||
|   get-ctx(ctx => { | ||||
|     let note = ctx.notes.at(note.id) | ||||
|     let y = y | ||||
|     if y == auto { | ||||
|       y = ctx.y | ||||
|     } | ||||
|      | ||||
|  | ||||
|     let PAD = if note.shape == "hex" {NOTE-HEX-PAD} else {NOTE-PAD} | ||||
|     let m = measure(box(note.content)) | ||||
|     let w = m.width / 1pt + PAD.last() * 2 | ||||
|     let h = m.height / 1pt + PAD.first() * 2 | ||||
|     let style = note.resolved-style | ||||
|     let shape = style.shape | ||||
|      | ||||
|     let PAD = if shape == "hex" {NOTE-HEX-PAD} else {NOTE-PAD} | ||||
|     let m = normalize-measure(box(note.content)) | ||||
|     let w = m.width + PAD.last() * 2 | ||||
|     let h = m.height + PAD.first() * 2 | ||||
|     let total-w = w | ||||
|     if note.shape == "default" { | ||||
|     if shape == "default" { | ||||
|       total-w += NOTE-CORNER-SIZE | ||||
|     } | ||||
|  | ||||
| @@ -106,31 +147,31 @@ | ||||
|     } | ||||
|     let y1 = y0 - h | ||||
|  | ||||
|     if note.shape == "default" { | ||||
|     if shape == "default" { | ||||
|       draw.line( | ||||
|         (x0, y0), | ||||
|         (x1, y0), | ||||
|         (x2, y0 - NOTE-CORNER-SIZE), | ||||
|         (x2, y1), | ||||
|         (x0, y1), | ||||
|         stroke: black + .5pt, | ||||
|         fill: note.color, | ||||
|         stroke: style.stroke, | ||||
|         fill: style.fill, | ||||
|         close: true | ||||
|       ) | ||||
|       draw.line( | ||||
|         (x1, y0), | ||||
|         (x1, y0 - NOTE-CORNER-SIZE), | ||||
|         (x2, y0 - NOTE-CORNER-SIZE), | ||||
|         stroke: black + .5pt | ||||
|         stroke: style.stroke | ||||
|       ) | ||||
|     } else if note.shape == "rect" { | ||||
|     } else if shape == "rect" { | ||||
|       draw.rect( | ||||
|         (x0, y0), | ||||
|         (x2, y1), | ||||
|         stroke: black + .5pt, | ||||
|         fill: note.color | ||||
|         stroke: style.stroke, | ||||
|         fill: style.fill | ||||
|       ) | ||||
|     } else if note.shape == "hex" { | ||||
|     } else if shape == "hex" { | ||||
|       let lx = x0 + PAD.last() | ||||
|       let rx = x2 - PAD.last() | ||||
|       let my = (y0 + y1) / 2 | ||||
| @@ -141,8 +182,8 @@ | ||||
|         (rx, y1), | ||||
|         (lx, y1), | ||||
|         (x0, my), | ||||
|         stroke: black + .5pt, | ||||
|         fill: note.color, | ||||
|         stroke: style.stroke, | ||||
|         fill: style.fill, | ||||
|         close: true | ||||
|       ) | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user