diff options
author | bptato <nincsnevem662@gmail.com> | 2024-09-01 16:35:51 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-09-01 17:07:05 +0200 |
commit | d07187fc186681e262afd67bd748206e40aef346 (patch) | |
tree | bef16c305013311c01f4ee821d03dc2550b75a95 /src/img | |
parent | d1ff49b0fc39171859cfa781f459a2e0242c2e79 (diff) | |
download | chawan-d07187fc186681e262afd67bd748206e40aef346.tar.gz |
canvas: make sure we don't link to QJS
Diffstat (limited to 'src/img')
-rw-r--r-- | src/img/painter.nim | 32 | ||||
-rw-r--r-- | src/img/path.nim | 26 |
2 files changed, 25 insertions, 33 deletions
diff --git a/src/img/painter.nim b/src/img/painter.nim index 0479bd77..c86ad0a2 100644 --- a/src/img/painter.nim +++ b/src/img/painter.nim @@ -1,20 +1,27 @@ import std/algorithm import std/unicode -import css/cssvalues import img/bitmap import img/path import types/color import types/line import types/vector -type CanvasFillRule* = enum - cfrNonZero = "nonzero" - cfrEvenOdd = "evenodd" +type + CanvasFillRule* = enum + cfrNonZero = "nonzero" + cfrEvenOdd = "evenodd" -type PaintCommand* = enum - pcSetDimensions, pcFillRect, pcStrokeRect, pcFillPath, pcStrokePath, - pcFillText, pcStrokeText + PaintCommand* = enum + pcSetDimensions, pcFillRect, pcStrokeRect, pcFillPath, pcStrokePath, + pcFillText, pcStrokeText + + CanvasTextAlign* = enum + ctaStart = "start" + ctaEnd = "end" + ctaLeft = "left" + ctaRight = "right" + ctaCenter = "center" # https://en.wikipedia.org/wiki/Bresenham's_line_algorithm#All_cases proc plotLineLow(bmp: Bitmap; x1, y1, x2, y2: int; color: ARGBColor) = @@ -181,7 +188,7 @@ proc drawBitmap(a, b: Bitmap; p: Vector2D) = a.setpxb(ax, ay, b.getpx(x, y)) proc fillText*(bmp: Bitmap; text: string; x, y: float64; color: ARGBColor; - textAlign: CSSTextAlign) = + textAlign: CanvasTextAlign) = var w = 0f64 var glyphs: seq[Bitmap] = @[] for r in text.runes: @@ -191,15 +198,14 @@ proc fillText*(bmp: Bitmap; text: string; x, y: float64; color: ARGBColor; var x = x #TODO rtl case textAlign - of TextAlignLeft, TextAlignStart: discard - of TextAlignRight, TextAlignEnd: x -= w - of TextAlignCenter: x -= w / 2 - else: doAssert false + of ctaLeft, ctaStart: discard + of ctaRight, ctaEnd: x -= w + of ctaCenter: x -= w / 2 for glyph in glyphs: bmp.drawBitmap(glyph, Vector2D(x: x, y: y - 8)) x += float64(glyph.width) proc strokeText*(bmp: Bitmap; text: string; x, y: float64; color: ARGBColor; - textAlign: CSSTextAlign) = + textAlign: CanvasTextAlign) = #TODO bmp.fillText(text, x, y, color, textAlign) diff --git a/src/img/path.nim b/src/img/path.nim index 5c1ce680..c5db0bea 100644 --- a/src/img/path.nim +++ b/src/img/path.nim @@ -4,8 +4,6 @@ import std/math import types/line import types/vector -import js/domexception -import types/opt type Path* = ref object @@ -303,13 +301,10 @@ proc bezierCurveTo*(path: Path; cp0x, cp0y, cp1x, cp1y, x, y: float64) = let p = Vector2D(x: x, y: y) path.addBezierSegment(cp0, cp1, p) -proc arcTo*(path: Path; x1, y1, x2, y2, radius: float64): Err[DOMException] = +proc arcTo*(path: Path; x1, y1, x2, y2, radius: float64) = for v in [x1, y1, x2, y2, radius]: if classify(v) in {fcInf, fcNegInf, fcNan}: - return ok() - if radius < 0: - return errDOMException("Expected positive radius, but got negative", - "IndexSizeError") + return path.ensureSubpath(x1, y1) #TODO this should be transformed by the inverse of the transformation matrix let v0 = path.subpaths[^1].points[^1] @@ -333,7 +328,6 @@ proc arcTo*(path: Path; x1, y1, x2, y2, radius: float64): Err[DOMException] = ) path.addStraightSegment(tv0) path.addArcSegment(origin, tv2, radius, true) #TODO always inner? - return ok() func resolveEllipsePoint(o: Vector2D; angle, radiusX, radiusY, rotation: float64): Vector2D = @@ -350,13 +344,10 @@ func resolveEllipsePoint(o: Vector2D; angle, radiusX, radiusY, return Vector2D(x: relx, y: rely).rotate(rotation) + o proc arc*(path: Path; x, y, radius, startAngle, endAngle: float64; - counterclockwise: bool): Err[DOMException] = + counterclockwise: bool) = for v in [x, y, radius, startAngle, endAngle]: if classify(v) in {fcInf, fcNegInf, fcNan}: - return ok() - if radius < 0: - return errDOMException("Expected positive radius, but got negative", - "IndexSizeError") + return let o = Vector2D(x: x, y: y) var s = resolveEllipsePoint(o, startAngle, radius, radius, 0) var e = resolveEllipsePoint(o, endAngle, radius, radius, 0) @@ -369,16 +360,12 @@ proc arc*(path: Path; x, y, radius, startAngle, endAngle: float64; else: path.moveTo(s) path.addArcSegment(o, e, radius, abs(startAngle - endAngle) < PI) - return ok() proc ellipse*(path: Path; x, y, radiusX, radiusY, rotation, startAngle, - endAngle: float64; counterclockwise: bool): Err[DOMException] = + endAngle: float64; counterclockwise: bool) = for v in [x, y, radiusX, radiusY, rotation, startAngle, endAngle]: if classify(v) in {fcInf, fcNegInf, fcNan}: - return ok() - if radiusX < 0 or radiusY < 0: - return errDOMException("Expected positive radius, but got negative", - "IndexSizeError") + return let o = Vector2D(x: x, y: y) var s = resolveEllipsePoint(o, startAngle, radiusX, radiusY, rotation) var e = resolveEllipsePoint(o, endAngle, radiusX, radiusY, rotation) @@ -391,7 +378,6 @@ proc ellipse*(path: Path; x, y, radiusX, radiusY, rotation, startAngle, else: path.moveTo(s) path.addEllipseSegment(o, e, radiusX, radiusY) - return ok() proc rect*(path: Path; x, y, w, h: float64) = for v in [x, y, w, h]: |