File: font.h

package info (click to toggle)
mathgl 8.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 248,044 kB
  • sloc: cpp: 87,365; ansic: 3,299; javascript: 3,284; pascal: 1,562; python: 52; sh: 51; makefile: 47; f90: 22
file content (155 lines) | stat: -rw-r--r-- 8,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***************************************************************************
 * font.h is part of Math Graphic Library
 * Copyright (C) 2007-2016 Alexey Balakin <mathgl.abalakin@gmail.ru>       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License  as       *
 *   published by the Free Software Foundation; either version 3 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
//-----------------------------------------------------------------------------
#ifndef _MGL_FONT_H_
#define _MGL_FONT_H_

#include "mgl2/define.h"
//-----------------------------------------------------------------------------
#define MGL_FONT_BOLD		0x01000000	// This value is used binary
#define MGL_FONT_ITAL		0x02000000	// This value is used binary
#define MGL_FONT_BOLD_ITAL	0x03000000
#define MGL_FONT_WIRE		0x04000000
#define MGL_FONT_OLINE		0x08000000
#define MGL_FONT_ULINE		0x10000000
#define MGL_FONT_ZEROW		0x20000000	// internal codes
#define MGL_FONT_UPPER		0x40000000
#define MGL_FONT_LOWER		0x80000000
#define MGL_FONT_ROMAN		0xfcffffff
#define MGL_FONT_MASK		0x000fffff	// NOTE: really, only 0xffff range is used due to WIN32 compatibility and FTGL limitation for the generated fonts
#define MGL_FACE_FACTOR		0x00100000
#define MGL_FACE_MASK		0x00f00000	// Range to font face (up to 16 font faces can be used simultaneously)
#define MGL_COLOR_MASK		0xffffff00
#define MGL_FONT_STYLE		0x3f000000
//-----------------------------------------------------------------------------
struct mglGlyphDescr
{
	wchar_t id;		///< Unicode ID for glyph
	int tr[4];		///< Shift of glyph description by triangles (for solid font)
	int ln[4];		///< Shift of glyph description by lines (for wire font)
	short numt[4];	///< Number of triangles in glyph description (for solid font)
	short numl[4];	///< Number of lines in glyph description (for wire font)
	short width[4];	///< Width of glyph for wire font
	short y1[4], y2[4];	///< minimal and maximal y-coordinates
	mglGlyphDescr()	{	memset(this,0,sizeof(mglGlyphDescr));	}
};
inline bool operator<(const mglGlyphDescr &a,const mglGlyphDescr &b)	{	return a.id<b.id;	}
inline bool operator>(const mglGlyphDescr &a,const mglGlyphDescr &b)	{	return a.id>b.id;	}
#if defined(_MSC_VER)
MGL_EXTERN template class MGL_EXPORT std::vector<mglGlyphDescr>;
#endif
//-----------------------------------------------------------------------------
extern const float mgl_fact;
struct MGL_EXPORT mglTeXsymb	{	unsigned kod;	const wchar_t *tex;	};
const float mgl_fgen = 4*14;
/// Get font color, style and align for internal parser
bool MGL_EXPORT mglGetStyle(const char *how, int *font, int *align=0);
long MGL_EXPORT mgl_internal_code(unsigned s, const std::vector<mglGlyphDescr> &glyphs);
class mglBase;
//-----------------------------------------------------------------------------
/// Class for font typeface and text plotting procedures
class MGL_EXPORT mglFont
{
public:
	mglBase *gr;	///< mglBase class used for drawing characters
	mglFont();
	mglFont(const char *name, const char *path=0);
	virtual ~mglFont();
	bool parse;		///< Parse LaTeX symbols

