Skip to content

Commit c111015

Browse files
bharatknvQuantumBadger
authored andcommitted
Limit height of emotes in comment flair and body
1 parent afb9274 commit c111015

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

Diff for: src/main/java/org/quantumbadger/redreader/reddit/prepared/RedditParsedComment.java

+29-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.graphics.Bitmap;
2121
import android.graphics.BitmapFactory;
2222
import android.text.style.ImageSpan;
23+
import android.util.TypedValue;
2324

2425
import androidx.annotation.NonNull;
2526
import androidx.annotation.Nullable;
@@ -35,6 +36,7 @@
3536
import org.quantumbadger.redreader.common.General;
3637
import org.quantumbadger.redreader.common.GenericFactory;
3738
import org.quantumbadger.redreader.common.Optional;
39+
import org.quantumbadger.redreader.common.PrefsUtility;
3840
import org.quantumbadger.redreader.common.Priority;
3941
import org.quantumbadger.redreader.common.datastream.SeekableInputStream;
4042
import org.quantumbadger.redreader.http.FailedRequestBody;
@@ -149,10 +151,33 @@ public void onDataStreamComplete(
149151
try (InputStream is = stream.create()) {
150152
image = BitmapFactory.decodeStream(is);
151153

152-
image = Bitmap.createScaledBitmap(image,
153-
image.getWidth() / 2,
154-
image.getHeight() / 2,
155-
true);
154+
if (image == null) {
155+
throw new IOException("Failed to decode bitmap");
156+
}
157+
158+
final int textSize = 11;
159+
final float maxImageHeightMultiple = 2.0F;
160+
161+
final float maxHeight = TypedValue.applyDimension(
162+
TypedValue.COMPLEX_UNIT_SP,
163+
PrefsUtility.appearance_fontscale_comment_headers()
164+
* textSize
165+
* maxImageHeightMultiple,
166+
activity.getApplicationContext()
167+
.getResources()
168+
.getDisplayMetrics());
169+
170+
if (image.getHeight() > maxHeight) {
171+
final float imageAspectRatio =
172+
(float) image.getHeight() / image.getWidth();
173+
174+
final float newImageWidth = maxHeight / imageAspectRatio;
175+
176+
image = Bitmap.createScaledBitmap(image,
177+
Math.round(newImageWidth),
178+
Math.round(maxHeight),
179+
true);
180+
}
156181

157182
if (image == null) {
158183
throw new IOException("Failed to decode bitmap");

Diff for: src/main/java/org/quantumbadger/redreader/reddit/prepared/html/HtmlRawElementImg.java

+26
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.text.Spannable;
2323
import android.text.SpannableStringBuilder;
2424
import android.text.style.ImageSpan;
25+
import android.util.TypedValue;
2526

2627
import androidx.annotation.NonNull;
2728
import androidx.annotation.Nullable;
@@ -36,6 +37,7 @@
3637
import org.quantumbadger.redreader.common.General;
3738
import org.quantumbadger.redreader.common.GenericFactory;
3839
import org.quantumbadger.redreader.common.Optional;
40+
import org.quantumbadger.redreader.common.PrefsUtility;
3941
import org.quantumbadger.redreader.common.Priority;
4042
import org.quantumbadger.redreader.common.RRError;
4143
import org.quantumbadger.redreader.common.UriString;
@@ -105,6 +107,30 @@ public void onDataStreamComplete(
105107
throw new IOException("Failed to decode bitmap");
106108
}
107109

110+
final int textSize = 18;
111+
final float maxImageHeightMultiple = 2.0F;
112+
113+
final float maxHeight = TypedValue.applyDimension(
114+
TypedValue.COMPLEX_UNIT_SP,
115+
PrefsUtility.appearance_fontscale_comment_headers()
116+
* textSize
117+
* maxImageHeightMultiple,
118+
activity.getApplicationContext()
119+
.getResources()
120+
.getDisplayMetrics());
121+
122+
if (image.getHeight() > maxHeight) {
123+
final float imageAspectRatio =
124+
(float) image.getHeight() / image.getWidth();
125+
126+
final float newImageWidth = maxHeight / imageAspectRatio;
127+
128+
image = Bitmap.createScaledBitmap(image,
129+
Math.round(newImageWidth),
130+
Math.round(maxHeight),
131+
true);
132+
}
133+
108134
final ImageSpan span = new ImageSpan(
109135
activity.getApplicationContext(),
110136
image);

0 commit comments

Comments
 (0)