XcfInfo.cpp
Go to the documentation of this file.
1 #include "XcfInfo.hpp"
2 
3 namespace iv
4 {
5 
6 XcfInfo::XcfInfo( Instance * inst, StreamResourceProvider const * provider, XcfInfo_Subprovider const *, ResourcePath const & path ) :
7  StreamResource( inst, provider, path ),
8  cm( inst, this, "XcfInfo" )
9 {
10  this->cm.inherits( this->StreamResource::cm );
11 
12  this->with_resource_stream( [&]( std::istream & in )
13  {
14  this->Parse( in );
15  });
16 }
17 
19 {
20  return this->_size;
21 }
22 
23 XcfInfo::Layer const & XcfInfo::layer( char const * name ) const
24 {
25  static XcfInfo::Layer empty;
26  auto it = this->_layers.find( name );
27  if( it == this->_layers.end() )
28  {
29  this->cm.warning( SRC_INFO, "Layer '", "' not found in XcfInfo resource ", this->resource_path() );
30  return empty;
31  }
32 
33  return it->second;
34 }
35 
36 void XcfInfo::Parse( std::istream & in )
37 {
38  Lex lex( this->instance() );
39  lex.DefineKeyword( "width" );
40  lex.DefineKeyword( "height" );
41  lex.DefineKeyword( "layer" );
42  lex.DisableNewlineSkipping();
43 
44  lex.Init( in );
45  Lex_LogTrace _mod( lex );
46 
47  while( !lex.Failed() && !lex.IsNext( Lex::Token::Eof ) )
48  {
49  if( lex.IsNextKeyword( "width" ) )
50  {
51  lex.AcceptKeyword( "width" );
52  int width = lex.AcceptDouble();
53 
54  if( !lex.Failed() )
55  this->_size.x = float( width );
56  }
57  else if( lex.IsNextKeyword( "height" ) )
58  {
59  lex.AcceptKeyword( "height" );
60  int height = lex.AcceptDouble();
61 
62  if( !lex.Failed() )
63  this->_size.y = float( height );
64  }
65  else if( lex.IsNextKeyword( "layer" ) )
66  {
67  lex.AcceptKeyword( "layer" );
68 
69  Layer layer;
70 
71  int global_order = lex.AcceptDouble();
72  (void)global_order;
73  std::string name = lex.AcceptString();
74 
75  layer.size.x = float( lex.AcceptDouble() );
76  layer.size.y = float( lex.AcceptDouble() );
77 
78  layer.global_left = float( lex.AcceptDouble() );
79  layer.global_right = float( lex.AcceptDouble() );
80  layer.global_top = float( lex.AcceptDouble() );
81  layer.global_bottom = float( lex.AcceptDouble() );
82 
83  layer.local_left = float( lex.AcceptDouble() );
84  layer.local_right = float( lex.AcceptDouble() );
85  layer.local_top = float( lex.AcceptDouble() );
86  layer.local_bottom = float( lex.AcceptDouble() );
87 
88  if( !lex.Failed() )
89  this->_layers[ name ] = layer;
90  }
91  else
92  {
93  lex.Accept( Lex::Token::Newline );
94  }
95 
96  lex.Recover( Lex::Token::Newline );
97  lex.Accept( Lex::Token::Newline );
98  }
99 }
100 
101 }