Skip to content

Commit

Permalink
Add subtitles font size and background switch
Browse files Browse the repository at this point in the history
Added five font sizes to choose from (Extra Large, Large, Normal, Small, Extra Small)
Added an option to disable the subtitles black background and text outline
  • Loading branch information
siankatabg committed Dec 22, 2021
1 parent b21a222 commit b0679f2
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ class UserPreferences(context: Context) : SharedPreferenceStore(
* Enable series thumbnails in home screen rows
*/
var seriesThumbnailsEnabled = Preference.boolean("pref_enable_series_thumbnails", true)

/**
* Enable subtitles background
*/
var subtitlesBackgroundEnabled = Preference.boolean("pref_enable_subtitles_background", true)

/**
* Set default subtitles font size
*/
var defaultSubtitlesSize = Preference.enum("pref_subtitles_size", SubtitlesSize.SUBS_SIZE_NORMAL)
}

init {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jellyfin.androidtv.preference.constant

import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.ui.preference.dsl.EnumDisplayOptions

enum class SubtitlesSize {
/**
* Sets the subtitles size to huge.
*/
@EnumDisplayOptions(R.string.subs_size_xlarge)
SUBS_SIZE_XLARGE,

/**
* Sets the subtitles size to big.
*/
@EnumDisplayOptions(R.string.subs_size_large)
SUBS_SIZE_LARGE,

/**
* Sets the subtitles size to normal.
*/
@EnumDisplayOptions(R.string.subs_size_normal)
SUBS_SIZE_NORMAL,

/**
* Sets the subtitles size to small.
*/
@EnumDisplayOptions(R.string.subs_size_small)
SUBS_SIZE_SMALL,

/**
* Sets the subtitles size to xsmall.
*/
@EnumDisplayOptions(R.string.subs_size_xsmall)
SUBS_SIZE_XSMALL
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import org.jellyfin.androidtv.R;
import org.jellyfin.androidtv.TvApp;
import org.jellyfin.androidtv.constant.CustomMessage;
import org.jellyfin.androidtv.preference.UserPreferences;
import org.jellyfin.androidtv.preference.constant.SubtitlesSize;
import org.jellyfin.androidtv.data.model.DataRefreshService;
import org.jellyfin.androidtv.databinding.OverlayTvGuideBinding;
import org.jellyfin.androidtv.databinding.VlcPlayerInterfaceBinding;
Expand All @@ -68,6 +70,7 @@
import org.jellyfin.androidtv.ui.presentation.CardPresenter;
import org.jellyfin.androidtv.ui.presentation.ChannelCardPresenter;
import org.jellyfin.androidtv.ui.presentation.PositionableListRowPresenter;
import org.jellyfin.androidtv.ui.shared.OutlineSpan;
import org.jellyfin.androidtv.ui.shared.PaddedLineBackgroundSpan;
import org.jellyfin.androidtv.util.DeviceUtils;
import org.jellyfin.androidtv.util.ImageUtils;
Expand Down Expand Up @@ -258,6 +261,26 @@ public void onActivityCreated(Bundle savedInstanceState) {
binding.subtitlesText.setShadowLayer(SUBTITLE_PADDING, 0, 0, Color.TRANSPARENT);
binding.subtitlesText.setPadding(SUBTITLE_PADDING, 0, SUBTITLE_PADDING, 0);

// Subtitles font size configuration
SubtitlesSize subtitlesSize = KoinJavaComponent.<UserPreferences>get(UserPreferences.class).get(UserPreferences.Companion.getDefaultSubtitlesSize());
switch (subtitlesSize) {
case SUBS_SIZE_XLARGE:
binding.subtitlesText.setTextSize(36);
break;
case SUBS_SIZE_LARGE:
binding.subtitlesText.setTextSize(32);
break;
case SUBS_SIZE_NORMAL:
binding.subtitlesText.setTextSize(28);
break;
case SUBS_SIZE_SMALL:
binding.subtitlesText.setTextSize(24);
break;
case SUBS_SIZE_XSMALL:
binding.subtitlesText.setTextSize(20);
break;
}

//pre-load animations
fadeOut = AnimationUtils.loadAnimation(requireContext(), R.anim.abc_fade_out);
fadeOut.setAnimationListener(hideAnimationListener);
Expand Down Expand Up @@ -1461,6 +1484,10 @@ private void renderSubtitles(@Nullable final String text) {
clearSubtitles();
return;
}

// Subtitles background switch
boolean subtitlesBackgroundEnabled = KoinJavaComponent.<UserPreferences>get(UserPreferences.class).get(UserPreferences.Companion.getSubtitlesBackgroundEnabled());

requireActivity().runOnUiThread(() -> {
// Encode whitespace as html entities
final String htmlText = text
Expand All @@ -1469,7 +1496,11 @@ private void renderSubtitles(@Nullable final String text) {

final SpannableString span = new SpannableString(TextUtilsKt.toHtmlSpanned(htmlText));
span.setSpan(new ForegroundColorSpan(Color.WHITE), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new PaddedLineBackgroundSpan(ContextCompat.getColor(requireContext(), R.color.black_opaque), SUBTITLE_PADDING), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (subtitlesBackgroundEnabled) {
span.setSpan(new PaddedLineBackgroundSpan(ContextCompat.getColor(requireContext(), R.color.black_opaque), SUBTITLE_PADDING), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
span.setSpan(new OutlineSpan(Color.BLACK, 4f), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

binding.subtitlesText.setText(span);
binding.subtitlesText.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jellyfin.androidtv.preference.UserPreferences
import org.jellyfin.androidtv.preference.constant.AudioBehavior
import org.jellyfin.androidtv.preference.constant.NextUpBehavior
import org.jellyfin.androidtv.preference.constant.PreferredVideoPlayer
import org.jellyfin.androidtv.preference.constant.SubtitlesSize
import org.jellyfin.androidtv.ui.preference.custom.DurationSeekBarPreference
import org.jellyfin.androidtv.ui.preference.dsl.*
import org.jellyfin.androidtv.util.DeviceUtils
Expand Down Expand Up @@ -148,4 +149,15 @@ fun OptionsScreen.playbackCategory(
}
depends { userPreferences[UserPreferences.videoPlayer] == PreferredVideoPlayer.EXTERNAL }
}

checkbox {
setTitle(R.string.pref_subtitles_background_title)
setContent(R.string.pref_subtitles_background_summary)
bind(userPreferences, UserPreferences.subtitlesBackgroundEnabled)
}

enum<SubtitlesSize> {
setTitle(R.string.pref_subtitles_size)
bind(userPreferences, UserPreferences.defaultSubtitlesSize)
}
}
65 changes: 65 additions & 0 deletions app/src/main/java/org/jellyfin/androidtv/ui/shared/OutlineSpan.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.jellyfin.androidtv.ui.shared

import android.graphics.Canvas
import android.graphics.Paint
import android.text.style.ReplacementSpan;
import androidx.annotation.ColorInt
import androidx.annotation.Dimension

/**
* Source: https://github.com/santaevpavel
*
* A class that draws the outlines of a text when given a stroke color and stroke width.
*/

class OutlineSpan(
@ColorInt private val strokeColor: Int,
@Dimension private val strokeWidth: Float
): ReplacementSpan() {

override fun getSize(
paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fontMetrics: Paint.FontMetricsInt?
): Int {
if (fontMetrics != null && paint.fontMetricsInt != null) {
fontMetrics.bottom = paint.fontMetricsInt.bottom
fontMetrics.top = paint.fontMetricsInt.top
fontMetrics.descent = paint.fontMetricsInt.descent
fontMetrics.leading = paint.fontMetricsInt.leading
fontMetrics.ascent = paint.fontMetricsInt.ascent;
}
return paint.measureText(text.toString().substring(start until end)).toInt()
}


override fun draw(
canvas: Canvas,
text: CharSequence,
start: Int,
end: Int,
x: Float,
top: Int,
y: Int,
bottom: Int,
paint: Paint
) {
val originTextColor = paint.color

paint.apply {
color = strokeColor
style = Paint.Style.STROKE
this.strokeWidth = this@OutlineSpan.strokeWidth
}
canvas.drawText(text, start, end, x, y.toFloat(), paint)

paint.apply {
color = originTextColor
style = Paint.Style.FILL
}
canvas.drawText(text, start, end, x, y.toFloat(), paint)
}

}
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,12 @@
<string name="home_section_i">Home section %1$d</string>
<string name="searchable_hint">Search Jellyfin</string>
<string name="searchable_settings_description">Media</string>
<string name="pref_subtitles_background_title">Subtitles background</string>
<string name="pref_subtitles_background_summary">Show a black background behind the subtitles</string>
<string name="subs_size_xlarge">Extra Large</string>
<string name="subs_size_large">Large</string>
<string name="subs_size_normal">Normal</string>
<string name="subs_size_small">Small</string>
<string name="subs_size_xsmall">Extra Small</string>
<string name="pref_subtitles_size">Subtitles font size</string>
</resources>

0 comments on commit b0679f2

Please sign in to comment.