msvcrt: Return child exit code in _pclose function.
[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_Armenian = 1,
33     Script_Balinese = 2,
34     Script_Bengali = 3,
35     Script_Buginese = 6,
36     Script_Canadian = 8,
37     Script_Cherokee = 11,
38     Script_Controls = 12,
39     Script_Coptic = 13,
40     Script_Cyrillic = 16,
41     Script_Devanagari = 18,
42     Script_Ethiopic = 19,
43     Script_Georgian = 20,
44     Script_Greek = 23,
45     Script_Gujarati = 24,
46     Script_Gurmukhi = 25,
47     Script_Hangul = 27,
48     Script_Hebrew = 29,
49     Script_Kannada = 32,
50     Script_Khmer = 36,
51     Script_Lao = 37,
52     Script_Latin = 38,
53     Script_Lepcha = 39,
54     Script_Limbu = 40,
55     Script_Malayalam = 44,
56     Script_Mongolian = 45,
57     Script_Myanmar = 46,
58     Script_New_TaiLue = 47,
59     Script_NKo = 48,
60     Script_Ogham = 49,
61     Script_OlChiki = 50,
62     Script_Oriya = 53,
63     Script_Runic = 58,
64     Script_Sinhala = 61,
65     Script_Sundanese = 62,
66     Script_Syriac = 64,
67     Script_TaiLe = 67,
68     Script_Tamil = 68,
69     Script_Telugu = 69,
70     Script_Thaana = 70,
71     Script_Thai = 71,
72     Script_Tibetan = 72,
73     Script_Symbol = 77,
74     Script_Unknown = (UINT16)-1
75 };
76
77 struct script_range {
78     UINT16 script;
79     DWORD first;
80     DWORD last;
81 };
82
83 static const struct script_range script_ranges[] = {
84     /* C0 Controls: U+0000–U+001F */
85     /* ASCII punctuation and symbols: U+0020–U+002F */
86     /* ASCII digits: U+0030–U+0039 */
87     /* ASCII punctuation and symbols: U+003A–U+0040 */
88     { Script_Symbol, 0x00, 0x040 },
89     /* Latin uppercase: U+0041–U+005A */
90     { Script_Latin, 0x41, 0x5a },
91     /* ASCII punctuation and symbols: U+005B–U+0060 */
92     { Script_Symbol, 0x5b, 0x060 },
93     /* Latin lowercase: U+0061–U+007A */
94     { Script_Latin, 0x61, 0x7a },
95     /* ASCII punctuation and symbols, control char DEL: U+007B–U+007F */
96     { Script_Symbol, 0x7b, 0x7f },
97     /* C1 Controls: U+0080–U+009F */
98     { Script_Controls, 0x80, 0x9f },
99     /* Latin-1 Supplement: U+00A0–U+00FF */
100     /* Latin Extended-A: U+0100–U+017F */
101     /* Latin Extended-B: U+0180–U+024F */
102     /* IPA Extensions: U+0250–U+02AF */
103     /* Spacing Modifier Letters: U+02B0–U+02FF */
104     { Script_Latin, 0xa0, 0x2ff },
105     /* Combining Diacritical Marks: U+0300–U+036F */
106     { Script_Symbol, 0x300, 0x36f },
107     /* Greek: U+0370–U+03E1 */
108     { Script_Greek, 0x370, 0x3e1 },
109     /* Coptic: U+03E2–U+03Ef */
110     { Script_Coptic, 0x3e2, 0x3ef },
111     /* Greek: U+03F0–U+03FF */
112     { Script_Greek, 0x3f0, 0x3ff },
113     /* Cyrillic: U+0400–U+04FF */
114     /* Cyrillic Supplement: U+0500–U+052F */
115     /* Cyrillic Supplement range is incomplete cause it's based on Unicode 5.2
116        that doesn't define some Abkhaz and Azerbaijani letters, we support Unicode 6.0 range here */
117     { Script_Cyrillic, 0x400, 0x52f },
118     /* Armenian: U+0530–U+058F */
119     { Script_Armenian, 0x530, 0x58f },
120     /* Hebrew: U+0590–U+05FF */
121     { Script_Hebrew, 0x590, 0x5ff },
122     /* Arabic: U+0600–U+06FF */
123     { Script_Arabic, 0x600, 0x6ff },
124     /* Syriac: U+0600–U+06FF */
125     { Script_Syriac, 0x700, 0x74f },
126     /* Arabic Supplement: U+0750–U+077F */
127     { Script_Arabic, 0x750, 0x77f },
128     /* Thaana: U+0780–U+07BF */
129     { Script_Thaana, 0x780, 0x7bf },
130     /* N'Ko: U+07C0–U+07FF */
131     { Script_NKo, 0x7c0, 0x7ff },
132     /* Devanagari: U+0900–U+097F */
133     { Script_Devanagari, 0x900, 0x97f },
134     /* Bengali: U+0980–U+09FF */
135     { Script_Bengali, 0x980, 0x9ff },
136     /* Gurmukhi: U+0A00–U+0A7F */
137     { Script_Gurmukhi, 0xa00, 0xa7f },
138     /* Gujarati: U+0A80–U+0AFF */
139     { Script_Gujarati, 0xa80, 0xaff },
140     /* Oriya: U+0B00–U+0B7F */
141     { Script_Oriya, 0xb00, 0xb7f },
142     /* Tamil: U+0B80–U+0BFF */
143     { Script_Tamil, 0xb80, 0xbff },
144     /* Telugu: U+0C00–U+0C7F */
145     { Script_Telugu, 0xc00, 0xc7f },
146     /* Kannada: U+0C80–U+0CFF */
147     { Script_Kannada, 0xc80, 0xcff },
148     /* Malayalam: U+0D00–U+0D7F */
149     { Script_Malayalam, 0xd00, 0xd7f },
150     /* Sinhala: U+0D80–U+0DFF */
151     { Script_Sinhala, 0xd80, 0xdff },
152     /* Thai: U+0E00–U+0E7F */
153     { Script_Thai, 0xe00, 0xe7f },
154     /* Lao: U+0E80–U+0EFF */
155     { Script_Lao, 0xe80, 0xeff },
156     /* Tibetan: U+0F00–U+0FFF */
157     { Script_Tibetan, 0xf00, 0xfff },
158     /* Myanmar: U+1000–U+109F */
159     { Script_Myanmar, 0x1000, 0x109f },
160     /* Georgian: U+10A0–U+10FF */
161     { Script_Georgian, 0x10a0, 0x10ff },
162     /* Hangul Jamo: U+1100–U+11FF */
163     { Script_Hangul, 0x1100, 0x11ff },
164     /* Ethiopic: U+1200–U+137F */
165     /* Ethiopic Extensions: U+1380–U+139F */
166     { Script_Ethiopic, 0x1200, 0x139f },
167     /* Cherokee: U+13A0–U+13FF */
168     { Script_Cherokee, 0x13a0, 0x13ff },
169     /* Canadian Aboriginal Syllabics: U+1400–U+167F */
170     { Script_Canadian, 0x1400, 0x167f },
171     /* Ogham: U+1680–U+169F */
172     { Script_Ogham, 0x1680, 0x169f },
173     /* Runic: U+16A0–U+16F0 */
174     { Script_Runic, 0x16a0, 0x16f0 },
175     /* Khmer: U+1780–U+17FF */
176     { Script_Khmer, 0x1780, 0x17ff },
177     /* Mongolian: U+1800–U+18AF */
178     { Script_Mongolian, 0x1800, 0x18af },
179     /* Limbu: U+1900–U+194F */
180     { Script_Limbu, 0x1900, 0x194f },
181     /* Tai Le: U+1950–U+197F */
182     { Script_TaiLe, 0x1950, 0x197f },
183     /* New Tai Lue: U+1980–U+19DF */
184     { Script_New_TaiLue, 0x1980, 0x19df },
185     /* Khmer Symbols: U+19E0–U+19FF */
186     { Script_Khmer, 0x19e0, 0x19ff },
187     /* Buginese: U+1A00–U+1A1F */
188     { Script_Buginese, 0x1a00, 0x1a1f },
189     /* Tai Tham: U+1A20–U+1AAF */
190     { Script_Symbol, 0x1a20, 0x1aaf },
191     /* Balinese: U+1B00–U+1B7F */
192     { Script_Balinese, 0x1b00, 0x1b7f },
193     /* Sundanese: U+1B80–U+1BBF */
194     { Script_Sundanese, 0x1b80, 0x1bbf },
195     /* Batak: U+1BC0–U+1BFF */
196     { Script_Symbol, 0x1bc0, 0x1bff },
197     /* Lepcha: U+1C00–U+1C4F */
198     { Script_Lepcha, 0x1c00, 0x1c4f },
199     /* Ol Chiki: U+1C50–U+1C7F */
200     { Script_OlChiki, 0x1c50, 0x1c7f },
201     /* Sundanese Supplement: U+1CC0–U+1CCF */
202     { Script_Symbol, 0x1cc0, 0x1ccf },
203     /* Vedic Extensions: U+1CD0-U+1CFF */
204     { Script_Devanagari, 0x1cd0, 0x1cff },
205     /* Phonetic Extensions: U+1D00–U+1DBF */
206     { Script_Latin, 0x1d00, 0x1dbf },
207     /* unsupported range */
208     { Script_Unknown }
209 };
210
211 static UINT16 get_char_script( WCHAR c )
212 {
213     DWORD ch = c;
214     int i;
215
216     for (i = 0; i < sizeof(script_ranges)/sizeof(struct script_range); i++)
217     {
218         const struct script_range *range = &script_ranges[i];
219         if (range->script == Script_Unknown || (range->first <= ch && range->last >= ch))
220             return range->script;
221     }
222
223     return Script_Unknown;
224 }
225
226 static HRESULT analyze_script(const WCHAR *text, UINT32 len, IDWriteTextAnalysisSink *sink)
227 {
228     DWRITE_SCRIPT_ANALYSIS sa;
229     UINT32 pos, i, length;
230
231     if (!len) return S_OK;
232
233     sa.script = get_char_script(*text);
234     sa.shapes = DWRITE_SCRIPT_SHAPES_DEFAULT;
235
236     pos = 0;
237     length = 1;
238
239     for (i = 1; i < len; i++)
240     {
241         UINT16 script = get_char_script(text[i]);
242
243         /* Script_Latin_Symb script type is ignored when preceded or followed by another script */
244         if (sa.script == Script_Symbol) sa.script = script;
245         if (script    == Script_Symbol) script = sa.script;
246         /* this is a length of a sequence to be reported next */
247         if (sa.script == script) length++;
248
249         if (sa.script != script)
250         {
251             HRESULT hr = IDWriteTextAnalysisSink_SetScriptAnalysis(sink, pos, length, &sa);
252             if (FAILED(hr)) return hr;
253             pos = i;
254             length = 1;
255             sa.script = script;
256         }
257     }
258
259     /* 1 length case or normal completion call */
260     return IDWriteTextAnalysisSink_SetScriptAnalysis(sink, pos, length, &sa);
261 }
262
263 static HRESULT WINAPI dwritetextanalyzer_QueryInterface(IDWriteTextAnalyzer *iface, REFIID riid, void **obj)
264 {
265     TRACE("(%s %p)\n", debugstr_guid(riid), obj);
266
267     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteTextAnalyzer))
268     {
269         *obj = iface;
270         return S_OK;
271     }
272
273     *obj = NULL;
274     return E_NOINTERFACE;
275
276 }
277
278 static ULONG WINAPI dwritetextanalyzer_AddRef(IDWriteTextAnalyzer *iface)
279 {
280     return 2;
281 }
282
283 static ULONG WINAPI dwritetextanalyzer_Release(IDWriteTextAnalyzer *iface)
284 {
285     return 1;
286 }
287
288 static HRESULT WINAPI dwritetextanalyzer_AnalyzeScript(IDWriteTextAnalyzer *iface,
289     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
290 {
291     const WCHAR *text;
292     HRESULT hr;
293     UINT32 len;
294
295     TRACE("(%p %u %u %p)\n", source, position, length, sink);
296
297     hr = IDWriteTextAnalysisSource_GetTextAtPosition(source, position, &text, &len);
298     if (FAILED(hr)) return hr;
299
300     return analyze_script(text, len, sink);
301 }
302
303 static HRESULT WINAPI dwritetextanalyzer_AnalyzeBidi(IDWriteTextAnalyzer *iface,
304     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
305 {
306     FIXME("(%p %u %u %p): stub\n", source, position, length, sink);
307     return E_NOTIMPL;
308 }
309
310 static HRESULT WINAPI dwritetextanalyzer_AnalyzeNumberSubstitution(IDWriteTextAnalyzer *iface,
311     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
312 {
313     FIXME("(%p %u %u %p): stub\n", source, position, length, sink);
314     return E_NOTIMPL;
315 }
316
317 static HRESULT WINAPI dwritetextanalyzer_AnalyzeLineBreakpoints(IDWriteTextAnalyzer *iface,
318     IDWriteTextAnalysisSource* source, UINT32 position, UINT32 length, IDWriteTextAnalysisSink* sink)
319 {
320     FIXME("(%p %u %u %p): stub\n", source, position, length, sink);
321     return E_NOTIMPL;
322 }
323
324 static HRESULT WINAPI dwritetextanalyzer_GetGlyphs(IDWriteTextAnalyzer *iface,
325     WCHAR const* text, UINT32 length, IDWriteFontFace* font_face, BOOL is_sideways,
326     BOOL is_rtl, DWRITE_SCRIPT_ANALYSIS const* analysis, WCHAR const* locale,
327     IDWriteNumberSubstitution* substitution, DWRITE_TYPOGRAPHIC_FEATURES const** features,
328     UINT32 const* feature_range_len, UINT32 feature_ranges, UINT32 max_glyph_count,
329     UINT16* clustermap, DWRITE_SHAPING_TEXT_PROPERTIES* text_props, UINT16* glyph_indices,
330     DWRITE_SHAPING_GLYPH_PROPERTIES* glyph_props, UINT32* actual_glyph_count)
331 {
332     FIXME("(%s:%u %p %d %d %p %s %p %p %p %u %u %p %p %p %p %p): stub\n", debugstr_wn(text, length),
333         length, font_face, is_sideways, is_rtl, analysis, debugstr_w(locale), substitution, features, feature_range_len,
334         feature_ranges, max_glyph_count, clustermap, text_props, glyph_indices, glyph_props, actual_glyph_count);
335     return E_NOTIMPL;
336 }
337
338 static HRESULT WINAPI dwritetextanalyzer_GetGlyphPlacements(IDWriteTextAnalyzer *iface,
339     WCHAR const* text, UINT16 const* clustermap, DWRITE_SHAPING_TEXT_PROPERTIES* props,
340     UINT32 text_len, UINT16 const* glyph_indices, DWRITE_SHAPING_GLYPH_PROPERTIES const* glyph_props,
341     UINT32 glyph_count, IDWriteFontFace * font_face, FLOAT fontEmSize, BOOL is_sideways, BOOL is_rtl,
342     DWRITE_SCRIPT_ANALYSIS const* analysis, WCHAR const* locale, DWRITE_TYPOGRAPHIC_FEATURES const** features,
343     UINT32 const* feature_range_len, UINT32 feature_ranges, FLOAT* glyph_advances, DWRITE_GLYPH_OFFSET* glyph_offsets)
344 {
345     FIXME("(%s %p %p %u %p %p %u %p %f %d %d %p %s %p %p %u %p %p): stub\n", debugstr_w(text),
346         clustermap, props, text_len, glyph_indices, glyph_props, glyph_count, font_face, fontEmSize, is_sideways,
347         is_rtl, analysis, debugstr_w(locale), features, feature_range_len, feature_ranges, glyph_advances, glyph_offsets);
348     return E_NOTIMPL;
349 }
350
351 static HRESULT WINAPI dwritetextanalyzer_GetGdiCompatibleGlyphPlacements(IDWriteTextAnalyzer *iface,
352     WCHAR const* text, UINT16 const* clustermap, DWRITE_SHAPING_TEXT_PROPERTIES* props,
353     UINT32 text_len, UINT16 const* glyph_indices, DWRITE_SHAPING_GLYPH_PROPERTIES const* glyph_props,
354     UINT32 glyph_count, IDWriteFontFace * font_face, FLOAT fontEmSize, FLOAT pixels_per_dip,
355     DWRITE_MATRIX const* transform, BOOL use_gdi_natural, BOOL is_sideways, BOOL is_rtl,
356     DWRITE_SCRIPT_ANALYSIS const* analysis, WCHAR const* locale, DWRITE_TYPOGRAPHIC_FEATURES const** features,
357     UINT32 const* feature_range_lengths, UINT32 feature_ranges, FLOAT* glyph_advances, DWRITE_GLYPH_OFFSET* glyph_offsets)
358 {
359     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),
360         clustermap, props, text_len, glyph_indices, glyph_props, glyph_count, font_face, fontEmSize, pixels_per_dip,
361         transform, use_gdi_natural, is_sideways, is_rtl, analysis, debugstr_w(locale), features, feature_range_lengths,
362         feature_ranges, glyph_advances, glyph_offsets);
363     return E_NOTIMPL;
364 }
365
366 static const struct IDWriteTextAnalyzerVtbl textanalyzervtbl = {
367     dwritetextanalyzer_QueryInterface,
368     dwritetextanalyzer_AddRef,
369     dwritetextanalyzer_Release,
370     dwritetextanalyzer_AnalyzeScript,
371     dwritetextanalyzer_AnalyzeBidi,
372     dwritetextanalyzer_AnalyzeNumberSubstitution,
373     dwritetextanalyzer_AnalyzeLineBreakpoints,
374     dwritetextanalyzer_GetGlyphs,
375     dwritetextanalyzer_GetGlyphPlacements,
376     dwritetextanalyzer_GetGdiCompatibleGlyphPlacements
377 };
378
379 static IDWriteTextAnalyzer textanalyzer = { &textanalyzervtbl };
380
381 HRESULT get_textanalyzer(IDWriteTextAnalyzer **ret)
382 {
383     *ret = &textanalyzer;
384     return S_OK;
385 }