1 /***************************************************************************
2 * Copyright (C) 2007 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_H_
35 #define _PDF_FONT_H_
36
37 #include "podofo/base/PdfDefines.h"
38 #include "podofo/base/PdfName.h"
39 #include "podofo/base/PdfEncodingFactory.h"
40 #include "PdfElement.h"
41 #include "PdfFontMetrics.h"
42
43 namespace PoDoFo {
44
45 class PdfObject;
46 class PdfPage;
47 class PdfWriter;
48
49
50
51 /** Before you can draw text on a PDF document, you have to create
52 * a font object first. You can reuse this font object as often
53 * as you want.
54 *
55 * Use PdfDocument::CreateFont to create a new font object.
56 * It will choose a correct subclass using PdfFontFactory.
57 *
58 * This is only an abstract base class which is implemented
59 * for different font formats.
60 */
61 class PODOFO_DOC_API PdfFont : public PdfElement {
62 friend class PdfFontFactory;
63
64 public:
65
66 /** Create a new PdfFont object which will introduce itself
67 * automatically to every page object it is used on.
68 *
69 * The font has a default font size of 12.0pt.
70 *
71 * \param pMetrics pointer to a font metrics object. The font in the PDF
72 * file will match this fontmetrics object. The metrics object is
73 * deleted along with the font.
74 * \param pEncoding the encoding of this font. The font will take ownership of this object
75 * depending on pEncoding->IsAutoDelete()
76 * \param pParent parent of the font object
77 *
78 */
79 PdfFont( PdfFontMetrics* pMetrics, const PdfEncoding* const pEncoding, PdfVecObjects* pParent );
80
81
82 /** Create a PdfFont based on an existing PdfObject
83 * \param pMetrics pointer to a font metrics object. The font in the PDF
84 * file will match this fontmetrics object. The metrics object is
85 * deleted along with the font.
86 * \param pEncoding the encoding of this font. The font will take ownership of this object
87 * depending on pEncoding->IsAutoDelete()
88 * \param pObject an existing PdfObject
89 */
90 PdfFont( PdfFontMetrics* pMetrics, const PdfEncoding* const pEncoding, PdfObject* pObject );
91
92 virtual ~PdfFont();
93
94 /** Set the font size before drawing with this font.
95 * \param fSize font size in points
96 */
97 inline void SetFontSize( float fSize );
98
99 /** Retrieve the current font size of this font object
100 * \returns the current font size
101 */
102 inline float GetFontSize() const;
103
104 /** Set the horizontal scaling of the font for compressing (< 100) and expanding (>100)
105 * \param fScale scaling in percent
106 */
107 inline void SetFontScale( float fScale );
108
109 /** Retrieve the current horizontal scaling of this font object
110 * \returns the current font scaling
111 */
112 inline float GetFontScale() const;
113
114 /** Set the character spacing of the font
115 * \param fCharSpace character spacing in percent
116 */
117 inline void SetFontCharSpace( float fCharSpace );
118
119 /** Retrieve the current character spacing of this font object
120 * \returns the current font character spacing
121 */
122 inline float GetFontCharSpace() const;
123
124 /** Set the word spacing of the font
125 * \param fWordSpace word spacing in PDF units
126 */
127 inline void SetWordSpace( float fWordSpace );
128
129 /** Retrieve the current word spacing of this font object
130 * \returns the current font word spacing in PDF units
131 */
132 inline float GetWordSpace() const;
133
134 /** Set the underlined property of the font
135 * \param bUnder if true any text drawn with this font
136 * by a PdfPainter will be underlined.
137 * Default is false
138 */
139 inline void SetUnderlined( bool bUnder );
140
141 /** \returns true if the font is underlined
142 * \see IsBold
143 * \see IsItalic
144 */
145 inline bool IsUnderlined() const;
146
147 /** \returns true if this font is bold
148 * \see IsItalic
149 * \see IsUnderlined
150 */
151 inline bool IsBold() const;
152
153 /** \returns true if this font is italic
154 * \see IsBold
155 * \see IsUnderlined
156 */
157 inline bool IsItalic() const;
158
159 /** Set the strikeout property of the font
160 * \param bStrikeOut if true any text drawn with this font
161 * by a PdfPainter will be strikedout.
162 * Default is false
163 */
164 inline void SetStrikeOut( bool bStrikeOut );
165
166 /** \returns true if the font is striked out
167 */
168 inline bool IsStrikeOut() const;
169
170 /** Returns the identifier of this font how it is known
171 * in the pages resource dictionary.
172 * \returns PdfName containing the identifier (e.g. /Ft13)
173 */
174 inline const PdfName & GetIdentifier() const;
175
176 /** Returns a reference to the fonts encoding
177 * \returns a PdfEncoding object.
178 */
179 inline const PdfEncoding* GetEncoding() const;
180
181 /** Returns a handle to the fontmetrics object of this font.
182 * This can be used for size calculations of text strings when
183 * drawn using this font.
184 * \returns a handle to the font metrics object
185 */
186 inline const PdfFontMetrics* GetFontMetrics() const;
187
188 // Peter Petrov 19 March 2009
189 /** Returns a handle to the fontmetrics object of this font.
190 * This can be used for size calculations of text strings when
191 * drawn using this font.
192 * \returns a handle to the font metrics object
193 */
194 inline PdfFontMetrics* GetFontMetrics2();
195
196 /** Write a PdfString to a PdfStream in a format so that it can
197 * be used with this font.
198 * This is used by PdfPainter::DrawText to display a text string.
199 * The following PDF operator will be Tj
200 *
201 * \param rsString a unicode or ansi string which will be displayed
202 * \param pStream the string will be appended to pStream without any leading
203 * or following whitespaces.
204 */
205 virtual void WriteStringToStream( const PdfString & rsString, PdfStream* pStream );
206
207 // Peter Petrov 24 September 2008
208 /** Embeds the font into PDF page
209 *
210 */
211 virtual void EmbedFont();
212
213 /** Remember the glyphs used in the string in case of subsetting
214 *
215 * \param sText the text string which should be printed (is not allowed to be NULL!)
216 * \param lStringLen draw only lLen characters of pszText
217 *
218 * Only call if IsSubsetting() returns true. Might throw an exception otherwise.
219 *
220 * \see IsSubsetting
221 */
222 virtual void AddUsedSubsettingGlyphs( const PdfString & sText, long lStringLen );
223
224 /** Remember the glyphname in case of subsetting
225 *
226 * \param pszGlyphName Name of the glyph to remember
227 */
228 virtual void AddUsedGlyphname( const char * pszGlyphName );
229
230 /** Embeds pending subset-font into PDF page
231 * Only call if IsSubsetting() returns true. Might throw an exception otherwise.
232 *
233 * \see IsSubsetting
234 */
235 virtual void EmbedSubsetFont();
236
237 /** Check if this is a subsetting font.
238 * \returns true if this is a subsetting font
239 */
240 inline bool IsSubsetting() const;
241
242 protected:
243 /** Get the base font name of this font
244 *
245 * \returns the base font name
246 */
247 inline const PdfName& GetBaseFont() const;
248
249 void InitBase14Font();
250
251
252 const PdfEncoding* const m_pEncoding;
253 PdfFontMetrics* m_pMetrics;
254
255 bool m_bBold;
256 bool m_bItalic;
257 bool m_bUnderlined;
258 bool m_bStrikedOut;
259
260 bool m_bWasEmbedded;
261 bool m_isBase14;
262 bool m_bIsSubsetting;
263 PdfName m_Identifier;
264
265 /** Used to specify if this represents a bold font
266 * \param bBold if true this is a bold font.
267 *
268 * \see IsBold
269 *
270 * This can be called by PdfFontFactory to tell this font
271 * object that it belongs to a bold font.
272 */
273 virtual void SetBold( bool bBold );
274
275 /** Used to specify if this represents an italic font
276 * \param bItalic if true this is an italic font.
277 *
278 * \see IsItalc
279 *
280 * This can be called by PdfFontFactory to tell this font
281 * object that it belongs to an italic font.
282 */
283 virtual void SetItalic( bool bItalic );
284
285
286 private:
287 /** default constructor, not implemented
288 */
289 PdfFont(void);
290 /** copy constructor, not implemented
291 */
292 PdfFont(const PdfFont& rhs);
293 /** assignment operator, not implemented
294 */
295 PdfFont& operator=(const PdfFont& rhs);
296
297 /** Initialize all variables
298 */
299 void InitVars();
300
301 PdfName m_BaseFont;
302 };
303
304 // -----------------------------------------------------
305 //
306 // -----------------------------------------------------
GetBaseFont()307 const PdfName& PdfFont::GetBaseFont() const
308 {
309 return m_BaseFont;
310 }
311
312 // -----------------------------------------------------
313 //
314 // -----------------------------------------------------
GetIdentifier()315 const PdfName & PdfFont::GetIdentifier() const
316 {
317 return m_Identifier;
318 }
319
320 // -----------------------------------------------------
321 //
322 // -----------------------------------------------------
SetFontSize(float fSize)323 void PdfFont::SetFontSize( float fSize )
324 {
325 m_pMetrics->SetFontSize( fSize );
326 }
327
328 // -----------------------------------------------------
329 //
330 // -----------------------------------------------------
GetFontSize()331 float PdfFont::GetFontSize() const
332 {
333 return m_pMetrics->GetFontSize();
334 }
335
336 // -----------------------------------------------------
337 //
338 // -----------------------------------------------------
SetFontScale(float fScale)339 void PdfFont::SetFontScale( float fScale )
340 {
341 m_pMetrics->SetFontScale( fScale );
342 }
343
344 // -----------------------------------------------------
345 //
346 // -----------------------------------------------------
GetFontScale()347 float PdfFont::GetFontScale() const
348 {
349 return m_pMetrics->GetFontScale();
350 }
351
352 // -----------------------------------------------------
353 //
354 // -----------------------------------------------------
SetFontCharSpace(float fCharSpace)355 void PdfFont::SetFontCharSpace( float fCharSpace )
356 {
357 m_pMetrics->SetFontCharSpace( fCharSpace );
358 }
359
360 // -----------------------------------------------------
361 //
362 // -----------------------------------------------------
GetFontCharSpace()363 float PdfFont::GetFontCharSpace() const
364 {
365 return m_pMetrics->GetFontCharSpace();
366 }
367
368 // -----------------------------------------------------
369 //
370 // -----------------------------------------------------
SetWordSpace(float fWordSpace)371 inline void PdfFont::SetWordSpace( float fWordSpace )
372 {
373 m_pMetrics->SetWordSpace( fWordSpace );
374 }
375
376 // -----------------------------------------------------
377 //
378 // -----------------------------------------------------
GetWordSpace()379 inline float PdfFont::GetWordSpace() const
380 {
381 return m_pMetrics->GetWordSpace();
382 }
383
384 // -----------------------------------------------------
385 //
386 // -----------------------------------------------------
GetEncoding()387 const PdfEncoding* PdfFont::GetEncoding() const
388 {
389 return m_pEncoding;
390
391 }
392
393 // -----------------------------------------------------
394 //
395 // -----------------------------------------------------
GetFontMetrics2()396 PdfFontMetrics* PdfFont::GetFontMetrics2()
397 {
398 return m_pMetrics;
399 }
400
401 // -----------------------------------------------------
402 //
403 // -----------------------------------------------------
GetFontMetrics()404 const PdfFontMetrics* PdfFont::GetFontMetrics() const
405 {
406 return m_pMetrics;
407 }
408
409 // -----------------------------------------------------
410 //
411 // -----------------------------------------------------
SetUnderlined(bool bUnder)412 void PdfFont::SetUnderlined( bool bUnder )
413 {
414 m_bUnderlined = bUnder;
415 }
416
417 // -----------------------------------------------------
418 //
419 // -----------------------------------------------------
IsUnderlined()420 bool PdfFont::IsUnderlined() const
421 {
422 return m_bUnderlined;
423 }
424
425 // -----------------------------------------------------
426 //
427 // -----------------------------------------------------
SetStrikeOut(bool bStrikeOut)428 void PdfFont::SetStrikeOut( bool bStrikeOut )
429 {
430 m_bStrikedOut = bStrikeOut;
431 }
432
433 // -----------------------------------------------------
434 //
435 // -----------------------------------------------------
IsStrikeOut()436 bool PdfFont::IsStrikeOut() const
437 {
438 return m_bStrikedOut;
439 }
440
441 // -----------------------------------------------------
442 //
443 // -----------------------------------------------------
IsBold()444 bool PdfFont::IsBold() const
445 {
446 return m_bBold;
447 }
448
449 // -----------------------------------------------------
450 //
451 // -----------------------------------------------------
IsItalic()452 bool PdfFont::IsItalic() const
453 {
454 return m_bItalic;
455 }
456
457 // -----------------------------------------------------
458 //
459 // -----------------------------------------------------
IsSubsetting()460 bool PdfFont::IsSubsetting() const
461 {
462 return m_bIsSubsetting;
463 }
464
465 };
466
467 #endif // _PDF_FONT_H_
468
469