Example #1
0
PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize()
{
    if (!m_buffer) {
        setErrorString("Empty Buffer");
        return nullptr;
    }

    // This is the largest web font size which we'll try to transcode.
    static const size_t maxWebFontSize = 30 * 1024 * 1024; // 30 MB
    if (m_buffer->size() > maxWebFontSize) {
        setErrorString("Web font size more than 30MB");
        return nullptr;
    }

    // A transcoded font is usually smaller than an original font.
    // However, it can be slightly bigger than the original one due to
    // name table replacement and/or padding for glyf table.
    //
    // With WOFF fonts, however, we'll be decompressing, so the result can be
    // much larger than the original.

    ots::ExpandingMemoryStream output(m_buffer->size(), maxWebFontSize);
    BlinkOTSContext otsContext;

    if (!otsContext.Process(&output, reinterpret_cast<const uint8_t*>(m_buffer->data()), m_buffer->size())) {
        setErrorString(otsContext.getErrorString());
        return nullptr;
    }

    const size_t transcodeLen = output.Tell();
    return SharedBuffer::create(static_cast<unsigned char*>(output.get()), transcodeLen);
}
Example #2
0
sk_sp<SkTypeface> WebFontDecoder::decode(SharedBuffer* buffer) {
  if (!buffer) {
    setErrorString("Empty Buffer");
    return nullptr;
  }

  // This is the largest web font size which we'll try to transcode.
  // TODO(bashi): 30MB seems low. Update the limit if necessary.
  static const size_t maxWebFontSize = 30 * 1024 * 1024;  // 30 MB
  if (buffer->size() > maxWebFontSize) {
    setErrorString("Web font size more than 30MB");
    return nullptr;
  }

  // Most web fonts are compressed, so the result can be much larger than
  // the original.
  ots::ExpandingMemoryStream output(buffer->size(), maxWebFontSize);
  double start = currentTime();
  BlinkOTSContext otsContext;
  const char* data = buffer->data();

  TRACE_EVENT_BEGIN0("blink", "DecodeFont");
  bool ok = otsContext.Process(&output, reinterpret_cast<const uint8_t*>(data),
                               buffer->size());
  TRACE_EVENT_END0("blink", "DecodeFont");

  if (!ok) {
    setErrorString(otsContext.getErrorString());
    return nullptr;
  }

  const size_t decodedLength = output.Tell();
  recordDecodeSpeedHistogram(data, buffer->size(), currentTime() - start,
                             decodedLength);

  sk_sp<SkData> skData = SkData::MakeWithCopy(output.get(), decodedLength);
  SkMemoryStream* stream = new SkMemoryStream(skData);
#if OS(WIN)
  sk_sp<SkTypeface> typeface(
      FontCache::fontCache()->fontManager()->createFromStream(stream));
#else
  sk_sp<SkTypeface> typeface = SkTypeface::MakeFromStream(stream);
#endif
  if (!typeface) {
    setErrorString("Not a valid font data");
    return nullptr;
  }

  m_decodedSize = decodedLength;
  return typeface;
}