001: /*
002: * SyntaxUtilities.java - Utility functions used by syntax colorizing
003: * Copyright (C) 1999 Slava Pestov
004: *
005: * You may use and modify this package for any purpose. Redistribution is
006: * permitted, in both source and binary form, provided that this notice
007: * remains intact in all source distributions of this package.
008: */
009:
010: package org.syntax.jedit;
011:
012: import org.syntax.jedit.tokenmarker.*;
013: import javax.swing.text.*;
014: import java.awt.*;
015:
016: /**
017: * Class with several utility functions used by jEdit's syntax colorizing
018: * subsystem.
019: *
020: * @author Slava Pestov
021: * @version $Id: SyntaxUtilities.java 3074 2004-11-08 04:24:58Z bquig $
022: */
023: public class SyntaxUtilities {
024: /**
025: * Checks if a subregion of a <code>Segment</code> is equal to a
026: * string.
027: * @param ignoreCase True if case should be ignored, false otherwise
028: * @param text The segment
029: * @param offset The offset into the segment
030: * @param match The string to match
031: */
032: public static boolean regionMatches(boolean ignoreCase,
033: Segment text, int offset, String match) {
034: int length = offset + match.length();
035: char[] textArray = text.array;
036: if (length > text.offset + text.count)
037: return false;
038: for (int i = offset, j = 0; i < length; i++, j++) {
039: char c1 = textArray[i];
040: char c2 = match.charAt(j);
041: if (ignoreCase) {
042: c1 = Character.toUpperCase(c1);
043: c2 = Character.toUpperCase(c2);
044: }
045: if (c1 != c2)
046: return false;
047: }
048: return true;
049: }
050:
051: /**
052: * Checks if a subregion of a <code>Segment</code> is equal to a
053: * character array.
054: * @param ignoreCase True if case should be ignored, false otherwise
055: * @param text The segment
056: * @param offset The offset into the segment
057: * @param match The character array to match
058: */
059: public static boolean regionMatches(boolean ignoreCase,
060: Segment text, int offset, char[] match) {
061: int length = offset + match.length;
062: char[] textArray = text.array;
063: if (length > text.offset + text.count)
064: return false;
065: for (int i = offset, j = 0; i < length; i++, j++) {
066: char c1 = textArray[i];
067: char c2 = match[j];
068: if (ignoreCase) {
069: c1 = Character.toUpperCase(c1);
070: c2 = Character.toUpperCase(c2);
071: }
072: if (c1 != c2)
073: return false;
074: }
075: return true;
076: }
077:
078: /**
079: * Returns the default style table. This can be passed to the
080: * <code>setStyles()</code> method of <code>SyntaxDocument</code>
081: * to use the default syntax styles.
082: */
083: public static SyntaxStyle[] getDefaultSyntaxStyles() {
084: SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
085:
086: styles[Token.COMMENT1] = new SyntaxStyle(Color.black, true,
087: false);
088: styles[Token.COMMENT2] = new SyntaxStyle(new Color(0x990033),
089: true, false);
090: styles[Token.KEYWORD1] = new SyntaxStyle(Color.black, false,
091: true);
092: styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta, false,
093: false);
094: styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),
095: false, false);
096: styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),
097: false, false);
098: styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),
099: false, true);
100: styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),
101: false, true);
102: styles[Token.OPERATOR] = new SyntaxStyle(Color.black, false,
103: true);
104: styles[Token.INVALID] = new SyntaxStyle(Color.red, false, true);
105:
106: return styles;
107: }
108:
109: /**
110: * Paints the specified line onto the graphics context. Note that this
111: * method munges the offset and count values of the segment.
112: * @param line The line segment
113: * @param tokens The token list for the line
114: * @param styles The syntax style list
115: * @param expander The tab expander used to determine tab stops. May
116: * be null
117: * @param gfx The graphics context
118: * @param x The x co-ordinate
119: * @param y The y co-ordinate
120: * @return The x co-ordinate, plus the width of the painted string
121: */
122: public static int paintSyntaxLine(Segment line, Token tokens,
123: SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
124: int x, int y) {
125: Font defaultFont = gfx.getFont();
126: Color defaultColor = gfx.getColor();
127:
128: int offset = 0;
129: for (;;) {
130: byte id = tokens.id;
131: if (id == Token.END)
132: break;
133:
134: int length = tokens.length;
135: if (id == Token.NULL) {
136: if (!defaultColor.equals(gfx.getColor()))
137: gfx.setColor(defaultColor);
138: if (!defaultFont.equals(gfx.getFont()))
139: gfx.setFont(defaultFont);
140: } else
141: styles[id].setGraphicsFlags(gfx, defaultFont);
142:
143: line.count = length;
144: x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
145: line.offset += length;
146: offset += length;
147:
148: tokens = tokens.next;
149: }
150:
151: return x;
152: }
153:
154: // private members
155: private SyntaxUtilities() {
156: }
157: }
|