Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subtitles font size and background configurations #1303

Merged
merged 2 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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)
siankatabg marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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);
siankatabg marked this conversation as resolved.
Show resolved Hide resolved
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}"
}
siankatabg marked this conversation as resolved.
Show resolved Hide resolved
bind(userPreferences, UserPreferences.defaultSubtitlesSize)
}
}
54 changes: 54 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,54 @@
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(
siankatabg marked this conversation as resolved.
Show resolved Hide resolved
): ReplacementSpan() {
siankatabg marked this conversation as resolved.
Show resolved Hide resolved

siankatabg marked this conversation as resolved.
Show resolved Hide resolved
override fun getSize(
paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fm: Paint.FontMetricsInt?
): Int {
if (fm != null) {
fm.ascent = paint.fontMetricsInt.ascent
fm.bottom = paint.fontMetricsInt.bottom
fm.descent = paint.fontMetricsInt.descent
fm.leading = paint.fontMetricsInt.leading
fm.top = paint.fontMetricsInt.top
}

return paint.measureText(text, start, end).toInt()
}
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved

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)
siankatabg marked this conversation as resolved.
Show resolved Hide resolved

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>
siankatabg marked this conversation as resolved.
Show resolved Hide resolved
<string name="pref_subtitles_background_summary">Show a black background behind the subtitles</string>
<string name="pref_subtitles_size">Subtitles font size</string>
siankatabg marked this conversation as resolved.
Show resolved Hide resolved
</resources>