Skip to content

Commit

Permalink
Add subtitles font size and background configurations
Browse files Browse the repository at this point in the history
Added seekbar for choosing subtitles font size (18-38 range, keeping 28 by default)
Added text outlining for better legibility and an option to disable the subtitles black background.
  • Loading branch information
siankatabg committed Dec 24, 2021
1 parent 6951a7f commit 11cd2f1
Show file tree
Hide file tree
Showing 5 changed files with 84 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("subtitles_background_enabled", true)

/**
* Set default subtitles font size
*/
var defaultSubtitlesSize = Preference.int("subtitles_size", 28)
}

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
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.data.model.DataRefreshService;
import org.jellyfin.androidtv.databinding.OverlayTvGuideBinding;
import org.jellyfin.androidtv.databinding.VlcPlayerInterfaceBinding;
Expand All @@ -68,6 +69,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 @@ -148,7 +150,9 @@ public class CustomPlaybackOverlayFragment extends Fragment implements IPlayback
private static final long SUBTITLE_RENDER_INTERVAL_MS = 50;
private SubtitleTrackInfo subtitleTrackInfo;
private int currentSubtitleIndex = 0;
private int subtitlesSize = KoinJavaComponent.<UserPreferences>get(UserPreferences.class).get(UserPreferences.Companion.getDefaultSubtitlesSize());
private long lastSubtitlePositionMs = 0;
private boolean subtitlesBackgroundEnabled = KoinJavaComponent.<UserPreferences>get(UserPreferences.class).get(UserPreferences.Companion.getSubtitlesBackgroundEnabled());

private final Lazy<ApiClient> apiClient = inject(ApiClient.class);
private final Lazy<MediaManager> mediaManager = inject(MediaManager.class);
Expand Down Expand Up @@ -258,6 +262,9 @@ 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
binding.subtitlesText.setTextSize(subtitlesSize);

//pre-load animations
fadeOut = AnimationUtils.loadAnimation(requireContext(), R.anim.abc_fade_out);
fadeOut.setAnimationListener(hideAnimationListener);
Expand Down Expand Up @@ -1469,7 +1476,8 @@ 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);
span.setSpan(new PaddedLineBackgroundSpan(ContextCompat.getColor(requireContext(), subtitlesBackgroundEnabled ? R.color.black_opaque : R.color.transparent), SUBTITLE_PADDING), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new OutlineSpan(), 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 @@ -148,4 +148,20 @@ 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)
}

seekbar {
setTitle(R.string.pref_subtitles_size)
min = 18
max = 38
valueFormatter = object : DurationSeekBarPreference.ValueFormatter() {
override fun display(value: Int) = "${value}"
}
bind(userPreferences, UserPreferences.defaultSubtitlesSize)
}
}
46 changes: 46 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,46 @@
package org.jellyfin.androidtv.ui.shared

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.style.ReplacementSpan

/**
* A class that draws the outlines of a text
*/
class OutlineSpan(
): ReplacementSpan() {

override fun getSize(
paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fontMetrics: Paint.FontMetricsInt?
): Int {
return paint.measureText(text, start, end).toInt()
}

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

paint.color = Color.BLACK
paint.style = Paint.Style.STROKE
paint.strokeWidth = 4f
canvas.drawText(text, start, end, x, y.toFloat(), paint)

paint.color = initialColor
paint.style = Paint.Style.FILL
canvas.drawText(text, start, end, x, y.toFloat(), paint)
}
}
3 changes: 3 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,7 @@
<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="pref_subtitles_size">Subtitles font size</string>
</resources>

0 comments on commit 11cd2f1

Please sign in to comment.