You can now import subtitle fonts :)

This commit is contained in:
Blatzar 2022-04-17 01:03:18 +02:00
parent d2beb5e253
commit f451b71b0b
3 changed files with 190 additions and 135 deletions

View File

@ -37,6 +37,7 @@ import com.lagradost.cloudstream3.utils.UIHelper.hideSystemUI
import com.lagradost.cloudstream3.utils.UIHelper.navigate
import com.lagradost.cloudstream3.utils.UIHelper.popCurrentPage
import kotlinx.android.synthetic.main.subtitle_settings.*
import java.io.File
const val SUBTITLE_KEY = "subtitle_settings"
const val SUBTITLE_AUTO_SELECT_KEY = "subs_auto_select"
@ -48,9 +49,10 @@ data class SaveCaptionStyle(
@JsonProperty("windowColor") var windowColor: Int,
@CaptionStyleCompat.EdgeType
@JsonProperty("edgeType") var edgeType: Int,
@JsonProperty("edgeColor") var edgeColor: Int,
@JsonProperty("edgeColor") var edgeColor: Int,
@FontRes
@JsonProperty("typeface") var typeface: Int?,
@JsonProperty("typefaceFilePath") var typefaceFilePath: String?,
/**in dp**/
@JsonProperty("elevation") var elevation: Int,
/**in sp**/
@ -64,17 +66,26 @@ class SubtitlesFragment : Fragment() {
val applyStyleEvent = Event<SaveCaptionStyle>()
fun Context.fromSaveToStyle(data: SaveCaptionStyle): CaptionStyleCompat {
val typeface = data.typeface
return CaptionStyleCompat(
data.foregroundColor,
data.backgroundColor,
data.windowColor,
data.edgeType,
data.edgeColor,
if (typeface == null) Typeface.SANS_SERIF else ResourcesCompat.getFont(
this,
typeface
)
data.typefaceFilePath?.let {
try {
// RuntimeException: Font asset not found
Typeface.createFromFile(File(it))
} catch (e: Exception) {
null
}
} ?: data.typeface?.let {
ResourcesCompat.getFont(
this,
it
)
}
?: Typeface.SANS_SERIF
)
}
@ -106,11 +117,24 @@ class SubtitlesFragment : Fragment() {
CaptionStyleCompat.EDGE_TYPE_OUTLINE,
getDefColor(1),
null,
null,
DEF_SUBS_ELEVATION,
null,
)
}
private fun Context.getSavedFonts(): List<File> {
val externalFiles = getExternalFilesDir(null) ?: return emptyList()
val fontDir = File(externalFiles.absolutePath + "/Fonts").also {
it.mkdir()
}
return fontDir.list()?.mapNotNull {
if (it.endsWith(".ttf")) {
File(fontDir.absolutePath + "/" + it)
} else null
} ?: listOf()
}
private fun Context.getCurrentStyle(): CaptionStyleCompat {
return fromSaveToStyle(getCurrentSavedStyle())
}
@ -191,6 +215,10 @@ class SubtitlesFragment : Fragment() {
hide = arguments?.getBoolean("hide") ?: true
onColorSelectedEvent += ::onColorSelected
onDialogDismissedEvent += ::onDialogDismissed
subs_import_text?.text = getString(R.string.subs_import_text).format(
context?.getExternalFilesDir(null)?.absolutePath.toString() + "/Fonts"
)
context?.fixPaddingStatusbar(subs_root)
@ -395,22 +423,38 @@ class SubtitlesFragment : Fragment() {
Pair(R.font.comic_sans, "Comic Sans"),
Pair(R.font.poppins_regular, "Poppins"),
)
val savedFontTypes = textView.context.getSavedFonts()
val currentIndex =
savedFontTypes.indexOfFirst { it.absolutePath == state.typefaceFilePath }
.let { index ->
if (index == -1)
fontTypes.indexOfFirst { it.first == state.typeface }
else index + fontTypes.size
}
//showBottomDialog
activity?.showDialog(
fontTypes.map { it.second },
fontTypes.map { it.first }.indexOf(state.typeface),
fontTypes.map { it.second } + savedFontTypes.map { it.name },
currentIndex,
(textView as TextView).text.toString(),
false,
dismissCallback
) { index ->
state.typeface = fontTypes.map { it.first }[index]
if (index < fontTypes.size) {
state.typeface = fontTypes[index].first
state.typefaceFilePath = null
} else {
state.typefaceFilePath = savedFontTypes[index - fontTypes.size].absolutePath
state.typeface = null
}
textView.context.updateState()
}
}
subs_font.setOnLongClickListener { textView ->
state.typeface = null
state.typefaceFilePath = null
textView.context.updateState()
showToast(activity, R.string.subs_default_reset_toast, Toast.LENGTH_SHORT)
return@setOnLongClickListener true

View File

@ -1,185 +1,195 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/subs_root"
android:background="?attr/primaryBlackBackground">
android:id="@+id/subs_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/primaryBlackBackground">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:textStyle="bold"
android:text="@string/subtitles_settings"
android:textSize="20sp"
android:textColor="?attr/textColor"
android:layout_width="match_parent"
android:layout_rowWeight="1"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/subtitles_settings"
android:textColor="?attr/textColor"
android:textSize="20sp"
android:textStyle="bold" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="75sp">
android:layout_width="match_parent"
android:layout_height="75sp">
<ImageView
android:scaleType="centerCrop"
android:src="@drawable/subtitles_preview_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/preview_background_img_des" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/preview_background_img_des"
android:scaleType="centerCrop"
android:src="@drawable/subtitles_preview_background" />
<com.google.android.exoplayer2.ui.SubtitleView
android:id="@+id/subtitle_text"
android:id="@+id/subtitle_text"
android:layout_width="match_parent"
android:layout_gravity="center"
android:foregroundGravity="center"
android:layout_height="match_parent" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:foregroundGravity="center" />
</FrameLayout>
<TextView
android:nextFocusDown="@id/subs_font_size"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_font"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:id="@+id/subs_font"
android:text="@string/subs_font"
style="@style/SettingsItem" />
android:nextFocusRight="@id/cancel_btt"
android:nextFocusDown="@id/subs_font_size"
android:text="@string/subs_font" />
<TextView
android:nextFocusUp="@id/subs_font"
android:nextFocusDown="@id/subs_text_color"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_font_size"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_font_size"
android:text="@string/subs_font_size"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_font"
android:nextFocusDown="@id/subs_text_color"
android:text="@string/subs_font_size" />
<TextView
android:nextFocusUp="@id/subs_font_size"
android:nextFocusDown="@id/subs_outline_color"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_text_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_text_color"
android:text="@string/subs_text_color"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_font_size"
android:nextFocusDown="@id/subs_outline_color"
android:text="@string/subs_text_color" />
<TextView
android:nextFocusUp="@id/subs_text_color"
android:nextFocusDown="@id/subs_background_color"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_outline_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_outline_color"
android:text="@string/subs_outline_color"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_text_color"
android:nextFocusDown="@id/subs_background_color"
android:text="@string/subs_outline_color" />
<TextView
android:nextFocusUp="@id/subs_outline_color"
android:nextFocusDown="@id/subs_window_color"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_background_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_background_color"
android:text="@string/subs_background_color"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_outline_color"
android:nextFocusDown="@id/subs_window_color"
android:text="@string/subs_background_color" />
<TextView
android:nextFocusUp="@id/subs_background_color"
android:nextFocusDown="@id/subs_edge_type"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_window_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_window_color"
android:text="@string/subs_window_color"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_background_color"
android:nextFocusDown="@id/subs_edge_type"
android:text="@string/subs_window_color" />
<TextView
android:nextFocusUp="@id/subs_window_color"
android:nextFocusDown="@id/subs_subtitle_elevation"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_edge_type"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_edge_type"
android:text="@string/subs_edge_type"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_window_color"
android:nextFocusDown="@id/subs_subtitle_elevation"
android:text="@string/subs_edge_type" />
<TextView
android:nextFocusUp="@id/subs_edge_type"
android:nextFocusDown="@id/subs_auto_select_language"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_subtitle_elevation"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_subtitle_elevation"
android:text="@string/subs_subtitle_elevation"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_edge_type"
android:nextFocusDown="@id/subs_auto_select_language"
android:text="@string/subs_subtitle_elevation" />
<TextView
android:nextFocusUp="@id/subs_subtitle_elevation"
android:nextFocusDown="@id/subs_download_languages"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_auto_select_language"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_auto_select_language"
android:text="@string/subs_auto_select_language"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_subtitle_elevation"
android:nextFocusDown="@id/subs_download_languages"
android:text="@string/subs_auto_select_language" />
<TextView
android:nextFocusUp="@id/subs_auto_select_language"
android:nextFocusDown="@id/apply_btt"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_download_languages"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_download_languages"
android:text="@string/subs_download_languages"
style="@style/SettingsItem" />
android:nextFocusUp="@id/subs_auto_select_language"
android:nextFocusDown="@id/apply_btt"
android:text="@string/subs_download_languages" />
<TextView
android:gravity="center"
android:text="@string/subs_hold_to_reset_to_default"
android:textSize="14sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:textColor="?attr/textColor"
android:layout_width="match_parent"
android:layout_rowWeight="1"
android:layout_height="wrap_content" />
android:gravity="center"
android:text="@string/subs_hold_to_reset_to_default"
android:textColor="?attr/textColor"
android:textSize="14sp" />
<TextView
android:id="@+id/subs_import_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:gravity="center"
android:text="@string/subs_import_text"
android:textColor="?attr/textColor"
android:textSize="14sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="bottom"
android:gravity="bottom|end"
android:layout_width="match_parent"
android:layout_height="60dp">
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:gravity="bottom|end"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:nextFocusUp="@id/subs_download_languages"
android:nextFocusRight="@id/cancel_btt"
style="@style/WhiteButton"
android:layout_gravity="center_vertical|end"
android:visibility="visible"
android:text="@string/sort_apply"
android:id="@+id/apply_btt"
android:layout_width="wrap_content">
android:id="@+id/apply_btt"
style="@style/WhiteButton"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|end"
android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_download_languages"
android:text="@string/sort_apply"
android:visibility="visible">
<requestFocus />
</com.google.android.material.button.MaterialButton>
<com.google.android.material.button.MaterialButton
android:nextFocusUp="@id/subs_download_languages"
android:nextFocusLeft="@id/apply_btt"
style="@style/BlackButton"
android:layout_gravity="center_vertical|end"
android:text="@string/sort_cancel"
android:id="@+id/cancel_btt"
android:layout_width="wrap_content" />
android:id="@+id/cancel_btt"
style="@style/BlackButton"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|end"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusUp="@id/subs_download_languages"
android:text="@string/sort_cancel" />
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -175,6 +175,7 @@
<string name="subs_auto_select_language">Auto-Select Language</string>
<string name="subs_download_languages">Download Languages</string>
<string name="subs_hold_to_reset_to_default">Hold to reset to default</string>
<string name="subs_import_text" formatted="true">Import fonts by placing them in %s</string>
<string name="continue_watching">Continue Watching</string>
<string name="action_remove_watching">Remove</string>