@@ -87,6 +87,75 @@ function initializeClickableSpan(): void {
8787 ClickableSpan = ClickableSpanImpl ;
8888}
8989
90+ interface BaselineAdjustedSpan {
91+ new ( fontSize : number , align ?: VerticalAlignment ) : android . text . style . MetricAffectingSpan ;
92+ }
93+
94+ let BaselineAdjustedSpan : BaselineAdjustedSpan ;
95+
96+ function initializeBaselineAdjustedSpan ( ) : void {
97+ if ( BaselineAdjustedSpan ) {
98+ return ;
99+ }
100+ class BaselineAdjustedSpanImpl extends android . text . style . MetricAffectingSpan {
101+ fontSize : number ;
102+ align : VerticalAlignment = "baseline" ;
103+
104+ constructor ( fontSize : number , align ?: VerticalAlignment ) {
105+ super ( ) ;
106+
107+ this . align = align ;
108+ this . fontSize = fontSize ;
109+ }
110+
111+ updateDrawState ( paint : android . text . TextPaint ) {
112+ this . updateState ( paint ) ;
113+ }
114+
115+ updateMeasureState ( paint : android . text . TextPaint ) {
116+ this . updateState ( paint ) ;
117+ }
118+
119+ updateState ( paint : android . text . TextPaint ) {
120+ const metrics = paint . getFontMetrics ( ) ;
121+
122+ if ( ! this . align || [ "baseline" , "stretch" ] . includes ( this . align ) ) {
123+ return ;
124+ }
125+
126+ if ( this . align === "top" ) {
127+ return paint . baselineShift = - this . fontSize - metrics . bottom - metrics . top ;
128+ }
129+
130+ if ( this . align === "bottom" ) {
131+ return paint . baselineShift = metrics . bottom ;
132+ }
133+
134+ if ( this . align === "text-top" ) {
135+ return paint . baselineShift = - this . fontSize - metrics . descent - metrics . ascent ;
136+ }
137+
138+ if ( this . align === "text-bottom" ) {
139+ return paint . baselineShift = metrics . bottom - metrics . descent ;
140+ }
141+
142+ if ( this . align === "middle" ) {
143+ return paint . baselineShift = ( metrics . descent - metrics . ascent ) / 2 - metrics . descent ;
144+ }
145+
146+ if ( this . align === "super" ) {
147+ return paint . baselineShift = - this . fontSize * .4 ;
148+ }
149+
150+ if ( this . align === "sub" ) {
151+ return paint . baselineShift = ( metrics . descent - metrics . ascent ) * .4 ;
152+ }
153+ }
154+ }
155+
156+ BaselineAdjustedSpan = BaselineAdjustedSpanImpl ;
157+ }
158+
90159export class TextBase extends TextBaseCommon {
91160 nativeViewProtected : android . widget . TextView ;
92161 nativeTextViewProtected : android . widget . TextView ;
@@ -442,62 +511,6 @@ function createSpannableStringBuilder(formattedString: FormattedString, defaultF
442511 return ssb ;
443512}
444513
445- class BaselineAdjustedSpan extends android . text . style . MetricAffectingSpan {
446- fontSize : number ;
447- align : VerticalAlignment = "baseline" ;
448-
449- constructor ( fontSize : number , align ?: VerticalAlignment ) {
450- super ( ) ;
451-
452- this . align = align ;
453- this . fontSize = fontSize ;
454- }
455-
456- updateDrawState ( paint : android . text . TextPaint ) {
457- this . updateState ( paint ) ;
458- }
459-
460- updateMeasureState ( paint : android . text . TextPaint ) {
461- this . updateState ( paint ) ;
462- }
463-
464- updateState ( paint : android . text . TextPaint ) {
465- const metrics = paint . getFontMetrics ( ) ;
466-
467- if ( ! this . align || [ "baseline" , "stretch" ] . includes ( this . align ) ) {
468- return ;
469- }
470-
471- if ( this . align === "top" ) {
472- return paint . baselineShift = - this . fontSize - metrics . bottom - metrics . top ;
473- }
474-
475- if ( this . align === "bottom" ) {
476- return paint . baselineShift = metrics . bottom ;
477- }
478-
479- if ( this . align === "text-top" ) {
480- return paint . baselineShift = - this . fontSize - metrics . descent - metrics . ascent ;
481- }
482-
483- if ( this . align === "text-bottom" ) {
484- return paint . baselineShift = metrics . bottom - metrics . descent ;
485- }
486-
487- if ( this . align === "middle" ) {
488- return paint . baselineShift = ( metrics . descent - metrics . ascent ) / 2 - metrics . descent ;
489- }
490-
491- if ( this . align === "super" ) {
492- return paint . baselineShift = - this . fontSize * .4 ;
493- }
494-
495- if ( this . align === "sub" ) {
496- return paint . baselineShift = ( metrics . descent - metrics . ascent ) * .4 ;
497- }
498- }
499- }
500-
501514function setSpanModifiers ( ssb : android . text . SpannableStringBuilder , span : Span , start : number , end : number , defaultFontSize : number ) : void {
502515 const spanStyle = span . style ;
503516 const bold = isBold ( spanStyle . fontWeight ) ;
@@ -553,7 +566,8 @@ function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span,
553566 }
554567
555568 if ( align ) {
556- ssb . setSpan ( new BaselineAdjustedSpan ( defaultFontSize * layout . getDisplayDensity ( ) , align ) , start , end , android . text . Spanned . SPAN_EXCLUSIVE_EXCLUSIVE ) ;
569+ initializeBaselineAdjustedSpan ( ) ;
570+ ssb . setSpan ( new BaselineAdjustedSpan ( defaultFontSize * layout . getDisplayDensity ( ) , align ) , start , end , android . text . Spanned . SPAN_EXCLUSIVE_EXCLUSIVE ) ;
557571 }
558572
559573 const tappable = span . tappable ;
0 commit comments