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

Fix broken subtitles line wrapping #1346

Merged
merged 3 commits into from
Jan 16, 2022
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 @@ -69,7 +69,6 @@
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 @@ -1467,11 +1466,11 @@ private void renderSubtitles(@Nullable final String text) {
.replaceAll("\\\\h", " ");

final SpannableString span = new SpannableString(TextUtilsKt.toHtmlSpanned(htmlText));
span.setSpan(new ForegroundColorSpan(Color.WHITE), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (subtitlesBackgroundEnabled) {
// Disable the text outlining when the background is enabled
binding.subtitlesText.setStrokeWidth(0.0f);
span.setSpan(new PaddedLineBackgroundSpan(ContextCompat.getColor(requireContext(), R.color.black_opaque), 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
55 changes: 0 additions & 55 deletions app/src/main/java/org/jellyfin/androidtv/ui/shared/OutlineSpan.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.jellyfin.androidtv.ui.shared

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import org.jellyfin.androidtv.R

class StrokeTextView : AppCompatTextView {
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved
private var strokeWidth = 0.0f
private var isDrawing: Boolean = false

constructor(
context: Context,
attrs: AttributeSet?,
defStyle: Int
) : super(context, attrs, defStyle) {
obtainStyledAttrs(context, attrs)
}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
obtainStyledAttrs(context, attrs)
}

constructor(context: Context) : super(context) {
obtainStyledAttrs(context, attrs = null)
}

private fun obtainStyledAttrs(context: Context, attrs: AttributeSet?) {
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView)
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved
strokeWidth = styledAttrs.getFloat(
R.styleable.StrokeTextView_stroke_width, 0.0f
)
styledAttrs.recycle()
}

// To prevent infinite call of onDraw because setTextColor calls invalidate()
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved
override fun invalidate() {
if (isDrawing) return
super.invalidate()
}

fun setStrokeWidth(strokeWidth: Float){
this.strokeWidth = strokeWidth
}
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved

override fun onDraw(canvas: Canvas?) {
if (strokeWidth > 0) {
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved
isDrawing = true
val initialColor = textColors

paint.style = Paint.Style.STROKE
paint.strokeWidth = strokeWidth
setTextColor(ContextCompat.getColor(context, R.color.black))
super.onDraw(canvas)

paint.style = Paint.Style.FILL
setTextColor(initialColor)
super.onDraw(canvas)
isDrawing = false
} else {
super.onDraw(canvas)
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/vlc_player_interface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
android:layout_height="match_parent"
android:layout_gravity="center" />

<TextView
<org.jellyfin.androidtv.ui.shared.StrokeTextView
android:id="@+id/subtitles_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -53,6 +53,7 @@
android:layout_marginBottom="48dp"
android:textColor="@color/white"
android:textSize="28sp"
app:stroke_width="5.0"
tools:text="Subtitles" />

</FrameLayout>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
<attr name="qrBackground" format="color" />
<attr name="qrForeground" format="color" />
</declare-styleable>
<declare-styleable name="StrokeTextView">
<attr name="stroke_width" format="float" />
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved
</declare-styleable>
</resources>