1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/components/declaration/qrcode/qrcode_declaration.h"
17 
18 #include "core/components/declaration/common/declaration_constants.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20 #include "frameworks/core/components/qrcode/qrcode_theme.h"
21 
22 namespace OHOS::Ace {
23 
24 using namespace Framework;
25 
InitSpecialized()26 void QrcodeDeclaration::InitSpecialized()
27 {
28     AddSpecializedAttribute(DeclarationConstants::DEFAULT_QRCODE_ATTR);
29     AddSpecializedStyle(DeclarationConstants::DEFAULT_QRCODE_STYLE);
30 }
31 
InitializeStyle()32 void QrcodeDeclaration::InitializeStyle()
33 {
34     auto qrcodeTheme = GetTheme<QrcodeTheme>();
35     if (!qrcodeTheme) {
36         return;
37     }
38     SetQrcodeColor(qrcodeTheme->GetQrcodeColor());
39     SetBackgroundColor(qrcodeTheme->GetBackgroundColor());
40     SetQrcodeType(qrcodeTheme->GetQrcodeType());
41     SetQrcodeWidth(qrcodeTheme->GetQrcodeWidth());
42     SetQrcodeHeight(qrcodeTheme->GetQrcodeHeight());
43 }
44 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)45 bool QrcodeDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
46 {
47     static const LinearMapNode<void (*)(QrcodeDeclaration&, const std::string&)> qrcodeAttrOperators[] = {
48         { DOM_QRCODE_TYPE,
49             [](QrcodeDeclaration& declaration, const std::string& value) {
50                 declaration.SetQrcodeType(ConvertStrToQrcodeType(value));
51             } },
52         { DOM_QRCODE_VALUE,
53             [](QrcodeDeclaration& declaration, const std::string& value) { declaration.SetValue(value); } },
54     };
55     auto operatorIter = BinarySearchFindIndex(qrcodeAttrOperators, ArraySize(qrcodeAttrOperators), attr.first.c_str());
56     if (operatorIter != -1) {
57         qrcodeAttrOperators[operatorIter].value(*this, attr.second);
58         return true;
59     }
60     return false;
61 }
62 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)63 bool QrcodeDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
64 {
65     const static LinearMapNode<void (*)(QrcodeDeclaration&, const std::string&)> qrcodeOperators[] = {
66         { DOM_QRCODE_BACKGROUND_COLOR,
67             [](QrcodeDeclaration& declaration, const std::string& value) {
68                 declaration.SetBackgroundColor(declaration.ParseColor(value));
69             } },
70         { DOM_QRCODE_COLOR,
71             [](QrcodeDeclaration& declaration, const std::string& value) {
72                 declaration.SetQrcodeColor(declaration.ParseColor(value));
73             } },
74         { DOM_QRCODE_HEIGHT,
75             [](QrcodeDeclaration& declaration, const std::string& value) {
76                 declaration.SetQrcodeHeight(declaration.ParseDimension(value));
77             } },
78         { DOM_QRCODE_WIDTH,
79             [](QrcodeDeclaration& declaration, const std::string& value) {
80                 declaration.SetQrcodeWidth(declaration.ParseDimension(value));
81             } },
82     };
83     auto operatorIter = BinarySearchFindIndex(qrcodeOperators, ArraySize(qrcodeOperators), style.first.c_str());
84     if (operatorIter != -1) {
85         qrcodeOperators[operatorIter].value(*this, style.second);
86         return true;
87     }
88     return false;
89 }
90 
91 } // namespace OHOS::Ace
92