src/Tools/jEdit/src/font_info.scala
author wenzelm
Fri, 01 Apr 2022 17:06:10 +0200
changeset 75393 87ebf5a50283
parent 73343 d0378baf7d06
child 75394 42267c650205
permissions -rw-r--r--
clarified formatting, for the sake of scala3;
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
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    15
import org.gjt.sp.jedit.{jEdit, View}
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
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    34
  def main(scale: Double = 1.0): Font_Info =
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    35
    Font_Info(main_family(), main_size(scale))
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    36
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    37
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    38
  /* incremental size change */
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    39
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    40
  object main_change {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    41
    private def change_size(change: Float => Float): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
    42
      GUI_Thread.require {}
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    43
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    44
      val size0 = main_size()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    45
      val size = restrict_size(change(size0)).round
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    46
      if (size != size0) {
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    47
        jEdit.setIntegerProperty("view.fontsize", size)
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    48
        jEdit.propertiesChanged()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    49
        jEdit.saveSettings()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    50
        jEdit.getActiveView().getStatus.setMessageAndClear("Text font size: " + size)
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    51
      }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    52
    }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    53
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
    54
    // owned by GUI thread
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    55
    private var steps = 0
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    56
    private val delay = Delay.last(PIDE.options.seconds("editor_input_delay"), gui = true) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    57
      change_size(size => {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    58
        var i = size.round
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    59
        while (steps != 0 && i > 0) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    60
          if (steps > 0) { i += (i / 10) max 1; steps -= 1 }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    61
          else { i -= (i / 10) max 1; steps += 1 }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    62
        }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    63
        steps = 0
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    64
        i.toFloat
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    65
      })
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    66
    }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    67
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    68
    def step(i: Int): Unit = {
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    69
      steps += i
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    70
      delay.invoke()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    71
    }
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    72
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    73
    def reset(size: Float): Unit = {
55827
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    74
      delay.revoke()
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    75
      steps = 0
8a881f83e206 clarified module structure;
wenzelm
parents: 55826
diff changeset
    76
      change_size(_ => size)
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    77
    }
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    78
  }
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56662
diff changeset
    79
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56662
diff changeset
    80
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56662
diff changeset
    81
  /* zoom box */
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56662
diff changeset
    82
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56662
diff changeset
    83
  abstract class Zoom_Box extends GUI.Zoom_Box { tooltip = "Zoom factor for output font size" }
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    84
}
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    85
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73343
diff changeset
    86
sealed case class Font_Info(family: String, size: Float) {
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    87
  def font: Font = new Font(family, Font.PLAIN, size.round)
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents:
diff changeset
    88
}