bindings work

This commit is contained in:
Ryan Oldenburg 2021-09-07 18:46:55 -05:00
parent 163a087366
commit f64b99c950
9 changed files with 60 additions and 21 deletions

View file

@ -115,7 +115,7 @@ Here are some examples of using Pixie for realtime rendering with some popular w
### Text ### Text
[examples/text.nim](examples/text.nim) [examples/text.nim](examples/text.nim)
```nim ```nim
var font = readFont("tests/fonts/Roboto-Regular_1.ttf") var font = readFont("examples/data/Roboto-Regular_1.ttf")
font.size = 20 font.size = 20
let text = "Typesetting is the arrangement and composition of text in graphic design and publishing in both digital and traditional medias." let text = "Typesetting is the arrangement and composition of text in graphic design and publishing in both digital and traditional medias."
@ -127,7 +127,7 @@ image.fillText(font.typeset(text, vec2(180, 180)), translate(vec2(10, 10)))
### Text spans ### Text spans
[examples/text_spans.nim](examples/text_spans.nim) [examples/text_spans.nim](examples/text_spans.nim)
```nim ```nim
let typeface = readTypeface("tests/fonts/Ubuntu-Regular_1.ttf") let typeface = readTypeface("examples/data/Ubuntu-Regular_1.ttf")
proc newFont(typeface: Typeface, size: float32, color: Color): Font = proc newFont(typeface: Typeface, size: float32, color: Color): Font =
result = newFont(typeface) result = newFont(typeface)
@ -272,7 +272,7 @@ path.polygon(
) )
let paint = newPaint(pkImageTiled) let paint = newPaint(pkImageTiled)
paint.image = readImage("tests/images/png/baboon.png") paint.image = readImage("examples/data/baboon.png")
paint.imageMat = scale(vec2(0.08, 0.08)) paint.imageMat = scale(vec2(0.08, 0.08))
image.fillPath(path, paint) image.fillPath(path, paint)

View file

