Source code: com/memoire/jedit/JEditSyntaxUtilities.java
1
2 package com.memoire.jedit;
3 import com.memoire.jedit.*;
4
5 /*
6 * JEditSyntaxUtilities.java - Utility functions used by syntax colorizing
7 * Copyright (C) 1999 Slava Pestov
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 */
23
24
25
26 import javax.swing.text.*;
27 import java.awt.*;
28
29 /**
30 * Class with several utility functions used by jEdit's syntax colorizing
31 * subsystem.
32 *
33 * @author Slava Pestov
34 * @version $Id: JEditSyntaxUtilities.java,v 1.3 2001/07/25 17:39:05 desnoix Exp $
35 */
36 public class JEditSyntaxUtilities
37 {
38 /**
39 * Checks if a subregion of a <code>Segment</code> is equal to a
40 * string.
41 * @param ignoreCase True if case should be ignored, false otherwise
42 * @param text The segment
43 * @param offset The offset into the segment
44 * @param match The string to match
45 */
46 public static boolean regionMatches(boolean ignoreCase, Segment text,
47 int offset, String match)
48 {
49 int length = offset + match.length();
50 char[] textArray = text.array;
51 if(length > text.offset + text.count)
52 return false;
53 for(int i = offset, j = 0; i < length; i++, j++)
54 {
55 char c1 = textArray[i];
56 char c2 = match.charAt(j);
57 if(ignoreCase)
58 {
59 c1 = Character.toUpperCase(c1);
60 c2 = Character.toUpperCase(c2);
61 }
62 if(c1 != c2)
63 return false;
64 }
65 return true;
66 }
67
68 /**
69 * Checks if a subregion of a <code>Segment</code> is equal to a
70 * character array.
71 * @param ignoreCase True if case should be ignored, false otherwise
72 * @param text The segment
73 * @param offset The offset into the segment
74 * @param match The character array to match
75 */
76 public static boolean regionMatches(boolean ignoreCase, Segment text,
77 int offset, char[] match)
78 {
79 int length = offset + match.length;
80 char[] textArray = text.array;
81 if(length > text.offset + text.count)
82 return false;
83 for(int i = offset, j = 0; i < length; i++, j++)
84 {
85 char c1 = textArray[i];
86 char c2 = match[j];
87 if(ignoreCase)
88 {
89 c1 = Character.toUpperCase(c1);
90 c2 = Character.toUpperCase(c2);
91 }
92 if(c1 != c2)
93 return false;
94 }
95 return true;
96 }
97
98 /**
99 * Returns the default style table. This can be passed to the
100 * <code>setStyles()</code> method of <code>JEditSyntaxDocument</code>
101 * to use the default syntax styles.
102 */
103 public static JEditSyntaxStyle[] getDefaultJEditSyntaxStyles()
104 {
105 JEditSyntaxStyle[] styles = new JEditSyntaxStyle[JEditToken.ID_COUNT];
106
107 styles[JEditToken.COMMENT1] = new JEditSyntaxStyle(Color.black,true,false);
108 styles[JEditToken.COMMENT2] = new JEditSyntaxStyle(new Color(0x990033),true,false);
109 styles[JEditToken.KEYWORD1] = new JEditSyntaxStyle(Color.black,false,true);
110 styles[JEditToken.KEYWORD2] = new JEditSyntaxStyle(Color.magenta,false,false);
111 styles[JEditToken.KEYWORD3] = new JEditSyntaxStyle(new Color(0x009600),false,false);
112 styles[JEditToken.LITERAL1] = new JEditSyntaxStyle(new Color(0x650099),false,false);
113 styles[JEditToken.LITERAL2] = new JEditSyntaxStyle(new Color(0x650099),false,true);
114 styles[JEditToken.LABEL] = new JEditSyntaxStyle(new Color(0x990033),false,true);
115 styles[JEditToken.OPERATOR] = new JEditSyntaxStyle(Color.black,false,true);
116 styles[JEditToken.INVALID] = new JEditSyntaxStyle(Color.red,false,true);
117
118 return styles;
119 }
120
121 /**
122 * Paints the specified line onto the graphics context. Note that this
123 * method munges the offset and count values of the segment.
124 * @param line The line segment
125 * @param tokens The token list for the line
126 * @param styles The syntax style list
127 * @param expander The tab expander used to determine tab stops. May
128 * be null
129 * @param gfx The graphics context
130 * @param x The x co-ordinate
131 * @param y The y co-ordinate
132 * @return The x co-ordinate, plus the width of the painted string
133 */
134 public static int paintSyntaxLine(Segment line, JEditToken tokens,
135 JEditSyntaxStyle[] styles, TabExpander expander, Graphics gfx,
136 int x, int y)
137 {
138 Font defaultFont = gfx.getFont();
139 Color defaultColor = gfx.getColor();
140
141 int offset = 0;
142 for(;;)
143 {
144 byte id = tokens.id;
145 if(id == JEditToken.END)
146 break;
147
148 int length = tokens.length;
149 if(id == JEditToken.NULL)
150 {
151 if(!defaultColor.equals(gfx.getColor()))
152 gfx.setColor(defaultColor);
153 if(!defaultFont.equals(gfx.getFont()))
154 gfx.setFont(defaultFont);
155 }
156 else
157 styles[id].setGraphicsFlags(gfx,defaultFont);
158
159 line.count = length;
160 x = Utilities.drawTabbedText(line,x,y,gfx,expander,0);
161 line.offset += length;
162 offset += length;
163
164 tokens = tokens.next;
165 }
166
167 return x;
168 }
169
170 // private members
171 private JEditSyntaxUtilities() {}
172 }
173
174 /*
175 * ChangeLog:
176 * $Log: JEditSyntaxUtilities.java,v $
177 * Revision 1.3 2001/07/25 17:39:05 desnoix
178 * *** empty log message ***
179 *
180 * Revision 1.2 2001/06/21 18:50:04 desnoix
181 * *** empty log message ***
182 *
183 * Revision 1.1 2001/06/13 19:48:18 desnoix
184 * *** empty log message ***
185 *
186 * Revision 1.8 1999/07/08 06:06:04 sp
187 * Bug fixes and miscallaneous updates
188 *
189 * Revision 1.7 1999/07/05 04:38:39 sp
190 * Massive batch of changes... bug fixes, also new text component is in place.
191 * Have fun
192 *
193 * Revision 1.6 1999/06/07 06:36:32 sp
194 * Syntax `styling' (bold/italic tokens) added,
195 * plugin options dialog for plugin option panes
196 *
197 * Revision 1.5 1999/06/05 00:22:58 sp
198 * LGPL'd syntax package
199 *
200 * Revision 1.4 1999/04/19 05:38:20 sp
201 * Syntax API changes
202 *
203 * Revision 1.3 1999/04/02 02:39:46 sp
204 * Updated docs, console fix, getDefaultSyntaxColors() method, hypersearch update
205 *
206 * Revision 1.2 1999/03/27 02:46:17 sp
207 * SyntaxTextArea is now modular
208 *
209 * Revision 1.1 1999/03/13 09:11:46 sp
210 * Syntax code updates, code cleanups
211 *
212 */