Skip to content

Fixing texts alignments in RTL (Arabic, Persian, ...) #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/main/java/com/alamkanak/weekview/CanvasExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ internal fun Canvas.draw(staticLayout: StaticLayout) {
staticLayout.draw(this)
}

internal fun Canvas.drawRtl(staticLayout: StaticLayout) {
staticLayout.text.split("\n").forEachIndexed { index, text ->
val textPaint = staticLayout.paint
val xPos: Int = staticLayout.width / 2
drawText(
text,
xPos.toFloat(),
((staticLayout.height / 2) * (index + 1) - (textPaint.descent() + textPaint.ascent()) / 2),
textPaint
)
}
}

internal fun Canvas.drawVerticalLine(
horizontalOffset: Float,
startY: Float,
Expand Down
6 changes: 1 addition & 5 deletions core/src/main/java/com/alamkanak/weekview/EventChipDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ internal class EventChipDrawer(
) {
val bounds = eventChip.bounds

val horizontalOffset = if (viewState.isLtr) {
bounds.left + viewState.eventPaddingHorizontal
} else {
bounds.right - viewState.eventPaddingHorizontal
}
val horizontalOffset = bounds.left + viewState.eventPaddingHorizontal

val verticalOffset = if (eventChip.event.isAllDay) {
(bounds.height() - textLayout.height) / 2f
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/java/com/alamkanak/weekview/HeaderRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,16 @@ private class DateLabelsDrawer(
val key = date.toEpochDays()
val textLayout = dateLabelLayouts[key]

val offset = if (textLayout.text.toString().isArabic()) 0f else viewState.dayWidth / 2f
withTranslation(
x = startPixel + viewState.dayWidth / 2f,
x = startPixel + offset,
y = viewState.headerPadding,
) {
draw(textLayout)
if (textLayout.text.toString().isArabic()) {
drawRtl(textLayout)
} else {
draw(textLayout)
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/com/alamkanak/weekview/TextExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ internal fun CharSequence.semibold() = SpannableString(this).apply {

internal fun ViewState.getTextPaint(event: ResolvedWeekViewEntity): TextPaint {
val textPaint = TextPaint(if (event.isAllDay) allDayEventTextPaint else eventTextPaint)
textPaint.textAlign = if (isLtr) Paint.Align.LEFT else Paint.Align.RIGHT
textPaint.textAlign = Paint.Align.LEFT

val textColor = event.style.textColor
if (textColor != null) {
textPaint.color = textColor
}
return textPaint
}

internal fun String.isArabic(): Boolean {
var i = 0
while (i < this.length) {
val c = this.codePointAt(i)
if (c in 0x0600..0x06E0) return true
i += Character.charCount(c)
}
return false
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alamkanak.weekview

import android.graphics.Canvas
import android.text.Layout
import android.text.StaticLayout
import android.util.SparseArray

Expand Down Expand Up @@ -89,7 +90,8 @@ internal class TimeColumnRenderer(
val textLayouts = mutableListOf<StaticLayout>()

for (hour in displayedHours) {
val textLayout = timeFormatter(hour).toTextLayout(timeColumnTextPaint, width = Int.MAX_VALUE)
val alignment = if (timeFormatter(hour).isArabic()) Layout.Alignment.ALIGN_OPPOSITE else Layout.Alignment.ALIGN_NORMAL
val textLayout = timeFormatter(hour).toTextLayout(timeColumnTextPaint, width = Int.MAX_VALUE, alignment = alignment)
textLayouts += textLayout
timeLabelLayouts.put(hour, textLayout)
}
Expand Down