Imported Upstream version 58.1
[platform/upstream/icu.git] / source / samples / layout / ScriptCompositeFontInstance.cpp
1 /*
2  *******************************************************************************
3  *
4  *   Copyright (C) 2016 and later: Unicode, Inc. and others.
5  *   License & terms of use: http://www.unicode.org/copyright.html#License
6  *
7  *******************************************************************************
8  *******************************************************************************
9  *
10  *   Copyright (C) 1999-2003, International Business Machines
11  *   Corporation and others.  All Rights Reserved.
12  *
13  *******************************************************************************
14  *   file name:  ScriptCompositeFontInstance.cpp
15  *
16  *   created on: 02/05/2003
17  *   created by: Eric R. Mader
18  */
19
20 #include "layout/LETypes.h"
21
22 #include "unicode/uscript.h"
23
24 #include "FontMap.h"
25
26 #include "ScriptCompositeFontInstance.h"
27
28 const char ScriptCompositeFontInstance::fgClassID=0;
29
30 ScriptCompositeFontInstance::ScriptCompositeFontInstance(FontMap *fontMap)
31     : fFontMap(fontMap)
32 {
33     // nothing else to do
34 }
35
36 ScriptCompositeFontInstance::~ScriptCompositeFontInstance()
37 {
38     delete fFontMap;
39     fFontMap = NULL;
40 }
41
42 void ScriptCompositeFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
43 {
44     LEErrorCode status = LE_NO_ERROR;
45     le_int32 script = LE_GET_SUB_FONT(glyph);
46     const LEFontInstance *font = fFontMap->getScriptFont(script, status);
47
48     advance.fX = 0;
49     advance.fY = 0;
50
51     if (LE_SUCCESS(status)) {
52         font->getGlyphAdvance(LE_GET_GLYPH(glyph), advance);
53     }
54 }
55
56 le_bool ScriptCompositeFontInstance::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
57 {
58     LEErrorCode status = LE_NO_ERROR;
59     le_int32 script = LE_GET_SUB_FONT(glyph);
60     const LEFontInstance *font = fFontMap->getScriptFont(script, status);
61
62     if (LE_SUCCESS(status)) {
63         return font->getGlyphPoint(LE_GET_GLYPH(glyph), pointNumber, point);
64     }
65
66     return FALSE;
67 }
68
69 const LEFontInstance *ScriptCompositeFontInstance::getSubFont(const LEUnicode chars[], le_int32 *offset, le_int32 limit, le_int32 script, LEErrorCode &success) const
70 {
71     if (LE_FAILURE(success)) {
72         return NULL;
73     }
74
75     if (chars == NULL || *offset < 0 || limit < 0 || *offset >= limit || script < 0 || script >= scriptCodeCount) {
76         success = LE_ILLEGAL_ARGUMENT_ERROR;
77         return NULL;
78     }
79
80     const LEFontInstance *result = fFontMap->getScriptFont(script, success);
81
82     if (LE_FAILURE(success)) {
83         return NULL;
84     }
85
86     *offset = limit;
87     return result;
88 }
89
90 // This is the really stupid version that doesn't look in any other font for the character...
91 // It would be better to also look in the "DEFAULT:" font. Another thing to do would be to
92 // look in all the fonts in some order, script code order being the most obvious...
93 LEGlyphID ScriptCompositeFontInstance::mapCharToGlyph(LEUnicode32 ch) const
94 {
95     UErrorCode  error  = U_ZERO_ERROR;
96     LEErrorCode status = LE_NO_ERROR;
97     le_int32 script = uscript_getScript(ch, &error);
98     const LEFontInstance *scriptFont = fFontMap->getScriptFont(script, status);
99     LEGlyphID subFont = LE_SET_SUB_FONT(0, script);
100
101     if (LE_FAILURE(status)) {
102         return subFont;
103     }
104
105     LEGlyphID glyph = scriptFont->mapCharToGlyph(ch);
106
107     return LE_SET_GLYPH(subFont, glyph);
108 }
109