NekoX/TMessagesProj/src/main/java/org/telegram/ui/Components/LinkPath.java

123 lines
3.4 KiB
Java
Raw Normal View History

2015-06-29 19:12:11 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-06-29 19:12:11 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2015-06-29 19:12:11 +02:00
*/
package org.telegram.ui.Components;
import android.graphics.Path;
2019-01-23 18:03:33 +01:00
import android.graphics.RectF;
2019-02-08 03:30:32 +01:00
import android.os.Build;
2015-06-29 19:12:11 +02:00
import android.text.StaticLayout;
2019-01-23 18:03:33 +01:00
import org.telegram.messenger.AndroidUtilities;
2015-06-29 19:12:11 +02:00
public class LinkPath extends Path {
private StaticLayout currentLayout;
private int currentLine;
private float lastTop = -1;
2016-04-22 15:49:00 +02:00
private float heightOffset;
2019-01-23 18:03:33 +01:00
private boolean useRoundRect;
private RectF rect;
private boolean allowReset = true;
private int baselineShift;
2019-02-08 03:30:32 +01:00
private int lineHeight;
2019-01-23 18:03:33 +01:00
public LinkPath() {
super();
}
public LinkPath(boolean roundRect) {
super();
useRoundRect = roundRect;
}
2015-06-29 19:12:11 +02:00
2016-04-22 15:49:00 +02:00
public void setCurrentLayout(StaticLayout layout, int start, float yOffset) {
2015-06-29 19:12:11 +02:00
currentLayout = layout;
currentLine = layout.getLineForOffset(start);
lastTop = -1;
2016-04-22 15:49:00 +02:00
heightOffset = yOffset;
2019-02-08 03:30:32 +01:00
if (Build.VERSION.SDK_INT >= 28) {
int lineCount = layout.getLineCount();
if (lineCount > 0) {
lineHeight = layout.getLineBottom(lineCount - 1) - layout.getLineTop(lineCount - 1);
}
}
2015-06-29 19:12:11 +02:00
}
2019-01-23 18:03:33 +01:00
public void setAllowReset(boolean value) {
allowReset = value;
}
public void setUseRoundRect(boolean value) {
useRoundRect = value;
}
public boolean isUsingRoundRect() {
return useRoundRect;
}
public void setBaselineShift(int value) {
baselineShift = value;
}
2015-06-29 19:12:11 +02:00
@Override
public void addRect(float left, float top, float right, float bottom, Direction dir) {
2016-04-22 15:49:00 +02:00
top += heightOffset;
bottom += heightOffset;
2015-06-29 19:12:11 +02:00
if (lastTop == -1) {
lastTop = top;
} else if (lastTop != top) {
lastTop = top;
currentLine++;
}
float lineRight = currentLayout.getLineRight(currentLine);
float lineLeft = currentLayout.getLineLeft(currentLine);
2019-01-23 18:03:33 +01:00
if (left >= lineRight || left <= lineLeft && right <= lineLeft) {
2015-06-29 19:12:11 +02:00
return;
}
if (right > lineRight) {
right = lineRight;
}
if (left < lineLeft) {
left = lineLeft;
}
2019-01-23 18:03:33 +01:00
float y = top;
2019-02-08 03:30:32 +01:00
float y2;
if (Build.VERSION.SDK_INT >= 28) {
y2 = bottom;
if (bottom - top > lineHeight) {
y2 -= (bottom != currentLayout.getHeight() ? currentLayout.getSpacingAdd() : 0);
}
} else {
y2 = bottom - (bottom != currentLayout.getHeight() ? currentLayout.getSpacingAdd() : 0);
}
2019-01-23 18:03:33 +01:00
if (baselineShift < 0) {
y2 += baselineShift;
} else if (baselineShift > 0) {
y += baselineShift;
}
if (useRoundRect) {
if (rect == null) {
rect = new RectF();
}
rect.set(left - AndroidUtilities.dp(4), y, right + AndroidUtilities.dp(4), y2);
super.addRoundRect(rect, AndroidUtilities.dp(4), AndroidUtilities.dp(4), dir);
} else {
super.addRect(left, y, right, y2, dir);
}
}
@Override
public void reset() {
if (!allowReset) {
return;
}
super.reset();
2015-06-29 19:12:11 +02:00
}
}