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