	/// Load font data to memory. Normally used by constructor.
	bool Load(const char *base, const char *path=0);
	/// Load binary font data to memory. Normally used by constructor.
	bool LoadBin(const char *base, const char *path=0);
	/// Save binary font data
	size_t SaveBin(const char *fname);
	/// Free memory
	void Clear();
	/// Copy data from other font
	void Copy(mglFont *);
	/// Restore default font
	void Restore();
	/// Return true if font is loaded
	inline bool Ready() const	{	return GetNumGlyph()!=0;	}

	/// Get height of text
	float Height(int font) const MGL_FUNC_PURE;
	/// Get height of text
	float Height(const char *how) const MGL_FUNC_PURE;
	/// Print text string for font specified by string
	float Puts(const char *str,const char *how,float c1,float c2) const;
	/// Get width of text string for font specified by string
	float Width(const char *str, const char *how, float *y1=0, float *y2=0) const;
	/// Print text string for font specified by string
	float Puts(const wchar_t *str,const char *how,float c1,float c2) const;
	/// Get width of text string for font specified by string
	float Width(const wchar_t *str,const char *how, float *y1=0, float *y2=0) const;

	/// Set height scaling factor
	inline void HeightScale(float s=1)	{	Hscale = s>0?s:1;	}
	/// Get internal code for symbol
	inline long Internal(unsigned s) const	{	return mgl_internal_code(s,glyphs);	}
	/// Return number of glyphs
	inline unsigned GetNumGlyph() const	{	return glyphs.size();	};
	/// Return some of pointers
	inline const short *GetTr(int s, long j) const	{	return Buf+glyphs[j].tr[s];	}
	inline const short *GetLn(int s, long j) const	{	return Buf+glyphs[j].ln[s];	}
	inline int GetNt(int s, long j) const		{	return glyphs[j].numt[s];	}
	inline int GetNl(int s, long j) const		{	return glyphs[j].numl[s];	}
	inline short GetWidth(int s, long j) const	{	return glyphs[j].width[s];	}
	inline float GetFact(int s) const			{	return fact[s];	}
	inline wchar_t GetUnicode(long j) const		{	return j>=0?glyphs[j].id:0;	}
protected:
	std::vector<mglGlyphDescr> glyphs;	///< information about know glyphs
	float fact[4];	///< Divider for width of glyph
	short *Buf;		///< Buffer for glyph descriptions
	size_t numb;	///< Buffer size
	float Hscale;	///< Height scaling

	/// Print text string for font specified by integer constant
	float Puts(const wchar_t *str,int font,int align, float c1,float c2) const;
	/// Get height of text string for font specified by integer constant
	float Height(const wchar_t *str,int font) const;
	/// Get width of text string for font specified by integer constant
	float Width(const wchar_t *str,int font,int align, float &y1, float &y2) const;
	/// Replace TeX symbols by its UTF code and add font styles
	void Convert(const wchar_t *str, unsigned *res) const;
	/// Fill minimal and maximal y-coordinates
	void FillY12();

	/// Draw string recursively
	/* x,y - position, f - factor, style: 0x1 - italic, 0x2 - bold, 0x4 - overline, 0x8 - underline, 0x10 - empty (not draw) */
	float Puts(const unsigned *str, float x,float y,float f,int style,float c1,float c2, float &y1, float &y2) const;
	/// Parse LaTeX command
	unsigned Parse(const wchar_t *s) const;
	/// Get symbol for character ch with given font style
	unsigned Symbol(char ch) const MGL_FUNC_PURE;
private:
	float get_ptr(long &i,unsigned *str, unsigned **b1, unsigned **b2,float &w1,float &w2, float f1, float f2, int st) const;
	bool read_data(const char *fname, int s, std::vector<short> &buf, std::vector<mglGlyphDescr> &extra);
	void main_copy();
	bool read_main(const char *fname, std::vector<short> &buf);
	inline void mem_alloc(long numg)	{	glyphs.resize(numg);	}
	bool read_def();
	void draw_ouline(int st, float x, float y, float f, float g, float ww, float ccol) const;
};
//-----------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------