src/Tools/jEdit/src/token_markup.scala
author wenzelm
Sun Aug 21 20:04:02 2011 +0200 (2011-08-21)
changeset 44355 9c38bdc6d755
parent 44238 36120feb70ed
child 44356 f6a2e5ce2ce5
permissions -rw-r--r--
default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
     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   class Style_Extender extends SyntaxUtilities.StyleExtender
    83   {
    84     val max_user_fonts = 2
    85     if (Symbol.font_names.length > max_user_fonts)
    86       error("Too many user symbol fonts (max " + max_user_fonts + " permitted): " +
    87         Symbol.font_names.mkString(", "))
    88 
    89     override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
    90     {
    91       val new_styles = new Array[SyntaxStyle](full_range)
    92       for (i <- 0 until plain_range) {
    93         val style = styles(i)
    94         new_styles(i) = style
    95         new_styles(subscript(i.toByte)) = script_style(style, -1)
    96         new_styles(superscript(i.toByte)) = script_style(style, 1)
    97         new_styles(bold(i.toByte)) = bold_style(style)
    98         for (idx <- 0 until max_user_fonts)
    99           new_styles(user_font(idx, i.toByte)) = style
   100         for ((family, idx) <- Symbol.font_index)
   101           new_styles(user_font(idx, i.toByte)) = font_style(style, imitate_font(family, _))
   102       }
   103       new_styles(hidden) =
   104         new SyntaxStyle(Color.white, null,
   105           { val font = styles(0).getFont
   106             transform_font(new Font(font.getFamily, 0, 1),
   107               AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
   108       new_styles
   109     }
   110   }
   111 
   112   def extended_styles(text: CharSequence): Map[Text.Offset, Byte => Byte] =
   113   {
   114     // FIXME Symbol.bsub_decoded etc.
   115     def ctrl_style(sym: String): Option[Byte => Byte] =
   116       if (sym == Symbol.sub_decoded || sym == Symbol.isub_decoded) Some(subscript(_))
   117       else if (sym == Symbol.sup_decoded || sym == Symbol.isup_decoded) Some(superscript(_))
   118       else if (sym == Symbol.bold_decoded) Some(bold(_))
   119       else None
   120 
   121     var result = Map[Text.Offset, Byte => Byte]()
   122     def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
   123     {
   124       for (i <- start until stop) result += (i -> style)
   125     }
   126     var offset = 0
   127     var ctrl = ""
   128     for (sym <- Symbol.iterator(text)) {
   129       if (ctrl_style(sym).isDefined) ctrl = sym
   130       else if (ctrl != "") {
   131         if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
   132           mark(offset - ctrl.length, offset, _ => hidden)
   133           mark(offset, offset + sym.length, ctrl_style(ctrl).get)
   134         }
   135         ctrl = ""
   136       }
   137       Symbol.lookup_font(sym) match {
   138         case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
   139         case _ =>
   140       }
   141       offset += sym.length
   142     }
   143     result
   144   }
   145 
   146 
   147   /* token marker */
   148 
   149   private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
   150 
   151   private class Line_Context(val context: Scan.Context)
   152     extends TokenMarker.LineContext(isabelle_rules, null)
   153   {
   154     override def hashCode: Int = context.hashCode
   155     override def equals(that: Any): Boolean =
   156       that match {
   157         case other: Line_Context => context == other.context
   158         case _ => false
   159       }
   160   }
   161 
   162   class Marker extends TokenMarker
   163   {
   164     override def markTokens(context: TokenMarker.LineContext,
   165         handler: TokenHandler, line: Segment): TokenMarker.LineContext =
   166     {
   167       val context1 =
   168         if (Isabelle.session.is_ready) {
   169           val syntax = Isabelle.session.current_syntax()
   170     
   171           val ctxt =
   172             context match {
   173               case c: Line_Context => c.context
   174               case _ => Scan.Finished
   175             }
   176           val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
   177           val context1 = new Line_Context(ctxt1)
   178     
   179           val extended = extended_styles(line)
   180     
   181           var offset = 0
   182           for (token <- tokens) {
   183             val style = Isabelle_Markup.token_markup(syntax, token)
   184             val length = token.source.length
   185             val end_offset = offset + length
   186             if ((offset until end_offset) exists extended.isDefinedAt) {
   187               for (i <- offset until end_offset) {
   188                 val style1 =
   189                   extended.get(i) match {
   190                     case None => style
   191                     case Some(ext) => ext(style)
   192                   }
   193                 handler.handleToken(line, style1, i, 1, context1)
   194               }
   195             }
   196             else handler.handleToken(line, style, offset, length, context1)
   197             offset += length
   198           }
   199           handler.handleToken(line, JEditToken.END, line.count, 0, context1)
   200           context1
   201         }
   202         else {
   203           val context1 = new Line_Context(Scan.Finished)
   204           handler.handleToken(line, JEditToken.NULL, 0, line.count, context1)
   205           handler.handleToken(line, JEditToken.END, line.count, 0, context1)
   206           context1
   207         }
   208       val context2 = context1.intern
   209       handler.setLineContext(context2)
   210       context2
   211     }
   212   }
   213 
   214 
   215   /* mode provider */
   216 
   217   class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
   218   {
   219     for (mode <- orig_provider.getModes) addMode(mode)
   220 
   221     val isabelle_token_marker = new Token_Markup.Marker
   222 
   223     override def loadMode(mode: Mode, xmh: XModeHandler)
   224     {
   225       super.loadMode(mode, xmh)
   226       if (mode.getName == "isabelle")
   227         mode.setTokenMarker(isabelle_token_marker)
   228     }
   229   }
   230 }
   231