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_GRAPHITESHAPER_H
7 #define GFX_GRAPHITESHAPER_H
8 
9 #include "gfxFont.h"
10 
11 #include "mozilla/gfx/2D.h"
12 
13 #include "ThebesRLBoxTypes.h"
14 
15 struct gr_face;
16 struct gr_font;
17 struct gr_segment;
18 
19 class gfxGraphiteShaper : public gfxFontShaper {
20  public:
21   explicit gfxGraphiteShaper(gfxFont* aFont);
22   virtual ~gfxGraphiteShaper();
23 
24   bool ShapeText(DrawTarget* aDrawTarget, const char16_t* aText,
25                  uint32_t aOffset, uint32_t aLength, Script aScript,
26                  bool aVertical, RoundingFlags aRounding,
27                  gfxShapedText* aShapedText) override;
28 
29   static void Shutdown();
30 
31  protected:
32   nsresult SetGlyphsFromSegment(gfxShapedText* aShapedText, uint32_t aOffset,
33                                 uint32_t aLength, const char16_t* aText,
34                                 tainted_opaque_gr<char16_t*> t_aText,
35                                 tainted_opaque_gr<gr_segment*> aSegment,
36                                 RoundingFlags aRounding);
37 
38   // Graphite is run in a rlbox sandbox. Callback GrGetAdvance must be
39   // explicitly permitted. Since the sandbox is owned in gfxFontEntry class,
40   // gfxFontEntry needs access to the protected callback.
41   friend class gfxFontEntry;
42   static tainted_opaque_gr<float> GrGetAdvance(
43       rlbox_sandbox_gr& sandbox, tainted_opaque_gr<const void*> appFontHandle,
44       tainted_opaque_gr<uint16_t> glyphid);
45 
46   tainted_opaque_gr<gr_face*>
47       mGrFace;  // owned by the font entry; shaper must call
48                 // gfxFontEntry::ReleaseGrFace when finished with it
49   tainted_opaque_gr<gr_font*> mGrFont;  // owned by the shaper itself
50 
51   // All libGraphite functionality is sandboxed. This is the sandbox instance.
52   rlbox_sandbox_gr* mSandbox;
53 
54   // Holds the handle to the permitted callback into Firefox for the sandboxed
55   // libGraphite
56   sandbox_callback_gr<float (*)(const void*, uint16_t)>* mCallback;
57 
58   struct CallbackData {
59     // mFont is a pointer to the font that owns this shaper, so it will
60     // remain valid throughout our lifetime
61     gfxFont* MOZ_NON_OWNING_REF mFont;
62   };
63 
64   CallbackData mCallbackData;
65   static thread_local CallbackData* tl_GrGetAdvanceData;
66 
67   bool mFallbackToSmallCaps;  // special fallback for the petite-caps case
68 
69   // Convert HTML 'lang' (BCP47) to Graphite language code
70   static uint32_t GetGraphiteTagForLang(const nsCString& aLang);
71   static nsTHashtable<nsUint32HashKey>* sLanguageTags;
72 };
73 
74 #endif /* GFX_GRAPHITESHAPER_H */
75