Merge "Added Application::New(..., stylesheet)" into tizen
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / tizen-font-configuration-parser.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "tizen-font-configuration-parser.h"
19
20 #include <sstream>
21 #include <libxml/xmlmemory.h>
22 #include <libxml/parser.h>
23
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali
27 {
28
29 namespace TizenPlatform
30 {
31
32 namespace FontConfigurationParser
33 {
34
35 #if defined(DEBUG_ENABLED)
36 namespace
37 {
38 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TIZEN_FONT_CONFIGURATION_PARSER");
39 }
40 #endif
41
42 void Parse(std::string confFile, std::string& fontFamily, std::string& fontStyle)
43 {
44   bool familyParsed(false);
45   bool styleParsed(false);
46
47   if ( !confFile.empty() )
48   {
49     xmlDocPtr doc = xmlParseFile(confFile.c_str());
50     if(doc == NULL)
51     {
52       DALI_LOG_ERROR("Document %s not parsed successfully.\n", confFile.c_str());
53       return;
54     }
55
56     xmlNodePtr cur = xmlDocGetRootElement(doc);
57     if(cur == NULL)
58     {
59       DALI_LOG_ERROR("%s is Empty\n", confFile.c_str());
60       xmlFreeDoc(doc);
61       return;
62     }
63
64     if(xmlStrcmp(cur->name, (const xmlChar *)"fontconfig"))
65     {
66       DALI_LOG_ERROR("Document %s is of the wrong type, root node != fontconfig\n", confFile.c_str());
67       xmlFreeDoc(doc);
68       return;
69     }
70
71     cur = cur->xmlChildrenNode;
72
73     while(cur != NULL && !(familyParsed && styleParsed))
74     {
75       if((!xmlStrcmp(cur->name, (const xmlChar *)"match")))
76       {
77         xmlNodePtr cur2 = cur->xmlChildrenNode;
78         while(cur2 != NULL && !(familyParsed && styleParsed))
79         {
80           if((!xmlStrcmp(cur2->name, (const xmlChar *)"edit")))
81           {
82             xmlChar* xmlValue = xmlGetProp( cur2, (const xmlChar *)"name" );
83             if((!xmlStrcmp(xmlValue, (const xmlChar *)"family")))
84             {
85               xmlNodePtr cur3 = cur2->xmlChildrenNode;
86               while(cur3 != NULL && !(familyParsed && styleParsed))
87               {
88                 if((!xmlStrcmp(cur3->name, (const xmlChar *)"string")))
89                 {
90                   xmlChar* key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
91                   DALI_LOG_INFO(gLogFilter,Debug::Concise, "Document %s uses the following font family as default: %s\n", confFile.c_str(), key);
92                   std::stringstream str;
93                   str << key;
94                   fontFamily = str.str();
95                   xmlFree(key);
96                   familyParsed = true;
97                   break;
98                 }
99                 cur3 = cur3->next;
100               }
101             }
102             else if((!xmlStrcmp(xmlValue, (const xmlChar *)"style")))
103             {
104               xmlNodePtr cur3 = cur2->xmlChildrenNode;
105               while(cur3 != NULL && !(familyParsed && styleParsed))
106               {
107                 if((!xmlStrcmp(cur3->name, (const xmlChar *)"string")))
108                 {
109                   xmlChar* key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
110                   DALI_LOG_INFO(gLogFilter,Debug::Concise, "Document %s uses the following font style as default: %s\n", confFile.c_str(), key);
111                   std::stringstream str;
112                   str << key;
113                   fontStyle = str.str();
114                   xmlFree(key);
115                   styleParsed = true;
116                   break;
117                 }
118                 cur3 = cur3->next;
119               }
120             }
121             xmlFree( xmlValue );
122           }
123           cur2 = cur2->next;
124         }
125       }
126       cur = cur->next;
127     }
128
129     xmlFreeDoc(doc);
130   }
131 }
132
133 } // namespace FontConfParser
134
135 } // namespace TizenPlatform
136
137 } // namespace Dali