drawWord function

void drawWord(
  1. Painter p,
  2. List<WordLetter> items,
  3. WordStyle st
)

Draw letters as one word with 1 px bevel, 1 px outline, drop shadow, optional diagonal shine & white flash.

Implementation

void drawWord(Painter p, List<WordLetter> items, WordStyle st) {
  if (items.isEmpty) return;
  var x0d = double.infinity;
  var y0d = double.infinity;
  var x1d = double.negativeInfinity;
  var y1d = double.negativeInfinity;
  for (final it in items) {
    final h = it.g.rows.length * it.sy;
    final w = it.g.width * it.sx;
    final sl = it.slant.abs() * it.g.rows.length;
    x0d = math.min(x0d, it.x - sl);
    x1d = math.max(x1d, it.x + w + sl);
    y0d = math.min(y0d, it.y);
    y1d = math.max(y1d, it.y + h);
  }
  final ow = st.ow;
  final x0 = x0d.floor() - 1 - ow;
  final y0 = y0d.floor() - 1 - ow;
  final x1 = x1d.ceil() + 2 + ow;
  final y1 = y1d.ceil() + 3 + ow;
  final bw = x1 - x0;
  final bh = y1 - y0;
  if (bw * bh > _wordIds.length) {
    _wordIds = Int16List(bw * bh * 2);
    _wordEdges = Uint8List(bw * bh * 2);
  }
  _wordIds.fillRange(0, bw * bh, 0);

  for (var k = 0; k < items.length; k++) {
    final it = items[k];
    final g = it.g;
    final gh = g.rows.length;
    final h = gh * it.sy;
    final bot = it.y + h;
    final sl = it.slant;
    final pyEnd = bot.ceil();
    for (var py = it.y.floor(); py < pyEnd; py++) {
      final gy = ((py + 0.5 - it.y) / it.sy).floor();
      if (gy < 0 || gy >= gh) continue;
      final shift = (((bot - py - 0.5) / it.sy) * sl).round();
      final ry = py + 0.5 - it.y - gy * it.sy;
      final row = g.rows[gy];
      final pxEnd = (it.x + shift + g.width * it.sx).ceil();
      for (var px = (it.x + shift).floor() - 1; px <= pxEnd; px++) {
        final fx = px + 0.5 - it.x - shift;
        final gx = (fx / it.sx).floor();
        if (gx < 0 || gx >= g.width || !row[gx]) continue;
        final i = (py - y0) * bw + (px - x0);
        if (i < 0 || i >= bw * bh) continue;
        _wordIds[i] = k + 1;
        final rx = fx - gx * it.sx;
        final up = gy > 0 ? g.rows[gy - 1] : null;
        final dn = gy < gh - 1 ? g.rows[gy + 1] : null;
        bool open(List<bool>? r) =>
            r == null || (!_rowBit(r, gx) && !_rowBit(r, gx - 1) && !_rowBit(r, gx + 1));
        _wordEdges[i] = (ry < 1.0 && open(up) ? 1 : 0) |
            (ry >= it.sy - 1.0 && open(dn) ? 2 : 0) |
            (rx < 1.0 && !_rowBit(row, gx - 1) ? 4 : 0);
      }
    }
  }

  int at(int x, int y) =>
      (x < x0 || y < y0 || x >= x1 || y >= y1) ? 0 : _wordIds[(y - y0) * bw + (x - x0)];

  final fList = st.fill;
  final nF = fList.length;
  final shine = st.shine;
  final glint = st.glint ?? kWhite;
  final sy0 = st.shineY ?? y0.toDouble();

  for (var y = y0; y < y1; y++) {
    for (var x = x0; x < x1; x++) {
      final id = at(x, y);
      if (id != 0) {
        final it = items[id - 1];
        final v = (y + 0.5 - it.y) / (it.g.rows.length * it.sy);
        final idx = math.min(nF - 1, math.max(0, ((1.0 - v) * nF).floor()));
        var c = fList[idx];
        final e = _wordEdges[(y - y0) * bw + (x - x0)];
        if ((e & 1) != 0) {
          c = st.edge ?? fList[nF - 1];
        } else if ((e & 2) != 0) {
          c = st.under ?? fList[0];
        } else if ((e & 4) != 0 && idx < nF - 1) {
          c = fList[idx + 1];
        }
        if (shine != null) {
          final d = (x + 0.5 - (y + 0.5 - sy0) * 0.6 - shine).abs();
          if (d < 1.1) {
            c = kWhite;
          } else if (d < 2.4) {
            c = glint;
          }
        }
        if (it.flash > 0.6 || (it.flash > 0.25 && v < 0.5)) c = kWhite;
        p.dot(x, y, c, it.a);
        continue;
      }
      var n = 0;
      for (var dy = -ow; dy <= ow && n == 0; dy++) {
        for (var dx = -ow; dx <= ow && n == 0; dx++) {
          if (ow < 2 || dx.abs() + dy.abs() < 4) n = at(x + dx, y + dy);
        }
      }
      if (n != 0) {
        p.dot(x, y, st.outline, items[n - 1].a);
        continue;
      }
      var s = at(x - ow, y - 1 - ow);
      if (s == 0) s = at(x + 1 - ow, y - 1 - ow);
      if (s == 0) s = at(x - ow, y - ow);
      if (s != 0 && st.shadow != null) {
        p.dot(x, y, st.shadow!, items[s - 1].a * 0.9);
      }
    }
  }
}