Skip to content

Commit

Permalink
Add text support to Image
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Aug 29, 2022
1 parent 2a4dc0c commit efc3371
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions core/shared/src/main/scala/doodle/language/Basic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ trait Basic[F[_]]
with Shape[F]
with Size[F]
with Style[F]
with Text[F]
with Transform[F]
object Basic {
def picture[F[_], A](f: Basic[F] => F[A]): Picture[Basic, F, A] =
Expand Down
17 changes: 12 additions & 5 deletions image/shared/src/main/scala/doodle/image/Image.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package doodle
package image

import doodle.core._
import doodle.core.font.{Font => CoreFont}
import doodle.language.Basic

sealed abstract class Image extends Product with Serializable {
Expand Down Expand Up @@ -56,8 +57,8 @@ sealed abstract class Image extends Product with Serializable {
def noFill: Image =
NoFill(this)

// def font(font: Font): Image =
// ContextTransform(_.font(font), this)
def font(font: CoreFont): Image =
Font(this, font)

// Affine Transform -------------------------------------------------

Expand Down Expand Up @@ -133,7 +134,7 @@ object Image {
object Elements {
final case class OpenPath(elements: List[PathElement]) extends Path
final case class ClosedPath(elements: List[PathElement]) extends Path
// final case class Text(get: String) extends Image
final case class Text(get: String) extends Image
final case class Circle(d: Double) extends Image
final case class Rectangle(w: Double, h: Double) extends Image
final case class Triangle(w: Double, h: Double) extends Image
Expand All @@ -151,6 +152,7 @@ object Image {
extends Image
final case class NoStroke(image: Image) extends Image
final case class NoFill(image: Image) extends Image
final case class Font(image: Image, font: CoreFont) extends Image
// Debug
final case class Debug(image: Image, color: Color) extends Image

Expand All @@ -172,8 +174,8 @@ object Image {
OpenPath((PathElement.moveTo(0, 0) +: elements).toList)
}

// def text(characters: String): Image =
// Text(characters)
def text(characters: String): Image =
Text(characters)

def line(x: Double, y: Double): Image = {
val startX = -x / 2
Expand Down Expand Up @@ -274,6 +276,11 @@ object Image {
case ClosedPath(elements) =>
algebra.path(doodle.core.ClosedPath(elements))

case Text(t) =>
algebra.text(t)
case Font(image, f) =>
algebra.font(compile(image)(algebra), f)

case Circle(d) =>
algebra.circle(d)
case Rectangle(w, h) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object BoxesAndArrows {
FontWeight.normal,
FontSize.points((size / 2.0).toInt)
)
// val equals = text("=").font(font)
val equals = text("=").font(font)

val c = circle(size * 0.3).fillColor(Color.black)
val t = triangle(size * 0.6, size * 0.6).fillColor(Color.black)
Expand Down Expand Up @@ -49,24 +49,33 @@ object BoxesAndArrows {
val map: Image =
besideWithSpace(
List(
circleBox, /*text("map").font(font),*/ circleToTriangle,
/*equals,*/ triangleBox
circleBox,
text("map").font(font),
circleToTriangle,
equals,
triangleBox
)
)

val applicative: Image =
besideWithSpace(
List(
circleBox, /* text("|@|").font(font), */ triangleBox,
/*equals,*/ circleAndTriangleBox
circleBox,
text("|@|").font(font),
triangleBox,
equals,
circleAndTriangleBox
)
)

val flatMap: Image =
besideWithSpace(
List(
circleBox, /*text("flatMap").font(font),*/ circleToTriangleBox,
/*equals,*/ triangleBox
circleBox,
text("flatMap").font(font),
circleToTriangleBox,
equals,
triangleBox
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ object CreativeScala {
object point {
val point = Image.circle(5).at(40, 40).fillColor(Color.red).noStroke
val spacer = Image.square(5).noStroke.noFill
val xAxis = Image.line(40, 0) above spacer //above Image.text("x")
val yAxis = Image.line(0, 40) beside spacer //beside Image.text("y")
val xAxis = Image.line(40, 0) above spacer above Image.text("x")
val yAxis = Image.line(0, 40) beside spacer beside Image.text("y")
val cartesian = xAxis on yAxis on point
}

Expand Down

0 comments on commit efc3371

Please sign in to comment.