author | wenzelm |
Sun, 10 Nov 2024 12:33:20 +0100 | |
changeset 81420 | d25a241502c1 |
parent 81417 | 964b85e04f1f |
child 81776 | c6d8db03dfdc |
permissions | -rw-r--r-- |
81340 | 1 |
/* Title: Pure/GUI/gui.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Precise metric for smooth font rendering, notably for pretty-printing. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
import java.awt.{Font, RenderingHints} |
|
10 |
import java.awt.font.FontRenderContext |
|
11 |
import java.awt.geom.Rectangle2D |
|
12 |
||
13 |
||
14 |
object Font_Metric { |
|
15 |
val default_hints: RenderingHints = |
|
81342 | 16 |
new RenderingHints( |
17 |
java.util.Map.of( |
|
18 |
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON, |
|
19 |
RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON)) |
|
81340 | 20 |
|
21 |
val default_font: Font = new Font("Helvetica", Font.PLAIN, 12) |
|
22 |
val default_context: FontRenderContext = new FontRenderContext(null, true, true) |
|
23 |
val default: Font_Metric = new Font_Metric() |
|
24 |
} |
|
25 |
||
26 |
class Font_Metric( |
|
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 | 29 |
{ |
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 | 38 |
|
39 |
def string_bounds(str: String): Rectangle2D = font.getStringBounds(str, context) |
|
40 |
def string_width(str: String): Double = string_bounds(str).getWidth |
|
41 |
||
81344 | 42 |
protected def sample: String = "mix" |
43 |
val height: Double = string_bounds(sample).getHeight |
|
44 |
val average_width: Double = string_width(sample) / sample.length |
|
45 |
||
81340 | 46 |
val space_width: Double = string_width(Symbol.space) |
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 | 56 |
def average: Double = average_width / unit |
57 |
} |