Skip to content

Commit 12693b6

Browse files
authored
Fixed issue with Dimension Capping. (#137)
* Fixed issue with Dimension Capping. * Putting things back that should have not been changed.
1 parent b84f6d9 commit 12693b6

File tree

8 files changed

+135
-32
lines changed

8 files changed

+135
-32
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
# AMII Changelog
44

5+
## [0.13.2]
6+
7+
## Fixed
8+
9+
- Issue with dimension capping of assets.
10+
511
## [0.13.1]
612

713
### Fixed

docs/RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
### Fixed
22

33
- Images not showing up for users with an `'` in their file path.
4+
- Issue with dimension capping of assets.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
pluginGroup = io.unthrottled
55
pluginName_ = Anime Memes
6-
pluginVersion = 0.13.1
6+
pluginVersion = 0.13.2
77
pluginSinceBuild = 203.7148.57
88
pluginUntilBuild = 213.*
99
# Plugin Verifier integration -> https://github.com/JetBrains/gradle-intellij-plugin#plugin-verifier-dsl

src/main/java/io/unthrottled/amii/config/ui/PluginSettingsUI.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.unthrottled.amii.memes.DimensionCappingService;
2828
import io.unthrottled.amii.memes.PanelDismissalOptions;
2929
import io.unthrottled.amii.services.CharacterGatekeeper;
30+
import io.unthrottled.amii.services.GifService;
3031
import io.unthrottled.amii.tools.PluginMessageBundle;
3132
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
@@ -47,6 +48,8 @@
4748
import javax.swing.event.DocumentEvent;
4849
import javax.swing.event.DocumentListener;
4950
import javax.swing.event.HyperlinkEvent;
51+
52+
import java.awt.Dimension;
5053
import java.awt.event.ActionListener;
5154
import java.net.URI;
5255
import java.net.URISyntaxException;
@@ -293,8 +296,11 @@ private String getSettingsAniMeme() {
293296
String extraStyles =
294297
getFilePath(asset)
295298
.map(fileUrl -> DimensionCappingService.getCappingStyle(
296-
200, 200, fileUrl, true
299+
GifService.INSTANCE.getDimensions(fileUrl),
300+
new Dimension(100, 100)
297301
))
302+
.map(usableDimension ->
303+
"width='" + usableDimension.width + "' height='" + usableDimension.height + "'")
298304
.orElse("");
299305
String aniMeme = "<img src='" + asset + "' " + extraStyles + "/>\n";
300306
return aniMeme;
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
package io.unthrottled.amii.memes
22

3-
import io.unthrottled.amii.config.Config
4-
import io.unthrottled.amii.services.GifService
5-
import java.net.URI
3+
import java.awt.Dimension
64

75
object DimensionCappingService {
86

97
@JvmStatic
108
fun getCappingStyle(
11-
maxHeight: Int,
12-
maxWidth: Int,
13-
filePath: URI,
14-
shouldCap: Boolean = Config.instance.capDimensions
15-
): String {
16-
val setMaxHeight = maxHeight > 0
17-
val setMaxWidth = maxWidth > 0
18-
val memeDimensions = GifService.getDimensions(filePath)
19-
val memeHeight = memeDimensions.height
20-
val memeWidth = memeDimensions.width
21-
val heightIsGreaterThanOriginal = maxHeight < memeHeight
22-
val widthIsGreaterThanOriginal = maxWidth < memeWidth
23-
val needsToCap = heightIsGreaterThanOriginal || widthIsGreaterThanOriginal
24-
val canCap = (setMaxHeight || setMaxWidth) && shouldCap
9+
stickerDimensions: Dimension,
10+
maxDimension: Dimension,
11+
): Dimension {
12+
val maxHeight = maxDimension.height
13+
val maxWidth = maxDimension.width
14+
val shouldSetMaxHeight = maxHeight > 0
15+
val shouldSetMaxWidth = maxWidth > 0
16+
val comparisonMaxHeight = if (shouldSetMaxHeight) maxHeight else Int.MAX_VALUE
17+
val comparisonMaxWidth = if (shouldSetMaxWidth) maxWidth else Int.MAX_VALUE
18+
val stickerHeight = stickerDimensions.height
19+
val stickerWidth = stickerDimensions.width
20+
val stickerHeightGreaterThanCap = comparisonMaxHeight < stickerHeight
21+
val stickerWidthGreaterThanCap = comparisonMaxWidth < stickerWidth
22+
val needsToCap = stickerHeightGreaterThanCap || stickerWidthGreaterThanCap
23+
val canCap = (shouldSetMaxHeight || shouldSetMaxWidth)
2524
return if (needsToCap && canCap) {
26-
val heightIsGreater = memeHeight > memeWidth
2725
val (width, height) =
2826
when {
29-
heightIsGreaterThanOriginal &&
30-
heightIsGreater &&
31-
setMaxHeight ->
32-
(memeWidth / memeHeight.toDouble()) * maxHeight to maxHeight
33-
widthIsGreaterThanOriginal && setMaxWidth ->
34-
maxWidth to (memeHeight / memeWidth.toDouble()) * maxWidth
35-
else -> memeWidth to memeHeight
27+
shouldSetMaxHeight &&
28+
comparisonMaxHeight <= comparisonMaxWidth &&
29+
stickerHeightGreaterThanCap ->
30+
(stickerWidth / stickerHeight.toDouble()) * maxHeight to maxHeight
31+
shouldSetMaxWidth &&
32+
comparisonMaxWidth <= comparisonMaxHeight &&
33+
stickerWidthGreaterThanCap ->
34+
maxWidth to (stickerHeight / stickerWidth.toDouble()) * maxWidth
35+
else -> stickerWidth to stickerHeight
3636
}
37-
"""height='${height.toInt()}' width='${width.toInt()}'"""
37+
Dimension(width.toInt(), height.toInt())
3838
} else {
39-
""
39+
stickerDimensions
4040
}
4141
}
4242
}

src/main/kotlin/io/unthrottled/amii/memes/MemePanel.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,20 @@ class MemePanel(
300300

301301
private fun getExtraStyles(): String =
302302
if (Config.instance.capDimensions) {
303-
getCappedDimensions()
303+
val usableDimension = getCappedDimensions()
304+
"""width='${usableDimension.width}' height='${usableDimension.height}'"""
304305
} else {
305306
""
306307
}
307308

308-
private fun getCappedDimensions(): String {
309+
private fun getCappedDimensions(): Dimension {
309310
val maxHeight = Config.instance.maxMemeHeight
310311
val maxWidth = Config.instance.maxMemeWidth
311312
val filePath = visualMeme.filePath
312-
return getCappingStyle(maxHeight, maxWidth, filePath)
313+
return getCappingStyle(
314+
GifService.getDimensions(filePath),
315+
Dimension(maxWidth, maxHeight),
316+
)
313317
}
314318

315319
private fun positionMemePanel(settings: MemePanelSettings, width: Int, height: Int) {

src/main/kotlin/io/unthrottled/amii/onboarding/UpdateNotification.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private fun buildUpdateMessage(updateAsset: String): String =
2323
What's New?<br>
2424
<ul>
2525
<li>Added support for users with an ' in their path</li>
26+
<li>Fixed issue with dimension capping of assets.</li>
2627
</ul>
2728
<br>See the <a href="https://github.com/ani-memes/AMII#documentation">documentation</a> for features, usages, and configurations.
2829
<br>The <a href="https://github.com/ani-memes/AMII/blob/master/CHANGELOG.md">changelog</a> is available for more details.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package io.unthrottled.amii.memes
2+
3+
import org.assertj.core.api.Assertions
4+
import org.junit.Test
5+
import java.awt.Dimension
6+
7+
class DimensionCappingServiceTest {
8+
9+
@Test
10+
fun getCappingStyleShouldNotCapIfBothNegativeOne() {
11+
val result = DimensionCappingService.getCappingStyle(
12+
Dimension(69, 420),
13+
Dimension(-1, -1),
14+
)
15+
16+
Assertions.assertThat(result)
17+
.isEqualTo(Dimension(69, 420))
18+
}
19+
20+
@Test
21+
fun getCappingStyleShouldMaintainAspectRationWhenScalingWidthHeightGreater() {
22+
val result = DimensionCappingService.getCappingStyle(
23+
Dimension(20, 40),
24+
Dimension(10, -1),
25+
)
26+
27+
Assertions.assertThat(result)
28+
.isEqualTo(Dimension(10, 20))
29+
}
30+
31+
@Test
32+
fun getCappingStyleShouldMaintainAspectRationWhenScalingHeightHeightGreater() {
33+
val result = DimensionCappingService.getCappingStyle(
34+
Dimension(20, 40),
35+
Dimension(-1, 10),
36+
)
37+
38+
Assertions.assertThat(result)
39+
.isEqualTo(Dimension(5, 10))
40+
}
41+
42+
@Test
43+
fun getCappingStyleShouldMaintainAspectRationWhenScalingWidthWidthGreater() {
44+
val result = DimensionCappingService.getCappingStyle(
45+
Dimension(40, 20),
46+
Dimension(10, -1),
47+
)
48+
49+
Assertions.assertThat(result)
50+
.isEqualTo(Dimension(10, 5))
51+
}
52+
53+
@Test
54+
fun getCappingStyleShouldMaintainAspectRationWhenScalingHeightWidthGreater() {
55+
val result = DimensionCappingService.getCappingStyle(
56+
Dimension(40, 20),
57+
Dimension(-1, 10),
58+
)
59+
60+
Assertions.assertThat(result)
61+
.isEqualTo(Dimension(20, 10))
62+
}
63+
64+
@Test
65+
fun getCappingStyleShouldMaintainAspectRatioAndRespectSmallestCapHeight() {
66+
val result = DimensionCappingService.getCappingStyle(
67+
Dimension(40, 20),
68+
Dimension(4, 10),
69+
)
70+
71+
Assertions.assertThat(result)
72+
.isEqualTo(Dimension(4, 2))
73+
}
74+
75+
@Test
76+
fun getCappingStyleShouldMaintainAspectRatioAndRespectSmallestCapWidth() {
77+
val result = DimensionCappingService.getCappingStyle(
78+
Dimension(40, 20),
79+
Dimension(10, 4),
80+
)
81+
82+
Assertions.assertThat(result)
83+
.isEqualTo(Dimension(8, 4))
84+
}
85+
}

0 commit comments

Comments
 (0)