Skip to content

Commit

Permalink
Support configurable vertical centering via `LineHeightStyle.Alignmen…
Browse files Browse the repository at this point in the history
…t` (#1569)

Fixes JetBrains/compose-multiplatform#2602
([CMP-2602](https://youtrack.jetbrains.com/issue/CMP-2602))

Desktop | Android
--- | ---
<img width="400"
src="/~https://github.com/user-attachments/assets/5428e2a4-3a11-4755-8bc7-b66177bcc2a8">
| <img width="400"
src="/~https://github.com/user-attachments/assets/67db44bc-a41d-4c23-9245-aab6fee07f3e">

## Testing
Added a sample page into mpp demo
This should be tested by QA

## Release Notes
### Features - Multiple Platforms
- Support configurable vertical text centering via
`LineHeightStyle.Alignment`
  • Loading branch information
MatkovIvan authored Sep 23, 2024
1 parent d96615c commit 5b07907
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 118 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ val MainScreen = Screen.Selection(
BugReproducers,
Screen.Example("Example1") { Example1() },
Screen.Example("ImageViewer") { ImageViewer() },
Screen.Example("TextDirection") { TextDirection() },
Screen.Example("FontFamilies") { FontFamilies() },
Screen.Example("LottieAnimation") { LottieAnimation() },
Screen.Fullscreen("ApplicationLayouts") { ApplicationLayouts(it) },
Screen.Example("GraphicsLayerSettings") { GraphicsLayerSettings() },
Screen.Example("Blending") { Blending() },
Screen.Example("FontRasterization") { FontRasterization() },
Screen.Example("InteropOrder") { InteropOrder() },
AndroidTextFieldSamples,
Screen.Example("Android TextBrushDemo") { TextBrushDemo() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.mpp.demo.components.material3.ModalNavigationDrawerExamp
import androidx.compose.mpp.demo.components.material3.SearchBarExample
import androidx.compose.mpp.demo.components.material3.WindowSizeClassExample
import androidx.compose.mpp.demo.components.popup.Popups
import androidx.compose.mpp.demo.components.text.TextDemos
import androidx.compose.mpp.demo.textfield.TextFields
import androidx.compose.runtime.Composable

Expand All @@ -56,6 +57,7 @@ val Components = Screen.Selection(
"Components",
Popups,
Dialogs,
TextDemos,
TextFields,
LazyLayouts,
MaterialComponents,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 The Android Open Source Project
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package androidx.compose.mpp.demo
package androidx.compose.mpp.demo.components.text

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -29,27 +29,23 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp


@Composable
fun FontFamilies() {
val state = rememberScrollState()
MaterialTheme {
Column(
modifier = Modifier
.fillMaxSize()
.padding(10.dp)
.verticalScroll(state),
verticalArrangement = Arrangement
.spacedBy(10.dp)
) {
for (fontFamily in listOf(
FontFamily.SansSerif,
FontFamily.Serif,
FontFamily.Monospace,
FontFamily.Cursive
)) {
FontFamilyShowcase(fontFamily)
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(10.dp)
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement
.spacedBy(10.dp)
) {
for (fontFamily in listOf(
FontFamily.SansSerif,
FontFamily.Serif,
FontFamily.Monospace,
FontFamily.Cursive
)) {
FontFamilyShowcase(fontFamily)
}
}
}
Expand All @@ -60,15 +56,16 @@ private fun FontFamilyShowcase(fontFamily: FontFamily) {
Text(
text = "$fontFamily"
)
Text(
text = "The quick brown fox jumps over the lazy dog.",
val textStyle = MaterialTheme.typography.h3.copy(
fontFamily = fontFamily,
style = MaterialTheme.typography.h3
)
Text(
TextWithMetrics(
text = "The quick brown fox jumps over the lazy dog.",
style = textStyle
)
TextWithMetrics(
text = "1234567890",
fontFamily = fontFamily,
style = MaterialTheme.typography.h3
style = textStyle
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.mpp.demo.components.text

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.FontHinting
import androidx.compose.ui.text.FontRasterizationSettings
import androidx.compose.ui.text.FontSmoothing
import androidx.compose.ui.text.PlatformParagraphStyle
import androidx.compose.ui.text.PlatformTextStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp

@OptIn(ExperimentalTextApi::class)
@Composable
fun FontRasterization() {
val state = rememberScrollState()
Column(
modifier = Modifier.fillMaxSize().padding(10.dp).verticalScroll(state),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
val hintingOptions = listOf(FontHinting.None, FontHinting.Slight, FontHinting.Normal, FontHinting.Full)
val isAutoHintingForcedOptions = listOf(false, true)
val subpixelOptions = listOf(false, true)
val smoothingOptions = listOf(FontSmoothing.None, FontSmoothing.AntiAlias, FontSmoothing.SubpixelAntiAlias)
val text = "Lorem ipsum"

for (hinting in hintingOptions) {
Row(
horizontalArrangement = Arrangement.spacedBy(10.dp)
) {
for (smoothing in smoothingOptions) {
Column(
modifier = Modifier.weight(1f).border(1.dp, Color.Black).padding(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
for (subpixel in subpixelOptions) {
for (autoHintingForced in isAutoHintingForcedOptions) {
FontRasterizationSample(
text = text,
modifier = Modifier.offset(x = 0.25.dp, y = 0.25.dp),
hinting = hinting,
smoothing = smoothing,
subpixelPositioning = subpixel,
autoHintingForced = autoHintingForced
)
}
}
}
}
}
}
}
}

@OptIn(ExperimentalTextApi::class)
@Composable
private fun FontRasterizationSample(
text: String,
modifier: Modifier,
hinting: FontHinting,
smoothing: FontSmoothing,
subpixelPositioning: Boolean,
autoHintingForced: Boolean
) {
BasicText(
text = text,
modifier = modifier,
style = TextStyle(
platformStyle = PlatformTextStyle(
spanStyle = null,
paragraphStyle = PlatformParagraphStyle(
fontRasterizationSettings = FontRasterizationSettings(
smoothing = smoothing,
hinting = hinting,
subpixelPositioning = subpixelPositioning,
autoHintingForced = autoHintingForced
)
)
)
)
)
}
Loading

0 comments on commit 5b07907

Please sign in to comment.