DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (409f3966645a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "TextRenderer.h"
#include "FontData.h"
#include "ConsolasFontData.h"
#include "png.h"
#include "mozilla/Base64.h"
#include "mozilla/layers/Compositor.h"
#include "mozilla/layers/TextureHost.h"
#include "mozilla/layers/Effects.h"

namespace mozilla {
namespace layers {

using namespace gfx;
using namespace std;

const Float sBackgroundOpacity = 0.8f;
const SurfaceFormat sTextureFormat = SurfaceFormat::B8G8R8A8;

static void PNGAPI info_callback(png_structp png_ptr, png_infop info_ptr)
{
  png_read_update_info(png_ptr, info_ptr);
}

static void PNGAPI row_callback(png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass)
{
  MOZ_ASSERT(sTextureFormat == SurfaceFormat::B8G8R8A8);

  TextRenderer::FontCache* cache =
    static_cast<TextRenderer::FontCache*>(png_get_progressive_ptr(png_ptr));

  uint32_t* dst = (uint32_t*)(cache->mMap.mData + cache->mMap.mStride * row_num);

  for (uint32_t x = 0; x < cache->mInfo->mTextureWidth; x++) {
    // We blend to a transparent white background, this will make text readable
    // even if it's on a dark background. Without hurting our ability to
    // interact with the content behind the text.
    Float alphaValue = Float(0xFF - new_row[x]) / 255.0f;
    Float baseValue = sBackgroundOpacity * (1.0f - alphaValue);
    Color pixelColor(baseValue, baseValue, baseValue, baseValue + alphaValue);
    dst[x] = pixelColor.ToABGR();
  }
}

TextRenderer::~TextRenderer()
{
}

TextRenderer::FontCache::~FontCache()
{
  mGlyphBitmaps->Unmap();
}

void
TextRenderer::RenderText(Compositor* aCompositor,
                         const string& aText,
                         const IntPoint& aOrigin,
                         const Matrix4x4& aTransform, uint32_t aTextSize,
                         uint32_t aTargetPixelWidth,
                         FontType aFontType)
{
  const FontBitmapInfo* info = GetFontInfo(aFontType);

  // For now we only have a bitmap font with a 24px cell size, so we just
  // scale it up if the user wants larger text.
  Float scaleFactor = Float(aTextSize) / Float(info->mCellHeight);
  aTargetPixelWidth /= scaleFactor;

  RefPtr<TextureSource> src = RenderText(
    aCompositor,
    aText,
    aTextSize,
    aTargetPixelWidth,
    aFontType);
  if (!src) {
    return;
  }

  RefPtr<EffectRGB> effect = new EffectRGB(src, true, SamplingFilter::LINEAR);
  EffectChain chain;
  chain.mPrimaryEffect = effect;

  Matrix4x4 transform = aTransform;
  transform.PreScale(scaleFactor, scaleFactor, 1.0f);

  IntRect drawRect(aOrigin, src->GetSize());
  IntRect clip(-10000, -10000, 20000, 20000);
  aCompositor->DrawQuad(Rect(drawRect), clip, chain, 1.0f, transform);
}

RefPtr<TextureSource>
TextRenderer::RenderText(TextureSourceProvider* aProvider,
                         const string& aText,
                         uint32_t aTextSize,
                         uint32_t aTargetPixelWidth,
                         FontType aFontType)
{
  if (!EnsureInitialized(aFontType)) {
    return nullptr;
  }

  FontCache* cache = mFonts[aFontType].get();
  const FontBitmapInfo* info = cache->mInfo;

  uint32_t numLines = 1;
  uint32_t maxWidth = 0;
  uint32_t lineWidth = 0;
  // Calculate the size of the surface needed to draw all the glyphs.
  for (uint32_t i = 0; i < aText.length(); i++) {
    // Insert a line break if we go past the TargetPixelWidth.
    // XXX - this has the downside of overrunning the intended width, causing
    // things at the edge of a window to be cut off.
    if (aText[i] == '\n' || (aText[i] == ' ' && lineWidth > aTargetPixelWidth)) {
      numLines++;
      lineWidth = 0;
      continue;
    }

    lineWidth += info->GetGlyphWidth(aText[i]);
    maxWidth = std::max(lineWidth, maxWidth);
  }

  // Create a surface to draw our glyphs to.
  RefPtr<DataSourceSurface> textSurf =
    Factory::CreateDataSourceSurface(IntSize(maxWidth, numLines * info->mCellHeight), sTextureFormat);
  if (NS_WARN_IF(!textSurf)) {
    return nullptr;
  }

  DataSourceSurface::MappedSurface map;
  if (NS_WARN_IF(!textSurf->Map(DataSourceSurface::MapType::READ_WRITE, &map))) {
    return nullptr;
  }

  // Initialize the surface to transparent white.
  memset(map.mData, uint8_t(sBackgroundOpacity * 255.0f),
         numLines * info->mCellHeight * map.mStride);

  uint32_t currentXPos = 0;
  uint32_t currentYPos = 0;

  const unsigned int kGlyphsPerLine = info->mTextureWidth / info->mCellWidth;

  // Copy our glyphs onto the surface.
  for (uint32_t i = 0; i < aText.length(); i++) {
    if (aText[i] == '\n' || (aText[i] == ' ' && currentXPos > aTargetPixelWidth)) {
      currentYPos += info->mCellHeight;
      currentXPos = 0;
      continue;
    }

    uint32_t index = aText[i] - info->mFirstChar;
    uint32_t glyphXOffset = (index % kGlyphsPerLine) * info->mCellWidth * BytesPerPixel(sTextureFormat);
    uint32_t truncatedLine = index / kGlyphsPerLine;
    uint32_t glyphYOffset =  truncatedLine * info->mCellHeight * cache->mMap.mStride;

    uint32_t glyphWidth = info->GetGlyphWidth(aText[i]);

    for (uint32_t y = 0; y < info->mCellHeight; y++) {
      memcpy(map.mData + (y + currentYPos) * map.mStride + currentXPos * BytesPerPixel(sTextureFormat),
             cache->mMap.mData + glyphYOffset + y * cache->mMap.mStride + glyphXOffset,
             glyphWidth * BytesPerPixel(sTextureFormat));
    }

    currentXPos += glyphWidth;
  }

  textSurf->Unmap();

  RefPtr<DataTextureSource> src = aProvider->CreateDataTextureSource();

  if (!src->Update(textSurf)) {
    // Upload failed.
    return nullptr;
  }

  return src;
}

/* static */ const FontBitmapInfo*
TextRenderer::GetFontInfo(FontType aType)
{
  switch (aType) {
  case FontType::Default:
    return &sDefaultCompositorFont;
  case FontType::FixedWidth:
    return &sFixedWidthCompositorFont;
  default:
    MOZ_ASSERT_UNREACHABLE("unknown font type");
    return nullptr;
  }
}

bool
TextRenderer::EnsureInitialized(FontType aType)
{
  if (mFonts[aType]) {
    return true;
  }

  const FontBitmapInfo* info = GetFontInfo(aType);

  IntSize size(info->mTextureWidth, info->mTextureHeight);
  RefPtr<DataSourceSurface> surface = Factory::CreateDataSourceSurface(size, sTextureFormat);
  if (NS_WARN_IF(!surface)) {
    return false;
  }

  DataSourceSurface::MappedSurface map;
  if (NS_WARN_IF(!surface->Map(DataSourceSurface::MapType::READ_WRITE, &map))) {
    return false;
  }

  UniquePtr<FontCache> cache = MakeUnique<FontCache>();
  cache->mGlyphBitmaps = surface;
  cache->mMap = map;
  cache->mInfo = info;

  png_structp png_ptr = NULL;
  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);

  png_set_progressive_read_fn(png_ptr, cache.get(), info_callback, row_callback, nullptr);
  png_infop info_ptr = NULL;
  info_ptr = png_create_info_struct(png_ptr);

  png_process_data(png_ptr, info_ptr, (uint8_t*)info->mPNG, info->mPNGLength);

  png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);

  mFonts[aType] = std::move(cache);
  return true;
}

} // namespace layers
} // namespace mozilla