react-native-advanced-text 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,7 @@ import android.text.Selection
26
26
  import android.graphics.Typeface
27
27
  import androidx.core.text.getSpans
28
28
  import com.facebook.react.common.assets.ReactFontManager
29
+ import kotlin.math.ceil
29
30
 
30
31
  class AdvancedTextView : TextView {
31
32
 
@@ -294,7 +295,7 @@ class AdvancedTextView : TextView {
294
295
  }
295
296
 
296
297
 
297
- setLineSpacing(0f, lineHeightMultiplier)
298
+ applyLineHeight(spannableString)
298
299
 
299
300
  // Android's letterSpacing is in em; the prop is in dp/points. The ratio
300
301
  // dp / fontSize is scale-invariant, so no density conversion is needed.
@@ -311,6 +312,41 @@ class AdvancedTextView : TextView {
311
312
  }
312
313
  }
313
314
 
315
+ /**
316
+ * Applies `lineHeightMultiplier` as a CSS-style, exact-per-line-height span
317
+ * instead of `setLineSpacing(0, multiplier)`.
318
+ *
319
+ * The Fabric ShadowNode measures `lineHeight` by asking React Native's own
320
+ * TextLayoutManager to lay out a fragment with that lineHeight -- which RN
321
+ * implements with its own CSS-style line-height span (every line's box is
322
+ * forced to exactly `lineHeight`, ascent/descent redistributed evenly).
323
+ * That is a *different* algorithm from `setLineSpacing`, which scales the
324
+ * font's natural per-line advance instead; the two do not produce the same
325
+ * total height, and the gap between them is added on every wrapped line,
326
+ * so it grows with text length. Using the same CSS-style algorithm here
327
+ * (see CssLineHeightSpan) keeps this view's rendered height identical to
328
+ * what was measured, regardless of how many lines the text wraps onto.
329
+ */
330
+ private fun applyLineHeight(spannableString: SpannableString) {
331
+ if (lineHeightMultiplier == 1.0f || spannableString.isEmpty()) {
332
+ return
333
+ }
334
+
335
+ val metrics = paint.fontMetricsInt
336
+ val naturalLineHeight = -metrics.ascent + metrics.descent
337
+ if (naturalLineHeight <= 0) {
338
+ return
339
+ }
340
+
341
+ val targetLineHeight = ceil(lineHeightMultiplier * naturalLineHeight).toInt()
342
+ spannableString.setSpan(
343
+ CssLineHeightSpan(targetLineHeight),
344
+ 0,
345
+ spannableString.length,
346
+ Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
347
+ )
348
+ }
349
+
314
350
  override fun onDraw(canvas: Canvas) {
315
351
  drawHighlightedWordBackgrounds(canvas)
316
352
  super.onDraw(canvas)
@@ -0,0 +1,47 @@
1
+ package com.advancedtext
2
+
3
+ import android.graphics.Paint
4
+ import android.text.style.LineHeightSpan
5
+ import kotlin.math.ceil
6
+ import kotlin.math.floor
7
+
8
+ /**
9
+ * A [LineHeightSpan] that forces every line's box to exactly [lineHeightPx],
10
+ * following CSS `line-height` semantics: the difference between the target
11
+ * height and the font's natural ascent+descent ("leading") is split evenly
12
+ * above and below, and the very first line's top / very last line's bottom
13
+ * are pinned back to the font's own ascent/descent so the half-leading at
14
+ * the block's own edges doesn't inflate its total height.
15
+ *
16
+ * This exists because `AdvancedTextView`'s Fabric ShadowNode measures the
17
+ * `lineHeight` prop through React Native's `TextLayoutManager`, which applies
18
+ * `lineHeight` using this exact CSS-style algorithm internally -- a different
19
+ * one from Android's own `TextView.setLineSpacing(0, multiplier)`. Rendering
20
+ * with `setLineSpacing` instead of this span made the real height diverge
21
+ * from the measured one by a per-line amount, which is why the discrepancy
22
+ * grew with the number of wrapped lines.
23
+ *
24
+ * See: https://www.w3.org/TR/css-inline-3/#inline-height
25
+ */
26
+ internal class CssLineHeightSpan(private val lineHeightPx: Int) : LineHeightSpan {
27
+
28
+ override fun chooseHeight(
29
+ text: CharSequence,
30
+ start: Int,
31
+ end: Int,
32
+ spanstartv: Int,
33
+ v: Int,
34
+ fm: Paint.FontMetricsInt
35
+ ) {
36
+ val leading = lineHeightPx - ((-fm.ascent) + fm.descent)
37
+ fm.ascent -= ceil(leading / 2.0f).toInt()
38
+ fm.descent += floor(leading / 2.0f).toInt()
39
+
40
+ if (start == 0) {
41
+ fm.top = fm.ascent
42
+ }
43
+ if (end == text.length) {
44
+ fm.bottom = fm.descent
45
+ }
46
+ }
47
+ }
@@ -52,8 +52,7 @@ Float AdvancedTextViewShadowNode::measureNaturalSingleLineHeight(
52
52
  const LayoutContext& layoutContext,
53
53
  const TextAttributes& baseTextAttributes) const {
54
54
  // Same font/size/weight/style as the real fragment, but with `lineHeight`
55
- // cleared so this reports the font's own, unscaled single-line height --
56
- // exactly what Android's `setLineSpacing(0, multiplier)` multiplies.
55
+ // cleared so this reports the font's own, unscaled single-line height.
57
56
  auto probeAttributes = baseTextAttributes;
58
57
  probeAttributes.lineHeight = std::numeric_limits<Float>::quiet_NaN();
59
58
 
@@ -65,6 +64,14 @@ Float AdvancedTextViewShadowNode::measureNaturalSingleLineHeight(
65
64
  });
66
65
 
67
66
  auto paragraphAttributes = ParagraphAttributes{};
67
+ // Measure the tight ascent+descent box (no extra top/bottom accent
68
+ // padding). This is the same "natural line height" basis
69
+ // CssLineHeightSpan.kt derives from `Paint.getFontMetricsInt()` on the
70
+ // view side (which is unaffected by `includeFontPadding`, a Layout-level
71
+ // setting) -- measuring with padding included here would double count it
72
+ // once per rendered line once the resulting value is multiplied and
73
+ // applied to the whole paragraph.
74
+ paragraphAttributes.includeFontPadding = false;
68
75
 
69
76
  auto textLayoutContext = TextLayoutContext{};
70
77
  textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
@@ -114,22 +121,22 @@ AttributedString AdvancedTextViewShadowNode::getAttributedString(
114
121
  }
115
122
 
116
123
  // The component treats `lineHeight` as a multiple of the font's own
117
- // natural line height (see setLineSpacing(0, multiplier) in
118
- // AdvancedTextView.kt / the NSParagraphStyle-based spacing in
119
- // AdvancedTextView.mm) -- NOT the absolute per-line value
120
- // `TextAttributes.lineHeight` expects. A multiplier of 1 *is* the natural
121
- // line height, so there is nothing to override in that case (and
122
- // overriding it with an approximation, as this used to do unconditionally,
123
- // made even the "no lineHeight override" case measure taller or shorter
124
- // than the real render).
124
+ // natural line height (see CssLineHeightSpan.kt on Android / the
125
+ // NSParagraphStyle-based spacing in AdvancedTextView.mm on iOS) -- NOT the
126
+ // absolute per-line value `TextAttributes.lineHeight` expects. A multiplier
127
+ // of 1 *is* the natural line height, so there is nothing to override in
128
+ // that case (overriding it with an approximation, as this used to do
129
+ // unconditionally, made even the "no lineHeight override" case measure
130
+ // taller or shorter than the real render).
125
131
  if (props.lineHeight > 0 && props.lineHeight != 1.0) {
126
132
  #if defined(ANDROID)
127
133
  // Android's natural single-line height depends on the resolved
128
134
  // font/size/weight and isn't a fixed ratio of fontSize, so a constant
129
- // approximation drifts from AdvancedTextView.kt's real
130
- // setLineSpacing()-based layout by an amount that compounds with every
131
- // wrapped line -- which is why the error scaled with paragraph length.
132
- // Measure the real natural height for these exact attributes instead.
135
+ // approximation drifts from the real value CssLineHeightSpan.kt derives
136
+ // from Paint.getFontMetricsInt() -- by an amount that compounds with
137
+ // every wrapped line, which is why the error scaled with paragraph
138
+ // length. Measure the real natural height for these exact attributes
139
+ // instead, the same way the view derives it.
133
140
  auto naturalLineHeight =
134
141
  measureNaturalSingleLineHeight(layoutContext, textAttributes);
135
142
  textAttributes.lineHeight = naturalLineHeight > 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-advanced-text",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": " Advanced text component for React Native with custom select options.",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",