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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.
  3 
  4 #ifndef tools_xml_styles
  5 #define tools_xml_styles
  6 
  7 #include "../vpair"
  8 #include "../sg/style_colormap"
  9 #include "../smatch"
 10 #include "../sout"
 11 #include "../forit"
 12 #include "../sbeg"
 13 
 14 namespace tools {
 15 namespace xml {
 16 
 17 class styles {
 18 public:
 19   styles(std::ostream& a_out):m_out(a_out){}
 20   virtual ~styles(){}
 21 public:
 22   styles(const styles& a_from)
 23   :m_out(a_from.m_out)
 24   ,m_named_styles(a_from.m_named_styles)
 25   ,m_aliases(a_from.m_aliases)
 26   ,m_cmaps(a_from.m_cmaps)
 27   {}
 28   styles& operator=(const styles& a_from){
 29     m_named_styles = a_from.m_named_styles;
 30     m_aliases = a_from.m_aliases;
 31     m_cmaps = a_from.m_cmaps;
 32     return *this;
 33   }
 34 public:
 35   std::ostream& out() const {return m_out;}
 36 
 37   const sg::cmaps_t& cmaps() const {return m_cmaps;}
 38   sg::cmaps_t& cmaps() {return m_cmaps;}
 39 
 40   typedef std::pair<std::string,std::string> style_item_t;
 41   typedef std::vector<style_item_t> style_t;
 42   typedef std::pair<std::string,style_t> named_style_t;
 43 
 44   const std::vector<named_style_t>& named_styles() const {return m_named_styles;}
 45   std::vector<named_style_t>& named_styles() {return m_named_styles;}
 46 
 47   typedef std::pair<std::string,std::string> alias_t;
 48   const std::vector<alias_t>& aliases() const {return m_aliases;}
 49   std::vector<alias_t>& aliases() {return m_aliases;}
 50 
 51   const style_t* find_style(const std::string& a_name) const {
 52     tools_vforcit(named_style_t,m_named_styles,it){
 53       if((*it).first==a_name) return &((*it).second);
 54     }
 55     return 0;
 56   }
 57 
 58 /*
 59   style_t* find_style(const std::string& a_name) {
 60     tools_vforit(named_style_t,m_named_styles,it){
 61       if((*it).first==a_name) return &((*it).second);
 62     }
 63     return 0;
 64   }
 65   void remove_item_in_style(style_t& a_style,const std::string& a_item) {
 66     remove<std::string,std::string>(a_style,a_item);
 67   }
 68 */
 69 
 70   void find_styles(const std::string& a_pattern,std::vector<std::string>& a_v) const {
 71     a_v.clear();
 72     tools_vforcit(named_style_t,m_named_styles,it){
 73       if(match((*it).first,a_pattern)) a_v.push_back((*it).first);
 74     }
 75   }
 76 
 77   void add_style(const std::string& a_name,const style_t& a_style) {
 78     tools_vforit(named_style_t,m_named_styles,it){
 79       if((*it).first==a_name) { //override
 80         //::printf("debug : styles::add_style : override : \"%s\"\n",a_name.c_str());
 81         (*it).second = a_style;
 82         return;
 83       }
 84     }
 85     //::printf("debug : styles::add_style : push_back \"%s\"\n",a_name.c_str());
 86     m_named_styles.push_back(named_style_t(a_name,a_style));
 87   }
 88 
 89   void remove_styles(const std::string& a_pattern) {
 90     std::vector<named_style_t>::iterator it;
 91     for(it=m_named_styles.begin();it!=m_named_styles.end();) {
 92       if(match((*it).first,a_pattern)) {
 93         it = m_named_styles.erase(it);
 94       } else {
 95         it++;
 96       }
 97     }
 98   }
 99 
100   void append(const styles& a_from) {
101     //::printf("debug : styles::append ...\n");
102     tools_vforcit(named_style_t,a_from.m_named_styles,it) {
103       //::printf("debug : styles::append \"%s\"\n",(*it).first.c_str());
104       m_named_styles.push_back(*it);
105     }
106   }
107 
108   bool is_alias(const std::string& a_wanted,std::string& a_style) const {
109     if(find(m_aliases,a_wanted,a_style)) return true;
110     a_style = a_wanted;
111     return false;
112   }
113 
114 /*
115   bool res_string(const std::string& a_style,
116                          const std::string& a_key,
117                          std::string& a_v) const {
118     std::map<std::string,style_t>::const_iterator it = m_styles.find(a_style);
119     if(it==m_styles.end()) {a_v.clear();return false;}
120     const style_t& sty = (*it).second;
121     if(!find(sty,a_key,a_v)) {a_v.clear();return false;}
122     return true;
123   }
124 */
125   template <class T>
126   bool res_value(const style_t& a_sty,const std::string& a_key,T& a_v,const std::string& a_msg) const {
127     //NOTE : if ret false, we do not set a_v to something.
128 
129     std::string _s;
130     if(!find(a_sty,a_key,_s)) {
131       if(a_msg.size()) {
132         m_out << "tools::xml::styles::res_value :"
133               << " key " << sout(a_key) << " not found"
134               << " in style " << sout(a_msg) << "."
135               << std::endl;
136       }
137       return false;
138     }
139     T v;
140     if(!to(_s,v)) {
141       m_out << "tools::xml::styles::res_value :"
142             << " for key " << sout(a_key) << " not found"
143             << " in style " << sout(a_msg) << "."
144             << " can't convert " << sout(_s) << " to value."
145             << std::endl;
146       return false;
147     }
148     a_v = v;
149     return true;
150   }
151 
152   template <class T>
153   bool res_value(const std::string& a_style,const std::string& a_key,T& a_v) const {
154     //NOTE : if ret false, we do not set a_v to something.
155 
156     //look if a_style is an alias :
157     std::string style;
158     is_alias(a_style,style);
159 
160     const style_t* sty = find_style(style);
161     if(!sty) {
162       //m_out << "tools::xml::styles::res_value :"
163       //      << " style " << sout(style) << " not found."
164       //      << std::endl;
165       return false;
166     }
167     T v;
168     if(!res_value<T>(*sty,a_key,v,style)) return false;
169     a_v = v;
170     return true;
171   }
172 
173   bool res_color(const style_t& a_sty,const std::string& a_key,colorf& a_color,const std::string& a_msg) const {
174     //NOTE : if ret false, we do not set a_color to something.
175     std::string _s;
176     if(!find(a_sty,a_key,_s)) {
177       if(a_msg.size()) {
178         m_out << "tools::xml::styles::res_color :"
179               << " key " << sout(a_key) << " not found"
180               << " in style " << sout(a_msg) << "."
181               << std::endl;
182       }
183       return false;
184     }
185     if(!sg::find_color(m_cmaps,_s,a_color)) {
186       m_out << "tools::xml::styles::res_color :"
187             << " key " << sout(a_key) << " is not a color"
188             << " in style " << sout(a_msg) << "."
189             << std::endl;
190       return false;
191     }
192     return true;
193   }
194 
195   bool res_color(const std::string& a_style,const std::string& a_key,colorf& a_color) const {
196     //NOTE : if ret false, we do not set a_color to something.
197 
198     //look if a_style is an alias :
199     std::string style;
200     is_alias(a_style,style);
201 
202     const style_t* sty = find_style(style);
203     if(!sty) {
204       //m_out << "tools::xml::styles::res_color :"
205       //      << " style " << sout(style) << " not found."
206       //      << std::endl;
207       return false;
208     }
209 
210     return res_color(*sty,a_key,a_color,style);
211   }
212 
213   bool res_bool(const style_t& a_sty,const std::string& a_key,bool& a_v,const std::string& a_msg) const {
214     //NOTE : if ret false, we do not set a_v to something.
215     std::string _s;
216     if(!find(a_sty,a_key,_s)) {
217       if(a_msg.size()) {
218         m_out << "tools::xml::styles::res_bool :"
219               << " key " << sout(a_key) << " not found"
220               << " in style " << sout(a_msg) << "."
221               << std::endl;
222       }
223       return false;
224     }
225     bool v;
226     if(!to(_s,v)) {
227       m_out << "tools::xml::styles::res_bool :"
228             << " for key " << sout(a_key) << " not found"
229             << " in style " << sout(a_msg) << "."
230             << " can't convert " << sout(_s) << " to bool."
231             << std::endl;
232       return false;
233     }
234     a_v = v;
235     return true;
236   }
237 
238   bool res_bool(const std::string& a_style,const std::string& a_key,bool& a_v) const {
239     //NOTE : if ret false, we do not set a_color to something.
240 
241     //look if a_style is an alias :
242     std::string style;
243     is_alias(a_style,style);
244 
245     const style_t* sty = find_style(style);
246     if(!sty) {
247       //m_out << "tools::xml::styles::res_color :"
248       //      << " style " << sout(style) << " not found."
249       //      << std::endl;
250       return false;
251     }
252 
253     return res_bool(*sty,a_key,a_v,style);
254   }
255 
256   // for plotter :
257   template <class T> //T = [style,text_style,line_style]
258   bool res_sg_style(const std::string& a_style,T& a_sg_style) const {
259     //NOTE : a_sg_style is changed according to what is found.
260     //       Then it is not fully reset by this method.
261     const style_t* sty = find_style(a_style);
262     if(!sty) {
263       //could be ok to not find a plotter sub style.
264       //m_out << "tools::sg::gui_viewer::res_sg_style :"
265       //      << " style " << sout(a_style) << " not found."
266       //      << std::endl;
267       return false;
268     }
269 
270     std::string _s;
271     tools_vforcit(style_item_t,*sty,vit) {
272       if(vit!=(*sty).begin()) _s += "\n";
273       _s += (*vit).first;
274       _s += " ";
275       _s += (*vit).second;
276     }
277     return a_sg_style.from_string(m_out,m_cmaps,_s);
278   }
279 
280   typedef sg::style_colormap cmap_t;
281   bool find_colormap(const std::string& a_name,cmap_t& a_cmap) const {
282     std::map<std::string,cmap_t>::const_iterator it = m_cmaps.find(a_name);
283     if(it==m_cmaps.end()) return false;
284     a_cmap = (*it).second;
285     return true;
286   }
287   void add_colormap(const std::string& a_name,const cmap_t& a_cmap) {
288     m_cmaps[a_name] = a_cmap;
289   }
290   //void clear_colormaps() {m_cmaps.clear();/*add default*/}
291 
292 public:
293   static bool is_plotter_style(const style_t& a_sty) {
294     tools_vforcit(style_item_t,a_sty,it) {
295       const std::string& key = (*it).first;
296       const std::string& sv = (*it).second;
297       if((key=="tag")&&(sv=="plotter_style")) return true;
298     }
299     return false;
300   }
301 
302 public:
303   void find_plotter_styles(std::vector<std::string>& a_v) const {
304     a_v.clear();
305     tools_vforcit(named_style_t,m_named_styles,it) {
306       if(is_plotter_style((*it).second)) a_v.push_back((*it).first);
307     }
308   }
309 
310   void dump() {
311     tools_vforcit(named_style_t,m_named_styles,it) {
312 
313       m_out << "/////////////////////////////////////" << std::endl;
314       m_out << "/// " << (*it).first << std::endl;
315       m_out << "/////////////////////////////////////" << std::endl;
316 
317       const style_t& sty = (*it).second;
318 
319       tools_vforcit(style_item_t,sty,vit) {
320         m_out << " " << (*vit).first << " " << (*vit).second << std::endl;
321       }
322 
323     }
324   }
325 
326   bool print_plotter_style(std::ostream& a_out,const std::string& a_style) {
327     std::string _beg = a_style+".";
328     bool found = false;
329     tools_vforcit(named_style_t,m_named_styles,it) {
330       const std::string& _name = (*it).first;
331       if((_name==a_style)||is_beg(_name,_beg)) {
332         found = true;
333         a_out << (*it).first << " :" << std::endl;
334         const style_t& sty = (*it).second;
335         tools_vforcit(style_item_t,sty,vit) {
336           if((*vit).first=="tag") continue;
337           a_out << " " << (*vit).first << " " << (*vit).second << std::endl;
338         }
339       }
340     }
341     return found;
342   }
343 
344   void list_plotter_styles(std::ostream& a_out) {
345     tools_vforcit(named_style_t,m_named_styles,it) {
346       const style_t& sty = (*it).second;
347       if(!styles::is_plotter_style(sty)) continue;
348       a_out << (*it).first << std::endl;
349     }
350   }
351 protected:
352   std::ostream& m_out;
353   //styles :
354   std::vector<named_style_t> m_named_styles;
355   std::vector<alias_t> m_aliases;
356   //cmaps :
357   sg::cmaps_t m_cmaps;
358 };
359 
360 }}
361 
362 #include "tree"
363 #include "../vmanip"
364 
365 namespace tools {
366 namespace xml {
367 
368 inline void load_style(styles& a_styles,const tree& a_tree) {
369   std::string name;
370   a_tree.attribute_value("name",name);
371   if(name.empty()) {
372     a_styles.out() << "tools::sg::gui_viewer::load_style :"
373           << " <style> without name."
374           << std::endl;
375     return;
376   }
377 
378   styles::style_t sty;
379 
380  {looper _for(a_tree);
381   while(element* _elem = _for.next_element()) {
382 
383       if(_elem->name()=="copy") {
384 
385         std::string from;
386         _elem->attribute_value("from",from); //expect another style name.
387         if(from.empty()) {
388           a_styles.out() << "tools::sg::gui_viewer::load_style :"
389                          << " <copy> without from."
390                          << std::endl;
391           return;
392         }
393 
394         const styles::style_t* csty = a_styles.find_style(from);
395         if(!csty) {
396           a_styles.out() << "tools::sg::gui_viewer::load_style :"
397                      << " <copy> : from " << sout(from) << " not found."
398                      << std::endl;
399           return;
400         }
401 
402         append(sty,*csty);
403 
404       } else {
405         sty.push_back(styles::style_item_t(_elem->name(),_elem->value()));
406       }
407 
408   }}
409 
410   if(sty.size()) a_styles.add_style(name,sty);
411 }
412 
413 inline void load_plotter_style(styles& a_styles,const tree& a_tree) {
414   std::string pname;
415   a_tree.attribute_value("name",pname);
416   if(pname.empty()) {
417     a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
418           << " <plotter_style> without name."
419           << std::endl;
420     return;
421   }
422 
423  {styles::style_t sty;
424   sty.push_back(styles::style_item_t("tag","plotter_style"));
425 
426  {looper _for(a_tree);
427   while(element* _elem = _for.next_element()) {
428 
429       if(_elem->name()=="copy") {
430 
431         std::string from;
432         _elem->attribute_value("from",from); //expect a plotter_style name.
433         if(from.empty()) {
434           a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
435                 << " <copy> without from."
436                 << std::endl;
437           return;
438         }
439 
440         const styles::style_t* csty = a_styles.find_style(from);
441         if(!csty) {
442           a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
443                 << " <copy> : from " << sout(from) << " not found."
444                 << std::endl;
445           return;
446         }
447         if(csty->size()) a_styles.add_style(pname,*csty);
448 
449         if(styles::is_plotter_style(*csty)) {
450           //search all <from>.<sub_style> styles :
451           std::string head = from+".";
452           std::string::size_type l = head.size();
453 
454           styles sts(a_styles.out());
455 
456           const std::vector<styles::named_style_t>& nss = a_styles.named_styles();
457           tools_vforcit(styles::named_style_t,nss,it){
458             const std::string& name = (*it).first;
459             if(name.substr(0,l)==head) {
460               std::string tail = name.substr(l,name.size()-l);
461               const styles::style_t& ssty = (*it).second;
462               if(ssty.size()) sts.add_style(pname+"."+tail,ssty);
463             }
464           }
465 
466           a_styles.append(sts);
467 
468         }
469 
470 
471       } else {
472         sty.push_back(styles::style_item_t(_elem->name(),_elem->value()));
473       }
474 
475   }}
476 
477   if(sty.size()) a_styles.add_style(pname,sty);}
478 
479  {looper _for(a_tree);
480   while(tree* _tree = _for.next_tree()) {
481 
482       const std::string& tag = _tree->tag_name();
483       if(tag=="style") {
484 
485         std::string name;
486         _tree->attribute_value("name",name);
487         if(name.empty()) {
488           a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
489                 << " <style> without name."
490                 << std::endl;
491           return;
492         }
493 
494        {styles::style_t sty;
495 
496        {looper _for2(*_tree);
497         while(element* _elem = _for2.next_element()) {
498           if(_elem->name()=="copy") {
499             std::string from;
500             _elem->attribute_value("from",from); //expect another style name.
501             if(from.empty()) {
502               a_styles.out() << "tools::sg::gui_viewer::load_plotter_style : (2) :"
503                              << " <copy> without from."
504                              << std::endl;
505               return;
506             }
507             const styles::style_t* csty = a_styles.find_style(from);
508             if(!csty) {
509               a_styles.out() << "tools::sg::gui_viewer::load_plotter_style : (2) :"
510                              << " <copy> : from " << sout(from) << " not found."
511                             << std::endl;
512               return;
513             }
514             append(sty,*csty);
515           } else {
516             sty.push_back(styles::style_item_t(_elem->name(),_elem->value()));
517           }
518         }}
519 
520         if(sty.size()) {
521           std::string path = pname+"."+name;
522           a_styles.add_style(path,sty);
523         }}
524 
525       } else {
526         a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
527               << " unexpected tag " << sout(tag) << "."
528               << std::endl;
529       }
530 
531   }}
532 
533 }
534 
535 inline bool scan_style_tree(styles& a_styles,const tree& a_tree) {
536 
537   if(a_tree.tag_name()!="styles") return false;
538 
539   // look for aliases :
540  {looper _for(a_tree);
541   while(element* _elem = _for.next_element()) {
542 
543       std::string name;
544       _elem->attribute_value("name",name);
545       if(name.empty()) {
546         a_styles.out() << "tools::sg::gui_viewer::load_style :"
547               << " <alias> without name."
548               << std::endl;
549         continue;
550       }
551       add<std::string,std::string>(a_styles.aliases(),name,_elem->value());
552   }}
553 
554   // scan children :
555  {looper _for(a_tree);
556   while(tree* _tree = _for.next_tree()) {
557 
558       const std::string& tag = _tree->tag_name();
559       if(tag=="style") {
560         load_style(a_styles,*_tree);
561       } else if(tag=="plotter_style") {
562         load_plotter_style(a_styles,*_tree);
563       }
564 
565   }}
566 
567   return true;
568 }
569 }}
570 
571 #endif