Update athen introduction to the texteditor authored by Markus Krug's avatar Markus Krug
......@@ -476,4 +476,96 @@ This method, however gets only called, if the widget resizes or if a new input i
}
```
## Displaying the text in different Fonts (that is the type, its size and styling)
We are currently sitting pretty. We are capable of calculating the bounds (x and y offsets alongside the width and height) of every single EditorLine. What is left is to actually display them. After wrapping the lines (as well as several other operations on the widget) we need to actually display the result. This is done in two steps. The first step is to actually *measure* the lines (huh again? i thought all is known!). To be fair, we still need to assign the topYOffset and the attribute whether a line is currently visible, this is done is the method ```measureLines()```
```java
private void measureLines(GC gcOff) {
int yOffset = 0;
// draw the content that is the text and the selection
yOffset = getTopMargin();
int lineOffset = 0;
boolean bottomlineSet = false;
for (EditorLine line : editorLines) {
// reset its styleranges
line.getStyleRanges().clear();
// draw the selection here
if (lineOffset < this.topLineOffset) {
lineOffset++;
line.setVisible(false);
continue;
}
// stop drawing if we are outside bounds
if (yOffset > getBounds().height - bottomMargin) {
line.setVisible(false);
if (!bottomlineSet) {
bottomLineOffset = line.getLineNumber();
bottomlineSet = true;
}
continue;
}
line.setVisible(true);
// update the line height
line.setTopYOffset(yOffset);
yOffset += line.getLineHeight();
// update by line spacing
yOffset += lineSpacing;
lineOffset++;
}
// if bottomline was never updates we set it to max!
if (!bottomlineSet) {
bottomLineOffset = editorLines.size() - 1;
}
}
```
Afterwards, we can finally draw the text. This is done in the method ```drawLines()```:
```java
private void drawLines(GC gcOff) {
Point validSelection = validateSelection();
if (validSelection()) {
setStyleRange(new StyleRange(validSelection.x, validSelection.y - validSelection.x,
Display.getCurrent().getSystemColor(SWT.COLOR_WHITE), colorSelection));
}
for (EditorLine line : editorLines) {
// draw the selection here
if (line.isVisible()) {
// drawLine(gcOff, line);
drawLine(gcOff, line.getTopYOffset(), validateSelection(), line);
}
}
}
```
This just iterates thorugh the lines until one is visible and draws it. Drawing a line can be done rather easy in SWT:
```java
gcOff.drawString(line.getContent(), 0, yOffset, true);
```
Note the last argument, which tells SWT to only draw the text using a transparent background (we want our annotations to be visible and not overwritten by the background of the text all the time - and the background is a box!)
To be continued...
Things that can be made faster:
* Binary search for the wrapping
* Only iterate through the visible lines instead of all lines while drawing
Things that are still missing:
* Allowing the text to be edited
* Respecting left and right margins
\ No newline at end of file