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 #ifndef _PDF_FONT_METRICS_BASE14_H_
35 #define _PDF_FONT_METRICS_BASE14_H_
36 
37 #include "podofo/base/PdfDefines.h"
38 #include "podofo/base/PdfRect.h"
39 #include "podofo/base/PdfVariant.h"
40 
41 #include "PdfFontMetrics.h"
42 
43 #include <string.h>
44 
45 /*
46   The following are the Base 14 fonts data copied from libharu.
47   - kaushik April 12 2010
48 */
49 
50 namespace PoDoFo {
51 
52 struct PODOFO_CharData;
53 
54 class PdfArray;
55 
56  /*
57    This is the main class to handle the base14 metric data.
58    The member functions are accessed only through PDFFontmetrics.
59    For eg. pdffontmetrics->GetFontSize would check if it is a base14 font,
60    and call PdfFontMetricsBase14->GetFontSize.
61 
62    This is done to ensure all existing paths work as is.
63    The changes to Base 14 get added without affecting the existing workflow and fit in exactly.
64 
65    Ideally PdfFontMetrics should be abstract or the metric related interface should be seperated out
66    from the implementation details - such as whether the font metric data is read from a file/buffer/hard coded.
67 
68    Kaushik : April 12th 2010
69 
70  */
71 class PODOFO_DOC_API PdfFontMetricsBase14 : public PdfFontMetrics {
72 public:
73 	PdfFontMetricsBase14(const char   *mfont_name,
74                          const PODOFO_CharData  *mwidths_table,
75                          bool             mis_font_specific,
76                          pdf_int16         mascent,
77                          pdf_int16         mdescent,
78                          pdf_uint16        mx_height,
79                          pdf_uint16        mcap_height,
80                          pdf_int16         mstrikeout_pos,
81                          pdf_int16         munderline_pos,
82                          const PdfRect &  mbbox);
83 
84     ~PdfFontMetricsBase14();
85 
86 	friend  const PdfFontMetricsBase14*
87 		PODOFO_Base14FontDef_FindBuiltinData  (const char  *font_name);
88 
89     /** Create a width array for this font which is a required part
90      *  of every font dictionary.
91      *  \param var the final width array is written to this PdfVariant
92      *  \param nFirst first character to be in the array
93      *  \param nLast last character code to be in the array
94      *  \param pEncoding encoding for correct character widths. If not passed default (latin1) encoding is used
95      */
96     virtual void GetWidthArray( PdfVariant & var, unsigned int nFirst, unsigned int nLast, const PdfEncoding* pEncoding = NULL ) const;
97 
98     /** Get the width of a single glyph id
99      *
100      *  \returns the width of a single glyph id
101      */
102     virtual double GetGlyphWidth( int nGlyphId ) const;
103 
104     /** Get the width of a single named glyph
105      *
106      *  \param pszGlyphname name of the glyph
107      *  \returns the width of a single named glyph
108      */
109 	virtual double GetGlyphWidth( const char* pszGlyphname ) const;
110 
111     /** Create the bounding box array as required by the PDF reference
112      *  so that it can be written directly to a PDF file.
113      *
114      *  \param array write the bounding box to this array.
115      */
116     virtual void GetBoundingBox( PdfArray & array ) const;
117 
118     /** Retrieve the width of the given character in PDF units in the current font
119      *  \param c character
120      *  \returns the width in PDF units
121      */
122     virtual double CharWidth( unsigned char c ) const;
123 
124     // Peter Petrov 20 March 2009
125     /** Retrieve the width of the given character in PDF units in the current font
126      *  \param c character
127      *  \returns the width in PDF units
128      */
129     virtual double UnicodeCharWidth( unsigned short c ) const;
130 
131     /** Retrieve the line spacing for this font
132      *  \returns the linespacing in PDF units
133      */
134     virtual double GetLineSpacing() const;
135 
136     /** Get the width of the underline for the current
137      *  font size in PDF units
138      *  \returns the thickness of the underline in PDF units
139      */
140     virtual double GetUnderlineThickness() const;
141 
142     /** Return the position of the underline for the current font
143      *  size in PDF units
144      *  \returns the underline position in PDF units
145      */
146     virtual double GetUnderlinePosition() const;
147 
148     /** Return the position of the strikeout for the current font
149      *  size in PDF units
150      *  \returns the underline position in PDF units
151      */
152     virtual double GetStrikeOutPosition() const;
153 
154     /** Get the width of the strikeout for the current
155      *  font size in PDF units
156      *  \returns the thickness of the strikeout in PDF units
157      */
158     virtual double GetStrikeoutThickness() const;
159 
160     /** Get a string with the postscript name of the font.
161      *  \returns the postscript name of the font or NULL string if no postscript name is available.
162      */
163     virtual const char* GetFontname() const;
164 
165     /** Get the weight of this font.
166      *  Used to build the font dictionay
167      *  \returns the weight of this font (500 is normal).
168      */
169     virtual  unsigned int GetWeight() const;
170 
171     /** Get the ascent of this font in PDF
172      *  units for the current font size.
173      *
174      *  \returns the ascender for this font
175      *
176      *  \see GetPdfAscent
177      */
178     virtual double GetAscent() const;
179 
180     /** Get the ascent of this font
181      *  Used to build the font dictionay
182      *  \returns the ascender for this font
183      *
184      *  \see GetAscent
185      */
186     virtual double GetPdfAscent() const;
187 
188     /** Get the descent of this font in PDF
189      *  units for the current font size.
190      *  This value is usually negative!
191      *
192      *  \returns the descender for this font
193      *
194      *  \see GetPdfDescent
195      */
196     virtual double GetDescent() const;
197 
198     /** Get the descent of this font
199      *  Used to build the font dictionay
200      *  \returns the descender for this font
201      *
202      *  \see GetDescent
203      */
204     virtual double GetPdfDescent() const;
205 
206     /** Get the italic angle of this font.
207      *  Used to build the font dictionay
208      *  \returns the italic angle of this font.
209      */
210     virtual int GetItalicAngle() const;
211 
212     /** Get the glyph id for a unicode character
213      *  in the current font.
214      *
215      *  \param lUnicode the unicode character value
216      *  \returns the glyhph id for the character or 0 if the glyph was not found.
217      */
218     virtual long GetGlyphId( long lUnicode ) const;
219 
220     /** Symbol fonts do need special treatment in a few cases.
221      *  Use this method to check if the current font is a symbol
222      *  font. Symbold fonts are detected by checking
223      *  if they use FT_ENCODING_MS_SYMBOL as internal encoding.
224      *
225      * \returns true if this is a symbol font
226      */
227     virtual bool IsSymbol() const;
228 
229     /** Get a pointer to the actual font data - if it was loaded from memory.
230      *  \returns a binary buffer of data containing the font data
231      */
232     virtual const char* GetFontData() const;
233 
234     /** Get the length of the actual font data - if it was loaded from memory.
235      *  \returns a the length of the font data
236      */
237     virtual pdf_long GetFontDataLen() const;
238 
239     inline double GetCapHeight() const;
240 
241     /** Get a glyph ID by Unicode value. This is needed for generation
242     *  array of widths to /Font element.
243     *
244     *  \param lUnicode the unicode character value
245     *  \returns a glyph ID
246     *  \see PdfFontType1Base14
247     */
248     long GetGlyphIdUnicode( long lUnicode ) const;
249 
250 private :
251 //	const PODOFO_Base14FontDefDataRec& base14font_data;
252 	const char      *font_name;
253     const PODOFO_CharData  *widths_table;
254     bool             is_font_specific;
255     pdf_int16            ascent;
256     pdf_int16            descent;
257     pdf_uint16           x_height;
258     pdf_uint16           cap_height;
259     PdfRect              bbox;
260 
261 	bool          m_bSymbol;  ///< Internal member to singnal a symbol font
262 
263     unsigned int  m_nWeight;
264     int           m_nItalicAngle;
265 
266 
267     double        m_dAscent;
268     double        m_dPdfAscent;
269     double        m_dDescent;
270     double        m_dPdfDescent;
271 
272     double        m_dLineSpacing;
273     double        m_dUnderlineThickness;
274     double        m_dUnderlinePosition;
275     double        m_dStrikeOutThickness;
276     double        m_dStrikeOutPosition;
277 
278 	int units_per_EM;
279 
280 };
281 
282 
283 const PdfFontMetricsBase14*
284 PODOFO_Base14FontDef_FindBuiltinData  (const char  *font_name);
285 
286 // -----------------------------------------------------
287 //
288 // -----------------------------------------------------
GetCapHeight()289 inline double PdfFontMetricsBase14::GetCapHeight() const
290 {
291     return cap_height;
292 }
293 
294 };
295 
296 #endif // _PDF_FONT_METRICS_BASE14_H_
297