From f8747fec397cc9d55869874995d40d71b381a0c9 Mon Sep 17 00:00:00 2001 From: T8RIN Date: Mon, 6 Jan 2025 16:47:41 +0300 Subject: [PATCH] Rework fonts to be able selecting custom ones --- .../settings/domain/model/DomainFontFamily.kt | 12 ++ .../presentation/model/UiFontFamily.kt | 177 ++++++++++++++---- .../{FontResSelector.kt => FontSelector.kt} | 116 +----------- .../draw/data/AndroidImageDrawApplier.kt | 12 +- .../feature/draw/domain/DrawMode.kt | 3 +- .../components/DrawModeSelector.kt | 9 +- .../components/utils/DrawUtils.kt | 12 +- .../markup_layers/domain/MarkupLayer.kt | 5 +- .../presentation/components/EditLayerSheet.kt | 9 +- .../presentation/components/LayerContent.kt | 6 +- .../presentation/ScanQrCodeContent.kt | 2 +- .../data/AndroidWatermarkApplier.kt | 15 +- .../watermarking/domain/WatermarkParams.kt | 5 +- .../WatermarkParamsSelectionGroup.kt | 9 +- gradle/libs.versions.toml | 2 +- 15 files changed, 218 insertions(+), 176 deletions(-) rename core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/controls/selection/{FontResSelector.kt => FontSelector.kt} (63%) diff --git a/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/domain/model/DomainFontFamily.kt b/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/domain/model/DomainFontFamily.kt index 618117d7d3..704a6412ca 100644 --- a/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/domain/model/DomainFontFamily.kt +++ b/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/domain/model/DomainFontFamily.kt @@ -15,6 +15,8 @@ * along with this program. If not, see . */ +@file:Suppress("SpellCheckingInspection") + package ru.tech.imageresizershrinker.core.settings.domain.model sealed class DomainFontFamily(val ordinal: Int) { @@ -46,6 +48,11 @@ sealed class DomainFontFamily(val ordinal: Int) { data object Unisource : DomainFontFamily(26) data object System : DomainFontFamily(0) + class Custom( + val name: String?, + val filePath: String + ) : DomainFontFamily(-1) + companion object { fun fromOrdinal(int: Int?): DomainFontFamily? = when (int) { 0 -> System @@ -79,3 +86,8 @@ sealed class DomainFontFamily(val ordinal: Int) { } } } + +sealed interface FontType { + data class Resource(val resId: Int) : FontType + data class File(val path: String) : FontType +} \ No newline at end of file diff --git a/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/presentation/model/UiFontFamily.kt b/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/presentation/model/UiFontFamily.kt index 3d583314ad..dfcfcfefb9 100644 --- a/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/presentation/model/UiFontFamily.kt +++ b/core/settings/src/main/java/ru/tech/imageresizershrinker/core/settings/presentation/model/UiFontFamily.kt @@ -22,6 +22,7 @@ package ru.tech.imageresizershrinker.core.settings.presentation.model import android.os.Build import androidx.compose.runtime.saveable.Saver import androidx.compose.ui.text.ExperimentalTextApi +import androidx.compose.ui.text.font.AndroidFont import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle @@ -29,11 +30,13 @@ import androidx.compose.ui.text.font.FontVariation import androidx.compose.ui.text.font.FontWeight import ru.tech.imageresizershrinker.core.resources.R import ru.tech.imageresizershrinker.core.settings.domain.model.DomainFontFamily +import ru.tech.imageresizershrinker.core.settings.domain.model.FontType +import java.io.File sealed class UiFontFamily( val name: String?, private val variable: Boolean, - val fontRes: Int? + val type: FontType? = null ) { val isVariable: Boolean? get() = variable.takeIf { @@ -41,14 +44,47 @@ sealed class UiFontFamily( } val fontFamily: FontFamily - get() = fontRes?.let { - fontFamilyResource(resId = fontRes) + get() = type?.let { + when (it) { + is FontType.File -> fontFamilyFromFile(file = File(it.path)) + is FontType.Resource -> fontFamilyResource(resId = it.resId) + } } ?: FontFamily.Default + override fun equals(other: Any?): Boolean { + if (other !is UiFontFamily) return false + + if (name != other.name) return false + if (isVariable != other.isVariable) return false + if (type != other.type) return false + + return true + } + + constructor( + name: String?, + variable: Boolean, + fontRes: Int + ) : this( + name = name, + variable = variable, + type = FontType.Resource(fontRes) + ) + + constructor( + name: String?, + variable: Boolean, + filePath: String + ) : this( + name = name, + variable = variable, + type = FontType.File(filePath) + ) + operator fun component1() = fontFamily operator fun component2() = name operator fun component3() = isVariable - operator fun component4() = fontRes + operator fun component4() = type data object Montserrat : UiFontFamily( fontRes = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -67,7 +103,6 @@ sealed class UiFontFamily( ) data object System : UiFontFamily( - fontRes = null, name = null, variable = true ) @@ -218,6 +253,15 @@ sealed class UiFontFamily( variable = false ) + class Custom( + name: String?, + val filePath: String + ) : UiFontFamily( + name = name, + variable = false, + filePath = filePath + ) + fun asDomain(): DomainFontFamily { return when (this) { Caveat -> DomainFontFamily.Caveat @@ -247,6 +291,7 @@ sealed class UiFontFamily( LcdMoving -> DomainFontFamily.LcdMoving LcdOctagon -> DomainFontFamily.LcdOctagon Unisource -> DomainFontFamily.Unisource + is Custom -> DomainFontFamily.Custom(name, filePath) } } @@ -292,40 +337,58 @@ sealed class UiFontFamily( } ) } -} -fun DomainFontFamily.toUiFont(): UiFontFamily { - return when (this) { - DomainFontFamily.Caveat -> UiFontFamily.Caveat - DomainFontFamily.Comfortaa -> UiFontFamily.Comfortaa - DomainFontFamily.System -> UiFontFamily.System - DomainFontFamily.Handjet -> UiFontFamily.Handjet - DomainFontFamily.Jura -> UiFontFamily.Jura - DomainFontFamily.Montserrat -> UiFontFamily.Montserrat - DomainFontFamily.Podkova -> UiFontFamily.Podkova - DomainFontFamily.Tektur -> UiFontFamily.Tektur - DomainFontFamily.YsabeauSC -> UiFontFamily.YsabeauSC - DomainFontFamily.DejaVu -> UiFontFamily.DejaVu - DomainFontFamily.BadScript -> UiFontFamily.BadScript - DomainFontFamily.RuslanDisplay -> UiFontFamily.RuslanDisplay - DomainFontFamily.Catterdale -> UiFontFamily.Catterdale - DomainFontFamily.FRM32 -> UiFontFamily.FRM32 - DomainFontFamily.TokeelyBrookings -> UiFontFamily.TokeelyBrookings - DomainFontFamily.Nunito -> UiFontFamily.Nunito - DomainFontFamily.Nothing -> UiFontFamily.Nothing - DomainFontFamily.WOPRTweaked -> UiFontFamily.WOPRTweaked - DomainFontFamily.AlegreyaSans -> UiFontFamily.AlegreyaSans - DomainFontFamily.MinecraftGnu -> UiFontFamily.MinecraftGnu - DomainFontFamily.GraniteFixed -> UiFontFamily.GraniteFixed - DomainFontFamily.NokiaPixel -> UiFontFamily.NokiaPixel - DomainFontFamily.Ztivalia -> UiFontFamily.Ztivalia - DomainFontFamily.Axotrel -> UiFontFamily.Axotrel - DomainFontFamily.LcdMoving -> UiFontFamily.LcdMoving - DomainFontFamily.LcdOctagon -> UiFontFamily.LcdOctagon - DomainFontFamily.Unisource -> UiFontFamily.Unisource + override fun hashCode(): Int { + var result = name?.hashCode() ?: 0 + result = 31 * result + variable.hashCode() + result = 31 * result + (type?.hashCode() ?: 0) + result = 31 * result + (isVariable?.hashCode() ?: 0) + result = 31 * result + fontFamily.hashCode() + return result } } +fun FontType?.toUiFont(): UiFontFamily = when (this) { + is FontType.File -> UiFontFamily.Custom( + name = path.split(".").takeLast(2)[0], + filePath = path + ) + + is FontType.Resource -> UiFontFamily.entries.find { it.type == this } ?: UiFontFamily.System + null -> UiFontFamily.System +} + +fun DomainFontFamily.toUiFont(): UiFontFamily = when (this) { + DomainFontFamily.Caveat -> UiFontFamily.Caveat + DomainFontFamily.Comfortaa -> UiFontFamily.Comfortaa + DomainFontFamily.System -> UiFontFamily.System + DomainFontFamily.Handjet -> UiFontFamily.Handjet + DomainFontFamily.Jura -> UiFontFamily.Jura + DomainFontFamily.Montserrat -> UiFontFamily.Montserrat + DomainFontFamily.Podkova -> UiFontFamily.Podkova + DomainFontFamily.Tektur -> UiFontFamily.Tektur + DomainFontFamily.YsabeauSC -> UiFontFamily.YsabeauSC + DomainFontFamily.DejaVu -> UiFontFamily.DejaVu + DomainFontFamily.BadScript -> UiFontFamily.BadScript + DomainFontFamily.RuslanDisplay -> UiFontFamily.RuslanDisplay + DomainFontFamily.Catterdale -> UiFontFamily.Catterdale + DomainFontFamily.FRM32 -> UiFontFamily.FRM32 + DomainFontFamily.TokeelyBrookings -> UiFontFamily.TokeelyBrookings + DomainFontFamily.Nunito -> UiFontFamily.Nunito + DomainFontFamily.Nothing -> UiFontFamily.Nothing + DomainFontFamily.WOPRTweaked -> UiFontFamily.WOPRTweaked + DomainFontFamily.AlegreyaSans -> UiFontFamily.AlegreyaSans + DomainFontFamily.MinecraftGnu -> UiFontFamily.MinecraftGnu + DomainFontFamily.GraniteFixed -> UiFontFamily.GraniteFixed + DomainFontFamily.NokiaPixel -> UiFontFamily.NokiaPixel + DomainFontFamily.Ztivalia -> UiFontFamily.Ztivalia + DomainFontFamily.Axotrel -> UiFontFamily.Axotrel + DomainFontFamily.LcdMoving -> UiFontFamily.LcdMoving + DomainFontFamily.LcdOctagon -> UiFontFamily.LcdOctagon + DomainFontFamily.Unisource -> UiFontFamily.Unisource + is DomainFontFamily.Custom -> UiFontFamily.Custom(name, filePath) +} + @OptIn(ExperimentalTextApi::class) private fun fontFamilyResource(resId: Int) = FontFamily( Font( @@ -368,4 +431,48 @@ private fun fontFamilyResource(resId: Int) = FontFamily( style = FontStyle.Normal ) ) +) + +@OptIn(ExperimentalTextApi::class) +private fun fontFamilyFromFile(file: File) = FontFamily( + Font( + file = file, + weight = FontWeight.Light, + variationSettings = FontVariation.Settings( + weight = FontWeight.Light, + style = FontStyle.Normal + ) + ), + Font( + file = file, + weight = FontWeight.Normal, + variationSettings = FontVariation.Settings( + weight = FontWeight.Normal, + style = FontStyle.Normal + ) + ), + Font( + file = file, + weight = FontWeight.Medium, + variationSettings = FontVariation.Settings( + weight = FontWeight.Medium, + style = FontStyle.Normal + ) + ) as AndroidFont, + Font( + file = file, + weight = FontWeight.SemiBold, + variationSettings = FontVariation.Settings( + weight = FontWeight.SemiBold, + style = FontStyle.Normal + ) + ), + Font( + file = file, + weight = FontWeight.Bold, + variationSettings = FontVariation.Settings( + weight = FontWeight.Bold, + style = FontStyle.Normal + ) + ) ) \ No newline at end of file diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/controls/selection/FontResSelector.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/controls/selection/FontSelector.kt similarity index 63% rename from core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/controls/selection/FontResSelector.kt rename to core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/controls/selection/FontSelector.kt index deb7f0f451..a7d6f3840f 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/controls/selection/FontResSelector.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/controls/selection/FontSelector.kt @@ -67,119 +67,9 @@ import ru.tech.imageresizershrinker.core.ui.widget.modifier.scaleOnTap import ru.tech.imageresizershrinker.core.ui.widget.text.AutoSizeText import ru.tech.imageresizershrinker.core.ui.widget.text.TitleItem -@Composable -fun FontResSelector( - fontRes: Int, - onValueChange: (UiFontFamily) -> Unit, - modifier: Modifier = Modifier, - title: String = stringResource(R.string.font), - color: Color = MaterialTheme.colorScheme.surface, - shape: Shape = RoundedCornerShape(20.dp) -) { - Column( - modifier = modifier.container( - shape = shape, - color = color - ) - ) { - val fonts = remember { - UiFontFamily.entries - } - var expanded by rememberSaveable { mutableStateOf(false) } - Row(verticalAlignment = Alignment.CenterVertically) { - val scope = rememberCoroutineScope() - val confettiHostState = LocalConfettiHostState.current - val showConfetti: () -> Unit = { - scope.launch { - confettiHostState.showConfetti() - } - } - val rotation by animateFloatAsState(if (expanded) 180f else 0f) - TitleItem( - text = title, - icon = Icons.Outlined.TextFields, - modifier = Modifier.padding(top = 12.dp, start = 12.dp, bottom = 8.dp) - ) - Badge( - content = { - Text(fonts.size.toString()) - }, - containerColor = MaterialTheme.colorScheme.tertiary, - contentColor = MaterialTheme.colorScheme.onTertiary, - modifier = Modifier - .padding(horizontal = 2.dp) - .padding(bottom = 12.dp) - .scaleOnTap { - showConfetti() - } - ) - Spacer(modifier = Modifier.weight(1f)) - EnhancedIconButton( - containerColor = Color.Transparent, - onClick = { expanded = !expanded } - ) { - Icon( - imageVector = Icons.Rounded.KeyboardArrowDown, - contentDescription = "Expand", - modifier = Modifier.rotate(rotation) - ) - } - } - val state = rememberLazyStaggeredGridState() - LazyHorizontalStaggeredGrid( - verticalArrangement = Arrangement.spacedBy( - space = 8.dp, - alignment = Alignment.CenterVertically - ), - state = state, - horizontalItemSpacing = 8.dp, - rows = StaggeredGridCells.Adaptive(30.dp), - modifier = Modifier - .heightIn(max = animateDpAsState(if (expanded) 140.dp else 52.dp).value) - .fadingEdges( - scrollableState = state, - isVertical = false, - spanCount = 3 - ), - contentPadding = PaddingValues(8.dp) - ) { - items(fonts) { - val selected by remember(it, fontRes) { - derivedStateOf { - if (it == UiFontFamily.System) fontRes == 0 - else fontRes == it.fontRes - } - } - MaterialTheme( - typography = Typography(it) - ) { - EnhancedChip( - selected = selected, - onClick = { - onValueChange(it) - }, - selectedColor = MaterialTheme.colorScheme.secondary, - contentPadding = PaddingValues( - horizontal = 12.dp, - vertical = 8.dp - ), - modifier = Modifier.height(36.dp) - ) { - AutoSizeText( - text = it.name - ?: stringResource(id = R.string.system), - maxLines = 1 - ) - } - } - } - } - } -} - @Composable fun FontSelector( - font: UiFontFamily, + value: UiFontFamily, onValueChange: (UiFontFamily) -> Unit, modifier: Modifier = Modifier, title: String = stringResource(R.string.font), @@ -254,9 +144,9 @@ fun FontSelector( contentPadding = PaddingValues(8.dp) ) { items(fonts) { - val selected by remember(it, font) { + val selected by remember(it, value) { derivedStateOf { - it == font + it == value } } MaterialTheme( diff --git a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/data/AndroidImageDrawApplier.kt b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/data/AndroidImageDrawApplier.kt index 9fc1b59de9..3777210345 100644 --- a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/data/AndroidImageDrawApplier.kt +++ b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/data/AndroidImageDrawApplier.kt @@ -22,6 +22,7 @@ import android.graphics.Bitmap import android.graphics.BlurMaskFilter import android.graphics.Canvas import android.graphics.Matrix +import android.graphics.Typeface import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.BlendMode @@ -58,6 +59,7 @@ import ru.tech.imageresizershrinker.core.domain.transformation.Transformation import ru.tech.imageresizershrinker.core.filters.domain.FilterProvider import ru.tech.imageresizershrinker.core.filters.domain.model.Filter import ru.tech.imageresizershrinker.core.filters.domain.model.createFilter +import ru.tech.imageresizershrinker.core.settings.domain.model.FontType import ru.tech.imageresizershrinker.core.ui.shapes.MaterialStarShape import ru.tech.imageresizershrinker.core.ui.utils.helper.toImageModel import ru.tech.imageresizershrinker.feature.draw.data.utils.drawRepeatedBitmapOnPath @@ -273,8 +275,14 @@ internal class AndroidImageDrawApplier @Inject constructor( if (drawMode is DrawMode.Text && !isErasing) { isAntiAlias = true textSize = stroke - if (drawMode.font != 0) { - typeface = ResourcesCompat.getFont(context, drawMode.font) + if (drawMode.font != null) { + typeface = when (drawMode.font) { + is FontType.File -> Typeface.createFromFile(drawMode.font.path) + is FontType.Resource -> ResourcesCompat.getFont( + context, + drawMode.font.resId + ) + } } } } diff --git a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/domain/DrawMode.kt b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/domain/DrawMode.kt index 0a5518bc4b..4127d5c67d 100644 --- a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/domain/DrawMode.kt +++ b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/domain/DrawMode.kt @@ -19,6 +19,7 @@ package ru.tech.imageresizershrinker.feature.draw.domain import ru.tech.imageresizershrinker.core.domain.model.Pt import ru.tech.imageresizershrinker.core.filters.domain.model.Filter +import ru.tech.imageresizershrinker.core.settings.domain.model.FontType sealed class DrawMode(open val ordinal: Int) { data object Neon : DrawMode(2) @@ -41,7 +42,7 @@ sealed class DrawMode(open val ordinal: Int) { data class Text( val text: String = "Text", - val font: Int = 0, + val font: FontType? = null, val isRepeated: Boolean = false, val repeatingInterval: Pt = Pt.Zero ) : DrawMode(6) diff --git a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/DrawModeSelector.kt b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/DrawModeSelector.kt index c76df4552e..53733f4d4f 100644 --- a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/DrawModeSelector.kt +++ b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/DrawModeSelector.kt @@ -74,11 +74,12 @@ import ru.tech.imageresizershrinker.core.resources.icons.Cube import ru.tech.imageresizershrinker.core.resources.icons.Highlighter import ru.tech.imageresizershrinker.core.resources.icons.NeonBrush import ru.tech.imageresizershrinker.core.resources.icons.Pen +import ru.tech.imageresizershrinker.core.settings.presentation.model.toUiFont import ru.tech.imageresizershrinker.core.ui.theme.mixedContainer import ru.tech.imageresizershrinker.core.ui.widget.buttons.SupportingButton import ru.tech.imageresizershrinker.core.ui.widget.buttons.ToggleGroupButton import ru.tech.imageresizershrinker.core.ui.widget.controls.resize_group.components.BlurRadiusSelector -import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.FontResSelector +import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.FontSelector import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.ImageSelector import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedModalBottomSheet @@ -206,12 +207,12 @@ fun DrawModeSelector( } ) Spacer(modifier = Modifier.height(4.dp)) - FontResSelector( - fontRes = (value as? DrawMode.Text)?.font ?: 0, + FontSelector( + value = (value as? DrawMode.Text)?.font.toUiFont(), onValueChange = { onValueChange( (value as? DrawMode.Text)?.copy( - font = it.fontRes ?: 0 + font = it.type ) ?: value ) }, diff --git a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/utils/DrawUtils.kt b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/utils/DrawUtils.kt index 51fe4d5427..ba55222722 100644 --- a/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/utils/DrawUtils.kt +++ b/feature/draw/src/main/java/ru/tech/imageresizershrinker/feature/draw/presentation/components/utils/DrawUtils.kt @@ -23,6 +23,7 @@ import android.graphics.Bitmap import android.graphics.BlurMaskFilter import android.graphics.Canvas import android.graphics.Matrix +import android.graphics.Typeface import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.State @@ -66,6 +67,7 @@ import ru.tech.imageresizershrinker.core.filters.domain.model.Filter import ru.tech.imageresizershrinker.core.filters.presentation.model.UiNativeStackBlurFilter import ru.tech.imageresizershrinker.core.filters.presentation.model.UiPixelationFilter import ru.tech.imageresizershrinker.core.filters.presentation.model.toUiFilter +import ru.tech.imageresizershrinker.core.settings.domain.model.FontType import ru.tech.imageresizershrinker.core.ui.shapes.MaterialStarShape import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.density import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalImageLoader @@ -184,8 +186,14 @@ internal fun rememberPaint( if (drawMode is DrawMode.Text && !isEraserOn) { isAntiAlias = true textSize = strokeWidth.toPx(canvasSize) - if (drawMode.font != 0) { - typeface = ResourcesCompat.getFont(context, drawMode.font) + if (drawMode.font != null) { + typeface = when (drawMode.font) { + is FontType.File -> Typeface.createFromFile(drawMode.font.path) + is FontType.Resource -> ResourcesCompat.getFont( + context, + drawMode.font.resId + ) + } } } } diff --git a/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/domain/MarkupLayer.kt b/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/domain/MarkupLayer.kt index 080d743e63..3759b76920 100644 --- a/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/domain/MarkupLayer.kt +++ b/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/domain/MarkupLayer.kt @@ -19,6 +19,7 @@ package ru.tech.imageresizershrinker.feature.markup_layers.domain import ru.tech.imageresizershrinker.core.domain.model.IntegerSize import ru.tech.imageresizershrinker.core.domain.model.Outline +import ru.tech.imageresizershrinker.core.settings.domain.model.FontType data class MarkupLayer( val type: LayerType, @@ -70,7 +71,7 @@ sealed interface LayerType { data class Text( val color: Int, val size: Float, - val font: Int, + val font: FontType?, val backgroundColor: Int, val text: String, val decorations: List, @@ -86,7 +87,7 @@ sealed interface LayerType { Text( color = -16777216, size = 0.2f, - font = 0, + font = null, backgroundColor = 0, text = "Text", decorations = listOf(), diff --git a/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/EditLayerSheet.kt b/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/EditLayerSheet.kt index 07b54565b1..a21387a4b7 100644 --- a/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/EditLayerSheet.kt +++ b/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/EditLayerSheet.kt @@ -50,12 +50,13 @@ import ru.tech.imageresizershrinker.core.domain.model.Outline import ru.tech.imageresizershrinker.core.domain.utils.ListUtils.toggle import ru.tech.imageresizershrinker.core.resources.R import ru.tech.imageresizershrinker.core.resources.icons.MiniEditLarge +import ru.tech.imageresizershrinker.core.settings.presentation.model.toUiFont import ru.tech.imageresizershrinker.core.ui.theme.inverseByLuma import ru.tech.imageresizershrinker.core.ui.theme.takeColorFromScheme import ru.tech.imageresizershrinker.core.ui.theme.toColor import ru.tech.imageresizershrinker.core.ui.utils.provider.SafeLocalContainerColor import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.ColorRowSelector -import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.FontResSelector +import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.FontSelector import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.ImageSelector import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton @@ -168,13 +169,13 @@ internal fun EditLayerSheet( singleLine = false ) Spacer(Modifier.height(8.dp)) - FontResSelector( - fontRes = type.font, + FontSelector( + value = type.font.toUiFont(), onValueChange = { onUpdateLayer( layer.copy( type.copy( - font = it.fontRes ?: 0 + font = it.type ) ) ) diff --git a/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/LayerContent.kt b/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/LayerContent.kt index 3c8b1eb1c1..e7588ec55f 100644 --- a/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/LayerContent.kt +++ b/feature/markup-layers/src/main/java/ru/tech/imageresizershrinker/feature/markup_layers/presentation/components/LayerContent.kt @@ -38,7 +38,7 @@ import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import coil3.request.ImageRequest -import ru.tech.imageresizershrinker.core.settings.presentation.model.UiFontFamily +import ru.tech.imageresizershrinker.core.settings.presentation.model.toUiFont import ru.tech.imageresizershrinker.core.ui.theme.toColor import ru.tech.imageresizershrinker.core.ui.widget.image.Picture import ru.tech.imageresizershrinker.core.ui.widget.text.OutlineParams @@ -73,9 +73,7 @@ internal fun LayerContent( color = type.color.toColor(), fontSize = (textFullSize * type.size / 5).sp, lineHeight = (textFullSize * type.size / 5).sp, - fontFamily = UiFontFamily.entries.firstOrNull { - (it.fontRes ?: 0) == type.font - }?.fontFamily, + fontFamily = type.font.toUiFont().fontFamily, textDecoration = TextDecoration.combine( type.decorations.mapNotNull { when (it) { diff --git a/feature/scan-qr-code/src/main/java/ru/tech/imageresizershrinker/feature/scan_qr_code/presentation/ScanQrCodeContent.kt b/feature/scan-qr-code/src/main/java/ru/tech/imageresizershrinker/feature/scan_qr_code/presentation/ScanQrCodeContent.kt index 5f5c811303..b3b0f7509a 100644 --- a/feature/scan-qr-code/src/main/java/ru/tech/imageresizershrinker/feature/scan_qr_code/presentation/ScanQrCodeContent.kt +++ b/feature/scan-qr-code/src/main/java/ru/tech/imageresizershrinker/feature/scan_qr_code/presentation/ScanQrCodeContent.kt @@ -323,7 +323,7 @@ fun ScanQrCodeContent( ) BoxAnimatedVisibility(visible = qrDescription.isNotEmpty()) { FontSelector( - font = qrDescriptionFont, + value = qrDescriptionFont, onValueChange = { qrDescriptionFont = it }, color = Color.Unspecified, shape = ContainerShapeDefaults.bottomShape, diff --git a/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/data/AndroidWatermarkApplier.kt b/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/data/AndroidWatermarkApplier.kt index 3bc452a220..c3ace44698 100644 --- a/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/data/AndroidWatermarkApplier.kt +++ b/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/data/AndroidWatermarkApplier.kt @@ -20,7 +20,9 @@ package ru.tech.imageresizershrinker.feature.watermarking.data import android.content.Context import android.graphics.Bitmap import android.graphics.Paint +import android.graphics.Typeface import android.os.Build +import androidx.core.content.res.ResourcesCompat import androidx.exifinterface.media.ExifInterface import com.watermark.androidwm.WatermarkBuilder import com.watermark.androidwm.bean.WatermarkImage @@ -32,6 +34,7 @@ import ru.tech.imageresizershrinker.core.data.image.utils.toPorterDuffMode import ru.tech.imageresizershrinker.core.domain.dispatchers.DispatchersHolder import ru.tech.imageresizershrinker.core.domain.image.ImageGetter import ru.tech.imageresizershrinker.core.domain.model.IntegerSize +import ru.tech.imageresizershrinker.core.settings.domain.model.FontType import ru.tech.imageresizershrinker.feature.watermarking.domain.WatermarkApplier import ru.tech.imageresizershrinker.feature.watermarking.domain.WatermarkParams import ru.tech.imageresizershrinker.feature.watermarking.domain.WatermarkingType @@ -66,7 +69,17 @@ internal class AndroidWatermarkApplier @Inject constructor( ) .setBackgroundColor(type.backgroundColor) .setTextColor(type.color) - .setTextFont(type.font) + .apply { + when (type.font) { + is FontType.File -> Typeface.createFromFile(type.font.path) + is FontType.Resource -> ResourcesCompat.getFont( + context, + type.font.resId + ) + + null -> null + }?.let(::setTextTypeface) + } .setTextStyle( Paint.Style.entries.first { it.ordinal == type.style } ) diff --git a/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/domain/WatermarkParams.kt b/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/domain/WatermarkParams.kt index 98a8c46947..3a3345c750 100644 --- a/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/domain/WatermarkParams.kt +++ b/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/domain/WatermarkParams.kt @@ -18,6 +18,7 @@ package ru.tech.imageresizershrinker.feature.watermarking.domain import ru.tech.imageresizershrinker.core.domain.image.model.BlendingMode +import ru.tech.imageresizershrinker.core.settings.domain.model.FontType data class WatermarkParams( val positionX: Float, @@ -48,7 +49,7 @@ sealed interface WatermarkingType { val color: Int, val style: Int, val size: Float, - val font: Int, + val font: FontType?, val backgroundColor: Int, val text: String, ) : WatermarkingType { @@ -58,7 +59,7 @@ sealed interface WatermarkingType { color = -16777216, style = 0, size = 0.1f, - font = 0, + font = null, backgroundColor = 0, text = "Watermark" ) diff --git a/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/presentation/components/WatermarkParamsSelectionGroup.kt b/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/presentation/components/WatermarkParamsSelectionGroup.kt index 121cf25bc6..6f4b9cc806 100644 --- a/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/presentation/components/WatermarkParamsSelectionGroup.kt +++ b/feature/watermarking/src/main/java/ru/tech/imageresizershrinker/feature/watermarking/presentation/components/WatermarkParamsSelectionGroup.kt @@ -43,11 +43,12 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import com.smarttoolfactory.colordetector.util.ColorUtil.roundToTwoDigits import ru.tech.imageresizershrinker.core.resources.R +import ru.tech.imageresizershrinker.core.settings.presentation.model.toUiFont import ru.tech.imageresizershrinker.core.ui.theme.toColor import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.AlphaSelector import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.BlendingModeSelector import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.ColorRowSelector -import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.FontResSelector +import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.FontSelector import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedSliderItem import ru.tech.imageresizershrinker.core.ui.widget.modifier.container import ru.tech.imageresizershrinker.core.ui.widget.other.ExpandableItem @@ -167,13 +168,13 @@ fun WatermarkParamsSelectionGroup( ?: return@AnimatedVisibility Column { - FontResSelector( - fontRes = type.font, + FontSelector( + value = type.font.toUiFont(), onValueChange = { onValueChange( value.copy( watermarkingType = type.copy( - font = it.fontRes ?: 0 + font = it.type ) ) ) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 896fc054a7..c101195ade 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,7 +8,7 @@ versionCode = "165" jvmTarget = "17" -imageToolboxLibs = "3.2.7" +imageToolboxLibs = "3.2.8" trickle = "1.2.2" avifCoder = "2.0.10"