|
1 | 1 | import 'dart:math' as math; |
2 | 2 |
|
3 | 3 | import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
4 | 5 | import 'package:flutter/widgets.dart'; |
5 | 6 | import 'package:flutter/rendering.dart'; |
6 | 7 |
|
@@ -231,12 +232,75 @@ class _KatexVlist extends StatelessWidget { |
231 | 232 | @override |
232 | 233 | Widget build(BuildContext context) { |
233 | 234 | final em = DefaultTextStyle.of(context).style.fontSize!; |
| 235 | + final defaultColor = DefaultTextStyle.of(context).style.color ; |
| 236 | + |
| 237 | + final lines = <_VlistLine>[]; |
| 238 | + for (final row in node.rows) { |
| 239 | + final lineInfo = _findLineInfo(row.node); |
| 240 | + if (lineInfo != null) { |
| 241 | + final y = (row.verticalOffsetEm - (lineInfo.heightEm ?? 0) + 0.9) * em; |
| 242 | + final thickness = lineInfo.borderWidthEm * em; |
| 243 | + lines.add(_VlistLine(y, thickness, lineInfo.color ?? defaultColor)); |
| 244 | + }} |
| 245 | + |
| 246 | + return CustomPaint( |
| 247 | + foregroundPainter: _KatexBorderPainter(lines), |
| 248 | + child: Stack( |
| 249 | + children: List.unmodifiable(node.rows.map((row) { |
| 250 | + return Transform.translate( |
| 251 | + offset: Offset(0, row.verticalOffsetEm * em), |
| 252 | + child: _KatexSpan(row.node)); |
| 253 | + })))); |
| 254 | + } |
| 255 | + ({double? heightEm, double borderWidthEm, Color? color})? _findLineInfo(KatexSpanNode span) { |
| 256 | + final borderWidth = span.styles.borderBottomWidthEm; |
| 257 | + final color = span.styles.color != null |
| 258 | + ? Color.fromARGB(span.styles.color!.a, span.styles.color!.r, span.styles.color!.g, span.styles.color!.b) |
| 259 | + : null; |
| 260 | + if (borderWidth != null) {return (heightEm: span.styles.heightEm, borderWidthEm: borderWidth, color: color);} |
| 261 | + |
| 262 | + if (span.nodes != null) { |
| 263 | + for (final child in span.nodes!) { |
| 264 | + if (child is KatexSpanNode) { |
| 265 | + final info = _findLineInfo(child); |
| 266 | + if (info != null) return info; |
| 267 | + }}} |
| 268 | + return null; |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +class _VlistLine { |
| 273 | + const _VlistLine(this.y, this.thickness, this.color); |
| 274 | + final double y; |
| 275 | + final double thickness; |
| 276 | + final Color? color; |
| 277 | +} |
| 278 | + |
| 279 | +class _KatexBorderPainter extends CustomPainter { |
| 280 | + const _KatexBorderPainter(this.lines); |
234 | 281 |
|
235 | | - return Stack(children: List.unmodifiable(node.rows.map((row) { |
236 | | - return Transform.translate( |
237 | | - offset: Offset(0, row.verticalOffsetEm * em), |
238 | | - child: _KatexSpan(row.node)); |
239 | | - }))); |
| 282 | + final List<_VlistLine> lines; |
| 283 | + |
| 284 | + @override |
| 285 | + void paint(Canvas canvas, Size size) { |
| 286 | + if (lines.isEmpty) return; |
| 287 | + final paint = Paint()..style = PaintingStyle.fill; |
| 288 | + for (final line in lines) { |
| 289 | + paint.color = line.color ?? Colors.black; |
| 290 | + canvas.drawRect(Rect.fromLTWH(0, line.y, size.width, line.thickness), paint); |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + @override |
| 295 | + bool shouldRepaint(covariant _KatexBorderPainter oldDelegate) { |
| 296 | + if (oldDelegate.lines.length != lines.length) return true; |
| 297 | + for (var i = 0; i < lines.length; i++) { |
| 298 | + if (oldDelegate.lines[i].y != lines[i].y || |
| 299 | + oldDelegate.lines[i].thickness != lines[i].thickness || |
| 300 | + oldDelegate.lines[i].color != lines[i].color) { |
| 301 | + return true; |
| 302 | + }} |
| 303 | + return false; |
240 | 304 | } |
241 | 305 | } |
242 | 306 |
|
|
0 commit comments