Commit 011d91b8 authored by h2b's avatar h2b

Font metrics separated.

parent c07d60cd
......@@ -29,22 +29,8 @@ package de.h2b.scala.lib.simgraf
* @note The attributes `size` and `emphasis` are implementation-dependent and
* may have no effect at all.
*/
abstract case class Font (size: Font.Size.Value = Font.Size.Normal,
emphasis: Set[Font.Emphasis.Value] = Set.empty) {
//TODO: → FontOps?
/**
* @return the (width, height) of this character in pixels
*/
def dimension (c: Char): (Int, Int)
/**
* @return the (width, height) of this string in pixels
*/
def dimension (s: String): (Int, Int)
}
case class Font (size: Font.Size.Value = Font.Size.Normal,
emphasis: Set[Font.Emphasis.Value] = Set.empty)
object Font {
......@@ -59,3 +45,23 @@ object Font {
}
}
trait FontMetrics { font: Font
/**
* @return the (width, height) of this character in pixels
*/
def dimension (c: Char): (Int, Int)
/**
* @return the (width, height) of this string in pixels
*/
def dimension (s: String): (Int, Int)
/**
* @return a new font metrics derived from this metrics using the specified
* font
*/
def derived (newFont: Font): FontMetrics
}
......@@ -60,8 +60,8 @@ abstract class World (val p1: Point, val p2: Point) {
screen.activeColor = col
}
def activeFont: Font = screen.activeFont
def activeFont_= (font: Font): Unit = {
def activeFont: Font with FontMetrics = screen.activeFont
def activeFont_= (font: Font with FontMetrics): Unit = {
screen.activeFont = font
}
......
......@@ -25,7 +25,7 @@ import java.io.{ File, IOException }
import scala.collection.JavaConverters.mapAsJavaMapConverter
import de.h2b.scala.lib.simgraf.{ Color, Font, Pixel }
import de.h2b.scala.lib.simgraf.{ Color, Font, FontMetrics, Pixel }
import de.h2b.scala.lib.util.{ Level, Logger }
import de.h2b.scala.lib.util.config.BundleConfig
import javax.imageio.ImageIO
......@@ -67,37 +67,47 @@ trait AwtScreenDriver extends ScreenDriver {
}
private def newFont (size: Font.Size.Value,
emphasis: Set[Font.Emphasis.Value]): Font = {
emphasis: Set[Font.Emphasis.Value]): Font with FontMetrics = {
val awtFont: AwtFont = graphics.getFont
var style = AwtFont.PLAIN
emphasis foreach {
_ match {
case Font.Emphasis.Bold style += AwtFont.BOLD
case Font.Emphasis.Italic style += AwtFont.ITALIC
}
}
var derived = awtFont.deriveFont(style)
size match {
//TODO: factors to be determined
case Font.Size.Tiny derived = derived.deriveFont(0.5f)
case Font.Size.Small derived = derived.deriveFont(0.8f)
case Font.Size.Normal ()
case Font.Size.Large derived = derived.deriveFont(1.2f)
case Font.Size.Huge derived = derived.deriveFont(2.0f)
}
graphics.setFont(derived)
val f = new Font(size, emphasis) {
graphics.setFont(sized(awtFont.deriveFont(styled(emphasis)), size))
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
}
f
}
private var _activeFont: Font = newFont(Font.Size.Normal, Set.empty)
def activeFont: Font = _activeFont
def activeFont_= (font: Font): Unit = {
private def sized (awtFont: AwtFont, size: Font.Size.Value) = {
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.Normal ()
case Font.Size.Large result = result.deriveFont(1.2f)
case Font.Size.Huge result = result.deriveFont(2.0f)
}
result
}
private def styled (emphasis: Set[Font.Emphasis.Value]) = {
var result = AwtFont.PLAIN
emphasis foreach {
_ match {
case Font.Emphasis.Bold result += AwtFont.BOLD
case Font.Emphasis.Italic result += AwtFont.ITALIC
}
}
result
}
private var _activeFont: Font with FontMetrics =
newFont(Font.Size.Normal, Set.empty)
def activeFont: Font with FontMetrics = _activeFont
def activeFont_= (font: Font with FontMetrics): Unit = {
val f = newFont(font.size, font.emphasis)
_activeFont = f
}
......@@ -257,7 +267,6 @@ trait AwtScreenDriver extends ScreenDriver {
}
}
}
object AwtScreenDriver {
......
......@@ -17,7 +17,7 @@
*/
package de.h2b.scala.lib.simgraf.driver
import de.h2b.scala.lib.simgraf.{ Color, Font, Pixel }
import de.h2b.scala.lib.simgraf.{ Color, Font, FontMetrics, Pixel }
/**
* Driver methods for screen graphics.
......@@ -57,7 +57,7 @@ abstract class ScreenDriver protected (val p0: Pixel, val width: Int,
* font to be used for the next operations (except for those that use
* their own font)
**/
var activeFont: Font
var activeFont: Font with FontMetrics
/**
* Sets pixel to color.
......
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