Skip to content

Commit df57a22

Browse files
committed
Limit height of emotes in comment flair and body
1 parent b855931 commit df57a22

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-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

+27
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import android.text.Spannable;
2323
import android.text.SpannableStringBuilder;
2424
import android.text.style.ImageSpan;
25+
import android.util.TypedValue;
26+
2527
import androidx.annotation.NonNull;
2628
import androidx.annotation.Nullable;
2729
import androidx.appcompat.app.AppCompatActivity;
@@ -34,6 +36,7 @@
3436
import org.quantumbadger.redreader.common.General;
3537
import org.quantumbadger.redreader.common.GenericFactory;
3638
import org.quantumbadger.redreader.common.Optional;
39+
import org.quantumbadger.redreader.common.PrefsUtility;
3740
import org.quantumbadger.redreader.common.Priority;
3841
import org.quantumbadger.redreader.common.datastream.SeekableInputStream;
3942
import org.quantumbadger.redreader.http.FailedRequestBody;
@@ -101,6 +104,30 @@ public void onDataStreamComplete(
101104
throw new IOException("Failed to decode bitmap");
102105
}
103106

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

0 commit comments

Comments
 (0)