Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions terminal-view/src/main/java/com/termux/view/TerminalRenderer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.termux.view;

import java.io.File;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
Expand All @@ -21,6 +21,10 @@ public final class TerminalRenderer {
final int mTextSize;
final Typeface mTypeface;
private final Paint mTextPaint = new Paint();
private Typeface regularTypeface;
private Typeface boldTypeface;
private Typeface italicTypeface;
private Typeface boldItalicTypeface;

/** The width of a single mono spaced character obtained by {@link Paint#measureText(String)} on a single 'X'. */
final float mFontWidth;
Expand Down Expand Up @@ -51,6 +55,27 @@ public TerminalRenderer(int textSize, Typeface typeface) {
sb.setCharAt(0, (char) i);
asciiMeasures[i] = mTextPaint.measureText(sb, 0, 1);
}


regularTypeface = typeface;

boldTypeface = loadFontOrFallback(
new File("/data/data/com.termux/files/home/.termux/font-bold.ttf"),
regularTypeface,
Typeface.BOLD
);

italicTypeface = loadFontOrFallback(
new File("/data/data/com.termux/files/home/.termux/font-italic.ttf"),
regularTypeface,
Typeface.ITALIC
);

boldItalicTypeface = loadFontOrFallback(
new File("/data/data/com.termux/files/home/.termux/font-bold-italic.ttf"),
regularTypeface,
Typeface.BOLD_ITALIC
);
}

/** Render the terminal to a canvas with at a specified row scroll, and an optional rectangular selection. */
Expand Down Expand Up @@ -214,26 +239,44 @@ private void drawTextRun(Canvas canvas, char[] text, int[] palette, float y, int
}

if ((effect & TextStyle.CHARACTER_ATTRIBUTE_INVISIBLE) == 0) {

if (dim) {
int red = (0xFF & (foreColor >> 16));
int green = (0xFF & (foreColor >> 8));
int blue = (0xFF & foreColor);
// Dim color handling used by libvte which in turn took it from xterm
// (https://bug735245.bugzilla-attachments.gnome.org/attachment.cgi?id=284267):

red = red * 2 / 3;
green = green * 2 / 3;
blue = blue * 2 / 3;

foreColor = 0xFF000000 + (red << 16) + (green << 8) + blue;
}

mTextPaint.setFakeBoldText(bold);
if (bold && italic) {
mTextPaint.setTypeface(boldItalicTypeface);
} else if (bold) {
mTextPaint.setTypeface(boldTypeface);
} else if (italic) {
mTextPaint.setTypeface(italicTypeface);
} else {
mTextPaint.setTypeface(regularTypeface);
}

mTextPaint.setUnderlineText(underline);
mTextPaint.setTextSkewX(italic ? -0.35f : 0.f);
mTextPaint.setStrikeThruText(strikeThrough);
mTextPaint.setColor(foreColor);

// The text alignment is the default Paint.Align.LEFT.
canvas.drawTextRun(text, startCharIndex, runWidthChars, startCharIndex, runWidthChars, left, y - mFontLineSpacingAndAscent, false, mTextPaint);
canvas.drawTextRun(
text,
startCharIndex,
runWidthChars,
startCharIndex,
runWidthChars,
left,
y - mFontLineSpacingAndAscent,
false,
mTextPaint
);
}

if (savedMatrix) canvas.restore();
Expand All @@ -246,4 +289,11 @@ public float getFontWidth() {
public int getFontLineSpacing() {
return mFontLineSpacing;
}


private Typeface loadFontOrFallback(File file, Typeface base, int style) {
return file.exists()
? Typeface.createFromFile(file)
: Typeface.create(base, style);
}
}