use of java.text.AttributedString in project processing by processing.
the class CompositionTextManager method getCommittedText.
public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex) {
int length = endIndex - beginIndex;
String textAreaString = textArea.getText(beginIndex, length);
return new AttributedString(textAreaString).getIterator();
}
use of java.text.AttributedString in project processing by processing.
the class InputMethodSupport method inputMethodTextChanged.
/////////////////////////////////////////////////////////////////////////////
// InputMethodListener
/////////////////////////////////////////////////////////////////////////////
/**
* Handles events from InputMethod.
*
* @param event event from Input Method.
*/
@Override
public void inputMethodTextChanged(InputMethodEvent event) {
if (Base.DEBUG) {
StringBuilder sb = new StringBuilder();
sb.append("#Called inputMethodTextChanged");
sb.append("\t ID: " + event.getID());
sb.append("\t timestamp: " + new java.util.Date(event.getWhen()));
sb.append("\t parmString: " + event.paramString());
Messages.log(sb.toString());
}
// text = composedText + commitedText
AttributedCharacterIterator text = event.getText();
committedCount = event.getCommittedCharacterCount();
// The caret for Input Method. If you type a character by a input method,
// original caret position will be incorrect. JEditTextArea is not
// implemented using AttributedString and TextLayout.
textArea.setCaretVisible(false);
// Korean Input Method
if (text != null && text.getEndIndex() - (text.getBeginIndex() + committedCount) <= 0) {
textArea.setCaretVisible(true);
}
// Japanese Input Method
if (text == null) {
textArea.setCaretVisible(true);
}
if (text != null) {
if (committedCount > 0) {
char[] insertion = new char[committedCount];
char c = text.first();
for (int i = 0; i < committedCount; i++) {
insertion[i] = c;
c = text.next();
}
// Insert this as a compound edit
textArea.setSelectedText(new String(insertion), true);
textArea.getInputHandler().handleInputMethodCommit();
}
CompositionTextPainter compositionPainter = textArea.getPainter().getCompositionTextpainter();
Messages.log("textArea.getCaretPosition() + committed_count: " + (textArea.getCaretPosition() + committedCount));
compositionPainter.setComposedTextLayout(getTextLayout(text, committedCount), textArea.getCaretPosition() + committedCount);
compositionPainter.setCaret(event.getCaret());
} else {
// otherwise hide the input method
CompositionTextPainter compositionPainter = textArea.getPainter().getCompositionTextpainter();
compositionPainter.setComposedTextLayout(null, 0);
compositionPainter.setCaret(null);
}
event.consume();
textArea.repaint();
}
use of java.text.AttributedString in project processing by processing.
the class InputMethodSupport method getTextLayout.
private TextLayout getTextLayout(AttributedCharacterIterator text, int committedCount) {
boolean antialias = Preferences.getBoolean("editor.smooth");
TextAreaPainter painter = textArea.getPainter();
// create attributed string with font info.
if (text.getEndIndex() - (text.getBeginIndex() + committedCount) > 0) {
composedTextString = new AttributedString(text, committedCount, text.getEndIndex(), CUSTOM_IM_ATTRIBUTES);
Font font = painter.getFontMetrics().getFont();
TextAreaDefaults defaults = textArea.getDefaults();
Color bgColor = defaults.lineHighlight ? defaults.lineHighlightColor : defaults.bgcolor;
composedTextString.addAttribute(TextAttribute.FONT, font);
composedTextString.addAttribute(TextAttribute.FOREGROUND, defaults.fgcolor);
composedTextString.addAttribute(TextAttribute.BACKGROUND, bgColor);
} else {
composedTextString = new AttributedString("");
return null;
}
// set hint of antialiasing to render target.
Graphics2D g2d = (Graphics2D) painter.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
FontRenderContext frc = g2d.getFontRenderContext();
if (Base.DEBUG) {
Messages.log("debug: FontRenderContext is Antialiased = " + frc.getAntiAliasingHint());
}
return new TextLayout(composedTextString.getIterator(), frc);
}
use of java.text.AttributedString in project processing by processing.
the class InputMethodSupport method getCommittedText.
@Override
public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex, AttributedCharacterIterator.Attribute[] attributes) {
int length = endIndex - beginIndex;
String textAreaString = textArea.getText(beginIndex, length);
return new AttributedString(textAreaString).getIterator();
}
use of java.text.AttributedString in project hid-serial by rayshobby.
the class StyledString method getLines.
/**
* Get the text layouts for display if the string has changed since last call
* to this method regenerate them.
*
* @param g2d Graphics2D display context
* @return a list of text layouts for rendering
*/
public LinkedList<TextLayoutInfo> getLines(Graphics2D g2d) {
if (font != g2d.getFont()) {
setFont(g2d.getFont());
invalidText = true;
}
if (invalidText) {
styledText = new AttributedString(plainText);
styledText = insertParagraphMarkers(plainText, styledText);
applyAttributes();
invalidText = false;
invalidLayout = true;
}
if (invalidLayout) {
linesInfo.clear();
if (plainText.length() > 0) {
textHeight = 0;
maxLineLength = 0;
maxLineHeight = 0;
nbrLines = 0;
AttributedCharacterIterator paragraph = styledText.getIterator(null, 0, plainText.length());
FontRenderContext frc = g2d.getFontRenderContext();
lineMeasurer = new LineBreakMeasurer(paragraph, frc);
float yposinpara = 0;
int charssofar = 0;
while (lineMeasurer.getPosition() < plainText.length()) {
TextLayout layout = lineMeasurer.nextLayout(wrapWidth);
float advance = layout.getVisibleAdvance();
if (justify) {
if (justify && advance > justifyRatio * wrapWidth) {
//System.out.println(layout.getVisibleAdvance() + " " + breakWidth + " "+ layout.get);
// If advance > breakWidth then we have a line break
float jw = (advance > wrapWidth) ? advance - wrapWidth : wrapWidth;
layout = layout.getJustifiedLayout(jw);
}
}
// Remember the longest and tallest value for a layout so far.
float lh = getHeight(layout);
if (lh > maxLineHeight)
maxLineHeight = lh;
textHeight += lh;
if (advance <= wrapWidth && advance > maxLineLength)
maxLineLength = advance;
// Store layout and line info
linesInfo.add(new TextLayoutInfo(nbrLines, layout, charssofar, layout.getCharacterCount(), yposinpara));
charssofar += layout.getCharacterCount();
yposinpara += lh;
nbrLines++;
}
}
invalidLayout = false;
}
return linesInfo;
}
Aggregations