src/Tools/jEdit/src/token_markup.scala
author wenzelm
Mon Jul 04 22:11:32 2011 +0200 (2011-07-04)
changeset 43661 39fdbd814c7f
parent 43553 df80747342cb
child 43675 8252d51d70e2
permissions -rw-r--r--
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm@43414
     1
/*  Title:      Tools/jEdit/src/token_markup.scala
wenzelm@43414
     2
    Author:     Makarius
wenzelm@43414
     3
wenzelm@43414
     4
Outer syntax token markup.
wenzelm@43414
     5
*/
wenzelm@43414
     6
wenzelm@43414
     7
package isabelle.jedit
wenzelm@43414
     8
wenzelm@43414
     9
wenzelm@43414
    10
import isabelle._
wenzelm@43414
    11
wenzelm@43482
    12
import java.awt.{Font, Color}
wenzelm@43502
    13
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
wenzelm@43491
    14
import java.awt.geom.AffineTransform
wenzelm@43482
    15
wenzelm@43482
    16
import org.gjt.sp.util.SyntaxUtilities
wenzelm@43452
    17
import org.gjt.sp.jedit.Mode
wenzelm@43452
    18
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
wenzelm@43482
    19
  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
wenzelm@43452
    20
wenzelm@43414
    21
import javax.swing.text.Segment
wenzelm@43414
    22
wenzelm@43414
    23
wenzelm@43414
    24
object Token_Markup
wenzelm@43414
    25
{
wenzelm@43502
    26
  /* font operations */
wenzelm@43502
    27
wenzelm@43502
    28
  private def font_metrics(font: Font): LineMetrics =
wenzelm@43502
    29
    font.getLineMetrics("", new FontRenderContext(null, false, false))
wenzelm@43502
    30
wenzelm@43502
    31
  private def imitate_font(family: String, font: Font): Font =
wenzelm@43502
    32
  {
wenzelm@43502
    33
    val font1 = new Font (family, font.getStyle, font.getSize)
wenzelm@43502
    34
    font1.deriveFont(font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
wenzelm@43502
    35
  }
wenzelm@43502
    36
wenzelm@43502
    37
  private def transform_font(font: Font, transform: AffineTransform): Font =
wenzelm@43502
    38
  {
wenzelm@43502
    39
    import scala.collection.JavaConversions._
wenzelm@43502
    40
    font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
wenzelm@43502
    41
  }
wenzelm@43502
    42
wenzelm@43502
    43
wenzelm@43443
    44
  /* extended syntax styles */
wenzelm@43414
    45
wenzelm@43440
    46
  private val plain_range: Int = JEditToken.ID_COUNT
wenzelm@43488
    47
  private val full_range = 6 * plain_range + 1
wenzelm@43414
    48
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@43414
    49
wenzelm@43414
    50
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@43414
    51
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@43460
    52
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@43487
    53
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
wenzelm@43487
    54
  val hidden: Byte = (6 * plain_range).toByte
wenzelm@43487
    55
wenzelm@43487
    56
  private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
wenzelm@43487
    57
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
wenzelm@43414
    58
wenzelm@43482
    59
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@43482
    60
  {
wenzelm@43502
    61
    font_style(style, font0 =>
wenzelm@43502
    62
      {
wenzelm@43502
    63
        import scala.collection.JavaConversions._
wenzelm@43502
    64
        val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
wenzelm@43502
    65
wenzelm@43502
    66
        def shift(y: Float): Font =
wenzelm@43502
    67
          transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
wenzelm@43502
    68
wenzelm@43502
    69
        val m0 = font_metrics(font0)
wenzelm@43502
    70
        val m1 = font_metrics(font1)
wenzelm@43502
    71
        val a = m1.getAscent - m0.getAscent
wenzelm@43502
    72
        val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
wenzelm@43502
    73
        if (a > 0.0f) shift(a)
wenzelm@43502
    74
        else if (b > 0.0f) shift(- b)
wenzelm@43502
    75
        else font1
wenzelm@43502
    76
      })
wenzelm@43482
    77
  }
wenzelm@43482
    78
wenzelm@43482
    79
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
wenzelm@43487
    80
    font_style(style, _.deriveFont(Font.BOLD))
wenzelm@43482
    81
wenzelm@43661
    82
  class Style_Extender extends SyntaxUtilities.StyleExtender
wenzelm@43482
    83
  {
wenzelm@43661
    84
    val symbols = Isabelle_System.symbols
wenzelm@43487
    85
    if (symbols.font_names.length > 2)
wenzelm@43487
    86
      error("Too many user symbol fonts (max 2 permitted): " + symbols.font_names.mkString(", "))
wenzelm@43487
    87
wenzelm@43482
    88
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@43482
    89
    {
wenzelm@43482
    90
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@43482
    91
      for (i <- 0 until plain_range) {
wenzelm@43482
    92
        val style = styles(i)
wenzelm@43482
    93
        new_styles(i) = style
wenzelm@43482
    94
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@43482
    95
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@43482
    96
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@43502
    97
        for ((family, idx) <- symbols.font_index)
wenzelm@43502
    98
          new_styles(user_font(idx, i.toByte)) = font_style(style, imitate_font(family, _))
wenzelm@43482
    99
      }
wenzelm@43502
   100
      new_styles(hidden) =
wenzelm@43502
   101
        new SyntaxStyle(Color.white, null,
wenzelm@43502
   102
          { val font = styles(0).getFont
wenzelm@43502
   103
            transform_font(new Font(font.getFamily, 0, 1),
wenzelm@43502
   104
              AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
wenzelm@43482
   105
      new_styles
wenzelm@43482
   106
    }
wenzelm@43482
   107
  }
wenzelm@43482
   108
wenzelm@43661
   109
  def extended_styles(text: CharSequence): Map[Text.Offset, Byte => Byte] =
wenzelm@43443
   110
  {
wenzelm@43661
   111
    val symbols = Isabelle_System.symbols
wenzelm@43661
   112
wenzelm@43511
   113
    // FIXME symbols.bsub_decoded etc.
wenzelm@43482
   114
    def ctrl_style(sym: String): Option[Byte => Byte] =
wenzelm@43482
   115
      if (symbols.is_subscript_decoded(sym)) Some(subscript(_))
wenzelm@43482
   116
      else if (symbols.is_superscript_decoded(sym)) Some(superscript(_))
wenzelm@43511
   117
      else if (sym == symbols.bold_decoded) Some(bold(_))
wenzelm@43482
   118
      else None
wenzelm@43455
   119
wenzelm@43482
   120
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@43482
   121
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@43482
   122
    {
wenzelm@43482
   123
      for (i <- start until stop) result += (i -> style)
wenzelm@43482
   124
    }
wenzelm@43482
   125
    var offset = 0
wenzelm@43482
   126
    var ctrl = ""
wenzelm@43489
   127
    for (sym <- Symbol.iterator_string(text)) {
wenzelm@43482
   128
      if (ctrl_style(sym).isDefined) ctrl = sym
wenzelm@43482
   129
      else if (ctrl != "") {
wenzelm@43487
   130
        if (symbols.is_controllable(sym) && sym != "\"" && !symbols.fonts.isDefinedAt(sym)) {
wenzelm@43482
   131
          mark(offset - ctrl.length, offset, _ => hidden)
wenzelm@43482
   132
          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
wenzelm@43482
   133
        }
wenzelm@43482
   134
        ctrl = ""
wenzelm@43443
   135
      }
wenzelm@43488
   136
      symbols.lookup_font(sym) match {
wenzelm@43488
   137
        case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
wenzelm@43487
   138
        case _ =>
wenzelm@43487
   139
      }
wenzelm@43482
   140
      offset += sym.length
wenzelm@43443
   141
    }
wenzelm@43482
   142
    result
wenzelm@43443
   143
  }
wenzelm@43443
   144
wenzelm@43414
   145
wenzelm@43414
   146
  /* token marker */
wenzelm@43414
   147
wenzelm@43414
   148
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
wenzelm@43414
   149
wenzelm@43429
   150
  private class Line_Context(val context: Scan.Context)
wenzelm@43429
   151
    extends TokenMarker.LineContext(isabelle_rules, null)
wenzelm@43414
   152
  {
wenzelm@43414
   153
    override def hashCode: Int = context.hashCode
wenzelm@43414
   154
    override def equals(that: Any): Boolean =
wenzelm@43414
   155
      that match {
wenzelm@43414
   156
        case other: Line_Context => context == other.context
wenzelm@43414
   157
        case _ => false
wenzelm@43414
   158
      }
wenzelm@43414
   159
  }
wenzelm@43414
   160
wenzelm@43452
   161
  class Marker extends TokenMarker
wenzelm@43452
   162
  {
wenzelm@43452
   163
    override def markTokens(context: TokenMarker.LineContext,
wenzelm@43452
   164
        handler: TokenHandler, line: Segment): TokenMarker.LineContext =
wenzelm@43452
   165
    {
wenzelm@43553
   166
      val context1 =
wenzelm@43553
   167
        if (Isabelle.session.is_ready) {
wenzelm@43553
   168
          val syntax = Isabelle.session.current_syntax()
wenzelm@43553
   169
    
wenzelm@43553
   170
          val ctxt =
wenzelm@43553
   171
            context match {
wenzelm@43553
   172
              case c: Line_Context => c.context
wenzelm@43553
   173
              case _ => Scan.Finished
wenzelm@43553
   174
            }
wenzelm@43553
   175
          val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
wenzelm@43553
   176
          val context1 = new Line_Context(ctxt1)
wenzelm@43553
   177
    
wenzelm@43661
   178
          val extended = extended_styles(line)
wenzelm@43553
   179
    
wenzelm@43553
   180
          var offset = 0
wenzelm@43553
   181
          for (token <- tokens) {
wenzelm@43553
   182
            val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43553
   183
            val length = token.source.length
wenzelm@43553
   184
            val end_offset = offset + length
wenzelm@43553
   185
            if ((offset until end_offset) exists extended.isDefinedAt) {
wenzelm@43553
   186
              for (i <- offset until end_offset) {
wenzelm@43553
   187
                val style1 =
wenzelm@43553
   188
                  extended.get(i) match {
wenzelm@43553
   189
                    case None => style
wenzelm@43553
   190
                    case Some(ext) => ext(style)
wenzelm@43553
   191
                  }
wenzelm@43553
   192
                handler.handleToken(line, style1, i, 1, context1)
wenzelm@43553
   193
              }
wenzelm@43553
   194
            }
wenzelm@43553
   195
            else handler.handleToken(line, style, offset, length, context1)
wenzelm@43553
   196
            offset += length
wenzelm@43553
   197
          }
wenzelm@43553
   198
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43553
   199
          context1
wenzelm@43452
   200
        }
wenzelm@43553
   201
        else {
wenzelm@43553
   202
          val context1 = new Line_Context(Scan.Finished)
wenzelm@43553
   203
          handler.handleToken(line, JEditToken.NULL, 0, line.count, context1)
wenzelm@43553
   204
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43553
   205
          context1
wenzelm@43414
   206
        }
wenzelm@43452
   207
      val context2 = context1.intern
wenzelm@43452
   208
      handler.setLineContext(context2)
wenzelm@43452
   209
      context2
wenzelm@43452
   210
    }
wenzelm@43452
   211
  }
wenzelm@43452
   212
wenzelm@43416
   213
wenzelm@43452
   214
  /* mode provider */
wenzelm@43452
   215
wenzelm@43452
   216
  class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
wenzelm@43452
   217
  {
wenzelm@43452
   218
    for (mode <- orig_provider.getModes) addMode(mode)
wenzelm@43452
   219
wenzelm@43452
   220
    val isabelle_token_marker = new Token_Markup.Marker
wenzelm@43452
   221
wenzelm@43452
   222
    override def loadMode(mode: Mode, xmh: XModeHandler)
wenzelm@43452
   223
    {
wenzelm@43452
   224
      super.loadMode(mode, xmh)
wenzelm@43452
   225
      if (mode.getName == "isabelle")
wenzelm@43452
   226
        mode.setTokenMarker(isabelle_token_marker)
wenzelm@43414
   227
    }
wenzelm@43452
   228
  }
wenzelm@43414
   229
}
wenzelm@43414
   230