Centers text within the width

 @return [String]

 @api public
426320
def align_center(text, width, fill: SPACE, separator: NEWLINE)
      return text if width.nil?
      each_line(text, separator) do |line|
        width_diff = width - display_width(line)
        if width_diff > 0
          right_count = (width_diff.to_f / 2).ceil
          left_count  =  width_diff - right_count
          [fill * left_count, line, fill * right_count].join
        else
          line
        end
      end
    end
138681
private int getCharWidth(String text, Align align)
    {
        final int width;
        if (align == Align.RIGHT)
        {
            width = getTextWidth(text);
        }
        else if (align == Align.CENTER)
        {
            width = getTextWidth(text) / 2;
        }
        else
        {
            width = 0;
        }
        return width;
    }
1356689
def margin(text):
    
    """
    lines = str(text).split('\n')
    return '\n'.join('  {}  '.format(l) for l in lines)
232162
def width
      string = to_s
      max_width = 0
      string.each_line do |line|
        max_width = [max_width, line.uncolored.chomp.size].max
      end
      max_width
    end
497013
function centerText(text, n, length) {
  // Account for text length first
  n -= length || text.length;

  // Pad to be even
  if (n % 2 == 1) {
    text = ' ' + text;
  }

  // Round n to lowest number
  n = Math.floor(n / 2);

  // Make spacer
  var spacer = ' '.repeat(n);

  // Fill in text
  return spacer + text + spacer;
}
110612
def center
      point = location
      dimensions = size
      Selenium::WebDriver::Point.new(point.x + (dimensions['width'] / 2),
                                     point.y + (dimensions['height'] / 2))
    end
479032
def draw_foreground
      case @justify
        when :left
          rel_x = 0.0
          draw_x = x + padding_left

        when :right
          rel_x = 1.0
          draw_x = x + rect.width - padding_right

        when :center
          rel_x = 0.5
          draw_x = (x + padding_left) + (rect.width - padding_right - padding_left) / 2.0
      end

      font.draw_rel(@text, draw_x, y + padding_top, z, rel_x, 0, 1, 1, color)
    end
368294
def _block_width(self):
        
        section = self.sections[-1]
        return Emu(
            section.page_width - section.left_margin - section.right_margin
        )
456773
function () {
        var self = this, layoutRect = self._super();

        if (self.settings.multiline) {
          var size = DomUtils.getSize(self.getEl());

          // Check if the text fits within maxW if not then try word wrapping it
          if (size.width > layoutRect.maxW) {
            layoutRect.minW = layoutRect.maxW;
            self.classes.add('multiline');
          }

          self.getEl().style.width = layoutRect.minW + 'px';
          layoutRect.startMinH = layoutRect.h = layoutRect.minH = Math.min(layoutRect.maxH, DomUtils.getSize(self.getEl()).height);
        }

        return layoutRect;
      }
571289
function alignCenter (msg) {
    var cols = process.stdout.columns;
    var len = msg.length;

    if (cols > len) {
        var pad = Math.floor((cols - len) / 2);
        msg = Array(pad).join(" ") + msg;
    }
    return msg;
}
599858
function get_margin_width()
    {
        //ignore image width, use same width as on predefined bullet ListBullet
        //for proper alignment of bullet image and text. Allow image to not fitting on left border.
        //This controls the extra indentation of text to make room for the bullet image.
        //Here use actual image size, not predefined bullet size
        //return $this->_frame->get_style()->get_font_size()*ListBullet::BULLET_SIZE +
        //  2 * ListBullet::BULLET_PADDING;

        // Small hack to prevent indenting of list text
        // Image Might not exist, then position like on list_bullet_frame_decorator fallback to none.
        if ($this->_frame->get_style()->list_style_position === "outside" || $this->_width == 0) {
            return 0;
        }
        //This aligns the "inside" image position with the text.
        //The text starts to the right of the image.
        //Between the image and the text there is an added margin of image width.
        //Where this comes from is unknown.
        //The corresponding ListBullet sets a smaller margin. bullet size?
        return $this->_width + 2 * ListBullet::BULLET_PADDING;
    }
871270
def centerize(src, dst_shape, margin_color=None):
    
    if src.shape[:2] == dst_shape[:2]:
        return src
    centerized = np.zeros(dst_shape, dtype=src.dtype)
    if margin_color:
        centerized[:, :] = margin_color
    pad_vertical, pad_horizontal = 0, 0
    h, w = src.shape[:2]
    dst_h, dst_w = dst_shape[:2]
    if h < dst_h:
        pad_vertical = (dst_h - h) // 2
    if w < dst_w:
        pad_horizontal = (dst_w - w) // 2
    centerized[pad_vertical:pad_vertical + h,
               pad_horizontal:pad_horizontal + w] = src
    return centerized
217734
def textalign(text, maxlength, align='left'):
    
    if align == 'left':
        return text
    elif align == 'centre' or align == 'center':
        spaces = ' ' * (int((maxlength - len(text)) / 2))
    elif align == 'right':
        spaces = (maxlength - len(text))
    else:
        raise ValueError("Invalid alignment specified.")
    return spaces + text
1117941
def adjustTextMargins( self ):
        
        left_buttons = self._buttons.get(Qt.AlignLeft, [])
        
        if left_buttons:
            bwidth = left_buttons[-1].pos().x() + left_buttons[-1].width() - 4
        else:
            bwidth = 0 + (max(8, self.cornerRadius()) - 8)
        
        ico = self.icon()
        if ico and not ico.isNull():
            bwidth += self.iconSize().width()
        
        self.setTextMargins(bwidth, 0, 0, 0)
1353731
def calculate_width(font_size, text)
      return 0 if text.nil?
      @d.pointsize = font_size
      @d.font = @font if @font
      @d.get_type_metrics(@base_image, text.to_s).width
    end
1145426
def space():
    ''''''
    @Parser
    def space_parser(text, index=0):
        if index < len(text) and text[index].isspace():
            return Value.success(index + 1, text[index])
        else:
            return Value.failure(index, 'one space')
    return space_parser
476
function breakLines(tokens, maxWidth) {
    if (!maxWidth) {
        maxWidth = Infinity;
    }
    let i = 0;
    let lineLength = 0;
    let lastTokenWithSpace = -1;
    while (i < tokens.length) { /* take all text tokens, remove space, apply linebreaks */
        let token = tokens[i];
        if (token.type == TYPE_NEWLINE) { /* reset */
            lineLength = 0;
            lastTokenWithSpace = -1;
        }
        if (token.type != TYPE_TEXT) { /* skip non-text tokens */
            i++;
            continue;
        }
        /* remove spaces at the beginning of line */
        while (lineLength == 0 && token.value.charAt(0) == " ") {
            token.value = token.value.substring(1);
        }
        /* forced newline? insert two new tokens after this one */
        let index = token.value.indexOf("\n");
        if (index != -1) {
            token.value = breakInsideToken(tokens, i, index, true);
            /* if there are spaces at the end, we must remove them (we do not want the line too long) */
            let arr = token.value.split("");
            while (arr.length && arr[arr.length - 1] == " ") {
                arr.pop();
            }
            token.value = arr.join("");
        }
        /* token degenerated? */
        if (!token.value.length) {
            tokens.splice(i, 1);
            continue;
        }
        if (lineLength + token.value.length > maxWidth) { /* line too long, find a suitable breaking spot */
            /* is it possible to break within this token? */
            let index = -1;
            while (1) {
                let nextIndex = token.value.indexOf(" ", index + 1);
                if (nextIndex == -1) {
                    break;
                }
                if (lineLength + nextIndex > maxWidth) {
                    break;
                }
                index = nextIndex;
            }
            if (index != -1) { /* break at space within this one */
                token.value = breakInsideToken(tokens, i, index, true);
            }
            else if (lastTokenWithSpace != -1) { /* is there a previous token where a break can occur? */
                let token = tokens[lastTokenWithSpace];
                let breakIndex = token.value.lastIndexOf(" ");
                token.value = breakInsideToken(tokens, lastTokenWithSpace, breakIndex, true);
                i = lastTokenWithSpace;
            }
            else { /* force break in this token */
                token.value = breakInsideToken(tokens, i, maxWidth - lineLength, false);
            }
        }
        else { /* line not long, continue */
            lineLength += token.value.length;
            if (token.value.indexOf(" ") != -1) {
                lastTokenWithSpace = i;
            }
        }
        i++; /* advance to next token */
    }
    tokens.push({ type: TYPE_NEWLINE }); /* insert fake newline to fix the last text line */
    /* remove trailing space from text tokens before newlines */
    let lastTextToken = null;
    for (let i = 0; i < tokens.length; i++) {
        let token = tokens[i];
        switch (token.type) {
            case TYPE_TEXT:
                lastTextToken = token;
                break;
            case TYPE_NEWLINE:
                if (lastTextToken) { /* remove trailing space */
                    let arr = lastTextToken.value.split("");
                    while (arr.length && arr[arr.length - 1] == " ") {
                        arr.pop();
                    }
                    lastTextToken.value = arr.join("");
                }
                lastTextToken = null;
                break;
        }
    }
    tokens.pop(); /* remove fake token */
    return tokens;
}