generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue with Dimension Capping. (#137)
* Fixed issue with Dimension Capping. * Putting things back that should have not been changed.
- Loading branch information
1 parent
b84f6d9
commit 12693b6
Showing
8 changed files
with
135 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 27 additions & 27 deletions
54
src/main/kotlin/io/unthrottled/amii/memes/DimensionCappingService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/kotlin/io/unthrottled/amii/memes/DimensionCappingServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |