Skip to content

Commit

Permalink
Add a property for it
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal committed Dec 29, 2023
1 parent 677212f commit ef35ec3
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import kotlin.math.roundToInt
* size than this composable.
* @param contentScale Define how the animation should be scaled if it has a different size than this Composable.
* @param clipToCompositionBounds Determines whether or not Lottie will clip the animation to the original animation composition bounds.
* @param clipTextToBoundingBox When true, if there is a bounding box set on a text layer (paragraph text), any text
* that overflows past its height will not be drawn.
* @param fontMap A map of keys to Typefaces. The key can be: "fName", "fFamily", or "fFamily-fStyle" as specified in your Lottie file.
* @param asyncUpdates When set to true, some parts of animation updates will be done off of the main thread.
* For more details, refer to the docs of [AsyncUpdates].
Expand All @@ -86,6 +88,7 @@ fun LottieAnimation(
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
clipToCompositionBounds: Boolean = true,
clipTextToBoundingBox: Boolean = false,
fontMap: Map<String, Typeface>? = null,
asyncUpdates: AsyncUpdates = AsyncUpdates.AUTOMATIC,
) {
Expand Down Expand Up @@ -124,6 +127,7 @@ fun LottieAnimation(
drawable.isApplyingOpacityToLayersEnabled = applyOpacityToLayers
drawable.maintainOriginalImageBounds = maintainOriginalImageBounds
drawable.clipToCompositionBounds = clipToCompositionBounds
drawable.clipTextToBoundingBox = clipTextToBoundingBox
drawable.progress = progress()
drawable.setBounds(0, 0, composition.bounds.width(), composition.bounds.height())
drawable.draw(canvas.nativeCanvas, matrix)
Expand Down Expand Up @@ -197,6 +201,7 @@ fun LottieAnimation(
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
clipToCompositionBounds: Boolean = true,
clipTextToBoundingBox: Boolean = false,
fontMap: Map<String, Typeface>? = null,
asyncUpdates: AsyncUpdates = AsyncUpdates.AUTOMATIC,
) {
Expand All @@ -222,6 +227,7 @@ fun LottieAnimation(
alignment = alignment,
contentScale = contentScale,
clipToCompositionBounds = clipToCompositionBounds,
clipTextToBoundingBox = clipTextToBoundingBox,
fontMap = fontMap,
asyncUpdates = asyncUpdates,
)
Expand Down
19 changes: 19 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
setClipToCompositionBounds(ta.getBoolean(R.styleable.LottieAnimationView_lottie_clipToCompositionBounds, true));
}

if (ta.hasValue(R.styleable.LottieAnimationView_lottie_clipTextToBoundingBox)) {
setClipTextToBoundingBox(ta.getBoolean(R.styleable.LottieAnimationView_lottie_clipTextToBoundingBox, false));
}

if (ta.hasValue(R.styleable.LottieAnimationView_lottie_defaultFontFileExtension)) {
setDefaultFontFileExtension(ta.getString(R.styleable.LottieAnimationView_lottie_defaultFontFileExtension));
}
Expand Down Expand Up @@ -1217,6 +1221,21 @@ public void setApplyingOpacityToLayersEnabled(boolean isApplyingOpacityToLayersE
lottieDrawable.setApplyingOpacityToLayersEnabled(isApplyingOpacityToLayersEnabled);
}

/**
* @see #setClipTextToBoundingBox(boolean)
*/
public boolean getClipTextToBoundingBox() {
return lottieDrawable.getClipTextToBoundingBox();
}

/**
* When true, if there is a bounding box set on a text layer (paragraph text), any text
* that overflows past its height will not be drawn.
*/
public void setClipTextToBoundingBox(boolean clipTextToBoundingBox) {
lottieDrawable.setClipTextToBoundingBox(clipTextToBoundingBox);
}

/**
* This API no longer has any effect.
*/
Expand Down
19 changes: 19 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private enum OnVisibleAction {
private boolean performanceTrackingEnabled;
private boolean outlineMasksAndMattes;
private boolean isApplyingOpacityToLayersEnabled;
private boolean clipTextToBoundingBox = false;

private RenderMode renderMode = RenderMode.AUTOMATIC;
/**
Expand Down Expand Up @@ -539,6 +540,24 @@ public boolean isApplyingOpacityToLayersEnabled() {
return isApplyingOpacityToLayersEnabled;
}

/**
* @see #setClipTextToBoundingBox(boolean)
*/
public boolean getClipTextToBoundingBox() {
return clipTextToBoundingBox;
}

/**
* When true, if there is a bounding box set on a text layer (paragraph text), any text
* that overflows past its height will not be drawn.
*/
public void setClipTextToBoundingBox(boolean clipTextToBoundingBox) {
if (clipTextToBoundingBox != this.clipToCompositionBounds) {
this.clipTextToBoundingBox = clipTextToBoundingBox;
invalidateSelf();
}
}

private void buildCompositionLayer() {
LottieComposition composition = this.composition;
if (composition == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void drawTextWithGlyphs(

canvas.save();

if (offsetCanvas(canvas, documentData, lineIndex, line.width)) {
if (offsetCanvas(canvas, documentData, lineIndex, line.width) || !lottieDrawable.getClipTextToBoundingBox()) {
drawGlyphTextLine(line.text, documentData, font, canvas, parentScale, fontScale, tracking);
}

Expand Down Expand Up @@ -273,7 +273,7 @@ private void drawTextWithFont(DocumentData documentData, Font font, Canvas canva

canvas.save();

if (offsetCanvas(canvas, documentData, lineIndex, line.width)) {
if (offsetCanvas(canvas, documentData, lineIndex, line.width) || !lottieDrawable.getClipTextToBoundingBox()) {
drawFontTextLine(line.text, documentData, canvas, tracking);
}

Expand Down
3 changes: 2 additions & 1 deletion lottie/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<attr name="lottie_ignoreDisabledSystemAnimations" format="boolean" />
<attr name="lottie_useCompositionFrameRate" format="boolean" />
<attr name="lottie_clipToCompositionBounds" format="boolean" />
<attr name="lottie_clipTextToBoundingBox" format="boolean" />
<!-- The default file extension that Lottie will use when finding fonts in assets/fonts/fontFamily.* -->
<attr name="lottie_defaultFontFileExtension" format="string" />
<!-- These values must be kept in sync with the RenderMode enum -->
Expand All @@ -37,4 +38,4 @@
<enum name="disabled" value="2" />
</attr>
</declare-styleable>
</resources>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.airbnb.lottie.model.LottieCompositionCache
import com.airbnb.lottie.snapshots.tests.ApplyOpacityToLayerTestCase
import com.airbnb.lottie.snapshots.tests.AssetsTestCase
import com.airbnb.lottie.snapshots.tests.ClipChildrenTestCase
import com.airbnb.lottie.snapshots.tests.ClipTextToBoundingBoxTestCase
import com.airbnb.lottie.snapshots.tests.ColorStateListColorFilterTestCase
import com.airbnb.lottie.snapshots.tests.ComposeDynamicPropertiesTestCase
import com.airbnb.lottie.snapshots.tests.ComposeScaleTypesTestCase
Expand Down Expand Up @@ -160,6 +161,7 @@ class LottieSnapshotTest {
SoftwareRenderingDynamicPropertiesInvalidationTestCase(),
SeekBarTestCase(),
CompositionFrameRate(),
ClipTextToBoundingBoxTestCase(),
)

withTimeout(TimeUnit.MINUTES.toMillis(45)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.airbnb.lottie.snapshots.tests

import com.airbnb.lottie.snapshots.SnapshotTestCase
import com.airbnb.lottie.snapshots.SnapshotTestCaseContext
import com.airbnb.lottie.snapshots.withFilmStripView

class ClipTextToBoundingBoxTestCase : SnapshotTestCase {
override suspend fun SnapshotTestCaseContext.run() {
withFilmStripView(
"Tests/SzGlyph.json",
"Clip glyph text to bounding box",
"Enabled"
) { filmStripView ->
filmStripView.setClipTextToBoundingBox(true)
}
withFilmStripView(
"Tests/SzGlyph.json",
"Clip glyph text to bounding box",
"Disabled"
) { filmStripView ->
filmStripView.setClipTextToBoundingBox(true)
}

withFilmStripView(
"Tests/SzFont.json",
"Clip font text to bounding box",
"Enabled"
) { filmStripView ->
filmStripView.setClipTextToBoundingBox(true)
}
withFilmStripView(
"Tests/SzFont.json",
"Clip font text to bounding box",
"Disabled"
) { filmStripView ->
filmStripView.setClipTextToBoundingBox(true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class FilmStripView @JvmOverloads constructor(
animationViews.forEach { it.setApplyingOpacityToLayersEnabled(isApplyingOpacityToLayersEnabled) }
}

fun setClipTextToBoundingBox(isApplyingOpacityToLayersEnabled: Boolean) {
animationViews.forEach { it.clipTextToBoundingBox = isApplyingOpacityToLayersEnabled }
}

fun setOutlineMasksAndMattes(outline: Boolean) {
animationViews.forEach { it.setOutlineMasksAndMattes(outline) }
}
Expand All @@ -58,4 +62,4 @@ class FilmStripView @JvmOverloads constructor(
repeat(decimals) { multiplier *= 10 }
return round(this * multiplier) / multiplier
}
}
}

0 comments on commit ef35ec3

Please sign in to comment.