TextEntityLayout constructor

TextEntityLayout(
  1. TextEntity entity,
  2. Size reference
)

Lays out entity against the source image reference.

Implementation

factory TextEntityLayout(TextEntity entity, Size reference) {
  final fontSize = entity.displayFontSize(reference);
  final padding = (fontSize * 0.24).floorToDouble();
  final wrapWidth = math.max(0.0, entity.width - 2 * padding);
  final resolved = entity.style.resolve(entity.colour);
  final base = TextStyle(
    fontFamily: mediaFontFamily,
    fontSize: fontSize,
    fontWeight: FontWeight.w600,
  );

  TextPainter lay(TextStyle style) => TextPainter(
    text: TextSpan(text: entity.text, style: style),
    textAlign: entity.align,
    textDirection: TextDirection.ltr,
  )..layout(minWidth: wrapWidth, maxWidth: wrapWidth);

  final glyphs = lay(
    base.copyWith(
      color: resolved.textColor,
      shadows: entity.style == TextEntityStyle.regular
          ? const [
              Shadow(
                offset: Offset(0, 4),
                blurRadius: 4,
                color: Color(0x66000000),
              ),
            ]
          : null,
    ),
  );

  final strokeWidth = fontSize / 9;
  final strokeColor = resolved.strokeColor;
  final outline = strokeColor == null
      ? null
      : lay(
          base.copyWith(
            foreground: Paint()
              ..style = PaintingStyle.stroke
              ..strokeWidth = strokeWidth
              ..strokeJoin = StrokeJoin.round
              ..color = strokeColor,
          ),
        );

  return TextEntityLayout._(
    glyphs: glyphs,
    outline: outline,
    fontSize: fontSize,
    padding: padding,
    strokeWidth: strokeWidth,
    frameColor: resolved.frameColor,
    lines: _lines(glyphs, entity, padding),
  );
}