1 /*
2  * Copyright (c) 2021-2022 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 #ifndef OHOS_ACELITE_STYLEMGR_CONDITION_ARBITRATOR
17 #define OHOS_ACELITE_STYLEMGR_CONDITION_ARBITRATOR
18 
19 #include "memory_heap.h"
20 #include "non_copyable.h"
21 #include "link_stack.h"
22 #include "link_queue.h"
23 
24 namespace OHOS {
25 namespace ACELite {
26 enum ConditionName : uint8_t {
27     WIDTH,
28     MIN_WIDTH,
29     MAX_WIDTH,
30     HEIGHT,
31     MIN_HEIGHT,
32     MAX_HEIGHT,
33     ASPECT_RATIO,
34     MIN_ASPECT_RATIO,
35     MAX_ASPECT_RATIO,
36     ROUND_SCREEN,
37     DEVICE_TYPE,
38     UNKOWN,
39     MAX_COUNT = UNKOWN
40 };
41 
42 class ConditionArbitrator final : public MemoryHeap {
43 public:
44     ACE_DISALLOW_COPY_AND_MOVE(ConditionArbitrator);
45     ConditionArbitrator() = default;
46     virtual ~ConditionArbitrator() = default;
47 
48     /**
49      * @brief determine whether the media query conditions are met based on
50      * the current device environment
51      * @return the result whether the condition is met
52      */
53     bool Decide(const char *conditions) const;
54 
55 private:
56     /**
57      * @brief judge the single condition is success or not
58      */
59     bool JudgeCondition(const char *condition) const;
60     bool JudgeConditionAction(ConditionName conditionId, const char *trimedTargetValue) const;
61     bool JudgeConditionByStrValue(ConditionName conditionId, const char *trimedTargetValue) const;
62     bool JudgeConditionByNumberValue(ConditionName conditionId, const char *targetValue) const;
63     bool CompareIntDimension(ConditionName conditionId, const char *targetValue) const;
64     bool CompareFloatDimension(ConditionName conditionId, const char *targetValue) const;
65     bool CompareAspectRatio(ConditionName conditionId, float targetRatioValue) const;
66     ConditionName GetConditionName(const char *conditionName) const;
67     bool IsFloatValueEqual(float left, float right, float precision) const;
68     bool IsOperator(const char *str) const;
69     const char *FindFirstPos(const char *conditions, uint8_t *size) const;
70     const char *FindNoSpacePos(const char *conditions) const;
71     bool DecomPositionConditions(const char *conditions, LinkQueue* queue) const;
72     void TransformExpression(const LinkQueue *queue, LinkQueue *expressionQueue) const;
73     bool Calculate(LinkQueue *expressionQueue) const;
74     bool Parse(const char *condition) const;
75     bool IsValid(const char *conditions) const;
76     void FreeMallocData(const LinkQueue *queue) const;
77     static constexpr float CONDITION_FLOAT_VALUE_EPRECISION = 1E-5;
78 };
79 } // namespace ACELite
80 } // namespace OHOS
81 #endif // OHOS_ACELITE_STYLEMGR_CONDITION_ARBITRATOR
82