Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/js/src/main/scala/doodle/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package object syntax {
extends AngleSyntax
with BitmapSyntax
with BlendSyntax
with ClipSyntax
with DebugSyntax
with LayoutSyntax
with NormalizedSyntax
Expand All @@ -37,6 +38,7 @@ package object syntax {
object angle extends AngleSyntax
object bitmap extends BitmapSyntax
object blend extends BlendSyntax
object clip extends ClipSyntax
object debug extends DebugSyntax
object layout extends LayoutSyntax
object normalized extends NormalizedSyntax
Expand Down
2 changes: 2 additions & 0 deletions core/jvm/src/main/scala/doodle/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package object syntax {
with BitmapSyntax
with BlendSyntax
with BufferedImageWriterSyntax
with ClipSyntax
with DebugSyntax
with LayoutSyntax
with NormalizedSyntax
Expand All @@ -42,6 +43,7 @@ package object syntax {
object bitmap extends BitmapSyntax
object blend extends BlendSyntax
object bufferedImageWriter extends BufferedImageWriterSyntax
object clip extends ClipSyntax
object debug extends DebugSyntax
object layout extends LayoutSyntax
object normalized extends NormalizedSyntax
Expand Down
35 changes: 35 additions & 0 deletions core/shared/src/main/scala/doodle/algebra/Clip.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package doodle
package algebra

import doodle.core.ClosedPath

trait Clip extends Algebra {

def clip[A](img: Drawing[A], clipPath: ClosedPath): Drawing[A]
}

trait ClipConstructor {
self: BaseConstructor { type Algebra <: Clip } =>

def clip(image: Picture[Unit], clipPath: ClosedPath): Picture[Unit] =
new Picture[Unit] {
def apply(implicit algebra: Algebra): algebra.Drawing[Unit] =
algebra.clip(image(algebra), clipPath)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package doodle
package algebra
package generic

import cats.data.State
import doodle.core.BoundingBox
import doodle.core.ClosedPath
import doodle.core.{Transform => Tx}

trait GenericClip[G[_]] extends Clip {
self: Algebra { type Drawing[A] = Finalized[G, A] } =>

trait ClipApi {

def clip[A](
tx: Tx,
img: Drawing[A],
clipPath: ClosedPath
): G[A]
}

def ClipApi: ClipApi

def clip[A](img: Drawing[A], clipPath: ClosedPath): Drawing[A] =
Finalized.leaf { dc =>
val strokeWidth = dc.strokeWidth.getOrElse(0.0)
val bb = BoundingBox.centered(strokeWidth, strokeWidth)
(
bb,
State.inspect(tx => ClipApi.clip(tx, img, clipPath))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ trait GenericShape[G[_]] extends Shape {
)
}

def circle(diameter: Double): Finalized[G, Unit] =
def circle(diameter: Double): Finalized[G, Unit] = {
Finalized.leaf { dc =>
val strokeWidth = dc.strokeWidth.getOrElse(0.0)
val bb =
Expand All @@ -89,6 +89,7 @@ trait GenericShape[G[_]] extends Shape {
State.inspect(tx => ShapeApi.circle(tx, dc.fill, dc.stroke, diameter))
)
}
}

def empty: Finalized[G, Unit] =
Finalized.leaf { _ =>
Expand Down
35 changes: 35 additions & 0 deletions core/shared/src/main/scala/doodle/syntax/ClipSyntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package doodle
package syntax

import doodle.algebra.Algebra
import doodle.algebra.Picture
import doodle.algebra.Clip
import doodle.core.ClosedPath

trait ClipSyntax {
implicit class ClipPictureOps[Alg <: Clip, A](
picture: Picture[Alg, A]
) {
def clip(clipPath: ClosedPath): Picture[Alg, A] =
new Picture[Alg, A] {
def apply(implicit algebra: Alg): algebra.Drawing[A] =
algebra.clip(picture(algebra), clipPath)
}
}
}
1 change: 1 addition & 0 deletions java2d/src/main/scala/doodle/java2d/algebra/Algebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final case class Algebra(
with Java2dFromBufferedImage
with Java2dFromBase64
with ReifiedBitmap
with ReifiedClip
with ReifiedPath
with ReifiedShape
with ReifiedText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import doodle.core.Point
import doodle.core.font.Font
import doodle.core.{Transform => Tx}
import doodle.java2d.algebra.reified.GraphicsContext
import doodle.core.ClosedPath

import java.awt.Graphics2D
import java.awt.geom.Rectangle2D
Expand Down Expand Up @@ -179,4 +180,16 @@ object Graphics2DGraphicsContext extends GraphicsContext[Graphics2D] {
gc.drawString(text, x.toFloat, y.toFloat)
}
}

def clip[C](
gc: Graphics2D
)(
transform: Tx,
img: Drawing[C],
clipPath: ClosedPath
): Unit = {
val clip_area = Java2D.toPath2D(clipPath.elements)
gc.setClip(clip_area)
//gc.clip(img)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import doodle.core.PathElement
import doodle.core.Point
import doodle.core.Transform
import doodle.core.font.Font
import doodle.core.ClosedPath

import java.awt.geom.Rectangle2D
import java.awt.image.BufferedImage
Expand Down Expand Up @@ -77,4 +78,12 @@ trait GraphicsContext[A] {
font: Font,
bounds: Rectangle2D
): Unit

def clip[C](
gc: A
)(
transform: Transform,
img: Drawing[C],
clipPath: ClosedPath
): Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import doodle.core.PathElement
import doodle.core.Point
import doodle.core.font.Font
import doodle.core.{Transform => Tx}
import doodle.core.ClosedPath

import java.awt.geom.Rectangle2D
import java.awt.image.BufferedImage
Expand All @@ -37,7 +38,7 @@ sealed abstract class Reified extends Product with Serializable {
/** finalTransform gives an transform applied after any other reified
* transform. Usually this is a transform from logical to screen coordinates.
*/
def render[A](gc: A, finalTransform: Tx)(implicit
def render[A, C](gc: A, finalTransform: Tx)(implicit
ctx: GraphicsContext[A]
): Unit =
this match {
Expand All @@ -53,6 +54,7 @@ sealed abstract class Reified extends Product with Serializable {

case FillCircle(tx, fill, diameter) =>
ctx.fillCircle(gc)(tx.andThen(finalTransform), fill, diameter)

case StrokeCircle(tx, stroke, diameter) =>
ctx.strokeCircle(gc)(tx.andThen(finalTransform), stroke, diameter)

Expand All @@ -71,6 +73,10 @@ sealed abstract class Reified extends Product with Serializable {

case Text(tx, _, stroke, text, font, bounds) =>
ctx.text(gc)(tx.andThen(finalTransform), stroke, text, font, bounds)

case Clip(tx, img, clipPath) =>
ctx.clip(gc)(tx.andThen(finalTransform), img, clipPath)

}
}
object Reified {
Expand Down Expand Up @@ -145,6 +151,12 @@ object Reified {
bounds: Rectangle2D
) extends Reified

final case class Clip[C](
transform: Tx,
img: Drawing[C],
clipPath: ClosedPath
) extends Reified

def fillRect(
transform: Tx,
fill: Fill,
Expand Down Expand Up @@ -211,4 +223,12 @@ object Reified {
bounds: Rectangle2D
): Reified =
Text(transform, fill, stroke, text, font, bounds)

def clip[C](
transform: Tx,
img: Drawing[C],
clipPath: ClosedPath
): Reified = {
Clip(transform, img, clipPath)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package doodle
package java2d
package algebra
package reified

import cats.data.WriterT
import doodle.algebra.generic._
import doodle.core.BoundingBox
import doodle.core.ClosedPath
import doodle.core.{Transform => Tx}

import java.awt.Graphics2D

trait ReifiedClip extends GenericClip[Reification] {
self: Algebra {
type Drawing[A] <: doodle.java2d.Drawing[A]
def gc: Graphics2D
} =>

val ClipApi = new ClipApi {

def clip[A](
tx: Tx,
img: Drawing[A],
clipPath: ClosedPath
): Reification[A] = {
//???
WriterT.tell(List(Reified.clip(tx, img, clipPath)))
}
}
}
2 changes: 2 additions & 0 deletions java2d/src/main/scala/doodle/java2d/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ package object java2d extends Java2dToPicture {
doodle.algebra.Algebra
with Basic
with Bitmap
with Clip
with FromBufferedImage
with FromPngBase64
with FromGifBase64
Expand Down Expand Up @@ -79,6 +80,7 @@ package object java2d extends Java2dToPicture {
object Picture
extends BaseConstructor
with BitmapConstructor
with ClipConstructor
with FromGifBase64Constructor
with FromPngBase64Constructor
with FromJpgBase64Constructor
Expand Down