@@ -210,7 +210,7 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
210
210
setBounds(0 , 0 , sizeXPx, sizeYPx)
211
211
}
212
212
213
- var respectFontBounds: Boolean = false
213
+ var respectFontBounds: Boolean = Iconics .respectFontBoundsDefault
214
214
set(value) {
215
215
field = value
216
216
invalidateThis()
@@ -403,8 +403,8 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
403
403
if (icon == null && iconText == null ) return
404
404
val viewBounds = bounds
405
405
updatePaddingBounds(viewBounds)
406
- updateTextSize (viewBounds)
407
- offsetIcon(viewBounds )
406
+ updatePathBounds (viewBounds)
407
+ offsetIcon()
408
408
409
409
if (needMirroring()) {
410
410
// Mirror the drawable
@@ -440,7 +440,9 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
440
440
}
441
441
442
442
override fun onBoundsChange (bounds : Rect ) {
443
- offsetIcon(bounds)
443
+ updatePaddingBounds(bounds)
444
+ updatePathBounds(bounds)
445
+ offsetIcon()
444
446
runCatching { path.close() }
445
447
super .onBoundsChange(bounds)
446
448
}
@@ -519,34 +521,37 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
519
521
}
520
522
521
523
/* * 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 ) {
526
525
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)
528
530
path.computeBounds(pathBounds, true )
529
531
530
- if (! respectFontBounds) {
532
+ if (respectFontBounds) {
533
+ path.offset(viewBounds.exactCenterX(), paddingBounds.top + textSize - iconBrush.paint.fontMetrics.descent)
534
+ } else {
531
535
val deltaWidth = paddingBounds.width().toFloat() / pathBounds.width()
532
536
val deltaHeight = paddingBounds.height().toFloat() / pathBounds.height()
533
537
val delta = if (deltaWidth < deltaHeight) deltaWidth else deltaHeight
534
538
textSize * = delta
535
539
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)
537
541
path.computeBounds(pathBounds, true )
542
+ path.offset(paddingBounds.left - pathBounds.left, paddingBounds.top - pathBounds.top)
538
543
}
539
544
}
540
545
541
546
/* * 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
+ }
550
555
}
551
556
552
557
/* * Ensures the tint filter is consistent with the current tint color and mode. */
@@ -744,4 +749,4 @@ open class IconicsDrawable internal constructor() : WrappedDrawable() {
744
749
invalidateSelf()
745
750
return this
746
751
}
747
- }
752
+ }
0 commit comments