Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Bugfix: Font Family resolving in Release Builds #57

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,14 @@ class MdcThemeTest<T : AppCompatActivity>(activityClass: Class<T>) {
val typography = MaterialTheme.typography
val density = LocalDensity.current

val rubik = FontFamily(
Font(resId = R.font.rubik_300, weight = FontWeight.W300),
Font(resId = R.font.rubik_400, weight = FontWeight.W400),
Font(resId = R.font.rubik_500, weight = FontWeight.W500),
Font(resId = R.font.rubik_700, weight = FontWeight.W700)
)
val rubik300 = Font(R.font.rubik_300).toFontFamily()
val rubik400 = Font(R.font.rubik_400).toFontFamily()
val rubik500 = Font(R.font.rubik_500).toFontFamily()
val sansSerif = FontFamilyWithWeight(FontFamily.SansSerif)
val sansSerifLight = FontFamilyWithWeight(FontFamily.SansSerif, FontWeight.Light)
val sansSerifBlack = FontFamilyWithWeight(FontFamily.SansSerif, FontWeight.Black)
Expand Down Expand Up @@ -198,7 +203,7 @@ class MdcThemeTest<T : AppCompatActivity>(activityClass: Class<T>) {
}

typography.button.run {
assertEquals(rubik500, fontFamily)
assertEquals(rubik, fontFamily)
}

typography.caption.run {
Expand Down
31 changes: 31 additions & 0 deletions lib/src/androidTest/res/font/rubik.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
chrisbanes marked this conversation as resolved.
Show resolved Hide resolved
<!--
~ Copyright 2020 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
~
~ https://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.
-->

<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/rubik_300"
app:fontWeight="300" />
<font
app:font="@font/rubik_400"
app:fontWeight="400" />
<font
app:font="@font/rubik_500"
app:fontWeight="500" />
<font
app:font="@font/rubik_700"
app:fontWeight="700" />
</font-family>
2 changes: 1 addition & 1 deletion lib/src/androidTest/res/values/type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
</style>

<style name="TextAppearance.MdcThemeTest.Button" parent="TextAppearance.MaterialComponents.Button">
<item name="fontFamily">@font/rubik_500</item>
<item name="fontFamily">@font/rubik</item>
</style>

<style name="TextAppearance.MdcThemeTest.Caption" parent="TextAppearance.MaterialComponents.Caption">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ internal fun TypedArray.getFontFamilyOrNull(index: Int): FontFamilyWithWeight? {
"monospace" -> FontFamilyWithWeight(FontFamily.Monospace)
// TODO: Compose does not expose a FontFamily for all strings yet
else -> {
// If there's a resource ID and the string starts with res/font,
// If there's a resource ID and the string starts with res/,
// it's probably a @font resource
if (tv.resourceId != 0 && tv.string.startsWith("res/font")) {
if (tv.resourceId != 0 && tv.string.startsWith("res/")) {
chrisbanes marked this conversation as resolved.
Show resolved Hide resolved
// If we're running on API 23+ and the resource is an XML, we can parse
// the fonts into a full FontFamily.
if (Build.VERSION.SDK_INT >= 23 && tv.string.endsWith(".xml")) {
Expand Down
6 changes: 6 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ android {
composeOptions {
kotlinCompilerExtensionVersion Libs.AndroidX.Compose.version
}

buildTypes {
release {
signingConfig signingConfigs.debug
}
}
}

dependencies {
Expand Down
31 changes: 31 additions & 0 deletions sample/src/main/res/font/dancingscript.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
chrisbanes marked this conversation as resolved.
Show resolved Hide resolved
<!--
~ Copyright 2020 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
~
~ https://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.
-->

<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/dancingscript_regular"
app:fontWeight="400" />
<font
app:font="@font/dancingscript_medium"
app:fontWeight="500" />
<font
app:font="@font/dancingscript_semibold"
app:fontWeight="600" />
<font
app:font="@font/dancingscript_bold"
app:fontWeight="700" />
</font-family>
Binary file added sample/src/main/res/font/dancingscript_bold.ttf
Binary file not shown.
Binary file added sample/src/main/res/font/dancingscript_medium.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions sample/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<item name="colorSecondary">@color/pink_a200</item>
<item name="colorSecondaryVariant">@color/pink_dark</item>

<item name="textAppearanceHeadline3">@style/TextAppearance.Sample.Headline3</item>
<item name="textAppearanceSubtitle1">@style/TextAppearance.Sample.Subtitle1</item>
<item name="textAppearanceCaption">@style/TextAppearance.Sample.Caption</item>

Expand Down
6 changes: 6 additions & 0 deletions sample/src/main/res/values/type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
-->

<resources>

<style name="TextAppearance.Sample.Headline3"
parent="TextAppearance.MaterialComponents.Headline3">
<item name="fontFamily">@font/dancingscript</item>
</style>

<!-- Use TextAppearance.AppCompat.Subhead as parent to avoid inheriting letterSpacing -->
<style name="TextAppearance.Sample.Subtitle1"
parent="TextAppearance.AppCompat.Subhead">
Expand Down