1 /***************************************************************************
2 * Copyright (C) 2010 by Dominik Seichter *
3 * domseichter@web.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 * *
20 * In addition, as a special exception, the copyright holders give *
21 * permission to link the code of portions of this program with the *
22 * OpenSSL library under certain conditions as described in each *
23 * individual source file, and distribute linked combinations *
24 * including the two. *
25 * You must obey the GNU General Public License in all respects *
26 * for all of the code used other than OpenSSL. If you modify *
27 * file(s) with this exception, you may extend this exception to your *
28 * version of the file(s), but you are not obligated to do so. If you *
29 * do not wish to do so, delete this exception statement from your *
30 * version. If you delete this exception statement from all source *
31 * files in the program, then also delete it here. *
32 ***************************************************************************/
33
34 #include "PdfIdentityEncoding.h"
35
36 #include "base/PdfDefinesPrivate.h"
37
38 #include "base/PdfDictionary.h"
39 #include "base/PdfLocale.h"
40
41 #include "PdfFont.h"
42
43 #include <sstream>
44 #include <iostream>
45 #include <stack>
46 #include <iomanip>
47 #include <string>
48
49 using namespace std;
50
51 namespace PoDoFo {
52
PdfIdentityEncoding(int nFirstChar,int nLastChar,bool bAutoDelete,PdfObject * pToUnicode)53 PdfIdentityEncoding::PdfIdentityEncoding( int nFirstChar, int nLastChar, bool bAutoDelete, PdfObject *pToUnicode )
54 : PdfEncoding( nFirstChar, nLastChar, pToUnicode ), m_bAutoDelete( bAutoDelete )
55 {
56 // create a unique ID
57 std::ostringstream oss;
58 oss << "/Identity-H" << nFirstChar << "_" << nLastChar;
59
60 m_id = PdfName( oss.str() );
61 }
62
AddToDictionary(PdfDictionary & rDictionary) const63 void PdfIdentityEncoding::AddToDictionary( PdfDictionary & rDictionary ) const
64 {
65 rDictionary.AddKey( "Encoding", PdfName("Identity-H") );
66 }
67
GetCharCode(int nIndex) const68 pdf_utf16be PdfIdentityEncoding::GetCharCode( int nIndex ) const
69 {
70 if( nIndex < this->GetFirstChar() ||
71 nIndex > this->GetLastChar() )
72 {
73 PODOFO_RAISE_ERROR( ePdfError_ValueOutOfRange );
74 }
75
76 #ifdef PODOFO_IS_LITTLE_ENDIAN
77 return ((nIndex & 0xFF00) >> 8) | ((nIndex & 0x00FF) << 8);
78 #else
79 return static_cast<pdf_utf16be>(nIndex);
80 #endif // PODOFO_IS_LITTLE_ENDIAN
81 }
82
ConvertToUnicode(const PdfString & rEncodedString,const PdfFont * pFont) const83 PdfString PdfIdentityEncoding::ConvertToUnicode( const PdfString & rEncodedString, const PdfFont* pFont ) const
84 {
85 if(!m_toUnicode.empty())
86 {
87 return PdfEncoding::ConvertToUnicode(rEncodedString, pFont);
88 }
89 else {
90 /* Identity-H means 1-1 mapping */
91 //std::cout << "convertToUnicode(" << rEncodedString.IsUnicode() << ")" << std::endl;
92 return ( rEncodedString.IsUnicode() ) ? PdfString(rEncodedString) : rEncodedString.ToUnicode();
93 }
94 }
95
ConvertToEncoding(const PdfString & rString,const PdfFont * pFont) const96 PdfRefCountedBuffer PdfIdentityEncoding::ConvertToEncoding( const PdfString & rString, const PdfFont* pFont ) const
97 {
98 if(!m_toUnicode.empty())
99 {
100 return PdfEncoding::ConvertToEncoding(rString, pFont);
101 }
102 else if( pFont )
103 {
104 PdfString sStr = rString.ToUnicode();
105 const pdf_utf16be* pStr = sStr.GetUnicode();
106 PdfRefCountedBuffer buffer( sStr.GetLength() );
107 char* outp = buffer.GetBuffer();
108
109 // Get the string in UTF-16be format
110 long lGlyphId;
111 while( *pStr )
112 {
113 #ifdef PODOFO_IS_LITTLE_ENDIAN
114 lGlyphId = pFont->GetFontMetrics()->GetGlyphId( (((*pStr << 8) & 0xFF00) | ((*pStr >> 8) & 0x00FF)) );
115 #else
116 lGlyphId = pFont->GetFontMetrics()->GetGlyphId( *pStr );
117 #endif // PODOFO_IS_LITTLE_ENDIAN
118
119 outp[0] = static_cast<char>(lGlyphId >> 8);
120 outp[1] = static_cast<char>(lGlyphId);
121 outp += 2;
122
123 ++pStr;
124 }
125 return buffer;
126 }
127 else
128 {
129 PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
130 }
131
132 return PdfRefCountedBuffer();
133 }
134
135 }; /* namespace PoDoFo */
136