winealsa: Fix capture overrun logging.
[wine] / dlls / dwrite / analyzer.c
1 /*
2  *    Text analyzer
3  *
4  * Copyright 2012 Nikolay Sivov for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22
23 #include "dwrite.h"
24 #include "dwrite_private.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
29
30 enum scriptcode {
31     Script_Arabic = 0,
32     Script_C1Controls = 12,
33     Script_Coptic = 13,
34     Script_Cyrillic = 16,
35     Script_Greek = 23,
36     Script_Latin  = 38,
37     Script_Symbol = 77,
38     Script_Unknown = (UINT16)-1
39 };
40
41 struct script_range {
42     UINT16 script;
43     DWORD first;
44     DWORD last;
45 };
46
47 static const struct script_range script_ranges[] = {
48     /* C0 Controls: U+0000–U+001F */
49     /* ASCII punctuation and symbols: U+0020–U+002F */
50     /* ASCII digits: U+0030–U+0039 */
51     /* ASCII punctuation and symbols: U+003A–U+0040 */
52     { Script_Symbol, 0x00, 0x040 },
53     /* Latin uppercase: U+0041–U+005A */
54     { Script_Latin, 0x41, 0x5a },
55     /* ASCII punctuation and symbols: U+005B–U+0060 */
56     { Script_Symbol, 0x5b, 0x060 },
57     /* Latin lowercase: U+0061–U+007A */
58     { Script_Latin, 0x61, 0x7a },
59     /* ASCII punctuation and symbols, control char DEL: U+007B–U+007F */
60     { Script_Symbol, 0x7b, 0x7f },
61     /* C1 Controls: U+0080–U+009F */
62     { Script_C1Controls, 0x80, 0x9f },
63     /* Latin-1 Supplement: U+00A0–U+00FF */
64     /* Latin Extended-A: U+0100–U+017F */
65     /* Latin Extended-B: U+0180–U+024F */
66     /* IPA Extensions: U+0250–U+02AF */
67     /* Spacing Modifier Letters: U+02B0–U+02FF */
68     { Script_Latin, 0xa0, 0x2ff },
69     /* Combining Diacritical Marks: U+0300–U+036F */
70     { Script_Symbol, 0x300, 0x36f },
71     /* Greek: U+0370–U+03E1 */
72     { Script_Greek, 0x370, 0x3e1 },
73     /* Coptic: U+03E2–U+03Ef */
74     { Script_Coptic, 0x3e2, 0x3ef },
75     /* Greek: U+03F0–U+03FF */
76     { Script_Greek, 0x3f0, 0x3ff },
77     /* Cyrillic: U+0400–U+04FF */
78     /* Cyrillic Supplement: U+0500–U+052F */
79     /* Cyrillic Supplement range is incomplete cause it's based on Unicode 5.2
80        that doesn't define some Abkhaz and Azerbaijani letters, we support Unicode 6.0 range here */
81     { Script_Cyrillic, 0x400, 0x52f },
82     /* Arabic: U+0600–U+06FF */
83     { Script_Arabic, 0x600, 0x6ef },
84     /* unsupported range */
85     { Script_Unknown }
86 };
87
88 static UINT16 get_char_script( WCHAR c )
89 {
90     DWORD ch = c;
91     int i;
92
93     for (i = 0; i < sizeof(script_ranges)/sizeof(struct script_range); i++)
94     {
95         const struct script_range *range = &script_ranges[i];
96         if (range->script == Script_Unknown || (range->first <= ch && range->last >= ch))
97             return range->script;
98     }
99
100     return Script_Unknown;
101 }
102
103 static HRESULT analyze_script(const WCHAR *text, UINT32 len, IDWriteTextAnalysisSink *sink)
104 {
105     DWRITE_SCRIPT_ANALYSIS sa;
106     UINT32 pos, i, length;
107
108     if (!len) return S_OK;
109
110     sa.script = get_char_script(*text);
111     sa.shapes = DWRITE_SCRIPT_SHAPES_DEFAULT;
112
113     pos = 0;
114     length = 1;
115
116     for (i = 1; i < len; i++)
117     {
118         UINT16 script = get_char_script(text[i]);
119
120         /* Script_Latin_Symb script type is ignored when preceded or followed by another script */
121         if (sa.script == Script_Symbol) sa.script = script;
122         if (script    == Script_Symbol) script = sa.script;
123         /* this is a length of a sequence to be reported next */
124         if (sa.script == script) length++;
125
126         if (sa.script != script)
127         {
128             HRESULT hr = IDWriteTextAnalysisSink_SetScriptAnalysis(sink, pos, length, &sa);
129             if (FAILED(hr)) return hr;
130             pos = i;
131             length = 1;
132             sa.script = script;
133         }
134     }
135
136     /* 1 length case or normal completion call */
137     return IDWriteTextAnalysisSink_SetScriptAnalysis(sink, pos, length, &sa);
138 }
139
140 static HRESULT WINAPI dwritetextanalyzer_QueryInterface(IDWriteTextAnalyzer *iface, REFIID riid, void **obj)
141 {
142     TRACE("(%s %p)\n", debugstr_guid(riid), obj);
143
144     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteTextAnalyzer))
145     {
146         *obj = iface;
147         return S_OK;
148     }
149
150     *obj = NULL;
151     return E_NOINTERFACE;
152
153 }
154
155 static ULONG WINAPI dwritetextanalyzer_AddRef(IDWriteTextAnalyzer *iface)
156 {
157     return 2;
158 }
159
160 static ULONG WINAPI dwritetextanalyzer_Release(IDWriteTextAnalyzer *iface)
161 {
162     return 1;
163 }
164
165 static HRESULT WINAPI dwritetextanalyzer_AnalyzeScript(IDWriteTextAnalyzer *iface,
166     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
167 {
168     const WCHAR *text;
169     HRESULT hr;
170     UINT32 len;
171
172     TRACE("(%p %u %u %p)\n", source, position, length, sink);
173
174     hr = IDWriteTextAnalysisSource_GetTextAtPosition(source, position, &text, &len);
175     if (FAILED(hr)) return hr;
176
177     return analyze_script(text, len, sink);
178 }
179
180 static HRESULT WINAPI dwritetextanalyzer_AnalyzeBidi(IDWriteTextAnalyzer *iface,
181     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
182 {
183     FIXME("(%p %u %u %p): stub\n", source, position, length, sink);
184     return E_NOTIMPL;
185 }
186
187 static HRESULT WINAPI dwritetextanalyzer_AnalyzeNumberSubstitution(IDWriteTextAnalyzer *iface,
188     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
189 {
190     FIXME("(%p %u %u %p): stub\n", source, position, length, sink);
191     return E_NOTIMPL;
192 }
193
194 static HRESULT WINAPI dwritetextanalyzer_AnalyzeLineBreakpoints(IDWriteTextAnalyzer *iface,
195     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
196 {
197     FIXME("(%p %u %u %p): stub\n", source, position, length, sink);
198     return E_NOTIMPL;
199 }
200
201 static HRESULT WINAPI dwritetextanalyzer_GetGlyphs(IDWriteTextAnalyzer *iface,
202     WCHAR const* text, UINT32 length, IDWriteFontFace* font_face, BOOL is_sideways,
203     BOOL is_rtl, DWRITE_SCRIPT_ANALYSIS const* analysis, WCHAR const* locale,
204     IDWriteNumberSubstitution* substitution, DWRITE_TYPOGRAPHIC_FEATURES const** features,
205     UINT32 const* feature_range_len, UINT32 feature_ranges, UINT32 max_glyph_count,
206     UINT16* clustermap, DWRITE_SHAPING_TEXT_PROPERTIES* text_props, UINT16* glyph_indices,
207     DWRITE_SHAPING_GLYPH_PROPERTIES* glyph_props, UINT32* actual_glyph_count)
208 {
209     FIXME("(%s:%u %p %d %d %p %s %p %p %p %u %u %p %p %p %p %p): stub\n", debugstr_wn(text, length),
210         length, font_face, is_sideways, is_rtl, analysis, debugstr_w(locale), substitution, features, feature_range_len,
211         feature_ranges, max_glyph_count, clustermap, text_props, glyph_indices, glyph_props, actual_glyph_count);
212     return E_NOTIMPL;
213 }
214
215 static HRESULT WINAPI dwritetextanalyzer_GetGlyphPlacements(IDWriteTextAnalyzer *iface,
216     WCHAR const* text, UINT16 const* clustermap, DWRITE_SHAPING_TEXT_PROPERTIES* props,
217     UINT32 text_len, UINT16 const* glyph_indices, DWRITE_SHAPING_GLYPH_PROPERTIES const* glyph_props,
218     UINT32 glyph_count, IDWriteFontFace * font_face, FLOAT fontEmSize, BOOL is_sideways, BOOL is_rtl,
219     DWRITE_SCRIPT_ANALYSIS const* analysis, WCHAR const* locale, DWRITE_TYPOGRAPHIC_FEATURES const** features,
220     UINT32 const* feature_range_len, UINT32 feature_ranges, FLOAT* glyph_advances, DWRITE_GLYPH_OFFSET* glyph_offsets)
221 {
222     FIXME("(%s %p %p %u %p %p %u %p %f %d %d %p %s %p %p %u %p %p): stub\n", debugstr_w(text),
223         clustermap, props, text_len, glyph_indices, glyph_props, glyph_count, font_face, fontEmSize, is_sideways,
224         is_rtl, analysis, debugstr_w(locale), features, feature_range_len, feature_ranges, glyph_advances, glyph_offsets);
225     return E_NOTIMPL;
226 }
227
228 static HRESULT WINAPI dwritetextanalyzer_GetGdiCompatibleGlyphPlacements(IDWriteTextAnalyzer *iface,
229     WCHAR const* text, UINT16 const* clustermap, DWRITE_SHAPING_TEXT_PROPERTIES* props,
230     UINT32 text_len, UINT16 const* glyph_indices, DWRITE_SHAPING_GLYPH_PROPERTIES const* glyph_props,
231     UINT32 glyph_count, IDWriteFontFace * font_face, FLOAT fontEmSize, FLOAT pixels_per_dip,
232     DWRITE_MATRIX const* transform, BOOL use_gdi_natural, BOOL is_sideways, BOOL is_rtl,
233     DWRITE_SCRIPT_ANALYSIS const* analysis, WCHAR const* locale, DWRITE_TYPOGRAPHIC_FEATURES const** features,
234     UINT32 const* feature_range_lengths, UINT32 feature_ranges, FLOAT* glyph_advances, DWRITE_GLYPH_OFFSET* glyph_offsets)
235 {
236     FIXME("(%s %p %p %u %p %p %u %p %f %f %p %d %d %d %p %s %p %p %u %p %p): stub\n", debugstr_w(text),
237         clustermap, props, text_len, glyph_indices, glyph_props, glyph_count, font_face, fontEmSize, pixels_per_dip,
238         transform, use_gdi_natural, is_sideways, is_rtl, analysis, debugstr_w(locale), features, feature_range_lengths,
239         feature_ranges, glyph_advances, glyph_offsets);
240     return E_NOTIMPL;
241 }
242
243 static const struct IDWriteTextAnalyzerVtbl textanalyzervtbl = {
244     dwritetextanalyzer_QueryInterface,
245     dwritetextanalyzer_AddRef,
246     dwritetextanalyzer_Release,
247     dwritetextanalyzer_AnalyzeScript,
248     dwritetextanalyzer_AnalyzeBidi,
249     dwritetextanalyzer_AnalyzeNumberSubstitution,
250     dwritetextanalyzer_AnalyzeLineBreakpoints,
251     dwritetextanalyzer_GetGlyphs,
252     dwritetextanalyzer_GetGlyphPlacements,
253     dwritetextanalyzer_GetGdiCompatibleGlyphPlacements
254 };
255
256 static IDWriteTextAnalyzer textanalyzer = { &textanalyzervtbl };
257
258 HRESULT get_textanalyzer(IDWriteTextAnalyzer **ret)
259 {
260     *ret = &textanalyzer;
261     return S_OK;
262 }