<commit_before>import {textMetrics} from 'vega-scenegraph';

const Aligns = ['right', 'center', 'left'],
      Baselines = ['bottom', 'middle', 'top'];

export default function($, bitmaps, anchors, offsets, infPadding) {
  const width = $.width,
        height = $.height,
        bm0 = bitmaps[0],
        bm1 = bitmaps[1],
        n = offsets.length;

  return function(d) {
    const boundary = d.boundary,
          textHeight = d.datum.fontSize;

    // can not be placed if the mark is not visible in the graph bound
    if (!infPadding && (boundary[2] < 0 || boundary[5] < 0 || boundary[0] > width || boundary[3] > height)) {
      return false;
    }

    let textWidth = 0,
        dx, dy, isInside, sizeFactor, insideFactor,
        x1, x2, y1, y2, xc, yc,
        _x1, _x2, _y1, _y2;

    // for each anchor and offset
    for (let i=0; i<n; ++i) {
      dx = (anchors[i] & 0x3) - 1;
      dy = ((anchors[i] >>> 0x2) & 0x3) - 1;

      isInside = (dx === 0 && dy === 0) || offsets[i] < 0;
      sizeFactor = dx && dy ? Math.SQRT1_2 : 1;
      insideFactor = offsets[i] < 0 ? -1 : 1;

      x1 = boundary[1 + dx] + offsets[i] * dx * sizeFactor;
      yc = boundary[4 + dy] + (insideFactor * textHeight * dy) / 2 + offsets[i] * dy * sizeFactor;
      y1 = yc - textHeight / 2;
      y2 = yc + textHeight / 2;

      _x1 = $(x1);
      _y1 = $(y1);
      _y2 = $(y2);

      if (infPadding) {
        _x1 = _x1 < 0 ? 0 : _x1;
        _y1 = _y1 < 0 ? 0 : _y1;
        _y2 = _y2 >= $.height ? ($.height - 1) : _y2;
      }

      if (!textWidth) {
        // to avoid finding width of text label,
        if (!test(_x1, _x1, _y1, _y2, bm0, bm1, x1, x1, y1, y2, boundary, isInside)) {
          // skip this anchor/offset option if we fail to place a label with 1px width
          continue;
        } else {<commit_msg>if padding is inf, should bound x to chart's bound box in all direction<commit_after>import {textMetrics} from 'vega-scenegraph';

const Aligns = ['right', 'center', 'left'],
      Baselines = ['bottom', 'middle', 'top'];

export default function($, bitmaps, anchors, offsets, infPadding) {
  const width = $.width,
        height = $.height,
        bm0 = bitmaps[0],
        bm1 = bitmaps[1],
        n = offsets.length;

  return function(d) {
    const boundary = d.boundary,
          textHeight = d.datum.fontSize;

    // can not be placed if the mark is not visible in the graph bound
    if (!infPadding && (boundary[2] < 0 || boundary[5] < 0 || boundary[0] > width || boundary[3] > height)) {
      return false;
    }

    let textWidth = 0,
        dx, dy, isInside, sizeFactor, insideFactor,
        x1, x2, y1, y2, xc, yc,
        _x1, _x2, _y1, _y2;

    // for each anchor and offset
    for (let i=0; i<n; ++i) {
      dx = (anchors[i] & 0x3) - 1;
      dy = ((anchors[i] >>> 0x2) & 0x3) - 1;

      isInside = (dx === 0 && dy === 0) || offsets[i] < 0;
      sizeFactor = dx && dy ? Math.SQRT1_2 : 1;
      insideFactor = offsets[i] < 0 ? -1 : 1;

      x1 = boundary[1 + dx] + offsets[i] * dx * sizeFactor;
      yc = boundary[4 + dy] + (insideFactor * textHeight * dy) / 2 + offsets[i] * dy * sizeFactor;
      y1 = yc - textHeight / 2;
      y2 = yc + textHeight / 2;

      _x1 = $(x1);
      _y1 = $(y1);
      _y2 = $(y2);

      if (infPadding) {
        _x1 = _x1 < 0 ? 0 : _x1;
        _x1 = _x1 >= $.width ? ($.width - 1) : _x1;
        _y1 = _y1 < 0 ? 0 : _y1;
        _y2 = _y2 >= $.height ? ($.height - 1) : _y2;
      }

      if (!textWidth) {
        // to avoid finding width of text label,
        if (!test(_x1, _x1, _y1, _y2, bm0, bm1, x1, x1, y1, y2, boundary, isInside)) {
          // skip this anchor/offset option if we fail to place a label with 1px width
          continue;
        } else {
{'fraction_non_alphanumeric': 0.15664893617021278, 'fraction_numerical': 0.04148936170212766, 'mean_word_length': 2.047811993517018, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2227201', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1375, 'n_words': 427}