use of com.google.security.zynamics.zylib.gui.CodeDisplay.FormattedCharacterBuffer in project binnavi by google.
the class BaseTypeTableCellRenderer method convertDocumentToFormattedCharacterBuffer.
public static FormattedCharacterBuffer convertDocumentToFormattedCharacterBuffer(final StyledDocument document, Font font, int desiredWidth) {
// The following code calculates the number of rows and the number of columns required for the
// FormattedCharacterBuffer.
int width = desiredWidth, height = 0;
String text = "";
try {
text = document.getText(0, document.getLength());
} catch (BadLocationException e) {
// Cannot happen.
}
String[] chunks = text.split("\n");
height = chunks.length;
for (String line : chunks) {
// The +1 is necessary because we wish to store the newline characters in the
// FormattedCharacterBuffer.
width = Math.max(width, line.length() + 1);
}
// Height & width is calculated, now create the buffer.
FormattedCharacterBuffer result = new FormattedCharacterBuffer(height, width);
int lineindex = 0, columnindex = 0;
for (int index = 0; index < document.getLength(); ++index) {
if (text.charAt(index) != '\n') {
AttributeSet attributes = document.getCharacterElement(index).getAttributes();
Color foreground = document.getForeground(attributes);
Color background = document.getBackground(attributes);
columnindex++;
result.setAt(lineindex, columnindex, text.charAt(index), font, foreground, background);
} else {
columnindex = 0;
lineindex++;
}
}
return result;
}
Aggregations