LASi.h
Go to the documentation of this file.
#ifndef LASI_H
#define LASI_H
#include <string>
#include <ostream>
#include <sstream>
#include <map>
#include <pango/pango.h>
#include <ft2build.h>
#include FT_GLYPH_H
class FreetypeGlyphMgr;
class ContextMgr;
// These macros are needed for Visual C++ and other Windows compilers as well
// as gcc 4.x or higher with -fvisibility=hidden option.
// All functions/classes marked with LASIDLLIMPEXP can be imported
// from/exported to the Windows dll/gcc shared library.
// Has no impact on other platforms.
// Details handled identically to PLplot include/pldll.h(.in)
#if defined ( WIN32 )
/* Visual C/C++, Borland, MinGW and Watcom */
#if defined ( __VISUALC__ ) || defined ( _MSC_VER ) || defined ( __BORLANDC__ ) || defined ( __GNUC__ ) || defined ( __WATCOMC__ )
#define LASIDLLEXPORT __declspec( dllexport )
#define LASIDLLIMPORT __declspec( dllimport )
#else
#define LASIDLLEXPORT
#define LASIDLLIMPORT
#endif
#elif defined ( __CYGWIN__ )
#define LASIDLLEXPORT __declspec( dllexport )
#define LASIDLLIMPORT __declspec( dllimport )
#elif defined ( __GNUC__ ) && __GNUC__ > 3
// Follow ideas in http://gcc.gnu.org/wiki/Visibility for GCC version 4.x
// The following forces exported symbols specifically designated with
// LASIDLLEXPORT to be visible.
#define LASIDLLEXPORT __attribute__ ( ( visibility( "default" ) ) )
#define LASIDLLIMPORT
#endif
// For an unknown compiler or static built we clear the macros.
#ifndef LASIDLLEXPORT
#define LASIDLLEXPORT
#define LASIDLLIMPORT
#endif
#if defined(LASi_EXPORTS)
#define LASIDLLIMPEXP LASIDLLEXPORT
#define LASIDLLIMPEXP_DATA(type) LASIDLLEXPORT type
#elif defined(LASi_DLL)
#define LASIDLLIMPEXP LASIDLLIMPORT
#define LASIDLLIMPEXP_DATA(type) LASIDLLIMPORT type
#else
#define LASIDLLIMPEXP
#define LASIDLLIMPEXP_DATA(type) type
#endif
namespace LASi {
enum FontStyle{
NORMAL_STYLE,
OBLIQUE,
ITALIC
};
enum FontWeight{
ULTRALIGHT,
LIGHT,
NORMAL_WEIGHT,
BOLD,
ULTRABOLD,
HEAVY
};
enum FontVariant{
NORMAL_VARIANT,
SMALLCAPS
};
enum FontStretch{
ULTRACONDENSED,
EXTRACONDENSED,
CONDENSED,
SEMICONDENSED,
NORMAL_STRETCH,
SEMIEXPANDED,
EXPANDED,
EXTRAEXPANDED,
ULTRAEXPANDED
};
class PostscriptDocument;
class write_glyph_routine_to_stream;
class LASIDLLIMPEXP oPostscriptStream : public std::ostringstream {
public:
friend class PostscriptDocument;
friend class show;
friend class setFont;
friend class setFontSize;
oPostscriptStream(PostscriptDocument& psDoc) : _psDoc(psDoc) {}
protected:
PostscriptDocument& doc() {return _psDoc;}
private:
PostscriptDocument& _psDoc;
};
template<class T>
inline oPostscriptStream& operator<<(oPostscriptStream& os, T t) {
static_cast<std::ostream&>(os) << t;
return os;
}
class LASIDLLIMPEXP PostscriptDocument {
public:
friend class write_glyph_routine_to_stream; // helper class
friend class show;
PostscriptDocument();
~PostscriptDocument();
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
);
void setFontSize(const double size) {_fontSize = size;}
std::ostringstream& osHeader() {return _osHeader;}
oPostscriptStream& osBody() {return _osBody;}
oPostscriptStream& osFooter() {return _osFooter;}
void write(std::ostream& os, double llx=0, double lly=0, double urx=0, double ury=0);
void get_dimensions(const char* s, double *lineSpacing, double *xAdvance=NULL, double *yMin=NULL, double *yMax=NULL);
void get_dimensions(std::string s, double *lineSpacing, double *xAdvance=NULL, double *yMin=NULL, double *yMax=NULL);
protected:
class GlyphId {
public:
friend bool operator==(const GlyphId id1, const GlyphId id2) {
return id1._str == id2._str;
}
friend bool operator<(const GlyphId id1, const GlyphId id2) {
return id1._str < id2._str;
}
GlyphId() {}
GlyphId(FT_Face, const FT_UInt, uint32_t unichar);
std::string str() const {return _str;}
private:
std::string _str;
};
typedef std::map<GlyphId, FreetypeGlyphMgr> GlyphMap;
typedef void (PostscriptDocument::*GLYPH_FUNC)(
const GlyphMap::value_type&, void* contextData);
void invoke_glyph_routine(const GlyphMap::value_type&, void* contextData);
void accrue_dimensions( const GlyphMap::value_type&, void* contextData);
FT_Error PangoItem_do(const char* s, PangoItem* const pItem,
const GLYPH_FUNC func, void* contextData,
bool applyOffset = false);
void for_each_glyph_do(const std::string& s, const GLYPH_FUNC func, void* contextData,
bool applyOffset = false);
PangoContext* pangoContext() const;
std::string glyphProcName() const;
double getFontSize() {return _fontSize;}
class write_glyph_routine_to_stream {
private:
std::ostream& os;
PangoContext* pangoCtx;
public:
write_glyph_routine_to_stream(std::ostream& os, PangoContext* pangoCtx)
: os(os), pangoCtx(pangoCtx) {}
void operator()(PostscriptDocument::GlyphMap::value_type v);
};
private:
GlyphMap _glyphMap;
static const unsigned int DRAWING_SCALE;
// Use pointers instead of objects in order to minimize namespace pollution of .h file user
// Requires fwd declarations above.
ContextMgr* _pContextMgr; // manage PangoContext*
double _fontSize; // font size to be used when rendering next show()
std::ostringstream _osHeader; // Postscript header
oPostscriptStream _osBody; // Postscript body
oPostscriptStream _osFooter; // Postscript footer
};
class LASIDLLIMPEXP setFont {
public:
friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const setFont& x) {
x.apply(os);
return os;
}
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 )
: _family(family), _style(style), _weight(weight), _variant(variant), _stretch(stretch)
{}
protected:
void apply(oPostscriptStream& os) const {
os.doc().setFont(_family, _style,_weight, _variant, _stretch);
}
private:
const char* const _family;
const LASi::FontStyle _style;
const LASi::FontWeight _weight;
const LASi::FontVariant _variant;
const LASi::FontStretch _stretch;
};
class LASIDLLIMPEXP setFontSize {
public:
friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const setFontSize& x) {
x.apply(os);
return os;
}
setFontSize(double size) : _size(size) {}
protected:
void apply(oPostscriptStream& os) const {
os.doc().setFontSize(_size);
}
private:
double _size;
};
class LASIDLLIMPEXP show{
public:
friend inline oPostscriptStream& operator<<(oPostscriptStream& os, const show& x) {
x.apply(os);
return os;
}
show(const char* c_str ) : _str(c_str ) {}
show(std::string stl_str ) : _str(stl_str) {}
protected:
void apply(oPostscriptStream& os) const;
private:
std::string _str;
};
}
#endif
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::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::HEAVY
Definition: LASi.h:77
LASi::PostscriptDocument::GlyphId::str
std::string str() const
Definition: LASi.h:205
LASi::SEMIEXPANDED
Definition: LASi.h:91
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
Definition: LASi.h:63
LASi::CONDENSED
Definition: LASi.h:88
LASi::oPostscriptStream::oPostscriptStream
oPostscriptStream(PostscriptDocument &psDoc)
Definition: LASi.h:110
LASi::FontStretch
FontStretch
Definition: LASi.h:85
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::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
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::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::FontVariant
FontVariant
Definition: LASi.h:80
LASi::show::apply
void apply(oPostscriptStream &os) const
LASi::operator<<
oPostscriptStream & operator<<(oPostscriptStream &os, T t)
Definition: LASi.h:120
LASi::NORMAL_VARIANT
Definition: LASi.h:81
LASi::PostscriptDocument::osHeader
std::ostringstream & osHeader()
Returns stream for Postscript header.
Definition: LASi.h:156
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::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...
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