Permalink
Browse files

Re-write of GoogleFont to properly support Canvas and HTML loading wi…

…th integration of WebFontLoader.
  • Loading branch information...
darkfrog26 committed Apr 4, 2018
1 parent d70dcec commit b3ac2ebb94ed68b6a0581ddf36337ec0accbb510
View
@@ -31,9 +31,11 @@ val scribeVersion = "2.3.2"
val powerScalaVersion = "2.0.5"
val reactifyVersion = "2.3.0"
val hasherVersion = "1.2.1"
val canvgVersion = "1.4.0_1"
val openTypeVersion = "0.7.3"
val picaVersion = "3.0.5"
val webFontLoaderVersion = "1.6.28"
val akkaVersion = "2.5.11"
val scalaJSDOM = "0.9.5"
@@ -203,6 +205,7 @@ lazy val ui = crossProject(JSPlatform, JVMPlatform).in(file("ui"))
)
.jsSettings(
libraryDependencies ++= Seq(
"com.outr" %%% "webfontloader-scala-js" % webFontLoaderVersion,
"com.outr" %%% "canvg-scala-js" % canvgVersion,
"com.outr" %%% "opentype-scala-js" % openTypeVersion,
"com.outr" %%% "pica-scala-js" % picaVersion
@@ -14,7 +14,7 @@ object HelloWorld extends Screen with PathActivation {
override protected def title: String = "Hello World"
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 { _ =>
OpenTypeFont.fromURL(fontURL).map { fnt =>
View
@@ -1 +1 @@
sbt.version=1.1.1
sbt.version=1.1.2
@@ -1,14 +1,18 @@
package io
import io.youi.event.KeyEvent
import io.youi.font.{GoogleFont, GoogleFontWeight}
import io.youi.paint.Paint
import io.youi.spatial.NumericSize
import org.scalajs.dom.html.Div
import org.scalajs.dom.raw.CanvasRenderingContext2D
import org.scalajs.dom.{KeyboardEvent, document, html}
import reactify._
import typekit.{GoogleConfig, WebFont, WebFontConfiguration}
import scala.concurrent.{Future, Promise}
import scala.language.implicitConversions
import scala.scalajs.js
package object youi {
lazy val ppi: Double = {
@@ -24,6 +28,38 @@ package object youi {
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) {
def context: CanvasRenderingContext2D = canvas.getContext("2d").asInstanceOf[CanvasRenderingContext2D]
}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,3 @@
package io.youi.font
class GoogleFontSubset(val name: String) extends AnyVal
@@ -0,0 +1,5 @@
package io.youi.font
trait GoogleFontSubsets {
def all: Set[GoogleFontSubset]
}
@@ -0,0 +1,5 @@
package io.youi.font
import io.youi.net.URL
case class GoogleFontWeight(font: GoogleFont, name: String, ttf: URL)
@@ -20,19 +20,37 @@ object GoogleFontBuilder {
val jsonString = IO.stream(new net.URL(url.toString), new StringBuilder).toString
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 =>
s""" object `${font.family}` {
| val category: String = "${font.category}"
| val subsets: Set[String] = Set(${font.subsets.map(s => s""""$s"""").mkString(", ")})
val subsets = font.subsets.map { sub =>
s""" lazy val `$sub`: GoogleFontSubset = new GoogleFontSubset("$sub")"""
}
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}
|
| override lazy val weights: List[GoogleFontWeight] = List(${font.variants.map(s => s"`$s`").mkString(", ")})
| }""".stripMargin
}
val template =
s"""package io.youi.font
|
|import io.youi.net.URL
|
|trait GoogleFont {
| def family: String
| def category: String
| def subsets: GoogleFontSubsets
| def weights: List[GoogleFontWeight]
|}
|
|object GoogleFont {
| private val base: String = s"https://fonts.gstatic.com/s"
|
@@ -53,7 +71,7 @@ object GoogleFontBuilder {
def toTemplate: String = variants.map { variant =>
val url = files(variant)
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")
}
}

0 comments on commit b3ac2eb

Please sign in to comment.