forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 60
/
SkiaParagraph.skiko.kt
949 lines (848 loc) · 32.7 KB
/
SkiaParagraph.skiko.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.ui.text.platform
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.graphics.asSkiaPath
import androidx.compose.ui.graphics.isSpecified
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.graphics.toComposeRect
import androidx.compose.ui.text.AnnotatedString.Range
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.Paragraph
import androidx.compose.ui.text.ParagraphIntrinsics
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.ceilToInt
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontFamilyResolverImpl
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontSynthesis
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.font.SkiaFontLoader
import androidx.compose.ui.text.font.createFontFamilyResolver
import androidx.compose.ui.text.intl.LocaleList
import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.text.style.ResolvedTextDirection
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextGeometricTransform
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.isUnspecified
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.isSpecified
import androidx.compose.ui.unit.sp
import org.jetbrains.skia.Paint
import org.jetbrains.skia.paragraph.Alignment as SkAlignment
import org.jetbrains.skia.paragraph.BaselineMode
import org.jetbrains.skia.paragraph.Direction as SkDirection
import org.jetbrains.skia.paragraph.LineMetrics
import org.jetbrains.skia.paragraph.ParagraphBuilder
import org.jetbrains.skia.paragraph.ParagraphStyle
import org.jetbrains.skia.paragraph.PlaceholderAlignment
import org.jetbrains.skia.paragraph.PlaceholderStyle
import org.jetbrains.skia.paragraph.RectHeightMode
import org.jetbrains.skia.paragraph.RectWidthMode
import org.jetbrains.skia.paragraph.StrutStyle
import org.jetbrains.skia.paragraph.TextBox
import kotlin.math.floor
import org.jetbrains.skia.Rect as SkRect
import org.jetbrains.skia.paragraph.Paragraph as SkParagraph
import org.jetbrains.skia.paragraph.TextStyle as SkTextStyle
import org.jetbrains.skia.paragraph.TextIndent as SkTextIndent
import org.jetbrains.skia.FontStyle as SkFontStyle
import org.jetbrains.skia.Font as SkFont
import org.jetbrains.skia.paragraph.DecorationLineStyle as SkDecorationLineStyle
import org.jetbrains.skia.paragraph.DecorationStyle as SkDecorationStyle
import org.jetbrains.skia.paragraph.Shadow as SkShadow
private val DefaultFontSize = 16.sp
@Suppress("DEPRECATION")
@Deprecated(
"Font.ResourceLoader is deprecated, instead pass FontFamily.Resolver",
replaceWith = ReplaceWith("ActualParagraph(text, style, spanStyles, placeholders, " +
"maxLines, ellipsis, width, density, fontFamilyResolver)"),
)
internal actual fun ActualParagraph(
text: String,
style: TextStyle,
spanStyles: List<Range<SpanStyle>>,
placeholders: List<Range<Placeholder>>,
maxLines: Int,
ellipsis: Boolean,
width: Float,
density: Density,
@Suppress("DEPRECATION") resourceLoader: Font.ResourceLoader
): Paragraph = SkiaParagraph(
SkiaParagraphIntrinsics(
text,
style,
spanStyles,
placeholders,
density,
createFontFamilyResolver(resourceLoader)
),
maxLines,
ellipsis,
Constraints(maxWidth = width.ceilToInt())
)
internal actual fun ActualParagraph(
text: String,
style: TextStyle,
spanStyles: List<Range<SpanStyle>>,
placeholders: List<Range<Placeholder>>,
maxLines: Int,
ellipsis: Boolean,
constraints: Constraints,
density: Density,
fontFamilyResolver: FontFamily.Resolver
): Paragraph = SkiaParagraph(
SkiaParagraphIntrinsics(
text,
style,
spanStyles,
placeholders,
density,
fontFamilyResolver
),
maxLines,
ellipsis,
constraints
)
@Suppress("UNUSED_PARAMETER")
internal actual fun ActualParagraph(
paragraphIntrinsics: ParagraphIntrinsics,
maxLines: Int,
ellipsis: Boolean,
constraints: Constraints
): Paragraph = SkiaParagraph(
paragraphIntrinsics as SkiaParagraphIntrinsics,
maxLines,
ellipsis,
constraints
)
internal class SkiaParagraph(
intrinsics: ParagraphIntrinsics,
val maxLines: Int,
val ellipsis: Boolean,
val constraints: Constraints
) : Paragraph {
private val ellipsisChar = if (ellipsis) "\u2026" else ""
private val paragraphIntrinsics = intrinsics as SkiaParagraphIntrinsics
private val layouter = paragraphIntrinsics.layouter()
/**
* Paragraph isn't always immutable, it could be changed via [paint] method without
* rerunning layout
*/
private var para = layouter.layoutParagraph(
width = width,
maxLines = maxLines,
ellipsis = ellipsisChar
)
init {
para.layout(width)
}
private val text: String
get() = paragraphIntrinsics.text
override val width: Float
get() = constraints.maxWidth.toFloat()
override val height: Float
get() = para.height
override val minIntrinsicWidth: Float
get() = paragraphIntrinsics.minIntrinsicWidth
override val maxIntrinsicWidth: Float
get() = paragraphIntrinsics.maxIntrinsicWidth
override val firstBaseline: Float
get() = lineMetrics.firstOrNull()?.run { baseline.toFloat() } ?: 0f
override val lastBaseline: Float
get() = lineMetrics.lastOrNull()?.run { baseline.toFloat() } ?: 0f
override val didExceedMaxLines: Boolean
get() = para.didExceedMaxLines()
override val lineCount: Int
// workaround for https://bugs.chromium.org/p/skia/issues/detail?id=11321
get() = if (text == "") {
1
} else {
para.lineNumber.toInt()
}
override val placeholderRects: List<Rect?>
get() =
para.rectsForPlaceholders.map {
it.rect.toComposeRect()
}
override fun getPathForRange(start: Int, end: Int): Path {
val boxes = para.getRectsForRange(
start,
end,
RectHeightMode.MAX,
RectWidthMode.MAX
)
val path = Path()
for (b in boxes) {
path.asSkiaPath().addRect(b.rect)
}
return path
}
override fun getCursorRect(offset: Int): Rect {
val horizontal = getHorizontalPosition(offset, true)
val line = lineMetricsForOffset(offset)!!
return Rect(
horizontal,
(line.baseline - line.ascent).toFloat(),
horizontal,
(line.baseline + line.descent).toFloat()
)
}
override fun getLineLeft(lineIndex: Int): Float =
lineMetrics.getOrNull(lineIndex)?.left?.toFloat() ?: 0f
override fun getLineRight(lineIndex: Int): Float =
lineMetrics.getOrNull(lineIndex)?.right?.toFloat() ?: 0f
override fun getLineTop(lineIndex: Int) =
lineMetrics.getOrNull(lineIndex)?.let { line ->
floor((line.baseline - line.ascent).toFloat())
} ?: 0f
override fun getLineBottom(lineIndex: Int) =
lineMetrics.getOrNull(lineIndex)?.let { line ->
floor((line.baseline + line.descent).toFloat())
} ?: 0f
private fun lineMetricsForOffset(offset: Int): LineMetrics? {
val metrics = lineMetrics
for (line in metrics) {
if (offset < line.endIncludingNewline) {
return line
}
}
if (metrics.isEmpty()) {
return null
}
return metrics.last()
}
override fun getLineHeight(lineIndex: Int) = lineMetrics[lineIndex].height.toFloat()
override fun getLineWidth(lineIndex: Int) = lineMetrics[lineIndex].width.toFloat()
override fun getLineStart(lineIndex: Int) = lineMetrics[lineIndex].startIndex.toInt()
override fun getLineEnd(lineIndex: Int, visibleEnd: Boolean) =
if (visibleEnd) {
val metrics = lineMetrics[lineIndex]
// workarounds for https://bugs.chromium.org/p/skia/issues/detail?id=11321 :(
// we are waiting for fixes
if (lineIndex > 0 && metrics.startIndex < lineMetrics[lineIndex - 1].endIndex) {
metrics.endIndex.toInt()
} else if (
metrics.startIndex < text.length &&
text[metrics.startIndex.toInt()] == '\n'
) {
metrics.startIndex.toInt()
} else {
metrics.endExcludingWhitespaces.toInt()
}
} else {
lineMetrics[lineIndex].endIndex.toInt()
}
override fun isLineEllipsized(lineIndex: Int) = false
override fun getLineForOffset(offset: Int) =
lineMetricsForOffset(offset)?.run { lineNumber.toInt() }
?: 0
override fun getLineForVerticalPosition(vertical: Float): Int {
return 0
}
override fun getHorizontalPosition(offset: Int, usePrimaryDirection: Boolean): Float {
val prevBox = getBoxBackwardByOffset(offset)
val nextBox = getBoxForwardByOffset(offset)
return when {
prevBox == null && nextBox == null -> 0f
prevBox == null -> nextBox!!.cursorHorizontalPosition(true)
nextBox == null -> prevBox.cursorHorizontalPosition()
nextBox.direction == prevBox.direction -> nextBox.cursorHorizontalPosition(true)
// BiDi transition offset, we need to resolve ambiguity with usePrimaryDirection
// for details see comment for MultiParagraph.getHorizontalPosition
usePrimaryDirection -> prevBox.cursorHorizontalPosition()
else -> nextBox.cursorHorizontalPosition(true)
}
}
// workaround for https://bugs.chromium.org/p/skia/issues/detail?id=11321 :(
private val lineMetrics: Array<LineMetrics>
get() = if (text == "") {
val metrics = layouter.defaultFont.metrics
val ascent = -metrics.ascent.toDouble()
val descent = metrics.descent.toDouble()
val baseline = para.alphabeticBaseline.toDouble()
val height = with(layouter.paragraphStyle.strutStyle) {
if (isEnabled && !isHeightForced && isHeightOverridden && fontSize > 0.0f) {
(height * fontSize).toDouble()
} else {
ascent + descent
}
}
arrayOf(
LineMetrics(
0, 0, 0, 0, true,
ascent, descent, ascent, height, 0.0, 0.0, baseline, 0
)
)
} else {
@Suppress("UNCHECKED_CAST", "USELESS_CAST")
para.lineMetrics as Array<LineMetrics>
}
private fun getBoxForwardByOffset(offset: Int): TextBox? {
var to = offset + 1
while (to <= text.length) {
val box = para.getRectsForRange(
offset, to,
RectHeightMode.STRUT, RectWidthMode.TIGHT
).firstOrNull()
if (box != null) {
return box
}
to += 1
}
return null
}
private fun getBoxBackwardByOffset(offset: Int, end: Int = offset): TextBox? {
var from = offset - 1
while (from >= 0) {
val box = para.getRectsForRange(
from, end,
RectHeightMode.STRUT, RectWidthMode.TIGHT
).firstOrNull()
when {
(box == null) -> from -= 1
(text.get(from) == '\n') -> {
val bottom = box.rect.bottom + box.rect.bottom - box.rect.top
val rect = SkRect(0f, box.rect.bottom, 0f, bottom)
return TextBox(rect, box.direction)
}
else -> return box
}
}
return null
}
override fun getParagraphDirection(offset: Int): ResolvedTextDirection =
paragraphIntrinsics.textDirection
override fun getBidiRunDirection(offset: Int): ResolvedTextDirection =
when (getBoxForwardByOffset(offset)?.direction) {
org.jetbrains.skia.paragraph.Direction.RTL -> ResolvedTextDirection.Rtl
org.jetbrains.skia.paragraph.Direction.LTR -> ResolvedTextDirection.Ltr
null -> ResolvedTextDirection.Ltr
}
override fun getOffsetForPosition(position: Offset): Int {
return para.getGlyphPositionAtCoordinate(position.x, position.y).position
}
override fun getBoundingBox(offset: Int): Rect {
val box = getBoxForwardByOffset(offset) ?: getBoxBackwardByOffset(offset, text.length)!!
return box.rect.toComposeRect()
}
override fun getWordBoundary(offset: Int): TextRange {
return when {
(text[offset].isLetterOrDigit()) -> para.getWordBoundary(offset).let {
TextRange(it.start, it.end)
}
(text.getOrNull(offset - 1)?.isLetterOrDigit() ?: false) ->
para.getWordBoundary(offset - 1).let {
TextRange(it.start, it.end)
}
else -> TextRange(offset, offset)
}
}
// TODO(b/229518449): Implement an alternative to paint function that takes a brush.
override fun paint(
canvas: Canvas,
color: Color,
shadow: Shadow?,
textDecoration: TextDecoration?
) {
para = layouter.layoutParagraph(
width = width,
maxLines = maxLines,
ellipsis = ellipsisChar,
color = color,
shadow = shadow,
textDecoration = textDecoration
)
para.paint(canvas.nativeCanvas, 0.0f, 0.0f)
}
}
private fun fontSizeInHierarchy(density: Density, base: Float, other: TextUnit): Float {
return when {
other.isUnspecified -> base
other.isEm -> base * other.value
other.isSp -> with(density) { other.toPx() }
else -> error("Unexpected size in fontSizeInHierarchy")
}
}
// Computed ComputedStyles always have font/letter size in pixels for particular `density`.
// It's important because density could be changed in runtime and it should force
// SkTextStyle to be recalculated. Or we can have different densities in different windows.
internal data class ComputedStyle(
var color: Color,
var fontSize: Float,
var fontWeight: FontWeight?,
var fontStyle: FontStyle?,
var fontSynthesis: FontSynthesis?,
var fontFamily: FontFamily?,
var fontFeatureSettings: String?,
var letterSpacing: Float?,
var baselineShift: BaselineShift?,
var textGeometricTransform: TextGeometricTransform?,
var localeList: LocaleList?,
var background: Color = Color.Unspecified,
var textDecoration: TextDecoration?,
var shadow: Shadow?
) {
constructor(density: Density, spanStyle: SpanStyle) : this(
color = spanStyle.color,
fontSize = with(density) { spanStyle.fontSize.toPx() },
fontWeight = spanStyle.fontWeight,
fontStyle = spanStyle.fontStyle,
fontSynthesis = spanStyle.fontSynthesis,
fontFamily = spanStyle.fontFamily,
fontFeatureSettings = spanStyle.fontFeatureSettings,
letterSpacing = if (spanStyle.letterSpacing.isUnspecified) {
null
} else {
with(density) {
spanStyle.letterSpacing.toPx()
}
},
baselineShift = spanStyle.baselineShift,
textGeometricTransform = spanStyle.textGeometricTransform,
localeList = spanStyle.localeList,
background = spanStyle.background,
textDecoration = spanStyle.textDecoration,
shadow = spanStyle.shadow
)
@OptIn(ExperimentalTextApi::class)
fun toSkTextStyle(fontFamilyResolver: FontFamily.Resolver): SkTextStyle {
val res = SkTextStyle()
if (color != Color.Unspecified) {
res.color = color.toArgb()
}
fontStyle?.let {
res.fontStyle = it.toSkFontStyle()
}
textDecoration?.let {
res.decorationStyle = it.toSkDecorationStyle(this.color)
}
if (background != Color.Unspecified) {
res.background = Paint().also {
it.color = background.toArgb()
}
}
fontWeight?.let {
res.fontStyle = res.fontStyle.withWeight(it.weight)
}
shadow?.let {
res.addShadow(it.toSkShadow())
}
letterSpacing?.let {
res.letterSpacing = it
}
res.fontSize = fontSize
fontFamily?.let {
@Suppress("UNCHECKED_CAST")
val resolved = fontFamilyResolver.resolve(
it,
fontWeight ?: FontWeight.Normal,
fontStyle ?: FontStyle.Normal,
fontSynthesis ?: FontSynthesis.None
).value as FontLoadResult
res.fontFamilies = resolved.aliases.toTypedArray()
}
baselineShift?.let {
val fontMetrics = res.fontMetrics
res.baselineShift = it.multiplier * fontMetrics.ascent
}
return res
}
fun merge(density: Density, other: SpanStyle) {
val fontSize = fontSizeInHierarchy(density, fontSize, other.fontSize)
if (other.color.isSpecified) {
color = other.color
}
other.fontFamily?.let { fontFamily = it }
this.fontSize = fontSize
other.fontWeight?.let { fontWeight = it }
other.fontStyle?.let { fontStyle = it }
other.fontSynthesis?.let { fontSynthesis = it }
other.fontFeatureSettings?.let { fontFeatureSettings = it }
if (!other.letterSpacing.isUnspecified) {
when {
other.letterSpacing.isEm ->
letterSpacing = fontSize * other.letterSpacing.value
other.letterSpacing.isSp ->
letterSpacing = with(density) {
other.letterSpacing.toPx()
}
else -> throw UnsupportedOperationException()
}
}
other.baselineShift?.let { baselineShift = it }
other.textGeometricTransform?.let { textGeometricTransform = it }
other.localeList?.let { localeList = it }
if (other.background.isSpecified) {
background = other.background
}
other.textDecoration?.let { textDecoration = it }
other.shadow?.let { shadow = it }
}
}
internal expect class WeakHashMap<K, V> : MutableMap<K, V>
// Building of SkTextStyle is a relatively expensive operation. We enable simple caching by
// mapping SpanStyle to SkTextStyle. To increase the efficiency of this mapping we are making
// most of the computations before converting Compose paragraph styles to Skia paragraph
private val skTextStylesCache = WeakHashMap<ComputedStyle, SkTextStyle>()
internal class ParagraphBuilder(
val fontFamilyResolver: FontFamily.Resolver,
val text: String,
var textStyle: TextStyle,
var ellipsis: String = "",
var maxLines: Int = Int.MAX_VALUE,
val spanStyles: List<Range<SpanStyle>>,
val placeholders: List<Range<Placeholder>>,
val density: Density,
val textDirection: ResolvedTextDirection
) {
private lateinit var initialStyle: SpanStyle
private lateinit var defaultStyle: ComputedStyle
internal lateinit var paragraphStyle: ParagraphStyle
private set
private lateinit var ops: List<Op>
/**
* SkParagraph styles model doesn't match Compose's one.
* SkParagraph has only a stack-based push/pop styles interface that works great with Span
* trees.
* But in Compose we have a list of SpanStyles attached to arbitrary ranges, possibly
* overlapped, where a position in the list denotes style's priority
* We map Compose styles to SkParagraph styles by projecting every range start/end to single
* positions line and maintaining a list of active styles while building a paragraph. This list
* of active styles is being compiled into single SkParagraph's style for every chunk of text
*/
@OptIn(ExperimentalTextApi::class)
fun build(): SkParagraph {
initialStyle = textStyle.toSpanStyle().withDefaultFontSize()
defaultStyle = ComputedStyle(density, initialStyle)
ops = makeOps(
spanStyles,
placeholders
)
var pos = 0
val ps = textStyleToParagraphStyle(textStyle, defaultStyle)
paragraphStyle = ps
if (maxLines != Int.MAX_VALUE) {
ps.maxLinesCount = maxLines
ps.ellipsis = ellipsis
}
// this downcast is always safe because of sealed types and we control construction
@OptIn(ExperimentalTextApi::class)
val platformFontLoader = (fontFamilyResolver as FontFamilyResolverImpl).platformFontLoader
val fontCollection = when (platformFontLoader) {
is SkiaFontLoader -> platformFontLoader.fontCollection
else -> throw IllegalStateException("Unsupported font loader $platformFontLoader")
}
val pb = ParagraphBuilder(ps, fontCollection)
var addText = true
for (op in ops) {
if (addText && pos < op.position) {
pb.addText(text.subSequence(pos, op.position).toString())
}
when (op) {
is Op.StyleAdd -> {
// FontLoader may have changed, so ensure that Font resolution is still valid
fontFamilyResolver.resolve(
op.style.fontFamily,
op.style.fontWeight ?: FontWeight.Normal,
op.style.fontStyle ?: FontStyle.Normal,
op.style.fontSynthesis ?: FontSynthesis.All
)
pb.pushStyle(makeSkTextStyle(op.style))
}
is Op.PutPlaceholder -> {
val placeholderStyle =
PlaceholderStyle(
op.width,
op.height,
op.cut.placeholder.placeholderVerticalAlign
.toSkPlaceholderAlignment(),
// TODO: figure out how exactly we have to work with BaselineMode & offset
BaselineMode.ALPHABETIC,
0f
)
pb.addPlaceholder(placeholderStyle)
addText = false
}
is Op.EndPlaceholder -> {
addText = true
}
}
pos = op.position
}
if (addText && pos < text.length) {
pb.addText(text.subSequence(pos, text.length).toString())
}
return pb.build()
}
private sealed class Op {
abstract val position: Int
data class StyleAdd(
override val position: Int,
val style: ComputedStyle
) : Op()
data class PutPlaceholder(
val cut: Cut.PutPlaceholder,
var width: Float,
var height: Float
) : Op() {
override val position: Int by cut::position
}
data class EndPlaceholder(
val cut: Cut.EndPlaceholder
) : Op() {
override val position: Int by cut::position
}
}
private sealed class Cut {
abstract val position: Int
data class StyleAdd(
override val position: Int,
val style: SpanStyle
) : Cut()
data class StyleRemove(
override val position: Int,
val style: SpanStyle
) : Cut()
data class PutPlaceholder(
override val position: Int,
val placeholder: Placeholder,
) : Cut()
data class EndPlaceholder(override val position: Int) : Cut()
}
private fun makeOps(
spans: List<Range<SpanStyle>>,
placeholders: List<Range<Placeholder>>
): List<Op> {
val cuts = mutableListOf<Cut>()
for (span in spans) {
cuts.add(Cut.StyleAdd(span.start, span.item))
cuts.add(Cut.StyleRemove(span.end, span.item))
}
for (placeholder in placeholders) {
cuts.add(Cut.PutPlaceholder(placeholder.start, placeholder.item))
cuts.add(Cut.EndPlaceholder(placeholder.end))
}
val ops = mutableListOf<Op>(Op.StyleAdd(0, defaultStyle))
cuts.sortBy { it.position }
val activeStyles = mutableListOf(initialStyle)
for (cut in cuts) {
when {
cut is Cut.StyleAdd -> {
activeStyles.add(cut.style)
val prev = previousStyleAddAtTheSamePosition(cut.position, ops)
if (prev == null) {
ops.add(
Op.StyleAdd(
cut.position,
mergeStyles(activeStyles).also { it.merge(density, cut.style) }
)
)
} else {
prev.style.merge(density, cut.style)
}
}
cut is Cut.StyleRemove -> {
activeStyles.remove(cut.style)
ops.add(Op.StyleAdd(cut.position, mergeStyles(activeStyles)))
}
cut is Cut.PutPlaceholder -> {
val currentStyle = mergeStyles(activeStyles)
val op = Op.PutPlaceholder(
cut = cut,
width = fontSizeInHierarchy(
density,
currentStyle.fontSize,
cut.placeholder.width
),
height = fontSizeInHierarchy(
density,
currentStyle.fontSize,
cut.placeholder.height
),
)
ops.add(op)
}
cut is Cut.EndPlaceholder ->
ops.add(Op.EndPlaceholder(cut))
}
}
return ops
}
private fun mergeStyles(activeStyles: List<SpanStyle>): ComputedStyle {
// there is always at least one active style
val style = ComputedStyle(density, activeStyles[0])
for (i in 1 until activeStyles.size) {
style.merge(density, activeStyles[i])
}
return style
}
private fun previousStyleAddAtTheSamePosition(position: Int, ops: List<Op>): Op.StyleAdd? {
for (prevOp in ops.asReversed()) {
if (prevOp.position < position) return null
if (prevOp is Op.StyleAdd) return prevOp
}
return null
}
private fun textStyleToParagraphStyle(
style: TextStyle,
computedStyle: ComputedStyle
): ParagraphStyle {
val pStyle = ParagraphStyle()
pStyle.textStyle = makeSkTextStyle(computedStyle)
style.textAlign?.let {
pStyle.alignment = it.toSkAlignment()
}
if (style.lineHeight.isSpecified) {
val strutStyle = StrutStyle()
strutStyle.isEnabled = true
strutStyle.isHeightOverridden = true
val fontSize = with(density) {
style.fontSize.orDefaultFontSize().toPx()
}
val lineHeight = when {
style.lineHeight.isSp -> with(density) {
style.lineHeight.toPx()
}
style.lineHeight.isEm -> fontSize * style.lineHeight.value
else -> error("Unexpected size in textStyleToParagraphStyle")
}
strutStyle.height = lineHeight / fontSize
strutStyle.fontSize = fontSize
pStyle.strutStyle = strutStyle
}
pStyle.direction = textDirection.toSkDirection()
textStyle.textIndent?.run {
with(density) {
pStyle.textIndent = SkTextIndent(firstLine.toPx(), restLine.toPx())
}
}
return pStyle
}
private fun makeSkTextStyle(style: ComputedStyle): SkTextStyle {
return skTextStylesCache.getOrPut(style) {
style.toSkTextStyle(fontFamilyResolver)
}
}
@OptIn(ExperimentalTextApi::class)
internal val defaultFont by lazy {
val loadResult = textStyle.fontFamily?.let {
@Suppress("UNCHECKED_CAST")
fontFamilyResolver.resolve(
it,
textStyle.fontWeight ?: FontWeight.Normal,
textStyle.fontStyle ?: FontStyle.Normal,
textStyle.fontSynthesis ?: FontSynthesis.All
).value as FontLoadResult
}
SkFont(loadResult?.typeface, defaultStyle.fontSize)
}
internal val defaultHeight by lazy {
defaultFont.metrics.height
}
}
private fun TextUnit.orDefaultFontSize() = when {
isUnspecified -> DefaultFontSize
isEm -> DefaultFontSize * value
else -> this
}
private fun SpanStyle.withDefaultFontSize(): SpanStyle {
val fontSize = this.fontSize.orDefaultFontSize()
val letterSpacing = when {
this.letterSpacing.isEm -> fontSize * this.letterSpacing.value
else -> this.letterSpacing
}
return this.copy(
fontSize = fontSize,
letterSpacing = letterSpacing
)
}
fun FontStyle.toSkFontStyle(): SkFontStyle {
return when (this) {
FontStyle.Italic -> org.jetbrains.skia.FontStyle.ITALIC
else -> org.jetbrains.skia.FontStyle.NORMAL
}
}
fun TextDecoration.toSkDecorationStyle(color: Color): SkDecorationStyle {
val underline = contains(TextDecoration.Underline)
val overline = false
val lineThrough = contains(TextDecoration.LineThrough)
val gaps = false
val lineStyle = SkDecorationLineStyle.SOLID
val thicknessMultiplier = 1f
return SkDecorationStyle(
underline,
overline,
lineThrough,
gaps,
color.toArgb(),
lineStyle,
thicknessMultiplier
)
}
fun PlaceholderVerticalAlign.toSkPlaceholderAlignment(): PlaceholderAlignment {
return when (this) {
PlaceholderVerticalAlign.AboveBaseline -> PlaceholderAlignment.ABOVE_BASELINE
PlaceholderVerticalAlign.TextTop -> PlaceholderAlignment.TOP
PlaceholderVerticalAlign.TextBottom -> PlaceholderAlignment.BOTTOM
PlaceholderVerticalAlign.TextCenter -> PlaceholderAlignment.MIDDLE
// TODO: figure out how we have to handle it properly
PlaceholderVerticalAlign.Top -> PlaceholderAlignment.TOP
PlaceholderVerticalAlign.Bottom -> PlaceholderAlignment.BOTTOM
PlaceholderVerticalAlign.Center -> PlaceholderAlignment.MIDDLE
else -> error("Invalid PlaceholderVerticalAlign.")
}
}
internal fun Shadow.toSkShadow(): SkShadow {
return SkShadow(color.toArgb(), offset.x, offset.y, blurRadius.toDouble())
}
internal fun TextAlign.toSkAlignment(): SkAlignment {
return when (this) {
TextAlign.Left -> SkAlignment.LEFT
TextAlign.Right -> SkAlignment.RIGHT
TextAlign.Center -> SkAlignment.CENTER
TextAlign.Justify -> SkAlignment.JUSTIFY
TextAlign.Start -> SkAlignment.START
TextAlign.End -> SkAlignment.END
else -> error("Invalid TextAlign")
}
}
internal fun ResolvedTextDirection.toSkDirection(): SkDirection {
return when (this) {
ResolvedTextDirection.Ltr -> SkDirection.LTR
ResolvedTextDirection.Rtl -> SkDirection.RTL
}
}
internal fun TextBox.cursorHorizontalPosition(opposite: Boolean = false): Float {
return when (direction) {
SkDirection.LTR -> if (opposite) rect.left else rect.right
SkDirection.RTL -> if (opposite) rect.right else rect.left
}
}