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
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
28 // // A character encoding is a primitive thing which can only handle 256 code points.
29 // typedef unsigned char PositionType;
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_;
45 CharacterEncoding::CharacterEncoding( std::istream
& iFile
)
46 : encodingName_( NullPtr
< const char >( ) )
48 namePtrs_
.resize( TABLE_SIZE
, 0 );
52 CharacterEncoding::~CharacterEncoding( )
55 // Returns true on success.
57 CharacterEncoding::position_to_name( PositionType code
, const char ** dst
) const
59 *dst
= nameMem_
[ code
];
63 // Returns true on success.
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( ) )
78 CharacterEncoding::readfile( std::istream
& iFile
)
81 getToken( iFile
, & token
);
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 ;
106 CharacterEncoding::getToken( std::istream
& iFile
, std::string
* dst
)
109 while( (*dst
)[ 0 ] == '%' )
111 std::getline( iFile
, *dst
);