Switching to headingTextSizeMultipliers, adding validating annotations, adding example

This commit is contained in:
Eric Denman 2018-07-18 09:48:33 -07:00
parent 9bd5644150
commit fbef11207e
2 changed files with 13 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import android.support.annotation.FloatRange;
import android.support.annotation.IntRange; import android.support.annotation.IntRange;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.Size;
import android.text.TextPaint; import android.text.TextPaint;
import android.util.TypedValue; import android.util.TypedValue;
@ -178,8 +179,7 @@ public class SpannableTheme {
// by default, we use standard multipliers from the HTML spec (see HEADING_SIZES for values). // by default, we use standard multipliers from the HTML spec (see HEADING_SIZES for values).
// this library supports 6 heading sizes, so make sure the array you pass here has 6 elements. // this library supports 6 heading sizes, so make sure the array you pass here has 6 elements.
// note also that these values are in PIXELS, so you'll need to convert from SP first. protected final float[] headingTextSizeMultipliers;
protected final float[] headingTextSizes;
// by default `SCRIPT_DEF_TEXT_SIZE_RATIO` // by default `SCRIPT_DEF_TEXT_SIZE_RATIO`
protected final float scriptTextSizeRatio; protected final float scriptTextSizeRatio;
@ -223,7 +223,7 @@ public class SpannableTheme {
this.headingBreakHeight = builder.headingBreakHeight; this.headingBreakHeight = builder.headingBreakHeight;
this.headingBreakColor = builder.headingBreakColor; this.headingBreakColor = builder.headingBreakColor;
this.headingTypeface = builder.headingTypeface; this.headingTypeface = builder.headingTypeface;
this.headingTextSizes = builder.headingTextSizes; this.headingTextSizeMultipliers = builder.headingTextSizeMultipliers;
this.scriptTextSizeRatio = builder.scriptTextSizeRatio; this.scriptTextSizeRatio = builder.scriptTextSizeRatio;
this.thematicBreakColor = builder.thematicBreakColor; this.thematicBreakColor = builder.thematicBreakColor;
this.thematicBreakHeight = builder.thematicBreakHeight; this.thematicBreakHeight = builder.thematicBreakHeight;
@ -383,8 +383,8 @@ public class SpannableTheme {
} else { } else {
paint.setTypeface(headingTypeface); paint.setTypeface(headingTypeface);
} }
if (headingTextSizes != null && headingTextSizes.length >= level) { if (headingTextSizeMultipliers != null && headingTextSizeMultipliers.length >= level) {
paint.setTextSize(headingTextSizes[level - 1]); paint.setTextSize(paint.getTextSize() * headingTextSizeMultipliers[level - 1]);
} else { } else {
paint.setTextSize(paint.getTextSize() * HEADING_SIZES[level - 1]); paint.setTextSize(paint.getTextSize() * HEADING_SIZES[level - 1]);
} }
@ -510,7 +510,7 @@ public class SpannableTheme {
private int headingBreakHeight = -1; private int headingBreakHeight = -1;
private int headingBreakColor; private int headingBreakColor;
private Typeface headingTypeface; private Typeface headingTypeface;
private float[] headingTextSizes; private float[] headingTextSizeMultipliers;
private float scriptTextSizeRatio; private float scriptTextSizeRatio;
private int thematicBreakColor; private int thematicBreakColor;
private int thematicBreakHeight = -1; private int thematicBreakHeight = -1;
@ -541,7 +541,7 @@ public class SpannableTheme {
this.headingBreakHeight = theme.headingBreakHeight; this.headingBreakHeight = theme.headingBreakHeight;
this.headingBreakColor = theme.headingBreakColor; this.headingBreakColor = theme.headingBreakColor;
this.headingTypeface = theme.headingTypeface; this.headingTypeface = theme.headingTypeface;
this.headingTextSizes = theme.headingTextSizes; this.headingTextSizeMultipliers = theme.headingTextSizeMultipliers;
this.scriptTextSizeRatio = theme.scriptTextSizeRatio; this.scriptTextSizeRatio = theme.scriptTextSizeRatio;
this.thematicBreakColor = theme.thematicBreakColor; this.thematicBreakColor = theme.thematicBreakColor;
this.thematicBreakHeight = theme.thematicBreakHeight; this.thematicBreakHeight = theme.thematicBreakHeight;
@ -657,14 +657,14 @@ public class SpannableTheme {
} }
@NonNull @NonNull
public Builder headingTypeface(Typeface headingTypeface) { public Builder headingTypeface(@NonNull Typeface headingTypeface) {
this.headingTypeface = headingTypeface; this.headingTypeface = headingTypeface;
return this; return this;
} }
@NonNull @NonNull
public Builder headingTextSizes(float[] headingTextSizes) { public Builder headingTextSizeMultipliers(@Size(6) @NonNull float[] headingTextSizeMultipliers) {
this.headingTextSizes = headingTextSizes; this.headingTextSizeMultipliers = headingTextSizeMultipliers;
return this; return this;
} }

View File

@ -52,12 +52,14 @@ public class MainActivity extends Activity {
// better to provide a valid fallback option // better to provide a valid fallback option
final IconSpanProvider spanProvider = IconSpanProvider.create(this, 0); final IconSpanProvider spanProvider = IconSpanProvider.create(this, 0);
// create an instance of visitor to process parsed markdown final float[] textSizeMultipliers = new float[]{3f, 2f, 1.5f, 1f, .5f, .25f};
SpannableConfiguration configuration = SpannableConfiguration.builder(this) SpannableConfiguration configuration = SpannableConfiguration.builder(this)
.theme(SpannableTheme.builder() .theme(SpannableTheme.builder()
.headingTypeface(Typeface.MONOSPACE) .headingTypeface(Typeface.MONOSPACE)
.headingTextSizeMultipliers(textSizeMultipliers)
.build()) .build())
.build(); .build();
// create an instance of visitor to process parsed markdown
final IconVisitor visitor = new IconVisitor( final IconVisitor visitor = new IconVisitor(
configuration, configuration,
builder, builder,