Updating the changelog in the VERSION file, and version_sync.
[shapes.git] / source / characterencoding.cc
blob3980cb8545db7321cc74b51fd988d88966507484
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008 Henrik Tidefelt
19 #include "characterencoding.h"
20 #include "shapesexceptions.h"
23 using namespace FontMetrics;
25 // class CharacterEncoding
26 // {
27 // public:
28 // // A character encoding is a primitive thing which can only handle 256 code points.
29 // typedef unsigned char PositionType;
30 // private:
31 // RefCountPtr< const char > encodingName_;
33 // // This is only a memory, with no particular order of its items.
34 // PtrOwner_back_Access< std::vector< const char * > > nameMem_;
36 // // This is idexed by size_t and maps to a name or a null pointer.
37 // static const size_t TABLE_SIZE = 256;
38 // std::vector< const char * > namePtrs_;
40 // // This is the reverse map.
41 // std::map< const char *, PositionType, charPtrLess > nameMap_;
43 // public:
45 CharacterEncoding::CharacterEncoding( std::istream & iFile )
46 : encodingName_( NullPtr< const char >( ) )
48 namePtrs_.resize( TABLE_SIZE, 0 );
49 readfile( iFile );
52 CharacterEncoding::~CharacterEncoding( )
53 { }
55 // Returns true on success.
56 bool
57 CharacterEncoding::position_to_name( PositionType code, const char ** dst ) const
59 *dst = nameMem_[ code ];
60 return *dst != 0;
63 // Returns true on success.
64 bool
65 CharacterEncoding::name_to_position( const char * name, PositionType * dst ) const
67 typedef typeof nameMap_ MapType;
68 MapType::const_iterator i = nameMap_.find( name );
69 if( i == nameMap_.end( ) )
71 return false;
73 *dst = i->second;
74 return true;
77 void
78 CharacterEncoding::readfile( std::istream & iFile )
80 std::string token;
81 getToken( iFile, & token );
82 if( token[0] != '/' )
84 throw "Expected a leading / in the name of the character encoding.";
86 encodingName_ = strrefdup( token.c_str( ) + 1 );
88 getToken( iFile, & token );
89 // This is a bracket. Skip it.
91 getToken( iFile, & token );
93 for( unsigned char pos = 0; token[ 0 ] != ']'; ++pos, getToken( iFile, & token ) )
95 if( token[ 1 ] != '.' )
97 nameMem_.push_back( strdup( token.c_str( ) + 1 ) );
98 namePtrs_[ pos ] = nameMem_.back( );
99 nameMap_[ nameMem_.back( ) ] = pos;
100 // std::cerr << nameMem_.back( ) << " --> " << static_cast< int >( pos ) << std::endl ;
105 void
106 CharacterEncoding::getToken( std::istream & iFile, std::string * dst )
108 iFile >> *dst ;
109 while( (*dst)[ 0 ] == '%' )
111 std::getline( iFile, *dst );
112 iFile >> *dst ;