Skip to content

Commit

Permalink
Fixed issue with Dimension Capping. (#137)
Browse files Browse the repository at this point in the history
* Fixed issue with Dimension Capping.

* Putting things back that should have not been changed.
  • Loading branch information
Unthrottled authored Jan 2, 2022
1 parent b84f6d9 commit 12693b6
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

# AMII Changelog

## [0.13.2]

## Fixed

- Issue with dimension capping of assets.

## [0.13.1]

### Fixed
Expand Down
1 change: 1 addition & 0 deletions docs/RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### Fixed

- Images not showing up for users with an `'` in their file path.
- Issue with dimension capping of assets.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = io.unthrottled
pluginName_ = Anime Memes
pluginVersion = 0.13.1
pluginVersion = 0.13.2
pluginSinceBuild = 203.7148.57
pluginUntilBuild = 213.*
# Plugin Verifier integration -> https://github.com/JetBrains/gradle-intellij-plugin#plugin-verifier-dsl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.unthrottled.amii.memes.DimensionCappingService;
import io.unthrottled.amii.memes.PanelDismissalOptions;
import io.unthrottled.amii.services.CharacterGatekeeper;
import io.unthrottled.amii.services.GifService;
import io.unthrottled.amii.tools.PluginMessageBundle;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -47,6 +48,8 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.HyperlinkEvent;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -293,8 +296,11 @@ private String getSettingsAniMeme() {
String extraStyles =
getFilePath(asset)
.map(fileUrl -> DimensionCappingService.getCappingStyle(
200, 200, fileUrl, true
GifService.INSTANCE.getDimensions(fileUrl),
new Dimension(100, 100)
))
.map(usableDimension ->
"width='" + usableDimension.width + "' height='" + usableDimension.height + "'")
.orElse("");
String aniMeme = "<img src='" + asset + "' " + extraStyles + "/>\n";
return aniMeme;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package io.unthrottled.amii.memes

import io.unthrottled.amii.config.Config
import io.unthrottled.amii.services.GifService
import java.net.URI
import java.awt.Dimension

object DimensionCappingService {

@JvmStatic
fun getCappingStyle(
maxHeight: Int,
maxWidth: Int,
filePath: URI,
shouldCap: Boolean = Config.instance.capDimensions
): String {
val setMaxHeight = maxHeight > 0
val setMaxWidth = maxWidth > 0
val memeDimensions = GifService.getDimensions(filePath)
val memeHeight = memeDimensions.height
val memeWidth = memeDimensions.width
val heightIsGreaterThanOriginal = maxHeight < memeHeight
val widthIsGreaterThanOriginal = maxWidth < memeWidth
val needsToCap = heightIsGreaterThanOriginal || widthIsGreaterThanOriginal
val canCap = (setMaxHeight || setMaxWidth) && shouldCap
stickerDimensions: Dimension,
maxDimension: Dimension,
): Dimension {
val maxHeight = maxDimension.height
val maxWidth = maxDimension.width
val shouldSetMaxHeight = maxHeight > 0
val shouldSetMaxWidth = maxWidth > 0
val comparisonMaxHeight = if (shouldSetMaxHeight) maxHeight else Int.MAX_VALUE
val comparisonMaxWidth = if (shouldSetMaxWidth) maxWidth else Int.MAX_VALUE
val stickerHeight = stickerDimensions.height
val stickerWidth = stickerDimensions.width
val stickerHeightGreaterThanCap = comparisonMaxHeight < stickerHeight
val stickerWidthGreaterThanCap = comparisonMaxWidth < stickerWidth
val needsToCap = stickerHeightGreaterThanCap || stickerWidthGreaterThanCap
val canCap = (shouldSetMaxHeight || shouldSetMaxWidth)
return if (needsToCap && canCap) {
val heightIsGreater = memeHeight > memeWidth
val (width, height) =
when {
heightIsGreaterThanOriginal &&
heightIsGreater &&
setMaxHeight ->
(memeWidth / memeHeight.toDouble()) * maxHeight to maxHeight
widthIsGreaterThanOriginal && setMaxWidth ->
maxWidth to (memeHeight / memeWidth.toDouble()) * maxWidth
else -> memeWidth to memeHeight
shouldSetMaxHeight &&
comparisonMaxHeight <= comparisonMaxWidth &&
stickerHeightGreaterThanCap ->
(stickerWidth / stickerHeight.toDouble()) * maxHeight to maxHeight
shouldSetMaxWidth &&
comparisonMaxWidth <= comparisonMaxHeight &&
stickerWidthGreaterThanCap ->
maxWidth to (stickerHeight / stickerWidth.toDouble()) * maxWidth
else -> stickerWidth to stickerHeight
}
"""height='${height.toInt()}' width='${width.toInt()}'"""
Dimension(width.toInt(), height.toInt())
} else {
""
stickerDimensions
}
}
}
10 changes: 7 additions & 3 deletions src/main/kotlin/io/unthrottled/amii/memes/MemePanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,20 @@ class MemePanel(

private fun getExtraStyles(): String =
if (Config.instance.capDimensions) {
getCappedDimensions()
val usableDimension = getCappedDimensions()
"""width='${usableDimension.width}' height='${usableDimension.height}'"""
} else {
""
}

private fun getCappedDimensions(): String {
private fun getCappedDimensions(): Dimension {
val maxHeight = Config.instance.maxMemeHeight
val maxWidth = Config.instance.maxMemeWidth
val filePath = visualMeme.filePath
return getCappingStyle(maxHeight, maxWidth, filePath)
return getCappingStyle(
GifService.getDimensions(filePath),
Dimension(maxWidth, maxHeight),
)
}

private fun positionMemePanel(settings: MemePanelSettings, width: Int, height: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private fun buildUpdateMessage(updateAsset: String): String =
What's New?<br>
<ul>
<li>Added support for users with an ' in their path</li>
<li>Fixed issue with dimension capping of assets.</li>
</ul>
<br>See the <a href="https://github.com/ani-memes/AMII#documentation">documentation</a> for features, usages, and configurations.
<br>The <a href="https://github.com/ani-memes/AMII/blob/master/CHANGELOG.md">changelog</a> is available for more details.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.unthrottled.amii.memes

import org.assertj.core.api.Assertions
import org.junit.Test
import java.awt.Dimension

class DimensionCappingServiceTest {

@Test
fun getCappingStyleShouldNotCapIfBothNegativeOne() {
val result = DimensionCappingService.getCappingStyle(
Dimension(69, 420),
Dimension(-1, -1),
)

Assertions.assertThat(result)
.isEqualTo(Dimension(69, 420))
}

@Test
fun getCappingStyleShouldMaintainAspectRationWhenScalingWidthHeightGreater() {
val result = DimensionCappingService.getCappingStyle(
Dimension(20, 40),
Dimension(10, -1),
)

Assertions.assertThat(result)
.isEqualTo(Dimension(10, 20))
}

@Test
fun getCappingStyleShouldMaintainAspectRationWhenScalingHeightHeightGreater() {
val result = DimensionCappingService.getCappingStyle(
Dimension(20, 40),
Dimension(-1, 10),
)

Assertions.assertThat(result)
.isEqualTo(Dimension(5, 10))
}

@Test
fun getCappingStyleShouldMaintainAspectRationWhenScalingWidthWidthGreater() {
val result = DimensionCappingService.getCappingStyle(
Dimension(40, 20),
Dimension(10, -1),
)

Assertions.assertThat(result)
.isEqualTo(Dimension(10, 5))
}

@Test
fun getCappingStyleShouldMaintainAspectRationWhenScalingHeightWidthGreater() {
val result = DimensionCappingService.getCappingStyle(
Dimension(40, 20),
Dimension(-1, 10),
)

Assertions.assertThat(result)
.isEqualTo(Dimension(20, 10))
}

@Test
fun getCappingStyleShouldMaintainAspectRatioAndRespectSmallestCapHeight() {
val result = DimensionCappingService.getCappingStyle(
Dimension(40, 20),
Dimension(4, 10),
)

Assertions.assertThat(result)
.isEqualTo(Dimension(4, 2))
}

@Test
fun getCappingStyleShouldMaintainAspectRatioAndRespectSmallestCapWidth() {
val result = DimensionCappingService.getCappingStyle(
Dimension(40, 20),
Dimension(10, 4),
)

Assertions.assertThat(result)
.isEqualTo(Dimension(8, 4))
}
}

0 comments on commit 12693b6

Please sign in to comment.