📄 glyphvectortokenfactory.java
字号:
/*
* 11/03/2004
*
* GlyphVectorTokenFactory.java - Token factory for glyph vector tokens.
* Copyright (C) 2004 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.rsyntaxtextarea;
import java.awt.font.FontRenderContext;
import javax.swing.text.Segment;
/**
* Factory for glyph vector tokens.<p>
*
* NOTE: This class, along with GlyphVectorToken, is incorrect. It does NOT
* use the rendering hints specified for the text area; rather, it assumes
* anti-aliasing is on and fractional fontmetrics is off. This needs to be
* corrected.
*
* @author Robert Futrell
* @version 0.01
*/
class GlyphVectorTokenFactory implements TokenFactory {
private int size;
private int increment;
private GlyphVectorToken[] tokenList;
private int currentFreeToken;
private RSyntaxTextArea textArea;
/**
* FIXME: Have me represent the correct rendering hints!
*/
private static final FontRenderContext frc = new FontRenderContext(null, true, true);//false, false);
protected static final int DEFAULT_START_SIZE = 30;
protected static final int DEFAULT_INCREMENT = 10;
/*****************************************************************************/
/**
* Cosnstructor.
*/
public GlyphVectorTokenFactory(RSyntaxTextArea textArea) {
this(DEFAULT_START_SIZE, DEFAULT_INCREMENT, textArea);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param size The initial number of tokens in this factory.
* @param increment How many tokens to increment by when the stack gets
* empty.
*/
public GlyphVectorTokenFactory(int size, int increment,
RSyntaxTextArea textArea) {
this.size = size;
this.increment = increment;
this.currentFreeToken = 0;
setTextArea(textArea);
// Give us some tokens to initially work with.
tokenList = new GlyphVectorToken[size];
for (int i=0; i<size; i++)
tokenList[i] = createInternalUseOnlyToken();
}
/*****************************************************************************/
/**
* Adds tokens to the internal token list. This is called whenever a
* request is made and no more tokens are available.
*/
private final void augmentTokenList() {
GlyphVectorToken[] temp = new GlyphVectorToken[size + increment];
System.arraycopy(tokenList,0, temp,0, size);
size += increment;
tokenList = temp;
temp = null;
for (int i=0; i<increment; i++)
tokenList[size-i-1] = createInternalUseOnlyToken();
System.err.println("... size up to: " + size);
}
/*****************************************************************************/
/**
* Returns whether a given token type cannot have tabs.
*
* @return Whether a token of the given type can have tabs.
*/
private static final boolean cantHaveTabs(int tokenType) {
switch (tokenType) {
case Token.COMMENT:
case Token.COMMENT_EOL:
case Token.COMMENT_MULTILINE:
case Token.COMMENT_DOCUMENTATION:
case Token.LITERAL_STRING_DOUBLE_QUOTE:
case Token.LITERAL_CHAR:
case Token.LITERAL_BACKQUOTE:
case Token.WHITESPACE:
case Token.PREPROCESSOR:
case Token.ERROR_STRING_DOUBLE:
case Token.ERROR_CHAR:
return false;
default:
return true;
}
}
/*****************************************************************************/
/**
* Creates a token for use internally by this token factory. This method
* should NOT be called externally; only by this class and possibly
* subclasses.
*
* @return A token to add to this token factory's internal stack. If a
* subclass wants to produce a stack of a token other than
* {@link GlyphVectorToken}, then this method can be overridden to
* return a new instance of the desired token type.
*/
protected GlyphVectorToken createInternalUseOnlyToken() {
return new GlyphVectorToken();
}
/*****************************************************************************/
/**
* Returns a null token.
*
* @return A null token.
*/
public Token createToken() {
GlyphVectorToken token = tokenList[currentFreeToken];
token.text = null;
token.type = Token.NULL;
token.offset = -1;
token.setNextToken(null);
currentFreeToken++;
if (currentFreeToken==size)
augmentTokenList();
return token;
}
/*****************************************************************************/
/**
* Returns a token.
*
* @param line The segment from which to get the token's text.
* @param beg The starting offset of the token's text in the segment.
* @param end The ending offset of the token's text in the segment.
* @param startOffset The offset in the document of the token.
* @param type The type of token.
* @return The token.
*/
public Token createToken(final Segment line, final int beg,
final int end, final int startOffset, final int type) {
return createToken(line.array, beg,end, startOffset, type);
}
/*****************************************************************************/
/**
* Returns a token.
*
* @param line The segment from which to get the token's text.
* @param beg The starting offset of the token's text in the segment.
* @param end The ending offset of the token's text in the segment.
* @param startOffset The offset in the document of the token.
* @param type The type of token.
* @return The token.
*/
public Token createToken(final char[] line, final int beg,
final int end, final int startOffset, final int type) {
GlyphVectorToken token = tokenList[currentFreeToken];
token.set(line, beg,end, startOffset, type);
if (textArea!=null && cantHaveTabs(type))
token.initGlyphVector(textArea, frc);
currentFreeToken++;
if (currentFreeToken==size)
augmentTokenList();
return token;
}
/*****************************************************************************/
/**
* Resets the state of this token maker. This method should be called
* by the <code>TokenMaker</code> every time a token list is generated for
* a new line so the tokens can be reused.
*/
public void resetAllTokens() {
currentFreeToken = 0;
}
/*****************************************************************************/
/**
* Sets the text area from which token styles are retrieved.
*
* @param textArea The text area.
*/
public void setTextArea(RSyntaxTextArea textArea) {
this.textArea = textArea;
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -