From b32b13651f720010eec7cd519faf14109c489ad5 Mon Sep 17 00:00:00 2001
From: Eric Denman <edenman@gmail.com>
Date: Fri, 20 Jul 2018 09:22:40 -0700
Subject: [PATCH] Consolidate logic, add crash if header index is out of bounds

---
 .../ru/noties/markwon/spans/SpannableTheme.java  | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java b/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java
index 858acb95..678cf1ab 100644
--- a/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java
+++ b/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java
@@ -16,6 +16,9 @@ import android.support.annotation.Size;
 import android.text.TextPaint;
 import android.util.TypedValue;
 
+import java.util.Arrays;
+import java.util.Locale;
+
 @SuppressWarnings("WeakerAccess")
 public class SpannableTheme {
 
@@ -383,10 +386,17 @@ public class SpannableTheme {
         } else {
             paint.setTypeface(headingTypeface);
         }
-        if (headingTextSizeMultipliers != null && headingTextSizeMultipliers.length >= level) {
-            paint.setTextSize(paint.getTextSize() * headingTextSizeMultipliers[level - 1]);
+        final float[] textSizes = headingTextSizeMultipliers != null
+                ? headingTextSizeMultipliers
+                : HEADING_SIZES;
+
+        if (textSizes != null && textSizes.length >= level) {
+            paint.setTextSize(paint.getTextSize() * textSizes[level - 1]);
         } else {
-            paint.setTextSize(paint.getTextSize() * HEADING_SIZES[level - 1]);
+            throw new IllegalStateException(String.format(
+                    Locale.US,
+                    "Supplied heading level: %d is invalid, where configured heading sizes are: `%s`",
+                    level, Arrays.toString(textSizes)));
         }
     }