<commit_before>/*
 * Copyright 2010 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.typography.font.sfntly.data;

import java.io.IOException;
import java.io.InputStream;

/**
 * Writable font data wrapper. Supports writing of data primitives in the
 * TrueType / OpenType spec.
 *
 * @author <NAME>
 */
public final class WritableFontData extends ReadableFontData {

  /**
   * Constructs a writable font data object. If the length is specified as
   * positive then a fixed size font data object will be created. If the length
   * is zero or less then a growable font data object will be created and the
   * size will be used as an estimate to help in allocating the original space.
   *
   * @param length if length > 0 create a fixed length font data; otherwise
   *        create a growable font data
   * @return a new writable font data
   */
  public static final WritableFontData createWritableFontData(int length) {
    ByteArray<?> ba = null;
    if (length > 0) {
      ba = new MemoryByteArray(length);
      ba.setFilledLength(length);
    } else {
      ba = new GrowableMemoryByteArray();
    }
    WritableFontData wfd = new WritableFontData(ba);
    return wfd;
  }

  /**
   * Constructs a writable font data object. The new font data object will wrap
   * the bytes passed in to the factory and it will take ownership of those
   * bytes. They should not be used again by the caller.
   *
   * @param b the byte array to wrap
   * @return a new writable font data
   */
  public static final WritableFontData createWritableFontData(byte[] b) {
    ByteArray<?> ba = new MemoryByteArray(b);
    WritableFontData wfd = new WritableFontData(ba);
    return wfd;
  }
  
  /**
   * Constructs a writable font data object. The new font data object will wrap
   * a copy of the the data used by the original writable font data object passed in.
   *
   * @param original the source font data
   * @return a new writable font data
   */
  public static final WritableFontData createWritableFontData(ReadableFontData original) {
    ByteArray<?> ba = null;
    // TODO(stuartg): push this down into the BAs - maybe remove the difference between growable and fixed
    if (original.array.growable()) {
      ba = new GrowableMemoryByteArray();
    } else {
      ba = new MemoryByteArray(original.array.length());
    }
    original.array.copyTo(ba);
    
    WritableFontData wfd = new WritableFontData(ba);
    wfd.setCheckSumRanges(original.checkSumRange());
    return wfd;
  }
  
  /**
   * Constructor.
   *
   * @param array byte array to wrap
   */
  private WritableFontData(ByteArray<? extends ByteArray<?>> array) {
    super(array);
  }

  /**
   * Constructor with a lower bound.
   *
   * @param data other WritableFontData object to share data with
   * @param offset offset from the other WritableFontData's data
   */
  private WritableFontData(WritableFontData data, int offset) {
    super(data, offset);
  }

  /**
   * Constructor with lower bound and a length bound.
   *
   * @param data other WritableFontData object to share data with
   * @param offset offset from the other WritableFontData's data
   * @param length length of other WritableFontData's data to use
   */
  private WritableFontData(WritableFontData data, int offset, int length) {
    super(data, offset, length);
  }

  /**
   * Makes a slice of this FontData. The returned slice will share the data with
   * the original <code>FontData</code>.
   *
   * @param offset the start of the slice
   * @param length the number of bytes in the slice
   * @return a slice of the original FontData
   */
  @Override
  public WritableFontData slice(int offset, int length) {
    if (offset < 0 || (offset + length) > this.size()) {
      throw new IndexOutOfBoundsException("Attempt to bind data outside of its limits.");
    }<commit_msg>Hackety hack to work with some malformed fonts.<commit_after>/*
 * Copyright 2010 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.typography.font.sfntly.data;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

/**
 * Writable font data wrapper. Supports writing of data primitives in the
 * TrueType / OpenType spec.
 *
 * @author <NAME>
 */
public final class WritableFontData extends ReadableFontData {
  private static final Logger logger =
    Logger.getLogger(WritableFontData.class.getCanonicalName());
  
  /**
   * Constructs a writable font data object. If the length is specified as
   * positive then a fixed size font data object will be created. If the length
   * is zero or less then a growable font data object will be created and the
   * size will be used as an estimate to help in allocating the original space.
   *
   * @param length if length > 0 create a fixed length font data; otherwise
   *        create a growable font data
   * @return a new writable font data
   */
  public static final WritableFontData createWritableFontData(int length) {
    ByteArray<?> ba = null;
    if (length > 0) {
      ba = new MemoryByteArray(length);
      ba.setFilledLength(length);
    } else {
      ba = new GrowableMemoryByteArray();
    }
    WritableFontData wfd = new WritableFontData(ba);
    return wfd;
  }

  /**
   * Constructs a writable font data object. The new font data object will wrap
   * the bytes passed in to the factory and it will take ownership of those
   * bytes. They should not be used again by the caller.
   *
   * @param b the byte array to wrap
   * @return a new writable font data
   */
  public static final WritableFontData createWritableFontData(byte[] b) {
    ByteArray<?> ba = new MemoryByteArray(b);
    WritableFontData wfd = new WritableFontData(ba);
    return wfd;
  }
  
  /**
   * Constructs a writable font data object. The new font data object will wrap
   * a copy of the the data used by the original writable font data object passed in.
   *
   * @param original the source font data
   * @return a new writable font data
   */
  public static final WritableFontData createWritableFontData(ReadableFontData original) {
    ByteArray<?> ba = null;
    // TODO(stuartg): push this down into the BAs - maybe remove the difference between growable and fixed
    if (original.array.growable()) {
      ba = new GrowableMemoryByteArray();
    } else {
      ba = new MemoryByteArray(original.array.length());
    }
    original.array.copyTo(ba);
    
    WritableFontData wfd = new WritableFontData(ba);
    wfd.setCheckSumRanges(original.checkSumRange());
    return wfd;
  }
  
  /**
   * Constructor.
   *
   * @param array byte array to wrap
   */
  private WritableFontData(ByteArray<? extends ByteArray<?>> array) {
    super(array);
  }

  /**
   * Constructor with a lower bound.
   *
   * @param data other WritableFontData object to share data with
   * @param offset offset from the other WritableFontData's data
   */
  private WritableFontData(WritableFontData data, int offset) {
    super(data, offset);
  }

  /**
   * Constructor with lower bound and a length bound.
   *
   * @param data other WritableFontData object to share data with
   * @param offset offset from the other WritableFontData's data
   * @param length length of other WritableFontData's data to use
   */
  private WritableFontData(WritableFontData data, int offset, int length) {
    super(data, offset, length);
  }

  /**
   * Makes a slice of this FontData. The returned slice will share the data with
   * the original <code>FontData</code>.
   *
   * @param offset the start of the slice
   * @param length the number of bytes in the slice
   * @return a slice of the original FontData
   */
  @Override
  public WritableFontData slice(int offset, int length) {
    if (offset < 0) {
      throw new IndexOutOfBoundsException("Attempt to bind data outside of its limits.");
    } else if ((offset + length) > this.size()) {
      logger.warning("Attempt to bind data outside of its limits. Truncating slice.");
      length = this.size() - offset;
    }
{'fraction_non_alphanumeric': 0.07949055775142731, 'fraction_numerical': 0.0024154589371980675, 'mean_word_length': 3.207390300230947, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '6271783', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2527, 'n_words': 1060}