src/Pure/GUI/font_metric.scala
author wenzelm
Sun, 10 Nov 2024 12:33:20 +0100
changeset 81420 d25a241502c1
parent 81417 964b85e04f1f
child 81776 c6d8db03dfdc
permissions -rw-r--r--
tuned comments;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
81340
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/GUI/gui.scala
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     3
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     4
Precise metric for smooth font rendering, notably for pretty-printing.
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     5
*/
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     6
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     7
package isabelle
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     8
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
     9
import java.awt.{Font, RenderingHints}
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    10
import java.awt.font.FontRenderContext
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    11
import java.awt.geom.Rectangle2D
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    12
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    13
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    14
object Font_Metric {
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    15
  val default_hints: RenderingHints =
81342
wenzelm
parents: 81340
diff changeset
    16
    new RenderingHints(
wenzelm
parents: 81340
diff changeset
    17
      java.util.Map.of(
wenzelm
parents: 81340
diff changeset
    18
        RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON,
wenzelm
parents: 81340
diff changeset
    19
        RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON))
81340
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    20
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    21
  val default_font: Font = new Font("Helvetica", Font.PLAIN, 12)
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    22
  val default_context: FontRenderContext = new FontRenderContext(null, true, true)
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    23
  val default: Font_Metric = new Font_Metric()
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    24
}
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    25
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    26
class Font_Metric(
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    27
  val font: Font = Font_Metric.default_font,
81417
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    28
  val context: FontRenderContext = Font_Metric.default_context) extends Pretty.Metric
81340
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    29
{
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    30
  override def toString: String = font.toString
81417
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    31
  override def hashCode: Int = font.hashCode
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    32
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    33
  override def equals(that: Any): Boolean =
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    34
    that match {
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    35
      case other: Font_Metric => font == other.font && context == other.context
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    36
      case _ => false
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
    37
    }
81340
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    38
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    39
  def string_bounds(str: String): Rectangle2D = font.getStringBounds(str, context)
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    40
  def string_width(str: String): Double = string_bounds(str).getWidth
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    41
81344
wenzelm
parents: 81342
diff changeset
    42
  protected def sample: String = "mix"
wenzelm
parents: 81342
diff changeset
    43
  val height: Double = string_bounds(sample).getHeight
wenzelm
parents: 81342
diff changeset
    44
  val average_width: Double = string_width(sample) / sample.length
wenzelm
parents: 81342
diff changeset
    45
81340
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    46
  val space_width: Double = string_width(Symbol.space)
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    47
  override def unit: Double = space_width max 1.0
81375
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    48
  override def apply(s: String): Double = {
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    49
    val s1 =
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    50
      if (s.exists(c => Symbol.is_ascii_blank(c) && c != Symbol.space_char)) {
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    51
        s.map(c => if (Symbol.is_ascii_blank(c)) Symbol.space_char else c)
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    52
      }
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    53
      else s
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    54
    string_width(s1) / unit
ae5695161423 more robust and uniform metric, still with special treatment motivated by jEdit (see also 0cdfce0bf956);
wenzelm
parents: 81345
diff changeset
    55
  }
81340
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    56
  def average: Double = average_width / unit
30f7eb65d679 clarified signature;
wenzelm
parents:
diff changeset
    57
}