diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 2ea53c234..ebc396d4f 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -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 @@ -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) ) )), @@ -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 selectionHasExactlyOneMarker(start: Int, end: Int, type: Class): Boolean { val spanFound: Array = editableText.getSpans( start, diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt index f5f3bed1b..a6e83c467 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt @@ -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) { - 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) @@ -1251,4 +1251,11 @@ class BlockFormatter(editor: AztecText, } } } + + fun setTextSizeModifier(modifier: Int) { + headerStyle.styles.forEach { + it.value.fontSizeModifier = modifier + } + preformatStyle.preformatTextSize += modifier + } } diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHeadingSpan.kt b/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHeadingSpan.kt index 72fa828b3..b8e4e2a67 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHeadingSpan.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHeadingSpan.kt @@ -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() @@ -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() @@ -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 } } diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt b/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt index f836e836f..8cb39c679 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt @@ -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