Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
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
54 changes: 30 additions & 24 deletions lib/web_ui/lib/src/engine/text/measurement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ bool _newlinePredicate(int char) {
prop == LineCharProperty.CR;
}

/// Manages [ParagraphRuler] instances and caches them per unique
/// [ParagraphGeometricStyle].
///
/// All instances of [ParagraphRuler] should be created through this class.
class RulerManager {
RulerManager({required this.rulerCacheCapacity}) {
/// Hosts ruler DOM elements in a hidden container.
class RulerHost {
RulerHost() {
_rulerHost.style
..position = 'fixed'
..visibility = 'hidden'
Expand All @@ -41,8 +38,6 @@ class RulerManager {
registerHotRestartListener(dispose);
}

final int rulerCacheCapacity;

/// Hosts a cache of rulers that measure text.
///
/// This element exists purely for organizational purposes. Otherwise the
Expand All @@ -51,6 +46,28 @@ class RulerManager {
/// purpose.
final html.Element _rulerHost = html.Element.tag('flt-ruler-host');

/// Releases the resources used by this [RulerHost].
///
/// After this is called, this object is no longer usable.
void dispose() {
_rulerHost.remove();
}

/// Adds an element used for measuring text as a child of [_rulerHost].
void addElement(html.HtmlElement element) {
_rulerHost.append(element);
}
}

/// Manages [ParagraphRuler] instances and caches them per unique
/// [ParagraphGeometricStyle].
///
/// All instances of [ParagraphRuler] should be created through this class.
class RulerManager extends RulerHost {
RulerManager({required this.rulerCacheCapacity}): super();

final int rulerCacheCapacity;

/// The cache of rulers used to measure text.
///
/// Each ruler is keyed by paragraph style. This allows us to setup the
Expand Down Expand Up @@ -78,13 +95,6 @@ class RulerManager {
}
}

/// Releases the resources used by this [RulerManager].
///
/// After this is called, this object is no longer usable.
void dispose() {
_rulerHost.remove();
}

// Evicts all rulers from the cache.
void _evictAllRulers() {
_rulers.forEach((ParagraphGeometricStyle style, ParagraphRuler ruler) {
Expand Down Expand Up @@ -129,11 +139,6 @@ class RulerManager {
}
}

/// Adds an element used for measuring text as a child of [_rulerHost].
void addHostElement(html.DivElement element) {
_rulerHost.append(element);
}

/// Performs a cache lookup to find an existing [ParagraphRuler] for the given
/// [style] and if it can't find one in the cache, it would create one.
///
Expand Down Expand Up @@ -479,7 +484,7 @@ class DomTextMeasurementService extends TextMeasurementService {
height = naturalHeight;
} else {
// Lazily compute [lineHeight] when [maxLines] is not null.
lineHeight = ruler.lineHeightDimensions!.height;
lineHeight = ruler.lineHeight;
height = math.min(naturalHeight, maxLines * lineHeight);
}

Expand Down Expand Up @@ -587,8 +592,9 @@ class CanvasTextMeasurementService extends TextMeasurementService {
}
}

final double alphabeticBaseline = ruler.alphabeticBaseline;
final int lineCount = linesCalculator.lines.length;
final double lineHeight = ruler.lineHeightDimensions!.height;
final double lineHeight = ruler.lineHeight;
final double naturalHeight = lineCount * lineHeight;
final int? maxLines = style.maxLines;
final double height = maxLines == null
Expand All @@ -598,8 +604,8 @@ class CanvasTextMeasurementService extends TextMeasurementService {
final MeasurementResult result = MeasurementResult(
constraints.width,
isSingleLine: lineCount == 1,
alphabeticBaseline: ruler.alphabeticBaseline,
ideographicBaseline: ruler.alphabeticBaseline * _baselineRatioHack,
alphabeticBaseline: alphabeticBaseline,
ideographicBaseline: alphabeticBaseline * _baselineRatioHack,
height: height,
naturalHeight: naturalHeight,
lineHeight: lineHeight,
Expand Down
Loading