diff --git a/README.md b/README.md index ee8f9e3..a47163d 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ Here are some examples of using Pixie for realtime rendering with some popular w ### Text [examples/text.nim](examples/text.nim) ```nim -var font = readFont("tests/fonts/Roboto-Regular_1.ttf") +var font = readFont("examples/data/Roboto-Regular_1.ttf") font.size = 20 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 [examples/text_spans.nim](examples/text_spans.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 = result = newFont(typeface) @@ -272,7 +272,7 @@ path.polygon( ) 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)) image.fillPath(path, paint) diff --git a/bindings/bindings.nim b/bindings/bindings.nim index e4d5fc7..1260994 100644 --- a/bindings/bindings.nim +++ b/bindings/bindings.nim @@ -14,15 +14,42 @@ type x*, y*: float32 Matrix3* = object - a*, b*, c*, d*, e*, f*, g*, h*, i*: float32 + values*: array[9, float32] 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*( - ctx: Context, image: Image, dx, dy: float32 -) {.raises: [PixieError].} = - ctx.drawImage(image, dx, dy) +proc mul*(a, b: Matrix3): Matrix3 = + cast[Matrix3](cast[Mat3](a) * cast[Mat3](b)) + +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*( ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32 @@ -62,6 +89,8 @@ exportObject Vector2: exportObject Matrix3: constructor: matrix3 + procs: + mul(Matrix3, Matrix3) exportObject Rect: discard @@ -257,7 +286,7 @@ exportRefObject Context: setTransform transform(Context, Mat3) resetTransform - drawImage1 + drawImage(Context, Image, float32, float32) drawImage2 drawImage3 moveTo(Context, float32, float32) @@ -274,6 +303,7 @@ exportRefObject Context: clearRect(Context, float32, float32, float32, float32) fillRect(Context, float32, float32, float32, float32) strokeRect(Context, float32, float32, float32, float32) + strokeSegment(Context, float32, float32, float32, float32) fillText(Context, string, float32, float32) strokeText(Context, string, float32, float32) translate(Context, float32, float32) @@ -292,6 +322,10 @@ exportProcs: parsePath miterLimitToAngle angleToMiterLimit + parseColor + translate(float32, float32) + rotate(float32) + scale(float32, float32) writeFiles("bindings/generated", "pixie") diff --git a/examples/data/Roboto-Regular_1.ttf b/examples/data/Roboto-Regular_1.ttf new file mode 100644 index 0000000..2c97eea Binary files /dev/null and b/examples/data/Roboto-Regular_1.ttf differ diff --git a/examples/data/Ubuntu-Regular_1.ttf b/examples/data/Ubuntu-Regular_1.ttf new file mode 100644 index 0000000..2001d6e Binary files /dev/null and b/examples/data/Ubuntu-Regular_1.ttf differ diff --git a/examples/data/baboon.png b/examples/data/baboon.png new file mode 100644 index 0000000..a8556f2 Binary files /dev/null and b/examples/data/baboon.png differ diff --git a/examples/image_tiled.nim b/examples/image_tiled.nim index 2f5d3ce..b84f685 100644 --- a/examples/image_tiled.nim +++ b/examples/image_tiled.nim @@ -11,7 +11,7 @@ path.polygon( ) 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)) image.fillPath(path, paint) diff --git a/examples/text.nim b/examples/text.nim index 05f16f3..ab11b92 100644 --- a/examples/text.nim +++ b/examples/text.nim @@ -3,7 +3,7 @@ import pixie let image = newImage(200, 200) 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 let text = "Typesetting is the arrangement and composition of text in graphic design and publishing in both digital and traditional medias." diff --git a/examples/text_spans.nim b/examples/text_spans.nim index 1649d97..1976c60 100644 --- a/examples/text_spans.nim +++ b/examples/text_spans.nim @@ -3,7 +3,7 @@ import pixie let image = newImage(200, 200) 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 = result = newFont(typeface) diff --git a/src/pixie/contexts.nim b/src/pixie/contexts.nim index 48d95e7..329a459 100644 --- a/src/pixie/contexts.nim +++ b/src/pixie/contexts.nim @@ -679,6 +679,19 @@ proc polygon*( ## Adds an n-sided regular polygon at (x, y) of size to the current path. 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*( ctx: Context, rect: Rect, nw, ne, se, sw: float32 ) {.raises: [PixieError].} = @@ -709,14 +722,6 @@ proc strokeRoundedRect*( ## current strokeStyle and other context settings. 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*( ctx: Context, center: Vec2, rx, ry: float32 ) {.raises: [PixieError].} =