src/Tools/jEdit/src/font_info.scala
author wenzelm
Thu, 13 Feb 2025 16:19:48 +0100 (2 months ago)
changeset 82156 5d2ed7e56a49
parent 82142 508a673c87ac
child 82403 6e80327eb30a
permissions -rw-r--r--
tuned signature: more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
     1
/*  Title:      Tools/jEdit/src/font_info.scala
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
     3
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
     4
Font information, derived from main jEdit view font.
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
     5
*/
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
     6
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
     8
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
     9
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    10
import isabelle._
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    11
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    12
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    13
import java.awt.Font
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    14
82142
508a673c87ac removed unused imports;
wenzelm
parents: 81389
diff changeset
    15
import org.gjt.sp.jedit.jEdit
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    16
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    17
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    18
object Font_Info {
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    19
  /* size range */
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    20
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    21
  val min_size = 5
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    22
  val max_size = 250
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    23
73343
d0378baf7d06 tuned --- avoid deprecated conversions between certain number type;
wenzelm
parents: 73340
diff changeset
    24
  def restrict_size(size: Float): Float = size max min_size.toFloat min max_size.toFloat
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    25
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    26
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    27
  /* main jEdit font */
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    28
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    29
  def main_family(): String = jEdit.getProperty("view.font")
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    30
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    31
  def main_size(scale: Double = 1.0): Float =
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    32
    restrict_size(jEdit.getIntegerProperty("view.fontsize", 16).toFloat * scale.toFloat)
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    33
81389
5c8c94d5211a clarified signature: more accurate types;
wenzelm
parents: 81383
diff changeset
    34
  def main(scale: Double = 1.0, zoom: Zoom = null): Font_Info =
81383
75a2c6af8a02 clarified signature;
wenzelm
parents: 81381
diff changeset
    35
    Font_Info(main_family(), main_size(if (zoom == null) scale else scale * zoom.scale))
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    36
81389
5c8c94d5211a clarified signature: more accurate types;
wenzelm
parents: 81383
diff changeset
    37
  class Zoom extends GUI.Zoom { tooltip = "Zoom factor for output font size" }
5c8c94d5211a clarified signature: more accurate types;
wenzelm
parents: 81383
diff changeset
    38
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    39
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    40
  /* incremental size change */
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    41
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    42
  object main_change {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    43
    private def change_size(change: Float => Float): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
    44
      GUI_Thread.require {}
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    45
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    46
      val size0 = main_size()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    47
      val size = restrict_size(change(size0)).round
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    48
      if (size != size0) {
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    49
        jEdit.setIntegerProperty("view.fontsize", size)
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    50
        jEdit.propertiesChanged()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    51
        jEdit.saveSettings()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    52
        jEdit.getActiveView().getStatus.setMessageAndClear("Text font size: " + size)
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    53
      }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    54
    }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    55
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
    56
    // owned by GUI thread
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    57
    private var steps = 0
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 75839
diff changeset
    58
    private val delay = Delay.last(PIDE.session.input_delay, gui = true) {
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    59
      change_size { size =>
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    60
        var i = size.round
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    61
        while (steps != 0 && i > 0) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    62
          if (steps > 0) { i += (i / 10) max 1; steps -= 1 }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    63
          else { i -= (i / 10) max 1; steps += 1 }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    64
        }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    65
        steps = 0
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    66
        i.toFloat
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    67
      }
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    68
    }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    69
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    70
    def step(i: Int): Unit = {
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    71
      steps += i
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    72
      delay.invoke()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    73
    }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    74
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    75
    def reset(size: Float): Unit = {
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    76
      delay.revoke()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    77
      steps = 0
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    78
      change_size(_ => size)
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    79
    }
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    80
  }
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    81
}
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    82
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    83
sealed case class Font_Info(family: String, size: Float) {
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    84
  def font: Font = new Font(family, Font.PLAIN, size.round)
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    85
}