baseline.dart 2.78 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui' as ui;
6

7
import 'package:flutter/rendering.dart';
8

9 10 11 12 13 14 15 16 17 18 19
class _BaselinePainter extends CustomPainter {
  const _BaselinePainter({
    this.paragraph
  });

  final RenderParagraph paragraph;

  void paint(Canvas canvas, Size size) {
    double baseline = paragraph.getDistanceToBaseline(TextBaseline.alphabetic);
    double w = paragraph.getMaxIntrinsicWidth(new BoxConstraints.loose(size));
    double h = paragraph.getMaxIntrinsicHeight(new BoxConstraints.loose(size));
20 21 22 23 24 25

    Path path;
    Paint paint;

    // top and bottom
    path = new Path();
26 27 28 29
    path.moveTo(0.0, 0.0);
    path.lineTo(w, 0.0);
    path.moveTo(0.0, h);
    path.lineTo(w, h);
30
    paint = new Paint()
31 32
     ..color = const Color(0xFFFF9000)
     ..style = ui.PaintingStyle.stroke
33 34 35 36 37 38 39 40 41 42 43
     ..strokeWidth = 1.5;
    canvas.drawPath(path, paint);

    // baseline
    path = new Path();
    path.moveTo(0.0, baseline);
    path.lineTo(w, baseline);
    paint = new Paint()
     ..color = const Color(0xFF00FF90)
     ..style = ui.PaintingStyle.stroke
     ..strokeWidth = 1.5;
44 45 46 47 48 49 50
    canvas.drawPath(path, paint);
  }

  // TODO(abarth): We have no good way of detecting when the paragraph's intrinsic dimensions change.
  bool shouldRepaint(_BaselinePainter oldPainter) => true;
}

51 52
RenderBox getBox(double lh) {
  RenderParagraph paragraph = new RenderParagraph(
Eric Seidel's avatar
Eric Seidel committed
53
    new StyledTextSpan(
54 55 56
      new TextStyle(
        color: const Color(0xFF0000A0)
      ),
Hixie's avatar
Hixie committed
57
      <TextSpan>[
Eric Seidel's avatar
Eric Seidel committed
58 59
        new PlainTextSpan('test'),
        new StyledTextSpan(
60 61 62 63 64
          new TextStyle(
            fontFamily: 'serif',
            fontSize: 50.0,
            height: lh
          ),
Hixie's avatar
Hixie committed
65
          <TextSpan>[new PlainTextSpan('مرحبا Hello')]
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        )
      ]
    )
  );
  return new RenderPadding(
    padding: new EdgeDims.all(10.0),
    child: new RenderConstrainedBox(
      additionalConstraints: new BoxConstraints.tightFor(height: 200.0),
      child: new RenderDecoratedBox(
        decoration: new BoxDecoration(
          backgroundColor: const Color(0xFFFFFFFF)
        ),
        child: new RenderPadding(
          padding: new EdgeDims.all(10.0),
          child: new RenderCustomPaint(
81 82 83 84
            painter: new _BaselinePainter(
              paragraph: paragraph
            ),
            child: paragraph
85 86 87 88 89 90 91 92
          )
        )
      )
    )
  );
}

void main() {
Hixie's avatar
Hixie committed
93
  RenderBox root = new RenderFlex(children: <RenderBox>[
94 95 96 97
      new RenderConstrainedBox(
        additionalConstraints: new BoxConstraints.tightFor(height: 50.0)
      ),
      getBox(null),
98
      getBox(1.2),
99 100 101 102
    ],
    direction: FlexDirection.vertical,
    alignItems: FlexAlignItems.stretch
  );
Ian Hickson's avatar
Ian Hickson committed
103
  new RenderingFlutterBinding(root: root);
104
}