-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support exporting typography for Android (#82)
- Loading branch information
Showing
15 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
.../AndroidExample/app/src/main/java/com/redmadrobot/androidexample/ui/TypographyFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.redmadrobot.androidexample.ui | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import com.redmadrobot.androidexample.R | ||
|
||
class TypographyFragment: Fragment() { | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
return inflater.inflate(R.layout.fragment_typography, container, false) | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
48 changes: 48 additions & 0 deletions
48
Examples/AndroidExample/app/src/main/res/layout/fragment_typography.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/ConstraintLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_marginStart="16dp" | ||
android:layout_marginTop="16dp" | ||
android:orientation="vertical" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"> | ||
|
||
<TextView | ||
android:id="@+id/textViewTitle" | ||
style="@style/largeTitle" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/label_large_title" /> | ||
|
||
<TextView | ||
android:id="@+id/textViewHeader" | ||
style="@style/header" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/label_header" /> | ||
|
||
<TextView | ||
android:id="@+id/textViewBody" | ||
style="@style/body" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/label_body" /> | ||
|
||
<TextView | ||
android:id="@+id/textViewCaption" | ||
style="@style/caption" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/label_caption" /> | ||
|
||
</LinearLayout> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Examples/AndroidExample/app/src/main/res/values/typography.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<style name="body"> | ||
<item name="android:fontFamily">@font/ptsans_regular</item> | ||
<item name="android:textSize">16.0sp</item> | ||
</style> | ||
<style name="caption"> | ||
<item name="android:fontFamily">@font/ptsans_regular</item> | ||
<item name="android:textSize">14.0sp</item> | ||
</style> | ||
<style name="header"> | ||
<item name="android:fontFamily">@font/ptsans_bold</item> | ||
<item name="android:textSize">20.0sp</item> | ||
</style> | ||
<style name="largeTitle"> | ||
<item name="android:fontFamily">@font/ptsans_bold</item> | ||
<item name="android:textSize">34.0sp</item> | ||
</style> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Foundation | ||
import FigmaExportCore | ||
|
||
final public class AndroidTypographyExporter { | ||
|
||
private let outputDirectory: URL | ||
|
||
public init(outputDirectory: URL) { | ||
self.outputDirectory = outputDirectory | ||
} | ||
|
||
public func exportFonts(textStyles: [TextStyle]) throws -> [FileContents] { | ||
[makeFontsFile(textStyles: textStyles)] | ||
} | ||
|
||
private func makeFontsFile(textStyles: [TextStyle]) -> FileContents { | ||
let contents = prepareFontsDotXMLContents(textStyles) | ||
let directoryURL = outputDirectory.appendingPathComponent("values") | ||
let fileURL = URL(string: "typography.xml")! | ||
|
||
return FileContents( | ||
destination: Destination(directory: directoryURL, file: fileURL), | ||
data: contents | ||
) | ||
} | ||
|
||
private func prepareFontsDotXMLContents(_ textStyles: [TextStyle]) -> Data { | ||
let resources = XMLElement(name: "resources") | ||
let xml = XMLDocument(rootElement: resources) | ||
xml.version = "1.0" | ||
xml.characterEncoding = "utf-8" | ||
|
||
textStyles.forEach { textStyle in | ||
let textStyleNode = XMLElement(name: "style") | ||
textStyleNode.addAttribute(XMLNode.attribute(withName: "name", stringValue: textStyle.name) as! XMLNode) | ||
resources.addChild(textStyleNode) | ||
|
||
let fontFamilyItem = XMLElement(name: "item", stringValue: androidFontName(from: textStyle.fontName)) | ||
fontFamilyItem.addAttribute(XMLNode.attribute(withName: "name", stringValue: "android:fontFamily") as! XMLNode) | ||
textStyleNode.addChild(fontFamilyItem) | ||
|
||
let fontSizeItem = XMLElement(name: "item", stringValue: androidFontSize(from: textStyle.fontSize)) | ||
fontSizeItem.addAttribute(XMLNode.attribute(withName: "name", stringValue: "android:textSize") as! XMLNode) | ||
textStyleNode.addChild(fontSizeItem) | ||
} | ||
|
||
return xml.xmlData(options: .nodePrettyPrint) | ||
} | ||
|
||
private func androidFontName(from postscriptName: String) -> String { | ||
"@font/\(postscriptName.lowercased().replacingOccurrences(of: "-", with: "_"))" | ||
} | ||
|
||
private func androidFontSize(from points: Double) -> String { | ||
"\(points)sp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters