Skip to content

Commit

Permalink
Merge pull request #11 from geekkoz/feature/add_support_for_generate_…
Browse files Browse the repository at this point in the history
…android_fonts_files

Add support to generate android fonts xml files
  • Loading branch information
Jawnnypoo authored Feb 21, 2022
2 parents ae41abe + 3469328 commit 98539d3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/main/kotlin/com/commit451/resourcespoet/FontFamily.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.commit451.resourcespoet

/**
* Represents an Android Font
* See <a https:></a>//developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml">https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
*/
data class FontFamily
/**
* Construct a Font family
* @param fontStyle the font style
* @param fontWeight the font weight value
* @param font reference to the font file
*/
(val fontStyle: String, val fontWeight: String, val font: String)
54 changes: 42 additions & 12 deletions src/main/kotlin/com/commit451/resourcespoet/ResourcesPoet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class ResourcesPoet private constructor(

companion object {

private const val ELEMENT_RESOURCES = "resources"
enum class ELEMENT constructor(val elementName: String) {
RESOURCES("resources"),
FONT_FAMILIES("font-family")
}

private const val INDENT_DEFAULT = false

private val transformerFactory: TransformerFactory by lazy { TransformerFactory.newInstance() }
Expand All @@ -44,11 +48,18 @@ class ResourcesPoet private constructor(
* @return poet
*/
@JvmStatic
fun create(indent: Boolean = INDENT_DEFAULT): ResourcesPoet {
fun create(indent: Boolean = INDENT_DEFAULT, elementType: ELEMENT = ELEMENT.RESOURCES): ResourcesPoet {
val document = documentBuilder.newDocument()
val resources = document.createElement(ELEMENT_RESOURCES)
document.appendChild(resources)
return ResourcesPoet(document, resources, indent)
val element = document.createElement(elementType.elementName)
if (elementType == ELEMENT.FONT_FAMILIES) {
element.setAttributeNS(
"http://www.w3.org/2000/xmlns/",
"xmlns:android",
"http://schemas.android.com/apk/res/android"
)
}
document.appendChild(element)
return ResourcesPoet(document, element, indent)
}

/**
Expand Down Expand Up @@ -76,18 +87,22 @@ class ResourcesPoet private constructor(
* @return poet
*/
@JvmStatic
fun create(inputStream: InputStream, indent: Boolean = INDENT_DEFAULT): ResourcesPoet {
fun create(
inputStream: InputStream,
indent: Boolean = INDENT_DEFAULT,
elementType: ELEMENT = ELEMENT.RESOURCES
): ResourcesPoet {
try {
val document = documentBuilder.parse(inputStream)
val resources: Element
val list = document.getElementsByTagName(ELEMENT_RESOURCES)
val element: Element
val list = document.getElementsByTagName(elementType.elementName)
if (list == null || list.length == 0) {
resources = document.createElement(ELEMENT_RESOURCES)
document.appendChild(resources)
element = document.createElement(elementType.elementName)
document.appendChild(element)
} else {
resources = list.item(0) as Element
element = list.item(0) as Element
}
return ResourcesPoet(document, resources, indent)
return ResourcesPoet(document, element, indent)
} catch (e: IOException) {
throw IllegalStateException(
"Unable to parse the resource file you passed. Make sure it is properly formatted",
Expand Down Expand Up @@ -419,6 +434,21 @@ class ResourcesPoet private constructor(
return this
}

/**
* Add a font family attr
*
* @param fontFamily the defined Font Family
* @return poet
*/
fun addFontFamily(fontFamily: FontFamily): ResourcesPoet {
val element = document.createElement(Type.FONT.toString())
element.setAttribute("android:fontStyle", fontFamily.fontStyle)
element.setAttribute("android:fontWeight", fontFamily.fontWeight)
element.setAttribute("android:font", fontFamily.font)
resourceElement.appendChild(element)
return this
}

/**
* Add type to the XML file by its type. Currently supported types:
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/commit451/resourcespoet/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum class Type(private val xmlName: String) {
STRING("string"),
STRING_ARRAY("string-array"),
STYLE("style"),
TYPED_ARRAY("array");
TYPED_ARRAY("array"),
FONT("font");

override fun toString() = xmlName
}
16 changes: 16 additions & 0 deletions src/test/kotlin/com/commit451/resourcespoet/FontsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.commit451.resourcespoet

import org.junit.Test

/**
* Tests the font resource creation
*/
class FontsTest {

@Test
fun styleTest() {
val poet = ResourcesPoet.create(elementType = ResourcesPoet.Companion.ELEMENT.FONT_FAMILIES)
.addFontFamily(FontFamily("normal", "400", "@font/lobster_regular"))
TestUtil.assertEquals("fonts.xml", poet)
}
}
4 changes: 4 additions & 0 deletions src/test/resources/fonts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font android:font="@font/lobster_regular" android:fontStyle="normal" android:fontWeight="400"/>
</font-family>

0 comments on commit 98539d3

Please sign in to comment.