Skip to content

Commit

Permalink
Merge pull request #46 from Teamwork/android/xml-typography-font-family
Browse files Browse the repository at this point in the history
Android: Fix fontFamily in XML typography
  • Loading branch information
aidangrabe authored Dec 20, 2022
2 parents 801b1f2 + 30461fa commit df1e064
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
textDecoration => none
-->
<style name="typography_display_small">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_regular</item>
<item name="android:textSize">36sp</item>
<item name="android:letterSpacing">-0.03</item>
<item name="android:lineSpacingExtra">8sp</item>
Expand All @@ -34,7 +34,7 @@
textDecoration => none
-->
<style name="typography_display_medium">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_regular</item>
<item name="android:textSize">45sp</item>
<item name="android:letterSpacing">-0.03</item>
<item name="android:lineSpacingExtra">7sp</item>
Expand All @@ -54,7 +54,7 @@
textDecoration => none
-->
<style name="typography_display_large">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_regular</item>
<item name="android:textSize">57sp</item>
<item name="android:letterSpacing">-0.04</item>
<item name="android:lineSpacingExtra">7sp</item>
Expand All @@ -74,7 +74,7 @@
textDecoration => none
-->
<style name="typography_headline_small">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_regular</item>
<item name="android:textSize">24sp</item>
<item name="android:letterSpacing">-0.03</item>
<item name="android:lineSpacingExtra">8sp</item>
Expand All @@ -94,7 +94,7 @@
textDecoration => none
-->
<style name="typography_headline_medium">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_regular</item>
<item name="android:textSize">28sp</item>
<item name="android:letterSpacing">-0.03</item>
<item name="android:lineSpacingExtra">8sp</item>
Expand All @@ -114,7 +114,7 @@
textDecoration => none
-->
<style name="typography_headline_large">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_regular</item>
<item name="android:textSize">32sp</item>
<item name="android:letterSpacing">-0.02</item>
<item name="android:lineSpacingExtra">8sp</item>
Expand All @@ -134,7 +134,7 @@
textDecoration => none
-->
<style name="typography_title_small">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_medium</item>
<item name="android:textSize">14sp</item>
<item name="android:letterSpacing">-0.02</item>
<item name="android:lineSpacingExtra">6sp</item>
Expand All @@ -154,7 +154,7 @@
textDecoration => none
-->
<style name="typography_title_medium">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_medium</item>
<item name="android:textSize">16sp</item>
<item name="android:letterSpacing">-0.03</item>
<item name="android:lineSpacingExtra">8sp</item>
Expand All @@ -174,7 +174,7 @@
textDecoration => none
-->
<style name="typography_title_large">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_regular</item>
<item name="android:textSize">22sp</item>
<item name="android:letterSpacing">-0.02</item>
<item name="android:lineSpacingExtra">6sp</item>
Expand All @@ -194,7 +194,7 @@
textDecoration => none
-->
<style name="typography_label_small">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_medium</item>
<item name="android:textSize">11sp</item>
<item name="android:letterSpacing">0</item>
<item name="android:lineSpacingExtra">5sp</item>
Expand All @@ -214,7 +214,7 @@
textDecoration => none
-->
<style name="typography_label_medium">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_medium</item>
<item name="android:textSize">12sp</item>
<item name="android:letterSpacing">-0.01</item>
<item name="android:lineSpacingExtra">4sp</item>
Expand All @@ -234,7 +234,7 @@
textDecoration => none
-->
<style name="typography_label_large">
<item name="android:fontFamily">Poppins</item>
<item name="android:fontFamily">@font/poppins_medium</item>
<item name="android:textSize">14sp</item>
<item name="android:letterSpacing">-0.03</item>
<item name="android:lineSpacingExtra">6sp</item>
Expand All @@ -254,7 +254,7 @@
textDecoration => none
-->
<style name="typography_body_small">
<item name="android:fontFamily">Roboto</item>
<!-- no fontFamily specified as we want to use the default Roboto -->
<item name="android:textSize">12sp</item>
<item name="android:letterSpacing">0.03333333333333333</item>
<item name="android:lineSpacingExtra">4sp</item>
Expand All @@ -274,7 +274,7 @@
textDecoration => none
-->
<style name="typography_body_medium">
<item name="android:fontFamily">Roboto</item>
<!-- no fontFamily specified as we want to use the default Roboto -->
<item name="android:textSize">14sp</item>
<item name="android:letterSpacing">0.017857142857142856</item>
<item name="android:lineSpacingExtra">6sp</item>
Expand All @@ -294,7 +294,7 @@
textDecoration => none
-->
<style name="typography_body_large">
<item name="android:fontFamily">Roboto</item>
<!-- no fontFamily specified as we want to use the default Roboto -->
<item name="android:textSize">16sp</item>
<item name="android:letterSpacing">0.03125</item>
<item name="android:lineSpacingExtra">8sp</item>
Expand Down
17 changes: 16 additions & 1 deletion formats/androidTypography.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@ function getTextStyleForToken(token) {
${Object.keys(token.value).map(key => `${key} => ${token.value[key]}`).join("\n\t")}
-->
<style name="${token.name}">
<item name="android:fontFamily">${token.value.fontFamily}</item>
${getFontFamilyTag(token)}
<item name="android:textSize">${token.value.fontSize}sp</item>
<item name="android:letterSpacing">${calculateLetterSpacing(token.value.fontSize, token.value.letterSpacing)}</item>
${getLineHeightTag(token)}
</style>`;
}

function getFontFamilyTag(token) {
const fontFamilyValue = getFontFamilyValue(token.value.fontFamily, token.value.fontWeight);
if (fontFamilyValue == null) {
return "<!-- no fontFamily specified as we want to use the default Roboto -->"; // this is the default font, it's included in the system
}
return `<item name="android:fontFamily">${fontFamilyValue}</item>`
}

function getFontFamilyValue(fontFamily, fontWeight) {
if (fontFamily == "Roboto") {
return null; // this is the default font, it's included in the system
}
return `@font/${fontFamily.toLowerCase()}_${fontWeight.toLowerCase()}`;
}

function calculateLetterSpacing(fontSize, letterSpacing) {
if (indexOf(letterSpacing, "%") != -1) {
// We treat percentages as em units. Eg: -2% -> -0.02
Expand Down

0 comments on commit df1e064

Please sign in to comment.