Commit 71ef8ae4 authored by h2b's avatar h2b

Tests added and code reworked.

Not satisfying yet.
parent 011d91b8
......@@ -62,6 +62,6 @@ trait FontMetrics { font: Font ⇒
* @return a new font metrics derived from this metrics using the specified
* font
*/
def derived (newFont: Font): FontMetrics
def derived (newFont: Font): Font with FontMetrics
}
......@@ -69,13 +69,15 @@ trait AwtScreenDriver extends ScreenDriver {
private def newFont (size: Font.Size.Value,
emphasis: Set[Font.Emphasis.Value]): Font with FontMetrics = {
val awtFont: AwtFont = graphics.getFont
graphics.setFont(sized(awtFont.deriveFont(styled(emphasis)), size))
val newSizedFont = sized(awtFont, size)
val newStyledFont = styled(newSizedFont, emphasis)
graphics.setFont(newStyledFont)
val f = new Font(size, emphasis) with FontMetrics {
val metrics = graphics.getFontMetrics
val h = metrics.getHeight
def dimension (c: Char): (Int, Int) = (metrics.charWidth(c), h)
def dimension (s: String): (Int, Int) = (metrics.stringWidth(s), h)
def derived (newFont: Font): FontMetrics = this
def derived (newFont: Font): Font with FontMetrics = this
}
f
}
......@@ -84,21 +86,21 @@ trait AwtScreenDriver extends ScreenDriver {
var result = awtFont
size match {
//TODO: factors to be determined
case Font.Size.Tiny result = result.deriveFont(0.5f)
case Font.Size.Small result = result.deriveFont(0.8f)
case Font.Size.Tiny result = result.deriveFont(6f)
case Font.Size.Small result = result.deriveFont(10f)
case Font.Size.Normal ()
case Font.Size.Large result = result.deriveFont(1.2f)
case Font.Size.Huge result = result.deriveFont(2.0f)
case Font.Size.Large result = result.deriveFont(16f)
case Font.Size.Huge result = result.deriveFont(20f)
}
result
}
private def styled (emphasis: Set[Font.Emphasis.Value]) = {
var result = AwtFont.PLAIN
private def styled (awtFont: AwtFont, emphasis: Set[Font.Emphasis.Value]) = {
var result = awtFont.deriveFont(AwtFont.PLAIN)
emphasis foreach {
_ match {
case Font.Emphasis.Bold result += AwtFont.BOLD
case Font.Emphasis.Italic result += AwtFont.ITALIC
case Font.Emphasis.Bold result = result.deriveFont(AwtFont.BOLD)
case Font.Emphasis.Italic result = result.deriveFont(AwtFont.ITALIC)
}
}
result
......
/*
SimGraf - A Simple Scala Graphics Library
Copyright 2016-2018 Hans-Hermann Bode
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 de.h2b.scala.lib.simgraf
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import de.h2b.scala.lib.simgraf.Color._
import de.h2b.scala.lib.simgraf.driver.AwtScreenDriver
@RunWith(classOf[JUnitRunner])
class AwtScreenDriverTest extends FunSuite {
private val awt = Screen.awt(Pixel(500, 500), 250, 250, "Test screen")
test("Font metrics") {
val f0 = awt.activeFont
println(f0.dimension('X'))
println(f0.dimension("abc"))
var p = Pixel(-250, 200)
for (s Font.Size.values) {
awt.drawText(p, "abc")
p -= Pixel(0, 30)
awt.activeFont = f0.derived(Font(s))
println(s + ": X→" + awt.activeFont.dimension('X'),
"abc→" + awt.activeFont.dimension("abc"))
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment