@@ -210,7 +210,7 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
210210 setBounds(0 , 0 , sizeXPx, sizeYPx)
211211 }
212212
213- var respectFontBounds: Boolean = false
213+ var respectFontBounds: Boolean = Iconics .respectFontBoundsDefault
214214 set(value) {
215215 field = value
216216 invalidateThis()
@@ -403,8 +403,8 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
403403 if (icon == null && iconText == null ) return
404404 val viewBounds = bounds
405405 updatePaddingBounds(viewBounds)
406- updateTextSize (viewBounds)
407- offsetIcon(viewBounds )
406+ updatePathBounds (viewBounds)
407+ offsetIcon()
408408
409409 if (needMirroring()) {
410410 // Mirror the drawable
@@ -440,7 +440,9 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
440440 }
441441
442442 override fun onBoundsChange (bounds : Rect ) {
443- offsetIcon(bounds)
443+ updatePaddingBounds(bounds)
444+ updatePathBounds(bounds)
445+ offsetIcon()
444446 runCatching { path.close() }
445447 super .onBoundsChange(bounds)
446448 }
@@ -519,34 +521,37 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
519521 }
520522
521523 /* * Update the TextSize */
522- private fun updateTextSize (viewBounds : Rect ) {
523- var textSize = viewBounds.height().toFloat() * if (respectFontBounds) 1 else 2
524- iconBrush.paint.textSize = textSize
525-
524+ private fun updatePathBounds (viewBounds : Rect ) {
526525 val textValue = icon?.character?.toString() ? : iconText.toString()
527- iconBrush.paint.getTextPath(textValue, 0 , textValue.length, 0f , viewBounds.height().toFloat(), path)
526+
527+ var textSize = paddingBounds.height().toFloat()
528+ iconBrush.paint.textSize = textSize
529+ iconBrush.paint.getTextPath(textValue, 0 , textValue.length, 0f , 0f , path)
528530 path.computeBounds(pathBounds, true )
529531
530- if (! respectFontBounds) {
532+ if (respectFontBounds) {
533+ path.offset(viewBounds.exactCenterX(), paddingBounds.top + textSize - iconBrush.paint.fontMetrics.descent)
534+ } else {
531535 val deltaWidth = paddingBounds.width().toFloat() / pathBounds.width()
532536 val deltaHeight = paddingBounds.height().toFloat() / pathBounds.height()
533537 val delta = if (deltaWidth < deltaHeight) deltaWidth else deltaHeight
534538 textSize * = delta
535539 iconBrush.paint.textSize = textSize
536- iconBrush.paint.getTextPath(textValue, 0 , textValue.length, 0f , viewBounds.height().toFloat() , path)
540+ iconBrush.paint.getTextPath(textValue, 0 , textValue.length, 0f , 0f , path)
537541 path.computeBounds(pathBounds, true )
542+ path.offset(paddingBounds.left - pathBounds.left, paddingBounds.top - pathBounds.top)
538543 }
539544 }
540545
541546 /* * Set the icon offset */
542- private fun offsetIcon (viewBounds : Rect ) {
543- val startX = viewBounds.centerX() - pathBounds.width() / 2
544- val offsetX = startX - pathBounds.left
545-
546- val startY = viewBounds.centerY () - pathBounds.height( ) / 2
547- val offsetY = startY - pathBounds.top
548-
549- path.offset(offsetX + iconOffsetXPx, offsetY + iconOffsetYPx)
547+ private fun offsetIcon () {
548+ if (respectFontBounds) {
549+ path.offset(iconOffsetXPx.toFloat(), iconOffsetYPx.toFloat())
550+ } else {
551+ val offsetX = (paddingBounds.width () - pathBounds.width() ) / 2
552+ val offsetY = (paddingBounds.height() - pathBounds.height()) / 2
553+ path.offset(offsetX + iconOffsetXPx, offsetY + iconOffsetYPx)
554+ }
550555 }
551556
552557 /* * Ensures the tint filter is consistent with the current tint color and mode. */
@@ -744,4 +749,4 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
744749 invalidateSelf()
745750 return this
746751 }
747- }
752+ }
0 commit comments