Skip to content

Commit

Permalink
Merge pull request #1061 from wordpress-mobile/feature/text-size-modi…
Browse files Browse the repository at this point in the history
…fier

Add text size modifier
  • Loading branch information
nbradbury authored Sep 28, 2023
2 parents 29b449e + 5cdc4a1 commit caf8f43
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
22 changes: 22 additions & 0 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import android.text.TextWatcher
import android.text.style.SuggestionSpan
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.MotionEvent
Expand Down Expand Up @@ -495,26 +496,32 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
headerStyle = BlockFormatter.HeaderStyles(verticalHeadingMargin, mapOf(
AztecHeadingSpan.Heading.H1 to BlockFormatter.HeaderStyles.HeadingStyle(
styles.getDimensionPixelSize(R.styleable.AztecText_headingOneFontSize, 0),
0,
styles.getColor(R.styleable.AztecText_headingOneFontColor, 0)
),
AztecHeadingSpan.Heading.H2 to BlockFormatter.HeaderStyles.HeadingStyle(
styles.getDimensionPixelSize(R.styleable.AztecText_headingTwoFontSize, 0),
0,
styles.getColor(R.styleable.AztecText_headingTwoFontColor, 0)
),
AztecHeadingSpan.Heading.H3 to BlockFormatter.HeaderStyles.HeadingStyle(
styles.getDimensionPixelSize(R.styleable.AztecText_headingThreeFontSize, 0),
0,
styles.getColor(R.styleable.AztecText_headingThreeFontColor, 0)
),
AztecHeadingSpan.Heading.H4 to BlockFormatter.HeaderStyles.HeadingStyle(
styles.getDimensionPixelSize(R.styleable.AztecText_headingFourFontSize, 0),
0,
styles.getColor(R.styleable.AztecText_headingFourFontColor, 0)
),
AztecHeadingSpan.Heading.H5 to BlockFormatter.HeaderStyles.HeadingStyle(
styles.getDimensionPixelSize(R.styleable.AztecText_headingFiveFontSize, 0),
0,
styles.getColor(R.styleable.AztecText_headingFiveFontColor, 0)
),
AztecHeadingSpan.Heading.H6 to BlockFormatter.HeaderStyles.HeadingStyle(
styles.getDimensionPixelSize(R.styleable.AztecText_headingSixFontSize, 0),
0,
styles.getColor(R.styleable.AztecText_headingSixFontColor, 0)
)
)),
Expand Down Expand Up @@ -617,6 +624,21 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
isViewInitialized = true
}

/**
Sets the modifier that will be added to the base text size.
This is useful for situations where you have specified heading font size, instead or relying on default scaling.
Params: – textSizeModifierPx: the modifier in pixels
*/
fun setTextSizeModifier(textSizeModifierPx: Int) {
blockFormatter.setTextSizeModifier(textSizeModifierPx)
if (textSize + textSizeModifierPx >= 0) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize + textSizeModifierPx)
} else {
setTextSize(TypedValue.COMPLEX_UNIT_PX, 0f)
}
}

private fun <T> selectionHasExactlyOneMarker(start: Int, end: Int, type: Class<T>): Boolean {
val spanFound: Array<T> = editableText.getSpans(
start,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class BlockFormatter(editor: AztecText,
}

data class QuoteStyle(val quoteBackground: Int, val quoteColor: Int, val quoteTextColor: Int, val quoteBackgroundAlpha: Float, val quoteMargin: Int, val quotePadding: Int, val quoteWidth: Int, val verticalPadding: Int)
data class PreformatStyle(val preformatBackground: Int, val preformatBackgroundAlpha: Float, val preformatColor: Int, val verticalPadding: Int, val leadingMargin: Int, val preformatBorderColor: Int, val preformatBorderRadius: Int, val preformatBorderThickness: Int, val preformatTextSize: Int)
data class PreformatStyle(val preformatBackground: Int, val preformatBackgroundAlpha: Float, val preformatColor: Int, val verticalPadding: Int, val leadingMargin: Int, val preformatBorderColor: Int, val preformatBorderRadius: Int, val preformatBorderThickness: Int, var preformatTextSize: Int)
data class ListItemStyle(val strikeThroughCheckedItems: Boolean, val checkedItemsTextColor: Int)
data class HeaderStyles(val verticalPadding: Int, val styles: Map<AztecHeadingSpan.Heading, HeadingStyle>) {
data class HeadingStyle(val fontSize: Int, val fontColor: Int)
data class HeadingStyle(val fontSize: Int, var fontSizeModifier: Int, val fontColor: Int)
}
data class ExclusiveBlockStyles(val enabled: Boolean = false, val verticalParagraphMargin: Int)
data class ParagraphStyle(val verticalMargin: Int)
Expand Down Expand Up @@ -1251,4 +1251,11 @@ class BlockFormatter(editor: AztecText,
}
}
}

fun setTextSizeModifier(modifier: Int) {
headerStyle.styles.forEach {
it.value.fontSizeModifier = modifier
}
preformatStyle.preformatTextSize += modifier
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ open class AztecHeadingSpan(
when (val headingSize = getHeadingSize()) {
is HeadingSize.Scale -> {
textPaint.textSize *= heading.scale
if (textPaint.textSize + getSizeModifier() >= 0) {
textPaint.textSize += getSizeModifier()
} else {
textPaint.textSize = 0f
}
}
is HeadingSize.Size -> {
textPaint.textSize = headingSize.value.toFloat()
Expand All @@ -187,6 +192,11 @@ open class AztecHeadingSpan(
when (headingSize) {
is HeadingSize.Scale -> {
paint.textSize *= heading.scale
if (paint.textSize + getSizeModifier() >= 0) {
paint.textSize += getSizeModifier()
} else {
paint.textSize = 0f
}
}
is HeadingSize.Size -> {
paint.textSize = headingSize.value.toFloat()
Expand All @@ -198,10 +208,14 @@ open class AztecHeadingSpan(
}

private fun getHeadingSize(): HeadingSize {
return headerStyle.styles[heading]?.fontSize?.takeIf { it > 0 }?.let { HeadingSize.Size(it) }
return headerStyle.styles[heading]?.fontSize?.takeIf { it > 0 }?.let { HeadingSize.Size(it + getSizeModifier()) }
?: HeadingSize.Scale(heading.scale)
}

private fun getSizeModifier(): Int {
return headerStyle.styles[heading]?.fontSizeModifier ?: 0
}

private fun getHeadingColor(): Int? {
return headerStyle.styles[heading]?.fontColor?.takeIf { it != 0 }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ open class AztecTaskListSpan(
} else {
0.2 to 0.8
}
d.setBounds((leftBound - drawableHeight * startShift).toInt(), (baseline - drawableHeight * 0.8).toInt(), (leftBound + drawableHeight * endShift).toInt(), (baseline + drawableHeight * 0.2).toInt())

d.setBounds((leftBound - drawableHeight * startShift).toInt().coerceAtLeast(0),
(baseline - drawableHeight * 0.8).toInt(),
(leftBound + drawableHeight * endShift).toInt(),
(baseline + drawableHeight * 0.2).toInt())
d.draw(c)

p.color = oldColor
Expand Down

0 comments on commit caf8f43

Please sign in to comment.