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

Support Variable Fonts in Desktop, Native, Web #1623

Merged
merged 36 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9946159
add variable font to mpp demo
adamglin0 Oct 2, 2024
ba6e92f
add variationSettings
adamglin0 Oct 2, 2024
b8af5d1
add variationSettings in PlatformFont
adamglin0 Oct 2, 2024
d53477e
fix a bug
adamglin0 Oct 2, 2024
b960295
implement variable fonts
adamglin0 Oct 2, 2024
32c3789
remove an exception throwing
adamglin0 Oct 2, 2024
e8a61e8
add a few@Composable Font
adamglin0 Oct 2, 2024
bb0033f
add sample
adamglin0 Oct 3, 2024
86b96fc
add RobotFlex font to test
adamglin0 Oct 3, 2024
1fd7932
Add a font that does not support variables.
adamglin0 Oct 3, 2024
2b5529e
Merge remote-tracking branch 'origin/jb-main' into feature/variable-font
adamglin0 Oct 8, 2024
bc8fd85
revert FontFamilies.kt
adamglin0 Oct 8, 2024
a8f31c8
add VariableFonts.kt
adamglin0 Oct 8, 2024
95a7c9b
add js test
adamglin0 Oct 9, 2024
07b641e
add loadVariableFont in native
adamglin0 Oct 9, 2024
a47b25b
add fonts to xcodegen
adamglin0 Oct 9, 2024
93ee8a8
WIP
adamglin0 Oct 9, 2024
043c983
implement loadResource function
adamglin0 Oct 9, 2024
02f4d24
remove unused script
adamglin0 Oct 9, 2024
b3a0a4b
fix lint
adamglin0 Oct 9, 2024
2ddbaa2
update xcodegen
adamglin0 Oct 9, 2024
8b1a47c
remove used import
adamglin0 Oct 11, 2024
fd2858a
Revert the changes to readBytes.
adamglin0 Oct 11, 2024
07b1239
remove a Font function
adamglin0 Oct 11, 2024
efa23b8
rename func and update comments
adamglin0 Oct 11, 2024
d6ddfbc
update comment
adamglin0 Oct 11, 2024
67e71de
fix
adamglin0 Oct 11, 2024
ffcdaf0
fix
adamglin0 Oct 11, 2024
5e96ebd
fix
adamglin0 Oct 15, 2024
22de5c6
fix typo
adamglin0 Oct 15, 2024
23b8745
fix lint
adamglin0 Oct 16, 2024
a7391a9
keep binary compatibility
adamglin0 Oct 17, 2024
7e8485b
fix lint
adamglin0 Oct 17, 2024
f381153
fix binary compatibility
adamglin0 Oct 18, 2024
8d9422f
fix binary compatibility
adamglin0 Oct 18, 2024
c27142b
fix lint
adamglin0 Oct 21, 2024
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
1 change: 0 additions & 1 deletion compose/mpp/demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import androidx.build.JetbrainsAndroidXPlugin
import java.util.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

plugins {
Expand Down
5 changes: 4 additions & 1 deletion compose/mpp/demo/project.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ targets:
UILaunchStoryboardName: "LaunchStoryboard" # needs for proper iOS app size and modern features
CADisableMinimumFrameDurationOnPhone: true
sources:
- path: "src/commonMain/resources/RobotoFlex-VariableFont.ttf"
buildPhase: resources
# we don't have swift sources, but need a real path here (it doesn't matter what folder).
# Xcode copies the files from this path. We need to ensure no file duplicates.
- "src/swift"
- path: "src/swift"

settings:
LIBRARY_SEARCH_PATHS: "$(inherited)"
ENABLE_BITCODE: "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.mpp.demo.Screen
val TextDemos = Screen.Selection(
"Text",
Screen.Example("FontFamilies") { FontFamilies() },
Screen.Example("VariableFonts") { VariableFonts() },
Screen.Example("FontRasterization") { FontRasterization() },
Screen.Example("LineHeightStyle") { LineHeightStyleDemo() },
Screen.Example("TextDirection") { TextDirection() },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* 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.layout.Column
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Slider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontVariation
import androidx.compose.ui.text.font.toFontFamily
import androidx.compose.ui.text.platform.Font
import androidx.compose.ui.unit.sp

@Composable
fun VariableFonts() {
var robotFlexFontByteArray: ByteArray? by remember { mutableStateOf(null) }
LaunchedEffect(Unit) {
robotFlexFontByteArray = loadResource("RobotoFlex-VariableFont.ttf")
}
var opsz by remember { mutableStateOf(14f) }
var slnt by remember { mutableStateOf(0) }
var wdth by remember { mutableStateOf(100f) }
var wght by remember { mutableStateOf(400) }
var GRAD by remember { mutableStateOf(0) }
var XOPQ by remember { mutableStateOf(96) }
var XTRA by remember { mutableStateOf(468) }
var YOPQ by remember { mutableStateOf(79) }
var YTAS by remember { mutableStateOf(750) }
var YTDE by remember { mutableStateOf(-203) }
var YTFI by remember { mutableStateOf(738) }
var YTLC by remember { mutableStateOf(514) }
var YTUC by remember { mutableStateOf(712) }
val variationSettings = FontVariation.Settings(
FontVariation.Setting("opsz", opsz),
FontVariation.Setting("slnt", slnt.toFloat()),
FontVariation.Setting("wdth", wdth),
FontVariation.Setting("wght", wght.toFloat()),
FontVariation.Setting("GRAD", GRAD.toFloat()),
FontVariation.Setting("XOPQ", XOPQ.toFloat()),
FontVariation.Setting("XTRA", XTRA.toFloat()),
FontVariation.Setting("YOPQ", YOPQ.toFloat()),
FontVariation.Setting("YTAS", YTAS.toFloat()),
FontVariation.Setting("YTDE", YTDE.toFloat()),
FontVariation.Setting("YTFI", YTFI.toFloat()),
FontVariation.Setting("YTLC", YTLC.toFloat()),
FontVariation.Setting("YTUC", YTUC.toFloat()),
)
val fontFamily =
robotFlexFontByteArray?.let {
Font(
identity = "RobotFlex ${variationSettings.hashCode()}",
data = it,
variationSettings = variationSettings
).toFontFamily()
} ?: FontFamily.Default
Column {
Text(
"""
116 Part
O, no! it is an ever-fix`ed mark,
That looks on tempests and is never shaken;
""".trimIndent(),
fontFamily = fontFamily,
fontSize = 32.sp
)
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
Text("opsz $opsz")
Slider(
value = opsz.toFloat(),
onValueChange = { opsz = it },
valueRange = 8f..144f,
)
Text("slnt $slnt")
Slider(
value = slnt.toFloat(),
onValueChange = { slnt = it.toInt() },
valueRange = -10f..0f
)
Text("wdth $wdth")
Slider(
value = wdth.toFloat(),
onValueChange = { wdth = it },
valueRange = 25f..151f
)
Text("wght $wght")
Slider(
value = wght.toFloat(),
onValueChange = { wght = it.toInt() },
valueRange = 100f..1000f
)
Text("GRAD $GRAD")
Slider(
value = GRAD.toFloat(),
onValueChange = { GRAD = it.toInt() },
valueRange = -200f..150f
)
Text("XOPQ $XOPQ")
Slider(
value = XOPQ.toFloat(),
onValueChange = { XOPQ = it.toInt() },
valueRange = 27f..175f
)
Text("XTRA $XTRA")
Slider(
value = XTRA.toFloat(),
onValueChange = { XTRA = it.toInt() },
valueRange = 323f..603f
)
Text("YOPQ $YOPQ")
Slider(
value = YOPQ.toFloat(),
onValueChange = { YOPQ = it.toInt() },
valueRange = 25f..135f
)
Text("YTAS $YTAS")
Slider(
value = YTAS.toFloat(),
onValueChange = { YTAS = it.toInt() },
valueRange = 649f..854f
)
Text("YTDE $YTDE")
Slider(
value = YTDE.toFloat(),
onValueChange = { YTDE = it.toInt() },
valueRange = -305f..-98f
)
Text("YTFI $YTFI")
Slider(
value = YTFI.toFloat(),
onValueChange = { YTFI = it.toInt() },
valueRange = 560f..788f
)
Text("YTLC $YTLC")
Slider(
value = YTLC.toFloat(),
onValueChange = { YTLC = it.toInt() },
valueRange = 416f..570f
)
Text("YTUC $YTUC")
Slider(
value = YTUC.toFloat(),
onValueChange = { YTUC = it.toInt() },
valueRange = 528f..760f
)
}
}
}

expect suspend fun loadResource(file: String): ByteArray?
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.mpp.demo.getResourceBytes

actual suspend fun loadResource(file: String): ByteArray? {
return getResourceBytes(file)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 kotlinx.browser.window
import kotlinx.coroutines.await
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Int8Array
import org.w3c.fetch.Response


actual suspend fun loadResource(file: String): ByteArray? {
return fetch(file).arrayBuffer().await().asByteArray()
}

private suspend fun fetch(url: String): Response =
window.fetch(url).await()

@Suppress("CAST_NEVER_SUCCEEDS")
fun ArrayBuffer?.asByteArray(): ByteArray? = this?.run { Int8Array(this) as ByteArray }
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 kotlinx.cinterop.readBytes
import platform.Foundation.NSBundle
import platform.Foundation.NSData
import platform.Foundation.dataWithContentsOfFile

actual suspend fun loadResource(file: String): ByteArray? {
val lastDotIndex = file.lastIndexOf('.')
val name = file.substring(0, lastDotIndex)
val extension = file.substring(lastDotIndex + 1)
val filePath = NSBundle.mainBundle.pathForResource(name, extension)
val fileData = filePath?.let { NSData.dataWithContentsOfFile(it) }
?: throw Error("failed reading $file")
return fileData.bytes!!.readBytes(fileData.length.toInt())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.mpp.demo.loadRes
import androidx.compose.mpp.demo.toByteArray
import kotlin.js.js

actual suspend fun loadResource(file: String): ByteArray? {
return loadRes(file).toByteArray()
}
Loading
Loading