|
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
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_DWRITETEXTANALYSIS_H |
|
7 #define GFX_DWRITETEXTANALYSIS_H |
|
8 |
|
9 #include "gfxDWriteCommon.h" |
|
10 |
|
11 // Helper source/sink class for text analysis. |
|
12 class TextAnalysis |
|
13 : public IDWriteTextAnalysisSource, |
|
14 public IDWriteTextAnalysisSink |
|
15 { |
|
16 public: |
|
17 |
|
18 // IUnknown interface |
|
19 IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) |
|
20 { |
|
21 if (iid == __uuidof(IDWriteTextAnalysisSource)) { |
|
22 *ppObject = static_cast<IDWriteTextAnalysisSource*>(this); |
|
23 return S_OK; |
|
24 } else if (iid == __uuidof(IDWriteTextAnalysisSink)) { |
|
25 *ppObject = static_cast<IDWriteTextAnalysisSink*>(this); |
|
26 return S_OK; |
|
27 } else if (iid == __uuidof(IUnknown)) { |
|
28 *ppObject = |
|
29 static_cast<IUnknown*>(static_cast<IDWriteTextAnalysisSource*>(this)); |
|
30 return S_OK; |
|
31 } else { |
|
32 return E_NOINTERFACE; |
|
33 } |
|
34 } |
|
35 |
|
36 IFACEMETHOD_(ULONG, AddRef)() |
|
37 { |
|
38 return 1; |
|
39 } |
|
40 |
|
41 IFACEMETHOD_(ULONG, Release)() |
|
42 { |
|
43 return 1; |
|
44 } |
|
45 |
|
46 // A single contiguous run of characters containing the same analysis |
|
47 // results. |
|
48 struct Run |
|
49 { |
|
50 UINT32 mTextStart; // starting text position of this run |
|
51 UINT32 mTextLength; // number of contiguous code units covered |
|
52 UINT32 mGlyphStart; // starting glyph in the glyphs array |
|
53 UINT32 mGlyphCount; // number of glyphs associated with this run of |
|
54 // text |
|
55 DWRITE_SCRIPT_ANALYSIS mScript; |
|
56 UINT8 mBidiLevel; |
|
57 bool mIsSideways; |
|
58 |
|
59 inline bool ContainsTextPosition(UINT32 aTextPosition) const |
|
60 { |
|
61 return aTextPosition >= mTextStart |
|
62 && aTextPosition < mTextStart + mTextLength; |
|
63 } |
|
64 |
|
65 Run *nextRun; |
|
66 }; |
|
67 |
|
68 public: |
|
69 TextAnalysis(const wchar_t* text, |
|
70 UINT32 textLength, |
|
71 const wchar_t* localeName, |
|
72 DWRITE_READING_DIRECTION readingDirection); |
|
73 |
|
74 ~TextAnalysis(); |
|
75 |
|
76 STDMETHODIMP GenerateResults(IDWriteTextAnalyzer* textAnalyzer, |
|
77 Run **runHead); |
|
78 |
|
79 // IDWriteTextAnalysisSource implementation |
|
80 |
|
81 IFACEMETHODIMP GetTextAtPosition(UINT32 textPosition, |
|
82 OUT WCHAR const** textString, |
|
83 OUT UINT32* textLength); |
|
84 |
|
85 IFACEMETHODIMP GetTextBeforePosition(UINT32 textPosition, |
|
86 OUT WCHAR const** textString, |
|
87 OUT UINT32* textLength); |
|
88 |
|
89 IFACEMETHODIMP_(DWRITE_READING_DIRECTION) |
|
90 GetParagraphReadingDirection() throw(); |
|
91 |
|
92 IFACEMETHODIMP GetLocaleName(UINT32 textPosition, |
|
93 OUT UINT32* textLength, |
|
94 OUT WCHAR const** localeName); |
|
95 |
|
96 IFACEMETHODIMP |
|
97 GetNumberSubstitution(UINT32 textPosition, |
|
98 OUT UINT32* textLength, |
|
99 OUT IDWriteNumberSubstitution** numberSubstitution); |
|
100 |
|
101 // IDWriteTextAnalysisSink implementation |
|
102 |
|
103 IFACEMETHODIMP |
|
104 SetScriptAnalysis(UINT32 textPosition, |
|
105 UINT32 textLength, |
|
106 DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis); |
|
107 |
|
108 IFACEMETHODIMP |
|
109 SetLineBreakpoints(UINT32 textPosition, |
|
110 UINT32 textLength, |
|
111 const DWRITE_LINE_BREAKPOINT* lineBreakpoints); |
|
112 |
|
113 IFACEMETHODIMP SetBidiLevel(UINT32 textPosition, |
|
114 UINT32 textLength, |
|
115 UINT8 explicitLevel, |
|
116 UINT8 resolvedLevel); |
|
117 |
|
118 IFACEMETHODIMP |
|
119 SetNumberSubstitution(UINT32 textPosition, |
|
120 UINT32 textLength, |
|
121 IDWriteNumberSubstitution* numberSubstitution); |
|
122 |
|
123 protected: |
|
124 Run *FetchNextRun(IN OUT UINT32* textLength); |
|
125 |
|
126 void SetCurrentRun(UINT32 textPosition); |
|
127 |
|
128 void SplitCurrentRun(UINT32 splitPosition); |
|
129 |
|
130 protected: |
|
131 // Input |
|
132 // (weak references are fine here, since this class is a transient |
|
133 // stack-based helper that doesn't need to copy data) |
|
134 UINT32 mTextLength; |
|
135 const wchar_t* mText; |
|
136 const wchar_t* mLocaleName; |
|
137 DWRITE_READING_DIRECTION mReadingDirection; |
|
138 |
|
139 // Current processing state. |
|
140 Run *mCurrentRun; |
|
141 |
|
142 // Output is a list of runs starting here |
|
143 Run mRunHead; |
|
144 }; |
|
145 |
|
146 #endif /* GFX_DWRITETEXTANALYSIS_H */ |