LASi: LASi.h Source File

LASi

LASi.h
Go to the documentation of this file.
1 #ifndef LASI_H
2 #define LASI_H
3
9 #include <string>
10 #include <ostream>
11 #include <sstream>
12 #include <map>
13 #include <pango/pango.h>
14 #include <ft2build.h>
15 #include FT_GLYPH_H
16
17 class FreetypeGlyphMgr;
18 class ContextMgr;
19
20 // These macros are needed for Visual C++ and other Windows compilers as well
21 // as gcc 4.x or higher with -fvisibility=hidden option.
22 // All functions/classes marked with LASIDLLIMPEXP can be imported
23 // from/exported to the Windows dll/gcc shared library.
24 // Has no impact on other platforms.
25 // Details handled identically to PLplot include/pldll.h(.in)
26 #if defined ( WIN32 )
27 /* Visual C/C++, Borland, MinGW and Watcom */
28 #if defined ( __VISUALC__ ) || defined ( _MSC_VER ) || defined ( __BORLANDC__ ) || defined ( __GNUC__ ) || defined ( __WATCOMC__ )
29 #define LASIDLLEXPORT __declspec( dllexport )
30 #define LASIDLLIMPORT __declspec( dllimport )
31 #else
32 #define LASIDLLEXPORT
33 #define LASIDLLIMPORT
34 #endif
35 #elif defined ( __CYGWIN__ )
36 #define LASIDLLEXPORT __declspec( dllexport )
37 #define LASIDLLIMPORT __declspec( dllimport )
38 #elif defined ( __GNUC__ ) && __GNUC__ > 3
39 // Follow ideas in http://gcc.gnu.org/wiki/Visibility for GCC version 4.x
40 // The following forces exported symbols specifically designated with
41 // LASIDLLEXPORT to be visible.
42 #define LASIDLLEXPORT __attribute__ ( ( visibility( "default" ) ) )
43 #define LASIDLLIMPORT
44 #endif
45
46 // For an unknown compiler or static built we clear the macros.
47 #ifndef LASIDLLEXPORT
48 #define LASIDLLEXPORT
49 #define LASIDLLIMPORT
50 #endif
51
52 #if defined(LASi_EXPORTS)
53 #define LASIDLLIMPEXP LASIDLLEXPORT
54 #define LASIDLLIMPEXP_DATA(type) LASIDLLEXPORT type
55 #elif defined(LASi_DLL)
56 #define LASIDLLIMPEXP LASIDLLIMPORT
57 #define LASIDLLIMPEXP_DATA(type) LASIDLLIMPORT type
58 #else
59 #define LASIDLLIMPEXP
60 #define LASIDLLIMPEXP_DATA(type) type
61 #endif
62
63 namespace LASi {
64
65 enum FontStyle{
66 NORMAL_STYLE,
67 OBLIQUE,
68 ITALIC
69 };
70
71 enum FontWeight{
72 ULTRALIGHT,
73 LIGHT,
74 NORMAL_WEIGHT,
75 BOLD,
76 ULTRABOLD,
77 HEAVY
78 };
79
80 enum FontVariant{
81 NORMAL_VARIANT,
82 SMALLCAPS
83 };
84
85 enum FontStretch{
86 ULTRACONDENSED,
87 EXTRACONDENSED,
88 CONDENSED,
89 SEMICONDENSED,
90 NORMAL_STRETCH,
91 SEMIEXPANDED,
92 EXPANDED,
93 EXTRAEXPANDED,
94 ULTRAEXPANDED
95 };
96
97 class PostscriptDocument;
98 class write_glyph_routine_to_stream;
99
103 class LASIDLLIMPEXP oPostscriptStream : public std::ostringstream {
104 public:
105 friend class PostscriptDocument;
106 friend class show;
107 friend class setFont;
108 friend class setFontSize;
109
110 oPostscriptStream(PostscriptDocument& psDoc) : _psDoc(psDoc) {}
111
112 protected:
113 PostscriptDocument& doc() {return _psDoc;}
114
115 private:
116 PostscriptDocument& _psDoc;
117 };
118
119 template<class T>
120 inline oPostscriptStream& operator<<(oPostscriptStream& os, T t) {
121 static_cast<std::ostream&>(os) << t;
122 return os;
123 }
124
130 class LASIDLLIMPEXP PostscriptDocument {
131 public:
132 friend class write_glyph_routine_to_stream; // helper class
133 friend class show;
134
135 PostscriptDocument();
136 ~PostscriptDocument();
137
141 void setFont(
142 const char* const family = "sans",
143 LASi::FontStyle = LASi::NORMAL_STYLE,
144 LASi::FontWeight = LASi::NORMAL_WEIGHT,
145 LASi::FontVariant = LASi::NORMAL_VARIANT,
146 LASi::FontStretch = LASi::NORMAL_STRETCH
147 );
148
152 void setFontSize(const double size) {_fontSize = size;}
153
156 std::ostringstream& osHeader() {return _osHeader;}
157
160 oPostscriptStream& osBody() {return _osBody;}
161
164 oPostscriptStream& osFooter() {return _osFooter;}
165
175 void write(std::ostream& os, double llx=0, double lly=0, double urx=0, double ury=0);
176
183 void get_dimensions(const char* s, double *lineSpacing, double *xAdvance=NULL, double *yMin=NULL, double *yMax=NULL);
184 void get_dimensions(std::string s, double *lineSpacing, double *xAdvance=NULL, double *yMin=NULL, double *yMax=NULL);
185
186 protected:
191 class GlyphId {
192 public:
193 friend bool operator==(const GlyphId id1, const GlyphId id2) {
194 return id1._str == id2._str;
195 }
196
197 friend bool operator<(const GlyphId id1, const GlyphId id2) {
198 return id1._str < id2._str;
199 }
200
201 GlyphId() {}
202 GlyphId(FT_Face, const FT_UInt, uint32_t unichar);
203
205 std::string str() const {return _str;}
206
207 private:
209 std::string _str;
210 };
211
214 typedef std::map<GlyphId, FreetypeGlyphMgr> GlyphMap;
215
220 typedef void (PostscriptDocument::*GLYPH_FUNC)(
221 const GlyphMap::value_type&, void* contextData);
222
223 void invoke_glyph_routine(const GlyphMap::value_type&, void* contextData);
224
225 void accrue_dimensions( const GlyphMap::value_type&, void* contextData);
226
231 FT_Error PangoItem_do(const char* s, PangoItem* const pItem,
232 const GLYPH_FUNC func, void* contextData,
233 bool applyOffset = false);
234
237 void for_each_glyph_do(const std::string& s, const GLYPH_FUNC func, void* contextData,
238 bool applyOffset = false);
239
240 PangoContext* pangoContext() const;
241
244 std::string glyphProcName() const;
245
248 double getFontSize() {return _fontSize;}
249
252 class write_glyph_routine_to_stream {
253 private:
254 std::ostream& os;
255 PangoContext* pangoCtx;
256
257 public:
258 write_glyph_routine_to_stream(std::ostream& os, PangoContext* pangoCtx)
259 : os(os), pangoCtx(pangoCtx) {}
260 void operator()(PostscriptDocument::GlyphMap::value_type v);
261 };
262
263 private:
264 GlyphMap _glyphMap;
265
266 static const unsigned int DRAWING_SCALE;
267
268 // Use pointers instead of objects in order to minimize namespace pollution of .h file user
269 // Requires fwd declarations above.
270 ContextMgr* _pContextMgr; // manage PangoContext*
271 double _fontSize; // font size to be used when rendering next show()
272 std::ostringstream _osHeader; // Postscript header
273 oPostscriptStream _osBody; // Postscript body
274 oPostscriptStream _osFooter; // Postscript footer
275 };
276
279 class LASIDLLIMPEXP setFont {
280 public:
283 friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const setFont& x) {
284 x.apply(os);
285 return os;
286 }
287
291 setFont(
292 const char* const family = "sans",
293 const LASi::FontStyle style = LASi::NORMAL_STYLE,
294 const LASi::FontWeight weight = LASi::NORMAL_WEIGHT,
295 const LASi::FontVariant variant = LASi::NORMAL_VARIANT,
296 const LASi::FontStretch stretch = LASi::NORMAL_STRETCH )
297 : _family(family), _style(style), _weight(weight), _variant(variant), _stretch(stretch)
298 {}
299
300 protected:
301 void apply(oPostscriptStream& os) const {
302 os.doc().setFont(_family, _style,_weight, _variant, _stretch);
303 }
304
305 private:
306 const char* const _family;
307 const LASi::FontStyle _style;
308 const LASi::FontWeight _weight;
309 const LASi::FontVariant _variant;
310 const LASi::FontStretch _stretch;
311
312 };
313
316 class LASIDLLIMPEXP setFontSize {
317 public:
320 friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const setFontSize& x) {
321 x.apply(os);
322 return os;
323 }
324
329 setFontSize(double size) : _size(size) {}
330
331 protected:
332 void apply(oPostscriptStream& os) const {
333 os.doc().setFontSize(_size);
334 }
335
336 private:
337 double _size;
338 };
339
342 class LASIDLLIMPEXP show{
343 public:
346 friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const show& x) {
347 x.apply(os);
348 return os;
349 }
350
354 show(const char* c_str ) : _str(c_str ) {}
355 show(std::string stl_str ) : _str(stl_str) {}
356
357 protected:
358 void apply(oPostscriptStream& os) const;
359
360 private:
361 std::string _str;
362 };
363 }
364 #endif
365
LASi::PostscriptDocument::GlyphId::_str
std::string _str
Definition: LASi.h:209
LASi::ULTRAEXPANDED
Definition: LASi.h:94
LASi::setFontSize::operator<<
friend oPostscriptStream & operator<<(oPostscriptStream &os, const setFontSize &x)
Stream inserter for &#39;setFontSize&#39; stream manipulator.
Definition: LASi.h:320
LASi::show::operator<<
friend oPostscriptStream & operator<<(oPostscriptStream &os, const show &x)
stream inserter for &#39;show&#39; stream applicator
Definition: LASi.h:346
LASi::FontWeight
FontWeight
Definition: LASi.h:71
LASi::OBLIQUE
Definition: LASi.h:67
LASi::EXTRACONDENSED
Definition: LASi.h:87
LASi::BOLD
Definition: LASi.h:75
LASi::show
stream applicator applied to oPostscriptStream.
Definition: LASi.h:342
LASi::PostscriptDocument::_pContextMgr
ContextMgr * _pContextMgr
Definition: LASi.h:270
LASi::PostscriptDocument::GlyphId::operator==
friend bool operator==(const GlyphId id1, const GlyphId id2)
Definition: LASi.h:193
LASi::PostscriptDocument::getFontSize
double getFontSize()
Definition: LASi.h:248
LASi::setFont
stream manipulator applied to oPostscriptStream.
Definition: LASi.h:279
LASi::PostscriptDocument::DRAWING_SCALE
static const unsigned int DRAWING_SCALE
Definition: LASi.h:266
LASi::PostscriptDocument::write_glyph_routine_to_stream::pangoCtx
PangoContext * pangoCtx
Definition: LASi.h:255
LASi::HEAVY
Definition: LASi.h:77
LASi::PostscriptDocument::GlyphId::str
std::string str() const
Definition: LASi.h:205
LASi::PostscriptDocument::setFont
void setFont(const char *const family="sans", LASi::FontStyle=LASi::NORMAL_STYLE, LASi::FontWeight=LASi::NORMAL_WEIGHT, LASi::FontVariant=LASi::NORMAL_VARIANT, LASi::FontStretch=LASi::NORMAL_STRETCH)
Sets the font that all subsequent text written to bodyStream() or footerStream() will be rendered wit...
Definition: psDoc.cpp:180
LASi::show::_str
std::string _str
Definition: LASi.h:361
LASi::PostscriptDocument::_fontSize
double _fontSize
Definition: LASi.h:271
LASi::PostscriptDocument::_osFooter
oPostscriptStream _osFooter
Definition: LASi.h:274
LASi::PostscriptDocument::_osBody
oPostscriptStream _osBody
Definition: LASi.h:273
LASi::show::apply
void apply(oPostscriptStream &os) const
Definition: psDoc.cpp:624
LASi::SEMIEXPANDED
Definition: LASi.h:91
LASi::setFont::_style
const LASi::FontStyle _style
Definition: LASi.h:307
LASi::PostscriptDocument::GlyphId::operator<
friend bool operator<(const GlyphId id1, const GlyphId id2)
Definition: LASi.h:197
LASi::setFontSize
stream manipulator applied to oPostscriptStream.
Definition: LASi.h:316
LASi::LIGHT
Definition: LASi.h:73
LASi::oPostscriptStream
Just like any ordinary ostringstream, but maintains a reference to the PostscriptDocument.
Definition: LASi.h:103
LASi::NORMAL_STYLE
Definition: LASi.h:66
LASi::setFont::apply
void apply(oPostscriptStream &os) const
Definition: LASi.h:301
LASi::PostscriptDocument::_glyphMap
GlyphMap _glyphMap
Definition: LASi.h:264
FreetypeGlyphMgr
Manage FT_Glyph by insuring that FT_Glyph is handled correctly.
Definition: glyphMgr.h:22
LASi
Definition: LASi.h:63
LASi::PostscriptDocument::write_glyph_routine_to_stream::os
std::ostream & os
Definition: LASi.h:254
LASi::CONDENSED
Definition: LASi.h:88
LASi::oPostscriptStream::oPostscriptStream
oPostscriptStream(PostscriptDocument &psDoc)
Definition: LASi.h:110
LASi::FontStretch
FontStretch
Definition: LASi.h:85
LASi::oPostscriptStream::_psDoc
PostscriptDocument & _psDoc
Definition: LASi.h:116
LASi::PostscriptDocument::write_glyph_routine_to_stream
For internal use only.
Definition: LASi.h:252
LASi::show::show
show(const char *c_str)
Usage: os << show("some UTF-8 text") << ...
Definition: LASi.h:354
LASi::PostscriptDocument::GlyphId::GlyphId
GlyphId()
Definition: LASi.h:201
LASi::ITALIC
Definition: LASi.h:68
LASi::EXPANDED
Definition: LASi.h:92
LASi::PostscriptDocument::_osHeader
std::ostringstream _osHeader
Definition: LASi.h:272
LASi::setFont::setFont
setFont(const char *const family="sans", const LASi::FontStyle style=LASi::NORMAL_STYLE, const LASi::FontWeight weight=LASi::NORMAL_WEIGHT, const LASi::FontVariant variant=LASi::NORMAL_VARIANT, const LASi::FontStretch stretch=LASi::NORMAL_STRETCH)
Usage: os << setFont("Vera Sans",LASi::ITALIC,LASi::BOLD) << ...
Definition: LASi.h:291
LASi::setFontSize::apply
void apply(oPostscriptStream &os) const
Definition: LASi.h:332
LASi::ULTRACONDENSED
Definition: LASi.h:86
LASIDLLIMPEXP
#define LASIDLLIMPEXP
Definition: LASi.h:59
LASi::PostscriptDocument::osBody
oPostscriptStream & osBody()
Returns stream for Postscript body.
Definition: LASi.h:160
ContextMgr
Manage PangoContext*.
Definition: contextMgr.h:17
LASi::SEMICONDENSED
Definition: LASi.h:89
LASi::oPostscriptStream::doc
PostscriptDocument & doc()
Definition: LASi.h:113
LASi::FontStyle
FontStyle
Definition: LASi.h:65
LASi::NORMAL_STRETCH
Definition: LASi.h:90
LASi::PostscriptDocument::setFontSize
void setFontSize(const double size)
Sets the font size, in points, that all subsequent text written to bodyStream() or footerStream() wil...
Definition: LASi.h:152
LASi::setFont::_variant
const LASi::FontVariant _variant
Definition: LASi.h:309
LASi::PostscriptDocument
Composes Postscript document as three separate and independant streams for header, body and footer.
Definition: LASi.h:130
LASi::PostscriptDocument::GlyphId
For internal use only.
Definition: LASi.h:191
LASi::setFont::_family
const char *const _family
Definition: LASi.h:306
LASi::setFont::_stretch
const LASi::FontStretch _stretch
Definition: LASi.h:310
LASi::FontVariant
FontVariant
Definition: LASi.h:80
LASi::operator<<
oPostscriptStream & operator<<(oPostscriptStream &os, T t)
Definition: LASi.h:120
LASi::NORMAL_VARIANT
Definition: LASi.h:81
LASi::setFontSize::_size
double _size
Definition: LASi.h:337
LASi::PostscriptDocument::osHeader
std::ostringstream & osHeader()
Returns stream for Postscript header.
Definition: LASi.h:156
LASi::setFont::_weight
const LASi::FontWeight _weight
Definition: LASi.h:308
LASi::setFont::operator<<
friend oPostscriptStream & operator<<(oPostscriptStream &os, const setFont &x)
Stream inserter for &#39;setFont&#39; stream manipulator.
Definition: LASi.h:283
LASi::show::show
show(std::string stl_str)
Definition: LASi.h:355
LASi::PostscriptDocument::GlyphMap
std::map< GlyphId, FreetypeGlyphMgr > GlyphMap
Maps glyph routine name to FT_Glyph instance.
Definition: LASi.h:214
LASi::ULTRALIGHT
Definition: LASi.h:72
LASi::setFontSize::setFontSize
setFontSize(double size)
Usage: os << setFontSize(12) << ...
Definition: LASi.h:329
LASi::PostscriptDocument::osFooter
oPostscriptStream & osFooter()
Returns stream for Postscript footer.
Definition: LASi.h:164
LASi::EXTRAEXPANDED
Definition: LASi.h:93
LASi::PostscriptDocument::write_glyph_routine_to_stream::write_glyph_routine_to_stream
write_glyph_routine_to_stream(std::ostream &os, PangoContext *pangoCtx)
Definition: LASi.h:258
LASi::SMALLCAPS
Definition: LASi.h:82
LASi::ULTRABOLD
Definition: LASi.h:76
LASi::NORMAL_WEIGHT
Definition: LASi.h:74