From 37428fb28ce7649cc740bbcce311773df50b59a2 Mon Sep 17 00:00:00 2001 From: treeform Date: Tue, 1 Dec 2020 09:49:41 -0800 Subject: [PATCH] Change APIs some. Add 2 examples. --- README.md | 39 ++++++++++ examples/example.nim | 1 - examples/rounded_rectangle.nim | 25 +++++++ examples/rounded_rectangle.png | Bin 0 -> 2184 bytes examples/square.nim | 14 ++++ examples/square.png | Bin 0 -> 918 bytes src/pixie/images.nim | 4 ++ src/pixie/paths.nim | 128 +++++++++++++++------------------ tools/gen_readme.nim | 34 +++++++++ 9 files changed, 174 insertions(+), 71 deletions(-) delete mode 100644 examples/example.nim create mode 100644 examples/rounded_rectangle.nim create mode 100644 examples/rounded_rectangle.png create mode 100644 examples/square.nim create mode 100644 examples/square.png create mode 100644 tools/gen_readme.nim diff --git a/README.md b/README.md index 547d1bb..163224b 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,42 @@ This library is being actively developed and is not yet ready for use. Since you ## Testing `nimble test` + +## Examples + +### examples/rounded_rectangle.nim +```nim +var path = newPath() +let + x = 50.0 + y = 50.0 + w = 100.0 + h = 100.0 + nw = 25.0 + ne = 25.0 + se = 25.0 + sw = 25.0 +path.moveTo(x+nw, y) +path.arcTo(x+w, y, x+w, y+h, ne) +path.arcTo(x+w, y+h, x, y+h, se) +path.arcTo(x, y+h, x, y, sw) +path.arcTo(x, y, x+w, y, nw) +path.closePath() +path.closePath() +image.fillPath(path, rgba(255, 0, 0, 255)) +#image.strokePath(path, rgba(0, 0, 0, 255), strokeWidth = 5.0) +``` +![example output](examples/rounded_rectangle.png) + +### examples/square.nim +```nim +var p = newPath() +p.moveTo(50, 50) +p.lineTo(50, 150) +p.lineTo(150, 150) +p.lineTo(150, 50) +p.closePath() +image.fillPath(p, rgba(255, 0, 0, 255)) +#image.strokePath(p, rgba(0, 0, 0, 255), strokeWidth = 5.0) +``` +![example output](examples/square.png) diff --git a/examples/example.nim b/examples/example.nim deleted file mode 100644 index 0262224..0000000 --- a/examples/example.nim +++ /dev/null @@ -1 +0,0 @@ -## Include an example on how to use your library diff --git a/examples/rounded_rectangle.nim b/examples/rounded_rectangle.nim new file mode 100644 index 0000000..28f5896 --- /dev/null +++ b/examples/rounded_rectangle.nim @@ -0,0 +1,25 @@ +import pixie, chroma + +var image = newImageFill(200, 200, rgba(255, 255, 255, 255)) + +var path = newPath() +let + x = 50.0 + y = 50.0 + w = 100.0 + h = 100.0 + nw = 25.0 + ne = 25.0 + se = 25.0 + sw = 25.0 +path.moveTo(x+nw, y) +path.arcTo(x+w, y, x+w, y+h, ne) +path.arcTo(x+w, y+h, x, y+h, se) +path.arcTo(x, y+h, x, y, sw) +path.arcTo(x, y, x+w, y, nw) +path.closePath() +path.closePath() +image.fillPath(path, rgba(255, 0, 0, 255)) +#image.strokePath(path, rgba(0, 0, 0, 255), strokeWidth = 5.0) + +image.writeFile("examples/rounded_rectangle.png") diff --git a/examples/rounded_rectangle.png b/examples/rounded_rectangle.png new file mode 100644 index 0000000000000000000000000000000000000000..996594e17ba1eba45d8f1aaa07d5af4a3bef3bd1 GIT binary patch literal 2184 zcmds3X*3&%7Pdq!W2@Sxq|)KFI;d)lou-pmA|Z5;+DTiT2u;V@3Nc}5YiX5=DQzer z))G-B2yeRBmyuc%OM3=sDodqpRH{7l_nq_3d4JyfF+c9T-?{gm^ZmK^e)pE2ucwCE zVKq59ISsfM?A(4F`pZ-f?E9AUk129;DkwPYY``^n;Zo>Oz$L@n*R(pzc&JgH=7>~T zPj}BGp!1PtdvlSU7vM1F7ARR8uyA%jmT93-^QOv7Z8Y!4M2!D8a5zCFzdie2HPvi* zB;{Tf-$ZOl9NQrjZqJRS@xr&1o(b$XLK7#;$II`}4h;=?$wwiroCD+rV4$C~A#%zj zIZXw5V}OE#3gD`?$}5oeKhOS;3#ZQohPeCd28J6JY5ZsQg0+?|DzCDPLhwy{C68Tb zPXJ%OZEHKdXH?NQg_XU7@)?sYQ?NDuw6FSwlDFgJZFwnlH~bS*)k&AX=V%(cgx1ba_MUJ2PrpJJSd7QaQg%a zX{Q=jp7`M~6NZfdiTfflNJ_K2LQrLs< zUzFTl(^_rqr+y6{F?d}+K&xzI(SPze0^fziOp+tg8p@pyqy+az$YeNv_eKhk>?eW< z5({sb_&RX2&i4exKB4AYYbInjmf356ez&n+F+*G~5YbU<*1djCe5xqivoPU+WYB~? z-N0_`pLOWah_+rtx02V#OO7GE?57+RgWDi!Up^**Et*$|7HHIcJ1c>YAfOKt=W0rb zzJeE4r@HO_wo|}wdzO~^(6z|Ah>N#IpN8QV6xrf_1UhnwyCG~3Bh4RQ?ey->kdl@^ z?<8V()3I#oa%5`Rs@n6Q=1_Vy0-D2WSFWqML!LO3*?L@Pz*L~q&_?#s(RtoQ>v`UU z4>5FdZGs=7)aq(&0y*zcEaJ9=~rJG=4j z|BwJABNpsjjZ$^B0c;ec+?DXaKnF^JEN4c_-qaY@aFWWxkNH$mFuD>z`*j8fac~;H zKX+H#cDeo}7ZUP;xvRuW37Yj&&mNIqUo((lb8uDldCU-F()-MwadcS4#%f8edp;YP zNr8+F6vp=JpUqgGD6R8t5z8#d7Da#F;bDUwN*6psx&xdfLr!@2+5{yGc%8LHZS8AJ zDtdQjq1beoXHv7D^>y)2lfbz-5uu&Pau?>@94ByFf&O(zOyP$Y2H`-5ozXyaZ7^%K zQlf;ZL*6}-8OG^bl0M=d^C^zV2z1(ccO~+N$u;%oG1I>*BP`I;bchjl6NBY++$*7g zwG)@GvpG-pk6EYIQiAXa8>*GO!N%=xeBxRuJ5P_jwZe4^5H>SLW(b-NRc{fRrl}yi zKLrP`Y$sqx$)~a@PfyUin$QSO#reSBkC7T60zDM%M?PqqEJrlMS zZS;r7?yuo=mUn?B1dfb@YjCsLvNhGdfafjQRG#WAFU@$H?1Tulxl4uOZ| z<@fK&kk{{6G line.to.y - let x = at.x.clamp(0, image.width.float32) + let x = at.x.clamp(0, size.x) hits.add((x, winding)) hits.sort(proc(a, b: (float32, bool)): int = cmp(a[0], b[0])) @@ -623,103 +623,91 @@ proc fillPolygons*( {.pop.} proc fillPath*( - image: Image, - path: Path, - color: ColorRGBA - ): Image = - let polys = commandsToPolygons(path.commands) - image.fillPolygons(polys, color) + image: Image, + path: Path, + color: ColorRGBA +) = + let + polys = commandsToPolygons(path.commands) + tmp = fillPolygons(image.wh, polys, color) + image.draw(tmp) proc fillPath*( - image: Image, - path: string, - color: ColorRGBA - ): Image = - image.fillPath(parsePath(path), color) - -proc fillPath*( - image: Image, - path: string, - color: ColorRGBA, - pos: Vec2 - ): Image = - var polys = commandsToPolygons(parsePath(path).commands) - for poly in polys.mitems: - for i, p in poly.mpairs: - poly[i] = p + pos - image.fillPolygons(polys, color) - -proc fillPath*( - image: Image, - path: Path, - color: ColorRGBA, - mat: Mat3 - ): Image = + image: Image, + path: Path, + color: ColorRGBA, + mat: Mat3 +) = var polys = commandsToPolygons(path.commands) for poly in polys.mitems: for i, p in poly.mpairs: poly[i] = mat * p - image.fillPolygons(polys, color) + let tmp = fillPolygons(image.wh, polys, color) + image.draw(tmp) proc fillPath*( - image: Image, - path: string, - color: ColorRGBA, - mat: Mat3 - ): Image = + image: Image, + path: string, + color: ColorRGBA, + mat: Mat3 +) = image.fillPath(parsePath(path), color, mat) proc strokePath*( - image: Image, - path: Path, - color: ColorRGBA, - strokeWidth: float32, - # strokeLocation: StrokeLocation, - # strokeCap: StorkeCap, - # strokeJoin: StorkeJoin - ): Image = - let polys = commandsToPolygons(path.commands) - let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2) - let polys2 = strokePolygons(polys, strokeL, strokeR) - image.fillPolygons(polys2, color) + image: Image, + path: Path, + color: ColorRGBA, + strokeWidth: float32 = 1.0, + # strokeLocation: StrokeLocation, + # strokeCap: StorkeCap, + # strokeJoin: StorkeJoin +) = + let + polys = commandsToPolygons(path.commands) + (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2) + polys2 = strokePolygons(polys, strokeL, strokeR) + tmp = fillPolygons(image.wh, polys2, color) + image.draw(tmp) proc strokePath*( - image: Image, - path: string, - color: ColorRGBA, - strokeWidth: float32 - ): Image = + image: Image, + path: string, + color: ColorRGBA, + strokeWidth: float32 +) = image.strokePath(parsePath(path), color, strokeWidth) proc strokePath*( - image: Image, - path: string, - color: ColorRGBA, - strokeWidth: float32, - pos: Vec2 - ): Image = + image: Image, + path: string, + color: ColorRGBA, + strokeWidth: float32, + pos: Vec2 +) = var polys = commandsToPolygons(parsePath(path).commands) let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2) var polys2 = strokePolygons(polys, strokeL, strokeR) for poly in polys2.mitems: for i, p in poly.mpairs: poly[i] = p + pos - image.fillPolygons(polys2, color) + let tmp = fillPolygons(image.wh, polys2, color) + image.draw(tmp) proc strokePath*( - image: Image, - path: string, - color: ColorRGBA, - strokeWidth: float32, - mat: Mat3 - ): Image = + image: Image, + path: string, + color: ColorRGBA, + strokeWidth: float32, + mat: Mat3 +) = var polys = commandsToPolygons(parsePath(path).commands) let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2) var polys2 = strokePolygons(polys, strokeL, strokeR) for poly in polys2.mitems: for i, p in poly.mpairs: poly[i] = mat * p - image.fillPolygons(polys2, color) + let tmp = fillPolygons(image.wh, polys2, color) + image.draw(tmp) proc addPath*(path: Path, other: Path) = ## Adds a path to the current path. diff --git a/tools/gen_readme.nim b/tools/gen_readme.nim new file mode 100644 index 0000000..960b51f --- /dev/null +++ b/tools/gen_readme.nim @@ -0,0 +1,34 @@ +import os, strutils, osproc + +proc cutBetween(str, a, b: string): string = + let + cutA = str.find(a) + cutB = str.find(b) + if cutA == -1 or cutB == -1: + return "" + return str[cutA + a.len..