Skip to content

Commit

Permalink
Merge pull request #575 from mikepenz/develop
Browse files Browse the repository at this point in the history
dev -> main
  • Loading branch information
mikepenz authored Mar 26, 2021
2 parents 8c710cf + a2f6598 commit 13a4ae9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

## Latest releases 🛠

- Kotlin Next Gen | [v5.2.8](https://github.com/mikepenz/Android-Iconics/tree/v5.2.8)
- Kotlin Next Gen | [v5.2.8](https://github.com/mikepenz/Android-Iconics/tree/v5.2.8) | [v5.3.0-b01](https://github.com/mikepenz/Android-Iconics/tree/v5.3.0-b01)
- Kotlin | [v4.0.2](https://github.com/mikepenz/Android-Iconics/tree/v4.0.2)
- Java AndroidX | [v3.2.5](https://github.com/mikepenz/Android-Iconics/tree/v3.2.5)
- Java Appcompat | [v3.0.4](https://github.com/mikepenz/Android-Iconics/tree/v3.0.4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.mikepenz.iconics.sample.item.IconItem
import com.mikepenz.iconics.utils.IconicsUtils
import com.mikepenz.iconics.utils.backgroundColorRes
import com.mikepenz.iconics.utils.backgroundColorString
import com.mikepenz.iconics.utils.backgroundContourColorString
import com.mikepenz.iconics.utils.colorRes
import com.mikepenz.iconics.utils.contourColorRes
import com.mikepenz.iconics.utils.contourWidthDp
Expand Down Expand Up @@ -81,6 +82,10 @@ class IconsFragment : Fragment(R.layout.icons_fragment) {
adapter.notifyAdapterDataSetChanged()
}

fun respectFontBounds() {
adapter.notifyAdapterDataSetChanged()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = IconsFragmentBinding.bind(view)
Expand Down Expand Up @@ -132,6 +137,8 @@ class IconsFragment : Fragment(R.layout.icons_fragment) {
sizeDp = 144
paddingDp = 8
backgroundColorString = "#DDFFFFFF"
backgroundContourColorString = "#DD000000"
backgroundContourWidthPx = 4
roundedCornersDp = 12
}

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/mikepenz/iconics/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class MainActivity : AppCompatActivity() {
colorInt = Color.WHITE
}

menu.findItem(R.id.action_respect_bounds).isChecked = Iconics.respectFontBoundsDefault

return super.onCreateOptionsMenu(menu)
}

Expand Down Expand Up @@ -207,6 +209,12 @@ class MainActivity : AppCompatActivity() {
isShadowEnabled = item.isChecked
return true
}
R.id.action_respect_bounds -> {
item.isChecked = !item.isChecked
iconsFragment?.respectFontBounds()
Iconics.respectFontBoundsDefault = item.isChecked
return true
}
R.id.action_opensource -> {
LibsBuilder()
.withFields(R.string::class.java.fields)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
android:id="@+id/action_shadow"
android:checkable="true"
android:title="@string/action_shadow" />
<item
android:id="@+id/action_respect_bounds"
android:checkable="true"
android:title="@string/action_respect_bounds" />
<item
android:id="@+id/action_opensource"
android:title="@string/action_opensource"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<string name="action_randomize">Randomize</string>
<string name="action_shadow">Shadow</string>
<string name="action_respect_bounds">Respect font bounds</string>
<string name="action_opensource">Open Source</string>
<string name="action_playground">Playground</string>
<string name="action_automatic">Automatic Mapping</string>
Expand Down
20 changes: 11 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ buildscript {

ext {
release = [
versionName: "5.2.8",
versionCode: 50208
versionName: "5.3.0-b01",
versionCode: 50300
]

setup = [
Expand All @@ -31,7 +31,7 @@ buildscript {
]

versions = [
kotlin : '1.4.30',
kotlin : '1.4.31',
androidX : '1.0.0',
recyclerView : '1.1.0',
material : '1.3.0',
Expand All @@ -43,25 +43,26 @@ buildscript {
core: '1.3.2'
],
startup : '1.0.0',
detekt : '1.15.0',
aboutLibraries : '8.8.2',
detekt : '1.16.0',
aboutLibraries : '8.8.4',
materialDrawer : '8.3.3',
fastAdapter : '5.3.4',
fastAdapter : '5.3.5',
// compose
compose : '1.0.0-beta01'
compose : '1.0.0-beta03'
]
}

repositories {
google()
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.0-alpha08'
classpath 'com.android.tools.build:gradle:7.0.0-alpha12'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:${versions.detekt}"
classpath "com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:${versions.aboutLibraries}"
Expand All @@ -73,6 +74,7 @@ allprojects {

repositories {
google()
mavenCentral()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
Expand All @@ -84,4 +86,4 @@ subprojects {
dependencies {
detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:${versions.detekt}"
}
}
}
8 changes: 7 additions & 1 deletion iconics-core/src/main/java/com/mikepenz/iconics/Iconics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ object Iconics {

@JvmField var logger: IconicsLogger = IconicsLogger.DEFAULT

/**
* Set if Iconics should respect the original bounds of the icon.
* This applies to all Drawables (including views) as a default value.
*/
@JvmField var respectFontBoundsDefault = false

/**
* Initializes the `Context` required for different operations inside Iconics.
*/
Expand Down Expand Up @@ -339,4 +345,4 @@ object Iconics {
/** Defines where the icons should be applied to */
fun on(on: Button): BuilderView = BuilderView(fonts, on, styles, stylesFor)
}
}
}
45 changes: 25 additions & 20 deletions iconics-core/src/main/java/com/mikepenz/iconics/IconicsDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
setBounds(0, 0, sizeXPx, sizeYPx)
}

var respectFontBounds: Boolean = false
var respectFontBounds: Boolean = Iconics.respectFontBoundsDefault
set(value) {
field = value
invalidateThis()
Expand Down Expand Up @@ -403,8 +403,8 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
if (icon == null && iconText == null) return
val viewBounds = bounds
updatePaddingBounds(viewBounds)
updateTextSize(viewBounds)
offsetIcon(viewBounds)
updatePathBounds(viewBounds)
offsetIcon()

if (needMirroring()) {
// Mirror the drawable
Expand Down Expand Up @@ -440,7 +440,9 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
}

override fun onBoundsChange(bounds: Rect) {
offsetIcon(bounds)
updatePaddingBounds(bounds)
updatePathBounds(bounds)
offsetIcon()
runCatching { path.close() }
super.onBoundsChange(bounds)
}
Expand Down Expand Up @@ -519,34 +521,37 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
}

/** Update the TextSize */
private fun updateTextSize(viewBounds: Rect) {
var textSize = viewBounds.height().toFloat() * if (respectFontBounds) 1 else 2
iconBrush.paint.textSize = textSize

private fun updatePathBounds(viewBounds: Rect) {
val textValue = icon?.character?.toString() ?: iconText.toString()
iconBrush.paint.getTextPath(textValue, 0, textValue.length, 0f, viewBounds.height().toFloat(), path)

var textSize = paddingBounds.height().toFloat()
iconBrush.paint.textSize = textSize
iconBrush.paint.getTextPath(textValue, 0, textValue.length, 0f, 0f, path)
path.computeBounds(pathBounds, true)

if (!respectFontBounds) {
if (respectFontBounds) {
path.offset(viewBounds.exactCenterX(), paddingBounds.top + textSize - iconBrush.paint.fontMetrics.descent)
} else {
val deltaWidth = paddingBounds.width().toFloat() / pathBounds.width()
val deltaHeight = paddingBounds.height().toFloat() / pathBounds.height()
val delta = if (deltaWidth < deltaHeight) deltaWidth else deltaHeight
textSize *= delta
iconBrush.paint.textSize = textSize
iconBrush.paint.getTextPath(textValue, 0, textValue.length, 0f, viewBounds.height().toFloat(), path)
iconBrush.paint.getTextPath(textValue, 0, textValue.length, 0f, 0f, path)
path.computeBounds(pathBounds, true)
path.offset(paddingBounds.left - pathBounds.left, paddingBounds.top - pathBounds.top)
}
}

/** Set the icon offset */
private fun offsetIcon(viewBounds: Rect) {
val startX = viewBounds.centerX() - pathBounds.width() / 2
val offsetX = startX - pathBounds.left

val startY = viewBounds.centerY() - pathBounds.height() / 2
val offsetY = startY - pathBounds.top

path.offset(offsetX + iconOffsetXPx, offsetY + iconOffsetYPx)
private fun offsetIcon() {
if (respectFontBounds) {
path.offset(iconOffsetXPx.toFloat(), iconOffsetYPx.toFloat())
} else {
val offsetX = (paddingBounds.width() - pathBounds.width()) / 2
val offsetY = (paddingBounds.height() - pathBounds.height()) / 2
path.offset(offsetX + iconOffsetXPx, offsetY + iconOffsetYPx)
}
}

/** Ensures the tint filter is consistent with the current tint color and mode. */
Expand Down Expand Up @@ -744,4 +749,4 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
invalidateSelf()
return this
}
}
}

0 comments on commit 13a4ae9

Please sign in to comment.