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

iOS default fonts mapping #552

Merged
merged 4 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ val MainScreen = Screen.List(
Screen.Example("ImageViewer") { ImageViewer() },
Screen.Example("RoundedCornerCrashOnJS") { RoundedCornerCrashOnJS() },
Screen.Example("TextDirection") { TextDirection() },
Screen.Example("FontFamilies") { FontFamilies() },
LazyLayouts,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2023 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

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp


@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)
}
}
}
}

@Composable
fun FontFamilyShowcase(fontFamily: FontFamily) {
Column {
Text(
text = "$fontFamily"
)
Text(
text = "The quick brown fox jumps over the lazy dog.",
fontSize = 48.sp,
fontFamily = fontFamily
)
Text(
text = "1234567890",
fontSize = 48.sp,
fontFamily = fontFamily
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,6 @@ actual sealed class PlatformFont : Font {
get() = "${this::class.qualifiedName}|$identity"
}

internal actual val GenericFontFamiliesMapping by lazy {
when (Platform.Current) {
Platform.Windows ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.MacOS ->
mapOf(
FontFamily.SansSerif.name to listOf(
"System Font",
"Helvetica Neue",
"Helvetica"
),
FontFamily.Serif.name to listOf("Times"),
FontFamily.Monospace.name to listOf("Courier"),
FontFamily.Cursive.name to listOf("Apple Chancery")
)
Platform.Linux ->
mapOf(
FontFamily.SansSerif.name to listOf("Noto Sans", "DejaVu Sans"),
FontFamily.Serif.name to listOf("Noto Serif", "DejaVu Serif", "Times New Roman"),
FontFamily.Monospace.name to listOf("Noto Sans Mono", "DejaVu Sans Mono"),
// better alternative?
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.Unknown ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
}
}

/**
* Defines a Font using resource name.
*
Expand Down Expand Up @@ -214,21 +176,12 @@ private fun typefaceResource(resourceName: String): SkTypeface {
return SkTypeface.makeFromData(Data.makeFromBytes(bytes))
}

private enum class Platform {
Linux,
Windows,
MacOS,
Unknown;

companion object {
val Current by lazy {
val name = System.getProperty("os.name")
when {
name.startsWith("Linux") -> Linux
name.startsWith("Win") -> Windows
name == "Mac OS X" -> MacOS
else -> Unknown
}
}
internal actual fun currentPlatform(): Platform {
val name = System.getProperty("os.name")
return when {
name.startsWith("Linux") -> Platform.Linux
name.startsWith("Win") -> Platform.Windows
name == "Mac OS X" -> Platform.MacOS
else -> Platform.Unknown
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Android Open Source Project
* Copyright 2023 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 @@ -21,43 +21,6 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontListFontFamily
import org.jetbrains.skia.Data

internal actual val GenericFontFamiliesMapping by lazy {
when (Platform.Current) {
Platform.Windows ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.MacOS ->
mapOf(
FontFamily.SansSerif.name to listOf(
"Helvetica Neue",
"Helvetica"
),
FontFamily.Serif.name to listOf("Times"),
FontFamily.Monospace.name to listOf("Courier"),
FontFamily.Cursive.name to listOf("Apple Chancery")
)
Platform.Linux ->
mapOf(
FontFamily.SansSerif.name to listOf("Noto Sans", "DejaVu Sans"),
FontFamily.Serif.name to listOf("Noto Serif", "DejaVu Serif", "Times New Roman"),
FontFamily.Monospace.name to listOf("Noto Sans Mono", "DejaVu Sans Mono"),
// better alternative?
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.Unknown ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
}
}

internal actual fun loadTypeface(font: Font): SkTypeface {
if (font !is PlatformFont) {
throw IllegalArgumentException("Unsupported font type: $font")
Expand All @@ -67,25 +30,5 @@ internal actual fun loadTypeface(font: Font): SkTypeface {
}
}

private enum class Platform {
Linux,
Windows,
MacOS,
Unknown;

companion object {
val Current by lazy {
println("TODO: selecting MacOS unconditionally")
MacOS
/*
val name = System.getProperty("os.name")
when {
name.startsWith("Linux") -> Linux
name.startsWith("Win") -> Windows
name == "Mac OS X" -> MacOS
else -> Unknown
}
*/
}
}
}
// TODO: Select current platform
internal actual fun currentPlatform(): Platform = Platform.Unknown
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,11 @@
*/
package androidx.compose.ui.text.platform

import kotlin.native.OsFamily as NativeOsFamily
import kotlin.native.Platform as NativePlatform
import org.jetbrains.skia.Typeface as SkTypeface
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontListFontFamily
import org.jetbrains.skia.Data

internal actual val GenericFontFamiliesMapping by lazy {
when (Platform.Current) {
Platform.Windows ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.MacOS ->
mapOf(
FontFamily.SansSerif.name to listOf(
"Helvetica Neue",
"Helvetica"
),
FontFamily.Serif.name to listOf("Times"),
FontFamily.Monospace.name to listOf("Courier"),
FontFamily.Cursive.name to listOf("Apple Chancery")
)
Platform.Linux ->
mapOf(
FontFamily.SansSerif.name to listOf("Noto Sans", "DejaVu Sans"),
FontFamily.Serif.name to listOf("Noto Serif", "DejaVu Serif", "Times New Roman"),
FontFamily.Monospace.name to listOf("Noto Sans Mono", "DejaVu Sans Mono"),
// better alternative?
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.Unknown ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
}
}

internal actual fun loadTypeface(font: Font): SkTypeface {
if (font !is PlatformFont) {
throw IllegalArgumentException("Unsupported font type: $font")
Expand All @@ -72,18 +32,12 @@ internal actual fun loadTypeface(font: Font): SkTypeface {
}
}

private enum class Platform {
Linux,
Windows,
MacOS,
Unknown;

companion object {
val Current: Platform = when(NativePlatform.osFamily) {
NativeOsFamily.MACOSX -> MacOS
NativeOsFamily.LINUX -> Linux
NativeOsFamily.WINDOWS -> Windows
else -> Unknown
}
}
internal actual fun currentPlatform(): Platform = when (NativePlatform.osFamily) {
OsFamily.MACOSX -> Platform.MacOS
OsFamily.IOS -> Platform.IOS
OsFamily.LINUX -> Platform.Linux
OsFamily.WINDOWS -> Platform.Windows
OsFamily.TVOS -> Platform.TvOS
OsFamily.WATCHOS -> Platform.WatchOS
else -> Platform.Unknown
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,51 @@ internal class FontCache {
}
}

internal expect val GenericFontFamiliesMapping: Map<String, List<String>>
internal enum class Platform {
Unknown,
Linux,
Windows,
MacOS,
IOS,
TvOS,
WatchOS,
}

internal expect fun currentPlatform(): Platform
internal expect fun loadTypeface(font: Font): SkTypeface

internal val GenericFontFamiliesMapping: Map<String, List<String>> by lazy {
when (currentPlatform()) {
Platform.Linux ->
mapOf(
FontFamily.SansSerif.name to listOf("Noto Sans", "DejaVu Sans"),
FontFamily.Serif.name to listOf("Noto Serif", "DejaVu Serif", "Times New Roman"),
FontFamily.Monospace.name to listOf("Noto Sans Mono", "DejaVu Sans Mono"),
// better alternative?
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.Windows ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Platform.MacOS, Platform.IOS, Platform.TvOS, Platform.WatchOS ->
mapOf(
// .AppleSystem* aliases is the only legal way to get default SF and NY fonts.
FontFamily.SansSerif.name to listOf(".AppleSystemUIFont", "Helvetica Neue", "Helvetica"),
FontFamily.Serif.name to listOf(".AppleSystemUIFontSerif", "Times", "Times New Roman"),
FontFamily.Monospace.name to listOf(".AppleSystemUIFontMonospaced", "Menlo", "Courier"),
// Safari "font-family: cursive" real font names from macOS and iOS.
FontFamily.Cursive.name to listOf("Apple Chancery", "Snell Roundhand")
)
Platform.Unknown ->
mapOf(
FontFamily.SansSerif.name to listOf("Arial"),
FontFamily.Serif.name to listOf("Times New Roman"),
FontFamily.Monospace.name to listOf("Consolas"),
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
}
}