Skip to content

Commit

Permalink
V1.0.5 (#39)
Browse files Browse the repository at this point in the history
* Change LinkSpan to extend URLSpan. Allow default linkColor (if not set explicitly)

* Fit an image without dimensions to canvas width (and keep ratio)

* Add support for separate color for code blocks (#37)
  • Loading branch information
noties authored May 22, 2018
1 parent 35c39d0 commit a02e9bc
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

## Installation
```groovy
compile 'ru.noties:markwon:1.0.4'
compile 'ru.noties:markwon-image-loader:1.0.4' // optional
compile 'ru.noties:markwon-view:1.0.4' // optional
compile 'ru.noties:markwon:1.0.5'
compile 'ru.noties:markwon-image-loader:1.0.5' // optional
compile 'ru.noties:markwon-view:1.0.5' // optional
```

## Supported markdown features:
Expand Down
19 changes: 18 additions & 1 deletion library/src/main/java/ru/noties/markwon/spans/AsyncDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public AsyncDrawable(
this.imageSize = imageSize;
}

@NonNull
public String getDestination() {
return destination;
}
Expand Down Expand Up @@ -172,13 +173,29 @@ public int getIntrinsicHeight() {
*/
@NonNull
private Rect resolveBounds() {

final Rect rect;

if (imageSizeResolver == null
|| imageSize == null) {
rect = result.getBounds();

// @since 1.0.5
final Rect bounds = result.getBounds();
if (bounds.width() > canvasWidth) {

// let's scale image down, as we do not want to expand beyond canvas width
final float ratio = (float) bounds.width() / bounds.height();
final int height = (int) (canvasWidth / ratio + .5F);
rect = new Rect(0, 0, canvasWidth, height);

} else {
rect = bounds;
}

} else {
rect = imageSizeResolver.resolveImageSize(imageSize, result.getBounds(), canvasWidth, textSize);
}

return rect;
}
}
6 changes: 3 additions & 3 deletions library/src/main/java/ru/noties/markwon/spans/CodeSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public void updateMeasureState(TextPaint p) {
public void updateDrawState(TextPaint ds) {
apply(ds);
if (!multiline) {
ds.bgColor = theme.getCodeBackgroundColor(ds);
ds.bgColor = theme.getCodeBackgroundColor(ds, false);
}
}

private void apply(TextPaint p) {
theme.applyCodeTextStyle(p);
theme.applyCodeTextStyle(p, multiline);
}

@Override
Expand All @@ -50,7 +50,7 @@ public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int ba
if (multiline) {

paint.setStyle(Paint.Style.FILL);
paint.setColor(theme.getCodeBackgroundColor(p));
paint.setColor(theme.getCodeBackgroundColor(p, true));

final int left;
final int right;
Expand Down
4 changes: 3 additions & 1 deletion library/src/main/java/ru/noties/markwon/spans/LinkSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;

public class LinkSpan extends ClickableSpan {
public class LinkSpan extends URLSpan {

public interface Resolver {
void resolve(View view, @NonNull String link);
Expand All @@ -16,6 +17,7 @@ public interface Resolver {
private final Resolver resolver;

public LinkSpan(@NonNull SpannableTheme theme, @NonNull String link, @NonNull Resolver resolver) {
super(link);
this.theme = theme;
this.link = link;
this.resolver = resolver;
Expand Down
80 changes: 75 additions & 5 deletions library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ public static Builder builderWithDefaults(@NonNull Context context) {
final int linkColor = resolve(context, android.R.attr.textColorLink);
final int backgroundColor = resolve(context, android.R.attr.colorBackground);

// before 1.0.5 build had `linkColor` set, but in order for spans to use default link color
// set directly in widget (or any caller), we should not pass it here

final Dip dip = new Dip(context);
return new Builder()
.linkColor(linkColor)
.codeMultilineMargin(dip.toPx(8))
.blockMargin(dip.toPx(24))
.blockQuoteWidth(dip.toPx(4))
Expand Down Expand Up @@ -145,9 +147,15 @@ private static int resolve(Context context, @AttrRes int attr) {
// by default - main text color
protected final int codeTextColor;

// by default - codeTextColor
protected final int codeBlockTextColor;

// by default 0.1 alpha of textColor/codeTextColor
protected final int codeBackgroundColor;

// by default codeBackgroundColor
protected final int codeBlockBackgroundColor;

// by default `width` of a space char... it's fun and games, but span doesn't have access to paint in `getLeadingMargin`
// so, we need to set this value explicitly (think of an utility method, that takes TextView/TextPaint and measures space char)
protected final int codeMultilineMargin;
Expand Down Expand Up @@ -198,7 +206,9 @@ protected SpannableTheme(@NonNull Builder builder) {
this.bulletListItemStrokeWidth = builder.bulletListItemStrokeWidth;
this.bulletWidth = builder.bulletWidth;
this.codeTextColor = builder.codeTextColor;
this.codeBlockTextColor = builder.codeBlockTextColor;
this.codeBackgroundColor = builder.codeBackgroundColor;
this.codeBlockBackgroundColor = builder.codeBlockBackgroundColor;
this.codeMultilineMargin = builder.codeMultilineMargin;
this.codeTypeface = builder.codeTypeface;
this.codeTextSize = builder.codeTextSize;
Expand All @@ -214,12 +224,30 @@ protected SpannableTheme(@NonNull Builder builder) {
this.taskListDrawable = builder.taskListDrawable;
}

/**
* @since 1.0.5
*/
public void applyLinkStyle(@NonNull TextPaint paint) {
paint.setUnderlineText(true);
if (linkColor != 0) {
paint.setColor(linkColor);
} else {
// if linkColor is not specified during configuration -> use default one
paint.setColor(paint.linkColor);
}
}

public void applyLinkStyle(@NonNull Paint paint) {
paint.setUnderlineText(true);
if (linkColor != 0) {
// by default we will be using text color
paint.setColor(linkColor);
} else {
// @since 1.0.5, if link color is specified during configuration, _try_ to use the
// default one (if provided paint is an instance of TextPaint)
if (paint instanceof TextPaint) {
paint.setColor(((TextPaint) paint).linkColor);
}
}
}

Expand Down Expand Up @@ -278,9 +306,16 @@ public int getBulletWidth(int height) {
return width;
}

public void applyCodeTextStyle(@NonNull Paint paint) {
/**
* Modified in 1.0.5 to accept `multiline` argument
*/
public void applyCodeTextStyle(@NonNull Paint paint, boolean multiline) {

if (codeTextColor != 0) {
// @since 1.0.5 added handling of multiline code blocks
if (multiline
&& codeBlockTextColor != 0) {
paint.setColor(codeBlockTextColor);
} else if (codeTextColor != 0) {
paint.setColor(codeTextColor);
}

Expand Down Expand Up @@ -312,13 +347,23 @@ public int getCodeMultilineMargin() {
return codeMultilineMargin;
}

public int getCodeBackgroundColor(@NonNull Paint paint) {
/**
* Modified in 1.0.5 to accept `multiline` argument
*/
public int getCodeBackgroundColor(@NonNull Paint paint, boolean multiline) {

final int color;
if (codeBackgroundColor != 0) {

// @since 1.0.5 added handling of multiline code blocks
if (multiline
&& codeBlockBackgroundColor != 0) {
color = codeBlockBackgroundColor;
} else if (codeBackgroundColor != 0) {
color = codeBackgroundColor;
} else {
color = ColorUtils.applyAlpha(paint.getColor(), CODE_DEF_BACKGROUND_COLOR_ALPHA);
}

return color;
}

Expand Down Expand Up @@ -427,6 +472,7 @@ public Drawable getTaskListDrawable() {
return taskListDrawable;
}

@SuppressWarnings("unused")
public static class Builder {

private int linkColor;
Expand All @@ -437,7 +483,9 @@ public static class Builder {
private int bulletListItemStrokeWidth;
private int bulletWidth;
private int codeTextColor;
private int codeBlockTextColor; // @since 1.0.5
private int codeBackgroundColor;
private int codeBlockBackgroundColor; // @since 1.0.5
private int codeMultilineMargin;
private Typeface codeTypeface;
private int codeTextSize;
Expand All @@ -464,7 +512,9 @@ public static class Builder {
this.bulletListItemStrokeWidth = theme.bulletListItemStrokeWidth;
this.bulletWidth = theme.bulletWidth;
this.codeTextColor = theme.codeTextColor;
this.codeBlockTextColor = theme.codeBlockTextColor;
this.codeBackgroundColor = theme.codeBackgroundColor;
this.codeBlockBackgroundColor = theme.codeBlockBackgroundColor;
this.codeMultilineMargin = theme.codeMultilineMargin;
this.codeTypeface = theme.codeTypeface;
this.codeTextSize = theme.codeTextSize;
Expand Down Expand Up @@ -498,6 +548,7 @@ public Builder blockQuoteWidth(@Dimension int blockQuoteWidth) {
return this;
}

@SuppressWarnings("SameParameterValue")
@NonNull
public Builder blockQuoteColor(@ColorInt int blockQuoteColor) {
this.blockQuoteColor = blockQuoteColor;
Expand Down Expand Up @@ -528,12 +579,31 @@ public Builder codeTextColor(@ColorInt int codeTextColor) {
return this;
}

/**
* @since 1.0.5
*/
@NonNull
public Builder codeBlockTextColor(@ColorInt int codeBlockTextColor) {
this.codeBlockTextColor = codeBlockTextColor;
return this;
}

@SuppressWarnings("SameParameterValue")
@NonNull
public Builder codeBackgroundColor(@ColorInt int codeBackgroundColor) {
this.codeBackgroundColor = codeBackgroundColor;
return this;
}

/**
* @since 1.0.5
*/
@NonNull
public Builder codeBlockBackgroundColor(@ColorInt int codeBlockBackgroundColor) {
this.codeBlockBackgroundColor = codeBlockBackgroundColor;
return this;
}

@NonNull
public Builder codeMultilineMargin(@Dimension int codeMultilineMargin) {
this.codeMultilineMargin = codeMultilineMargin;
Expand Down

0 comments on commit a02e9bc

Please sign in to comment.