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{
69  };
70 
71  enum FontWeight{
78  };
79 
83  };
84 
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:
117  };
118 
119  template<class T>
121  static_cast<std::ostream&>(os) << t;
122  return os;
123  }
124 
131  public:
132  friend class write_glyph_routine_to_stream; // helper class
133  friend class show;
134 
137 
141  void setFont(
142  const char* const family = "sans",
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);
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 
229  void for_each_glyph_do(const std::string&, const GLYPH_FUNC, void* contextData,
230  bool applyOffset = false);
231 
232  PangoContext* pangoContext() const;
233 
236  std::string glyphProcName() const;
237 
240  double getFontSize() {return _fontSize;}
241 
245  private:
246  std::ostream& os;
247  PangoContext* pangoCtx;
248 
249  public:
250  write_glyph_routine_to_stream(std::ostream& os, PangoContext* pangoCtx)
251  : os(os), pangoCtx(pangoCtx) {}
252  void operator()(PostscriptDocument::GlyphMap::value_type v);
253  };
254 
255  private:
257 
258  static const unsigned int DRAWING_SCALE;
259 
260  // Use pointers instead of objects in order to minimize namespace pollution of .h file user
261  // Requires fwd declarations above.
262  ContextMgr* _pContextMgr; // manage PangoContext*
263  double _fontSize; // font size to be used when rendering next show()
264  std::ostringstream _osHeader; // Postscript header
265  oPostscriptStream _osBody; // Postscript body
266  oPostscriptStream _osFooter; // Postscript footer
267  };
268 
272  public:
275  friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const setFont& x) {
276  x.apply(os);
277  return os;
278  }
279 
284  const char* const family = "sans",
285  const LASi::FontStyle style = LASi::NORMAL_STYLE,
286  const LASi::FontWeight weight = LASi::NORMAL_WEIGHT,
287  const LASi::FontVariant variant = LASi::NORMAL_VARIANT,
288  const LASi::FontStretch stretch = LASi::NORMAL_STRETCH )
289  : _family(family), _style(style), _weight(weight), _variant(variant), _stretch(stretch)
290  {}
291 
292  protected:
293  void apply(oPostscriptStream& os) const {
294  os.doc().setFont(_family, _style,_weight, _variant, _stretch);
295  }
296 
297  private:
298  const char* const _family;
303 
304  };
305 
309  public:
313  x.apply(os);
314  return os;
315  }
316 
321  setFontSize(double size) : _size(size) {}
322 
323  protected:
324  void apply(oPostscriptStream& os) const {
325  os.doc().setFontSize(_size);
326  }
327 
328  private:
329  double _size;
330  };
331 
335  public:
338  friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const show& x) {
339  x.apply(os);
340  return os;
341  }
342 
346  show(const char* c_str ) : _str(c_str ) {}
347  show(std::string stl_str ) : _str(stl_str) {}
348 
349  protected:
350  void apply(oPostscriptStream& os) const;
351 
352  private:
353  std::string _str;
354  };
355 }
356 #endif
357