<commit_before>import 'dart:typed_data';
import '../bitmap_font.dart';
import '../color.dart';
import '../image.dart';
import 'draw_pixel.dart';

var _r_lut = Uint8List(256);
var _g_lut = Uint8List(256);
var _b_lut = Uint8List(256);
var _a_lut = Uint8List(256);

/// Draw a string horizontally into [image] horizontally into [image] at position
/// [x],[y] with the given [color].
///
/// You can load your own font, or use one of the existing ones
/// such as: [arial_14], [arial_24], or [arial_48].
Image drawString(Image image, BitmapFont font, int x, int y, String string,
    {int color = 0xffffffff}) {
  if (color != 0xffffffff) {
    int ca = getAlpha(color);
    if (ca == 0) {
      return image;
    }
    num da = ca / 255.0;
    num dr = getRed(color) / 255.0;
    num dg = getGreen(color) / 255.0;
    num db = getBlue(color) / 255.0;
    for (int i = 1; i < 256; ++i) {
      _r_lut[i] = (dr * i).toInt();
      _g_lut[i] = (dg * i).toInt();
      _b_lut[i] = (db * i).toInt();
      _a_lut[i] = (da * i).toInt();
    }
  }

  List<int> chars = string.codeUnits;
  for (int c in chars) {
    if (!font.characters.containsKey(c)) {
      x += font.base ~/ 2;
      continue;
    }

    BitmapFontCharacter ch = font.characters[c];

    int x2 = x + ch.width;
    int y2 = y + ch.height;
    int pi = 0;
    for (int yi = y; yi < y2; ++yi) {
      for (int xi = x; xi < x2; ++xi) {
        int p = ch.image[pi++];
        if (color != 0xffffffff) {
          p = getColor(_r_lut[getRed(p)], _g_lut[getGreen(p)],
              _b_lut[getBlue(p)], _a_lut[getAlpha(p)]);
        }
        drawPixel(image, xi + ch.xoffset, yi + ch.yoffset, p);
      }
    }

    x += ch.xadvance;
  }

  return image;
}

Image drawStringXCentered(Image image, BitmapFont font, int y, String string,
    {int color = 0xffffffff}) {
  int stringWidth = 0;
  List<int> chars = string.codeUnits;
  for (int c in chars) {
    if (!font.characters.containsKey(c)) {
      continue;
    }
    BitmapFontCharacter ch = font.characters[c];
    stringWidth += ch.xadvance;
  }

  int x = (image.width / 2).round() - (stringWidth / 2).round();
  return drawString(image, font, x, y, string, color: color);
}<commit_msg>Generalized drawStringCentered method, which now allows to automatically center in x or y the text.<commit_after>import 'dart:typed_data';
import '../bitmap_font.dart';
import '../color.dart';
import '../image.dart';
import 'draw_pixel.dart';

var _r_lut = Uint8List(256);
var _g_lut = Uint8List(256);
var _b_lut = Uint8List(256);
var _a_lut = Uint8List(256);

/// Draw a string horizontally into [image] horizontally into [image] at position
/// [x],[y] with the given [color].
///
/// You can load your own font, or use one of the existing ones
/// such as: [arial_14], [arial_24], or [arial_48].
Image drawString(Image image, BitmapFont font, int x, int y, String string,
    {int color = 0xffffffff}) {
  if (color != 0xffffffff) {
    int ca = getAlpha(color);
    if (ca == 0) {
      return image;
    }
    num da = ca / 255.0;
    num dr = getRed(color) / 255.0;
    num dg = getGreen(color) / 255.0;
    num db = getBlue(color) / 255.0;
    for (int i = 1; i < 256; ++i) {
      _r_lut[i] = (dr * i).toInt();
      _g_lut[i] = (dg * i).toInt();
      _b_lut[i] = (db * i).toInt();
      _a_lut[i] = (da * i).toInt();
    }
  }

  List<int> chars = string.codeUnits;
  for (int c in chars) {
    if (!font.characters.containsKey(c)) {
      x += font.base ~/ 2;
      continue;
    }

    BitmapFontCharacter ch = font.characters[c];

    int x2 = x + ch.width;
    int y2 = y + ch.height;
    int pi = 0;
    for (int yi = y; yi < y2; ++yi) {
      for (int xi = x; xi < x2; ++xi) {
        int p = ch.image[pi++];
        if (color != 0xffffffff) {
          p = getColor(_r_lut[getRed(p)], _g_lut[getGreen(p)],
              _b_lut[getBlue(p)], _a_lut[getAlpha(p)]);
        }
        drawPixel(image, xi + ch.xoffset, yi + ch.yoffset, p);
      }
    }

    x += ch.xadvance;
  }

  return image;
}

/// Draw a string horizontally into [image] at position
/// [x],[y] with the given [color]. 
/// If x is omitted text is automatically centered into [image]
/// If y is omitted text is automatically centered into [image].
/// If both x and y are provided it has the same behaviour of drawString method.
///
/// You can load your own font, or use one of the existing ones
/// such as: [arial_14], [arial_24], or [arial_48].
Image drawStringCentered(Image image, BitmapFont font, String string,
    {int x, int y, int color = 0xffffffff}) {
  int stringWidth = 0;
  int stringHeight = 0;

  if (x == null || y == null) {
    List<int> chars = string.codeUnits;
    for (int c in chars) {
      if (!font.characters.containsKey(c)) {
        continue;
      }
      BitmapFontCharacter ch = font.characters[c];
      stringWidth += ch.xadvance;
      if(ch.height + ch.yoffset > stringHeight) {
        stringHeight = ch.height + ch.yoffset;
      }
    }
  }

  int xPos, yPos;
  if(x == null) {
    xPos = (image.width / 2).round() - (stringWidth / 2).round();
  } else {
    xPos = x;
  }
  if(y == null) {
    yPos = (image.height / 2).round() - (stringHeight / 2).round();
  } else {
    yPos = y;
  }

  return drawString(image, font, xPos, yPos, string, color: color);
}
{'fraction_non_alphanumeric': 0.15976555114388352, 'fraction_numerical': 0.02287767063717149, 'mean_word_length': 2.5337341349365397, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4201679', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1928, 'n_words': 564}