Skip to content

Commit

Permalink
Re-write of GoogleFont to properly support Canvas and HTML loading wi…
Browse files Browse the repository at this point in the history
…th integration of WebFontLoader.
  • Loading branch information
darkfrog26 committed Apr 4, 2018
1 parent d70dcec commit b3ac2eb
Show file tree
Hide file tree
Showing 9 changed files with 13,054 additions and 5,517 deletions.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ val scribeVersion = "2.3.2"
val powerScalaVersion = "2.0.5" val powerScalaVersion = "2.0.5"
val reactifyVersion = "2.3.0" val reactifyVersion = "2.3.0"
val hasherVersion = "1.2.1" val hasherVersion = "1.2.1"

val canvgVersion = "1.4.0_1" val canvgVersion = "1.4.0_1"
val openTypeVersion = "0.7.3" val openTypeVersion = "0.7.3"
val picaVersion = "3.0.5" val picaVersion = "3.0.5"
val webFontLoaderVersion = "1.6.28"


val akkaVersion = "2.5.11" val akkaVersion = "2.5.11"
val scalaJSDOM = "0.9.5" val scalaJSDOM = "0.9.5"
Expand Down Expand Up @@ -203,6 +205,7 @@ lazy val ui = crossProject(JSPlatform, JVMPlatform).in(file("ui"))
) )
.jsSettings( .jsSettings(
libraryDependencies ++= Seq( libraryDependencies ++= Seq(
"com.outr" %%% "webfontloader-scala-js" % webFontLoaderVersion,
"com.outr" %%% "canvg-scala-js" % canvgVersion, "com.outr" %%% "canvg-scala-js" % canvgVersion,
"com.outr" %%% "opentype-scala-js" % openTypeVersion, "com.outr" %%% "opentype-scala-js" % openTypeVersion,
"com.outr" %%% "pica-scala-js" % picaVersion "com.outr" %%% "pica-scala-js" % picaVersion
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object HelloWorld extends Screen with PathActivation {
override protected def title: String = "Hello World" override protected def title: String = "Hello World"
override def path: String = "/examples/hello.html" override def path: String = "/examples/hello.html"


val fontURL: URL = GoogleFont.`Open Sans`.`regular` val fontURL: URL = GoogleFont.`Open Sans`.`regular`.ttf


override protected def init(): Future[Unit] = super.init().flatMap { _ => override protected def init(): Future[Unit] = super.init().flatMap { _ =>
OpenTypeFont.fromURL(fontURL).map { fnt => OpenTypeFont.fromURL(fontURL).map { fnt =>
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.1.1 sbt.version=1.1.2
36 changes: 36 additions & 0 deletions ui/js/src/main/scala/io/youi/youi.scala
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,18 @@
package io package io


import io.youi.event.KeyEvent import io.youi.event.KeyEvent
import io.youi.font.{GoogleFont, GoogleFontWeight}
import io.youi.paint.Paint import io.youi.paint.Paint
import io.youi.spatial.NumericSize import io.youi.spatial.NumericSize
import org.scalajs.dom.html.Div import org.scalajs.dom.html.Div
import org.scalajs.dom.raw.CanvasRenderingContext2D import org.scalajs.dom.raw.CanvasRenderingContext2D
import org.scalajs.dom.{KeyboardEvent, document, html} import org.scalajs.dom.{KeyboardEvent, document, html}
import reactify._ import reactify._
import typekit.{GoogleConfig, WebFont, WebFontConfiguration}


import scala.concurrent.{Future, Promise}
import scala.language.implicitConversions import scala.language.implicitConversions
import scala.scalajs.js


package object youi { package object youi {
lazy val ppi: Double = { lazy val ppi: Double = {
Expand All @@ -24,6 +28,38 @@ package object youi {


implicit def color2Paint(color: Color): Paint = Paint.color(color) implicit def color2Paint(color: Color): Paint = Paint.color(color)


implicit class ExtendedGoogleFont(font: GoogleFont) {
def load(): Future[GoogleFont] = {
val promise = Promise[GoogleFont]
val f: js.Function0[Unit] = () => {
promise.success(font)
}
WebFont.load(new WebFontConfiguration {
google = new GoogleConfig {
families = js.Array(font.family)
}
active = f
})
promise.future
}
}

implicit class ExtendedGoogleFontWeight(weight: GoogleFontWeight) {
def load(): Future[GoogleFontWeight] = {
val promise = Promise[GoogleFontWeight]
val f: js.Function0[Unit] = () => {
promise.success(weight)
}
WebFont.load(new WebFontConfiguration {
google = new GoogleConfig {
families = js.Array(s"${weight.font.family}:${weight.name}")
}
active = f
})
promise.future
}
}

implicit class ExtendedCanvas(canvas: html.Canvas) { implicit class ExtendedCanvas(canvas: html.Canvas) {
def context: CanvasRenderingContext2D = canvas.getContext("2d").asInstanceOf[CanvasRenderingContext2D] def context: CanvasRenderingContext2D = canvas.getContext("2d").asInstanceOf[CanvasRenderingContext2D]
} }
Expand Down
18,487 changes: 12,977 additions & 5,510 deletions ui/shared/src/main/scala/io/youi/font/GoogleFont.scala

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions ui/shared/src/main/scala/io/youi/font/GoogleFontSubset.scala
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.youi.font

class GoogleFontSubset(val name: String) extends AnyVal
5 changes: 5 additions & 0 deletions ui/shared/src/main/scala/io/youi/font/GoogleFontSubsets.scala
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.youi.font

trait GoogleFontSubsets {
def all: Set[GoogleFontSubset]
}
5 changes: 5 additions & 0 deletions ui/shared/src/main/scala/io/youi/font/GoogleFontWeight.scala
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.youi.font

import io.youi.net.URL

case class GoogleFontWeight(font: GoogleFont, name: String, ttf: URL)
28 changes: 23 additions & 5 deletions utilities/src/main/scala/io/youi/util/GoogleFontBuilder.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,19 +20,37 @@ object GoogleFontBuilder {
val jsonString = IO.stream(new net.URL(url.toString), new StringBuilder).toString val jsonString = IO.stream(new net.URL(url.toString), new StringBuilder).toString
val list = JsonUtil.fromJsonString[WebFontList](jsonString) val list = JsonUtil.fromJsonString[WebFontList](jsonString)


val file = new File("canvas/shared/src/main/scala/io/youi/font/GoogleFont.scala") val file = new File("ui/shared/src/main/scala/io/youi/font/GoogleFont.scala")
val fonts = list.items.map { font => val fonts = list.items.map { font =>
s""" object `${font.family}` { val subsets = font.subsets.map { sub =>
| val category: String = "${font.category}" s""" lazy val `$sub`: GoogleFontSubset = new GoogleFontSubset("$sub")"""
| val subsets: Set[String] = Set(${font.subsets.map(s => s""""$s"""").mkString(", ")}) }

s""" object `${font.family}` extends GoogleFont {
| override lazy val family: String = "${font.family}"
| override lazy val category: String = "${font.category}"
| override object subsets extends GoogleFontSubsets {
|${subsets.mkString("\n")}
|
| override lazy val all: Set[GoogleFontSubset] = Set(${font.subsets.map(s => s"`$s`").mkString(", ")})
| }
|${font.toTemplate} |${font.toTemplate}
|
| override lazy val weights: List[GoogleFontWeight] = List(${font.variants.map(s => s"`$s`").mkString(", ")})
| }""".stripMargin | }""".stripMargin
} }
val template = val template =
s"""package io.youi.font s"""package io.youi.font
| |
|import io.youi.net.URL |import io.youi.net.URL
| |
|trait GoogleFont {
| def family: String
| def category: String
| def subsets: GoogleFontSubsets
| def weights: List[GoogleFontWeight]
|}
|
|object GoogleFont { |object GoogleFont {
| private val base: String = s"https://fonts.gstatic.com/s" | private val base: String = s"https://fonts.gstatic.com/s"
| |
Expand All @@ -53,7 +71,7 @@ object GoogleFontBuilder {
def toTemplate: String = variants.map { variant => def toTemplate: String = variants.map { variant =>
val url = files(variant) val url = files(variant)
assert(url.startsWith(base)) assert(url.startsWith(base))
s""" def `$variant`: URL = URL(s"$$base${url.substring(base.length)}")""" s""" lazy val `$variant`: GoogleFontWeight = GoogleFontWeight(this, "$variant", URL(s"$$base${url.substring(base.length)}"))"""
}.mkString("\n") }.mkString("\n")
} }
} }

0 comments on commit b3ac2eb

Please sign in to comment.