Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/composite/TextRenderer.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef GFX_TextRenderer_H
8
#define GFX_TextRenderer_H
9
10
#include "mozilla/EnumeratedArray.h"
11
#include "mozilla/gfx/2D.h"
12
#include "mozilla/UniquePtr.h"
13
#include "nsISupportsImpl.h"
14
#include <string>
15
16
namespace mozilla {
17
namespace layers {
18
19
class Compositor;
20
class TextureSource;
21
class TextureSourceProvider;
22
struct FontBitmapInfo;
23
24
class TextRenderer
25
{
26
  ~TextRenderer();
27
28
public:
29
  NS_INLINE_DECL_REFCOUNTING(TextRenderer)
30
31
  enum class FontType {
32
    Default,
33
    FixedWidth,
34
    NumTypes
35
  };
36
37
  explicit TextRenderer()
38
0
  {}
39
40
  RefPtr<TextureSource>
41
  RenderText(TextureSourceProvider* aProvider,
42
             const std::string& aText,
43
             uint32_t aTextSize,
44
             uint32_t aTargetPixelWidth,
45
             FontType aFontType);
46
47
  void RenderText(Compositor* aCompositor,
48
                  const std::string& aText,
49
                  const gfx::IntPoint& aOrigin,
50
                  const gfx::Matrix4x4& aTransform, uint32_t aTextSize,
51
                  uint32_t aTargetPixelWidth,
52
                  FontType aFontType = FontType::Default);
53
54
  struct FontCache {
55
    ~FontCache();
56
    RefPtr<gfx::DataSourceSurface> mGlyphBitmaps;
57
    gfx::DataSourceSurface::MappedSurface mMap;
58
    const FontBitmapInfo* mInfo;
59
  };
60
61
protected:
62
  // Note that this may still fail to set mGlyphBitmaps to a valid value
63
  // if the underlying CreateDataSourceSurface fails for some reason.
64
  bool EnsureInitialized(FontType aType);
65
66
  static const FontBitmapInfo* GetFontInfo(FontType aType);
67
68
private:
69
  EnumeratedArray<FontType, FontType::NumTypes, UniquePtr<FontCache>> mFonts;
70
};
71
72
struct FontBitmapInfo {
73
  Maybe<unsigned int> mGlyphWidth;
74
  Maybe<const unsigned short*> mGlyphWidths;
75
  unsigned int mTextureWidth;
76
  unsigned int mTextureHeight;
77
  unsigned int mCellWidth;
78
  unsigned int mCellHeight;
79
  unsigned int mFirstChar;
80
  const unsigned char* mPNG;
81
  size_t mPNGLength;
82
83
0
  unsigned int GetGlyphWidth(char aGlyph) const {
84
0
    if (mGlyphWidth) {
85
0
      return mGlyphWidth.value();
86
0
    }
87
0
    MOZ_ASSERT(unsigned(aGlyph) >= mFirstChar);
88
0
    return mGlyphWidths.value()[unsigned(aGlyph) - mFirstChar];
89
0
  }
90
};
91
92
} // namespace layers
93
} // namespace mozilla
94
95
#endif