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