src/Tools/jEdit/src/syntax_style.scala
author wenzelm
Wed Mar 15 13:35:14 2017 +0100 (2017-03-15)
changeset 65258 a0701669d159
child 65259 41d12227d5dc
permissions -rw-r--r--
clarified modules;
wenzelm@65258
     1
/*  Title:      Tools/jEdit/src/syntax_style.scala
wenzelm@65258
     2
    Author:     Makarius
wenzelm@65258
     3
wenzelm@65258
     4
Support for extended syntax styles: subscript, superscript, bold, user fonts.
wenzelm@65258
     5
*/
wenzelm@65258
     6
wenzelm@65258
     7
package isabelle.jedit
wenzelm@65258
     8
wenzelm@65258
     9
wenzelm@65258
    10
import isabelle._
wenzelm@65258
    11
wenzelm@65258
    12
import java.awt.{Font, Color}
wenzelm@65258
    13
import java.awt.font.TextAttribute
wenzelm@65258
    14
import java.awt.geom.AffineTransform
wenzelm@65258
    15
wenzelm@65258
    16
import org.gjt.sp.util.SyntaxUtilities
wenzelm@65258
    17
import org.gjt.sp.jedit.syntax.{Token => JEditToken, SyntaxStyle}
wenzelm@65258
    18
wenzelm@65258
    19
wenzelm@65258
    20
object Syntax_Style
wenzelm@65258
    21
{
wenzelm@65258
    22
  private val plain_range: Int = JEditToken.ID_COUNT
wenzelm@65258
    23
  private val full_range = 6 * plain_range + 1
wenzelm@65258
    24
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@65258
    25
wenzelm@65258
    26
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@65258
    27
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@65258
    28
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@65258
    29
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
wenzelm@65258
    30
  val hidden: Byte = (6 * plain_range).toByte
wenzelm@65258
    31
wenzelm@65258
    32
  private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
wenzelm@65258
    33
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
wenzelm@65258
    34
wenzelm@65258
    35
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@65258
    36
  {
wenzelm@65258
    37
    font_style(style, font0 =>
wenzelm@65258
    38
      {
wenzelm@65258
    39
        import scala.collection.JavaConversions._
wenzelm@65258
    40
        val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
wenzelm@65258
    41
wenzelm@65258
    42
        def shift(y: Float): Font =
wenzelm@65258
    43
          GUI.transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
wenzelm@65258
    44
wenzelm@65258
    45
        val m0 = GUI.line_metrics(font0)
wenzelm@65258
    46
        val m1 = GUI.line_metrics(font1)
wenzelm@65258
    47
        val a = m1.getAscent - m0.getAscent
wenzelm@65258
    48
        val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
wenzelm@65258
    49
        if (a > 0.0f) shift(a)
wenzelm@65258
    50
        else if (b > 0.0f) shift(- b)
wenzelm@65258
    51
        else font1
wenzelm@65258
    52
      })
wenzelm@65258
    53
  }
wenzelm@65258
    54
wenzelm@65258
    55
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
wenzelm@65258
    56
    font_style(style, font => font.deriveFont(if (font.isBold) Font.PLAIN else Font.BOLD))
wenzelm@65258
    57
wenzelm@65258
    58
  val hidden_color: Color = new Color(255, 255, 255, 0)
wenzelm@65258
    59
wenzelm@65258
    60
  class Extender extends SyntaxUtilities.StyleExtender
wenzelm@65258
    61
  {
wenzelm@65258
    62
    val max_user_fonts = 2
wenzelm@65258
    63
    if (Symbol.font_names.length > max_user_fonts)
wenzelm@65258
    64
      error("Too many user symbol fonts (" + max_user_fonts + " permitted): " +
wenzelm@65258
    65
        Symbol.font_names.mkString(", "))
wenzelm@65258
    66
wenzelm@65258
    67
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@65258
    68
    {
wenzelm@65258
    69
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@65258
    70
      for (i <- 0 until plain_range) {
wenzelm@65258
    71
        val style = styles(i)
wenzelm@65258
    72
        new_styles(i) = style
wenzelm@65258
    73
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@65258
    74
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@65258
    75
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@65258
    76
        for (idx <- 0 until max_user_fonts)
wenzelm@65258
    77
          new_styles(user_font(idx, i.toByte)) = style
wenzelm@65258
    78
        for ((family, idx) <- Symbol.font_index)
wenzelm@65258
    79
          new_styles(user_font(idx, i.toByte)) = font_style(style, GUI.imitate_font(_, family))
wenzelm@65258
    80
      }
wenzelm@65258
    81
      new_styles(hidden) =
wenzelm@65258
    82
        new SyntaxStyle(hidden_color, null,
wenzelm@65258
    83
          { val font = styles(0).getFont
wenzelm@65258
    84
            GUI.transform_font(new Font(font.getFamily, 0, 1),
wenzelm@65258
    85
              AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
wenzelm@65258
    86
      new_styles
wenzelm@65258
    87
    }
wenzelm@65258
    88
  }
wenzelm@65258
    89
wenzelm@65258
    90
  def extended(text: CharSequence): Map[Text.Offset, Byte => Byte] =
wenzelm@65258
    91
  {
wenzelm@65258
    92
    // FIXME Symbol.bsub_decoded etc.
wenzelm@65258
    93
    def control_style(sym: String): Option[Byte => Byte] =
wenzelm@65258
    94
      if (sym == Symbol.sub_decoded) Some(subscript(_))
wenzelm@65258
    95
      else if (sym == Symbol.sup_decoded) Some(superscript(_))
wenzelm@65258
    96
      else if (sym == Symbol.bold_decoded) Some(bold(_))
wenzelm@65258
    97
      else None
wenzelm@65258
    98
wenzelm@65258
    99
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@65258
   100
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@65258
   101
    {
wenzelm@65258
   102
      for (i <- start until stop) result += (i -> style)
wenzelm@65258
   103
    }
wenzelm@65258
   104
    var offset = 0
wenzelm@65258
   105
    var control = ""
wenzelm@65258
   106
    for (sym <- Symbol.iterator(text)) {
wenzelm@65258
   107
      if (control_style(sym).isDefined) control = sym
wenzelm@65258
   108
      else if (control != "") {
wenzelm@65258
   109
        if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
wenzelm@65258
   110
          mark(offset - control.length, offset, _ => hidden)
wenzelm@65258
   111
          mark(offset, offset + sym.length, control_style(control).get)
wenzelm@65258
   112
        }
wenzelm@65258
   113
        control = ""
wenzelm@65258
   114
      }
wenzelm@65258
   115
      Symbol.lookup_font(sym) match {
wenzelm@65258
   116
        case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
wenzelm@65258
   117
        case _ =>
wenzelm@65258
   118
      }
wenzelm@65258
   119
      offset += sym.length
wenzelm@65258
   120
    }
wenzelm@65258
   121
    result
wenzelm@65258
   122
  }
wenzelm@65258
   123
}