Merge pull request #472 from treeform/copy

copy proc consistent
This commit is contained in:
Andre von Houck 2022-07-23 18:05:42 -07:00 committed by GitHub
commit 6f4260d12c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 11 deletions

View file

@ -182,12 +182,13 @@ exportRefObject Paint:
constructor:
newPaint(PaintKind)
procs:
newPaint(Paint)
copy(Paint)
exportRefObject Path:
constructor:
newPath
procs:
copy(Path)
transform(Path, Mat3)
addPath(Path, Path)
closePath(Path)
@ -233,6 +234,7 @@ exportRefObject Font:
strikethrough
noKerningAdjustments
procs:
copy(Font)
scale(Font)
defaultLineHeight
typeset(Font, string, Vec2, HorizontalAlignment, VerticalAlignment, bool)

View file

@ -88,13 +88,12 @@ proc state(ctx: Context): ContextState =
result.mat = ctx.mat
result.mask = if ctx.mask != nil: ctx.mask.copy() else: nil
proc save*(ctx: Context) {.inline, raises: [PixieError].} =
proc save*(ctx: Context) {.inline, raises: [].} =
## Saves the entire state of the context by pushing the current state onto
## a stack.
ctx.stateStack.add(ctx.state())
ctx.fillStyle = newPaint(ctx.fillStyle)
ctx.strokeStyle = newPaint(ctx.strokeStyle)
ctx.fillStyle = ctx.fillStyle.copy()
ctx.strokeStyle = ctx.strokeStyle.copy()
proc saveLayer*(ctx: Context) {.raises: [PixieError].} =
## Saves the entire state of the context by pushing the current state onto

View file

@ -581,7 +581,7 @@ proc newImage*(svg: Svg): Image {.raises: [PixieError].} =
blendMode = NormalBlend # Switch to normal when compositing multiple paths
if props.stroke != rgbx(0, 0, 0, 0) and props.strokeWidth > 0:
let paint = newPaint(props.stroke)
let paint = props.stroke.copy()
paint.color.a *= (props.opacity * props.strokeOpacity)
result.strokePath(
path,

View file

@ -213,6 +213,17 @@ proc newFont*(typeface: Typeface): Font {.raises: [].} =
result.paint = newPaint(SolidPaint)
result.paint.color = color(0, 0, 0, 1)
proc copy*(font: Font): Font {.raises: [].} =
result = Font()
result.typeface = font.typeface
result.size = font.size
result.lineHeight = font.lineHeight
result.paints = font.paints
result.textCase = font.textCase
result.underline = font.underline
result.strikethrough = font.strikethrough
result.noKerningAdjustments = font.noKerningAdjustments
proc newSpan*(text: string, font: Font): Span {.raises: [].} =
## Creates a span, associating a font with the text.
result = Span()

View file

@ -21,9 +21,11 @@ proc newMask*(image: Image): Mask {.hasSimd, raises: [PixieError].} =
for i in 0 ..< image.data.len:
result.data[i] = image.data[i].a
proc copy*(image: Image): Image {.raises: [PixieError].} =
proc copy*(image: Image): Image {.raises: [].} =
## Copies the image data into a new image.
result = newImage(image.width, image.height)
result = Image()
result.width = image.width
result.height = image.height
result.data = image.data
proc `$`*(image: Image): string {.raises: [].} =

View file

@ -7,9 +7,11 @@ type UnsafeMask = distinct Mask
when defined(release):
{.push checks: off.}
proc copy*(mask: Mask): Mask {.raises: [PixieError].} =
proc copy*(mask: Mask): Mask {.raises: [].} =
## Copies the image data into a new image.
result = newMask(mask.width, mask.height)
result = Mask()
result.width = mask.width
result.height = mask.height
result.data = mask.data
proc `$`*(mask: Mask): string {.raises: [].} =

View file

@ -34,7 +34,7 @@ proc newPaint*(kind: PaintKind): Paint {.raises: [].} =
## Create a new Paint.
result = Paint(kind: kind, opacity: 1, imageMat: mat3())
proc newPaint*(paint: Paint): Paint {.raises: [].} =
proc copy*(paint: Paint): Paint {.raises: [].} =
## Create a new Paint with the same properties.
result = newPaint(paint.kind)
result.blendMode = paint.blendMode

View file

@ -52,6 +52,12 @@ proc newPath*(): Path {.raises: [].} =
## Create a new Path.
Path()
proc copy*(path: Path): Path {.raises: [].} =
result = Path()
result.commands = path.commands
result.start = path.start
result.at = path.at
proc pixelScale(transform: Mat3): float32 =
## What is the largest scale factor of this transform?
max(