Skip to content

Commit

Permalink
improve sqrt drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kochenkov committed Jun 16, 2022
1 parent d469c53 commit fa5d354
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 31 deletions.
2 changes: 1 addition & 1 deletion equationdisplayerlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.vkochenkov'
artifactId = 'equationdisplayerlib'
version = '1.1'
version = '1.2'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class EquationItem(
) {
@Composable
fun Show(
fontParams: FontParams = FontParams()
fontParams: FontParams = FontParams(),
isIndex: Boolean = false
) {
var elementHigh by remember { mutableStateOf(0f) }
var elementWidth by remember { mutableStateOf(0f) }
Expand All @@ -40,7 +41,7 @@ class EquationItem(
Column {
if (sqrt != null) {
Row(
modifier = Modifier.size((fontParams.fontSize.value / 18).dp)
modifier = Modifier.size((fontParams.fontSize.value / 10).dp)
) { /* do nothing */ }
}
Row(
Expand All @@ -56,7 +57,7 @@ class EquationItem(
}
) {
DrawSqrt(elementWidth, elementHigh, fontParams)
ShowMainContent(line, underline, fontParams, elementWidthDp)
ShowMainContent(line, underline, fontParams, elementWidthDp, isIndex)
ShowIndices(superscript, subscript, fontParams, elementHighDp)
}
}
Expand All @@ -68,34 +69,35 @@ class EquationItem(
elementHigh: Float,
fontParams: FontParams,
) {
val strokeWidth = (fontParams.fontSize.value / 16)
val context = LocalContext.current

val density = context.resources.displayMetrics.density
val strokeWidth = (fontParams.fontSize.value / 10)
val color = Color.Black

if (sqrt != null) {
val addedWidth = elementWidth / 12
val downObliquePoint = 0 - addedWidth * 2
Column(
modifier = Modifier.width(addedWidth.dp)
) { /* do nothing */ }
val addedWidth = fontParams.fontSize.value * density
val downObliquePoint = addedWidth - addedWidth/2

Canvas(modifier = Modifier) {
/* horizontal */
drawLine(
start = Offset(x = 0f, y = 0 - elementHigh / 2),
end = Offset(x = elementWidth, y = 0 - elementHigh / 2),
start = Offset(x = elementWidth , y = 0 - elementHigh / 2),
end = Offset(x = addedWidth , y = 0 - elementHigh / 2),
color = color,
strokeWidth = strokeWidth
)
/* oblique */
/* oblique right */
drawLine(
start = Offset(x = downObliquePoint, y = elementHigh / 3),
end = Offset(x = 0f, y = 0 - elementHigh / 2),
start = Offset(x = addedWidth , y = 0 - elementHigh / 2),
end = Offset(x = downObliquePoint, y = elementHigh / 3),
color = color,
strokeWidth = strokeWidth
)
/* vertical start */
/* oblique left */
drawLine(
start = Offset(x = downObliquePoint, y = elementHigh / 3),
end = Offset(x = downObliquePoint, y = 0 - elementHigh / 4),
end = Offset(x = downObliquePoint / 2, y = 0f),
color = color,
strokeWidth = strokeWidth
)
Expand All @@ -108,17 +110,25 @@ class EquationItem(
line: Any,
underline: Any?,
fontParams: FontParams,
elementWidthDp: Dp
elementWidthDp: Dp,
isIndex: Boolean = false
) {
var paddingStart = 0.dp
if (sqrt != null) {
paddingStart = fontParams.fontSize.value.dp
}
Column(
horizontalAlignment = Alignment.CenterHorizontally
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.padding(start = paddingStart)
) {
var newFontParams = fontParams.copy()
if (underline is String || underline is EquationItem) {
//newFontParams = halfSizedFontParams(fontParams)
if (underline != null) {
if (isIndex) {
newFontParams = halfSizedFontParams(fontParams)
}
CheckTypeWithList(line, newFontParams)
Divider(
modifier = Modifier.width(elementWidthDp),
modifier = Modifier.width(elementWidthDp - paddingStart),
color = Color.Black,
thickness = (newFontParams.fontSize.value / 18).dp
)
Expand All @@ -142,34 +152,34 @@ class EquationItem(
) {
val newFontParams = halfSizedFontParams(fontParams)
Row {
CheckTypeWithList(superscript, newFontParams)
CheckTypeWithList(superscript, newFontParams, true)
}
Row {
CheckTypeWithList(subscript, newFontParams)
CheckTypeWithList(subscript, newFontParams, true)
}
}
}

@Composable
private fun CheckTypeWithList(item: Any?, fontParams: FontParams) {
private fun CheckTypeWithList(item: Any?, fontParams: FontParams, isIndex: Boolean = false) {
when (item) {
is List<*> -> {
Row(
verticalAlignment = Alignment.CenterVertically
) {
for (i in item) {
CheckBaseType(i, fontParams)
CheckBaseType(i, fontParams, isIndex)
}
}
}
else -> {
CheckBaseType(item, fontParams)
CheckBaseType(item, fontParams, isIndex)
}
}
}

@Composable
private fun CheckBaseType(item: Any?, fontParams: FontParams) {
private fun CheckBaseType(item: Any?, fontParams: FontParams, isIndex: Boolean = false) {
when (item) {
is String -> {
Text(
Expand All @@ -180,7 +190,7 @@ class EquationItem(
)
}
is EquationItem -> {
item.Show(fontParams)
item.Show(fontParams, isIndex)
}
else -> {
Text(text = "", fontSize = fontParams.fontSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,73 @@ class SampleActivity : ComponentActivity() {
) {
EquationItem(
line = listOf(
"f(x) = a",
"g + ",
EquationItem(
line = "x",
superscript = "2"
sqrt = 2
)
),
underline = listOf(
"r",
EquationItem(
line = "y",
sqrt = 2
)
)
).Show()

EquationItem(
line = listOf(
"x = ",
EquationItem(
line = EquationItem(
line = "x",
superscript = "2",
subscript = "2"
),
underline = "y",
sqrt = 2
)
)
).Show()

EquationItem(
line =

EquationItem(
line = listOf(
EquationItem(
line = "x",
superscript = "2"
),
" + ",
EquationItem(
line = "4",
superscript = EquationItem(
line = "1 + 1234",
underline = "2",
sqrt = 2
)
)
),
sqrt = 2,
underline = "hello"

),
underline = EquationItem(
line = "some value",
sqrt = 2
)
).Show()


EquationItem(
line = listOf(
"f(x) =",
EquationItem(
line = "ax",
superscript = "2",
sqrt = 2
),
" + bx + c"
),
Expand Down

0 comments on commit fa5d354

Please sign in to comment.