Commit c07d60cd authored by h2b's avatar h2b

Font handling added.

parent 8e5c4868
/*
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
/**
* @since 1.4.0
* @author h2b
*
* @constructor
*
* @param size
* @param emphasis
*
* @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)
}
object Font {
object Size extends Enumeration {
type Size = Value
val Tiny, Small, Normal, Large, Huge = Value
}
object Emphasis extends Enumeration {
type Emphasis = Value
val Bold, Italic = Value
}
}
......@@ -60,6 +60,11 @@ abstract class World (val p1: Point, val p2: Point) {
screen.activeColor = col
}
def activeFont: Font = screen.activeFont
def activeFont_= (font: Font): Unit = {
screen.activeFont = font
}
/**
* Specifies preferred thickness of curves in pixels. (Meant as a suggestion,
* not as a strict directive to be obeyed.)
......
......@@ -17,14 +17,15 @@
*/
package de.h2b.scala.lib.simgraf.driver
import java.awt.{ Color => AwtColor, Dimension, FileDialog, Frame, Graphics, GraphicsEnvironment, Rectangle, RenderingHints }
import java.awt.{ Color => AwtColor, Dimension, FileDialog, Font AwtFont,
Frame, Graphics, GraphicsEnvironment, Rectangle, RenderingHints }
import java.awt.event.{ ActionEvent, ActionListener, WindowEvent }
import java.awt.image.BufferedImage
import java.io.{ File, IOException }
import scala.collection.JavaConverters.mapAsJavaMapConverter
import de.h2b.scala.lib.simgraf.{ Color, Pixel }
import de.h2b.scala.lib.simgraf.{ Color, Font, Pixel }
import de.h2b.scala.lib.util.{ Level, Logger }
import de.h2b.scala.lib.util.config.BundleConfig
import javax.imageio.ImageIO
......@@ -65,6 +66,42 @@ trait AwtScreenDriver extends ScreenDriver {
graphics.setColor(toAwtColor(_activeColor))
}
private def newFont (size: Font.Size.Value,
emphasis: Set[Font.Emphasis.Value]): Font = {
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) {
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)
}
f
}
private var _activeFont: Font = newFont(Font.Size.Normal, Set.empty)
def activeFont: Font = _activeFont
def activeFont_= (font: Font): Unit = {
val f = newFont(font.size, font.emphasis)
_activeFont = f
}
protected val panel = new JPanel {
log.debug("panel entered")
override protected def paintComponent(g: Graphics): Unit = {
......
......@@ -17,7 +17,7 @@
*/
package de.h2b.scala.lib.simgraf.driver
import de.h2b.scala.lib.simgraf.{ Color, Pixel }
import de.h2b.scala.lib.simgraf.{ Color, Font, Pixel }
/**
* Driver methods for screen graphics.
......@@ -30,8 +30,9 @@ import de.h2b.scala.lib.simgraf.{ Color, Pixel }
* ensure clipping to that area, so that it is safe to use coordinates outside
* of it.
*
* Implementations have to provide the `activeColor` variable and the `draw...`
* and `fill...` methods as well as `getPixel` and `setPixel`.
* Implementations have to provide the `activeColor` and `activeFont`
* variables and the `draw...` and `fill...` methods as well as `getPixel`
* and `setPixel`.
*
* @author h2b
*
......@@ -52,6 +53,12 @@ abstract class ScreenDriver protected (val p0: Pixel, val width: Int,
**/
var activeColor: Color
/**
* font to be used for the next operations (except for those that use
* their own font)
**/
var activeFont: Font
/**
* 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