@ -14,15 +14,42 @@ type
x*, y*: float32 x*, y*: float32
Matrix3* = object Matrix3* = object
a*, b*, c*, d*, e*, f*, g*, h*, i*: float32 values*: array[9, float32]
proc matrix3*(): Matrix3 = proc matrix3*(): Matrix3 =
Matrix3(a: 1, b: 0, c: 0, d: 0, e: 1, f: 0, g: 0, h: 0, i: 1) result.values[0] = 1
result.values[4] = 1
result.values[8] = 1
proc drawImage1*( proc mul*(a, b: Matrix3): Matrix3 =
ctx: Context, image: Image, dx, dy: float32 cast[Matrix3](cast[Mat3](a) * cast[Mat3](b))
) {.raises: [PixieError].} =
ctx.drawImage(image, dx, dy) proc translate*(x, y: float32): Matrix3 =
result = matrix3()
result.values[6] = x
result.values[7] = y
proc rotate*(angle: float32): Matrix3 =
let
sin = sin(angle)
cos = cos(angle)
result = matrix3()
result.values[0] = cos
result.values[1] = -sin
result.values[3] = sin
result.values[4] = cos
proc scale*(x, y: float32): Matrix3 =
result = matrix3()
result.values[0] = x
result.values[4] = y
proc parseColor*(s: string): Color {.raises: [PixieError]} =
try:
result = parseHtmlColor(s)
except:
let e = getCurrentException()
raise newException(PixieError, e.msg, e)
proc drawImage2*( proc drawImage2*(
ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32 ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32
@ -62,6 +89,8 @@ exportObject Vector2:
exportObject Matrix3: exportObject Matrix3:
constructor: constructor:
matrix3 matrix3
procs:
mul(Matrix3, Matrix3)
exportObject Rect: exportObject Rect:
discard discard
@ -257,7 +286,7 @@ exportRefObject Context:
setTransform setTransform
transform(Context, Mat3) transform(Context, Mat3)
resetTransform resetTransform
drawImage1 drawImage(Context, Image, float32, float32)
drawImage2 drawImage2
drawImage3 drawImage3
moveTo(Context, float32, float32) moveTo(Context, float32, float32)
@ -274,6 +303,7 @@ exportRefObject Context:
clearRect(Context, float32, float32, float32, float32) clearRect(Context, float32, float32, float32, float32)
fillRect(Context, float32, float32, float32, float32) fillRect(Context, float32, float32, float32, float32)
strokeRect(Context, float32, float32, float32, float32) strokeRect(Context, float32, float32, float32, float32)
strokeSegment(Context, float32, float32, float32, float32)
fillText(Context, string, float32, float32) fillText(Context, string, float32, float32)
strokeText(Context, string, float32, float32) strokeText(Context, string, float32, float32)
translate(Context, float32, float32) translate(Context, float32, float32)
@ -292,6 +322,10 @@ exportProcs:
parsePath parsePath
miterLimitToAngle miterLimitToAngle
angleToMiterLimit angleToMiterLimit
parseColor
translate(float32, float32)
rotate(float32)
scale(float32, float32)
writeFiles("bindings/generated", "pixie") writeFiles("bindings/generated", "pixie")

Binary file not shown.

Binary file not shown.

BIN
examples/data/baboon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 636 KiB

View file

@ -11,7 +11,7 @@ path.polygon(
) )
let paint = newPaint(pkImageTiled) let paint = newPaint(pkImageTiled)
paint.image = readImage("tests/images/png/baboon.png") paint.image = readImage("examples/data/baboon.png")
paint.imageMat = scale(vec2(0.08, 0.08)) paint.imageMat = scale(vec2(0.08, 0.08))
image.fillPath(path, paint) image.fillPath(path, paint)

View file

@ -3,7 +3,7 @@ import pixie
let image = newImage(200, 200) let image = newImage(200, 200)
image.fill(rgba(255, 255, 255, 255)) image.fill(rgba(255, 255, 255, 255))
var font = readFont("tests/fonts/Roboto-Regular_1.ttf") var font = readFont("examples/data/Roboto-Regular_1.ttf")
font.size = 20 font.size = 20
let text = "Typesetting is the arrangement and composition of text in graphic design and publishing in both digital and traditional medias." let text = "Typesetting is the arrangement and composition of text in graphic design and publishing in both digital and traditional medias."

View file

@ -3,7 +3,7 @@ import pixie
let image = newImage(200, 200) let image = newImage(200, 200)
image.fill(rgba(255, 255, 255, 255)) image.fill(rgba(255, 255, 255, 255))
let typeface = readTypeface("tests/fonts/Ubuntu-Regular_1.ttf") let typeface = readTypeface("examples/data/Ubuntu-Regular_1.ttf")
proc newFont(typeface: Typeface, size: float32, color: Color): Font = proc newFont(typeface: Typeface, size: float32, color: Color): Font =
result = newFont(typeface) result = newFont(typeface)

View file

@ -679,6 +679,19 @@ proc polygon*(
## Adds an n-sided regular polygon at (x, y) of size to the current path. ## Adds an n-sided regular polygon at (x, y) of size to the current path.
ctx.path.polygon(pos, size, sides) ctx.path.polygon(pos, size, sides)
proc strokeSegment*(ctx: Context, segment: Segment) {.raises: [PixieError].} =
## Strokes a segment (draws a line from segment.at to segment.to) according
## to the current strokeStyle and other context settings.
let path = newPath()
path.moveTo(segment.at)
path.lineTo(segment.to)
ctx.stroke(path)
proc strokeSegment*(ctx: Context, ax, ay, bx, by: float32) {.raises: [PixieError].} =
## Strokes a segment (draws a line from ax, ay to bx, by) according
## to the current strokeStyle and other context settings.
ctx.strokeSegment(segment(vec2(ax, ay), vec2(bx, by)))
proc fillRoundedRect*( proc fillRoundedRect*(
ctx: Context, rect: Rect, nw, ne, se, sw: float32 ctx: Context, rect: Rect, nw, ne, se, sw: float32
) {.raises: [PixieError].} = ) {.raises: [PixieError].} =
@ -709,14 +722,6 @@ proc strokeRoundedRect*(
## current strokeStyle and other context settings. ## current strokeStyle and other context settings.
ctx.strokeRoundedRect(rect, radius, radius, radius, radius) ctx.strokeRoundedRect(rect, radius, radius, radius, radius)
proc strokeSegment*(ctx: Context, segment: Segment) {.raises: [PixieError].} =
## Strokes a segment (draws a line from segment.at to segment.to) according
## to the current strokeStyle and other context settings.
let path = newPath()
path.moveTo(segment.at)
path.lineTo(segment.to)
ctx.stroke(path)
proc fillEllipse*( proc fillEllipse*(
ctx: Context, center: Vec2, rx, ry: float32 ctx: Context, center: Vec2, rx, ry: float32
) {.raises: [PixieError].} = ) {.raises: [PixieError].} =