diff --git a/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java b/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java index a4bef7d37c..8dab042049 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java @@ -1,5 +1,5 @@ package com.termux.view; - +import java.io.File; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; @@ -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; @@ -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. */ @@ -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(); @@ -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); + } }