37 a2dFontMonkey(const wxString& filename = wxT(""), double size = 0.0 );
39 a2dFontMonkey(const a2dFontMonkey& other);
41 a2dFontMonkey( const a2dFontMonkey& other, CloneOptions options );
43 virtual ~a2dFontMonkey();
45 // Documented in base class
46 virtual wxRefObject* Clone( CloneOptions options ) const;
49 a2dFontMonkey& operator = (const a2dFontMonkey& other);
51 // Documented in base class
52 virtual bool operator == (const a2dFont& other) const;
59 void SetFilename(const wxString& filename);
64 wxString GetFilename( bool filenameonly = false ) const;
66 // Documented in base class
67 virtual double GetWidth( wxChar c ) const;
69 // Documented in base class
70 virtual double GetKerning( wxChar c1, wxChar c2 ) const;
72 // Documented in base class
73 static void GetInfoList( a2dFontInfoList& list );
75 // Documented in base class
76 static a2dFont* CreateFont( const a2dFontInfo& info, bool force = false );
83 DECLARE_DYNAMIC_CLASS(a2dFontMonkey)
88 \section classimp Class implementation.
90 The class implementation is also straightforward.
92 The problem you have to deal with is parsing the font. Also for improved
93 drawing speed, you might want to add some font caching here. (see also the
94 implementation of a2dFontFreetype::GetGlyph )
96 The extra parameter of m_fontinfo is free to use for anything, but please keep in
97 mind, that this should allways be in text format. In general it will be used for
98 storing the filename of the font. (see also a2dFontInfo )
103 IMPLEMENT_DYNAMIC_CLASS( a2dFontMonkey, a2dFont )
105 a2dFontMonkey::a2dFontMonkey(const wxString& filename, double size, int alignment)
106 : a2dFont(size, alignment)
108 m_fontinfo.SetType( wxT("Monkey font") );
109 SetFilename( filename );
112 a2dFontMonkey::a2dFontMonkey( const a2dFontMonkey& other )
115 m_fontinfo.SetType( wxT("Monkey font") );
122 a2dFontMonkey::a2dFontMonkey( const a2dFontMonkey& other, CloneOptions options )
123 : a2dFont( other, options )
128 SetFilename( other.m_filename );
131 a2dFontMonkey::~a2dFontMonkey()
133 // delete font storage
137 wxRefObject* a2dFontMonkey::Clone( CloneOptions options ) const
139 return new a2dFontMonkey( *this, options );
142 a2dFontMonkey& a2dFontMonkey::operator = ( const a2dFontMonkey& other )
144 a2dFont::operator = ( other );
145 SetFilename( m_filename );
149 bool a2dFontMonkey::operator == ( const a2dFont& other ) const
151 a2dFontMonkey* mkyfont = wxDynamicCast( &other, a2dFontMonkey );
152 return (mkyfont) && a2dFont::operator == ( other )
153 && ( m_filename == mkyfont->m_filename );
156 wxString a2dFontMonkey::GetFilename(bool filenameonly) const
160 wxFileName filename( m_filename );
161 return filename.GetFullName();
167 void a2dFontMonkey::SetFilename(const wxString& filename)
171 // delete previous font storage
174 m_filename = filename;
175 if ( m_filename.IsEmpty() )
178 wxString fname = a2dCanvasGlobals->GetFontPathList().FindValidPath( m_filename );
180 // open file and proces font data.
183 size = ...... // something loaded
186 // Set getextextent params normalized to the font height.
187 m_height = .... / size;
188 m_desc = .... / size;
189 m_lead = ..... / size; // or just 0.0;
195 double a2dFontMonkey::GetWidth( wxChar c ) const
200 return m_size * ..... // width of character c;
203 double a2dFontMonkey::GetKerning( wxChar c1, wxChar c2 ) const
208 return m_size * ..... // kerning between c1 and c2;
211 // create a list of fonts for the font selection dialog.
212 void a2dFontMonkey::GetInfoList( a2dFontInfoList& list )
214 wxPathList& pathlist = a2dCanvasGlobals->GetFontPathList();
215 for ( wxPathList::Node *node = pathlist.GetFirst(); node; node = node->GetNext() )
217 wxString path(node->GetData());
218 if (!wxDir::Exists( path ) )
222 wxDir::GetAllFiles( path, &files, wxT("*.mky"), wxDIR_FILES );
223 for ( unsigned int i = 0; i < files.GetCount(); i++)
225 wxFileName filename( files.Item(i) );
227 // get font name and style parameters.
228 wxString fontname = ....
229 wxString stylename = ....
231 // set font style for selection dialog.
232 list.Append( new a2dFontInfo(
233 wxT("Monkey font"), fontname, stylename, filename.GetFullName() ));
238 // try to make a font from the fontinfo.
239 a2dFont* a2dFontMonkey::CreateFont( const a2dFontInfo& info, bool force )
241 // todo implement search for alternatives, see a2dFontFreetype::CreateFont()
242 if ( info.GetType() == wxT("Monkey font") && !info.GetExtra().IsEmpty() )
244 a2dFontMonkey* font = new a2dFontMonkey( info.GetExtra(),
245 info.GetSize(), info.GetAlignment() );
256 \section morethings More things to do.
258 Now that we have a font class, with some a Createfont class does not mean, that
259 the font can now be created in a general way. The applications, do not know of
260 the existance of specific font classes. Therefore they will call a2dFont::CreateFont.
261 To enable the creation of your font, you should add this method to
262 - (stylebase.cpp) a2dFont* a2dFont::CreateFont( const a2dFontInfo& info );
264 Also to be able to select the font using the font selection dialog, add your class to:
265 - (stylebase.cpp) void a2dFont::GetInfoList( a2dFontInfoList& list );
268 \section drawing Drawing of the font
270 Add a specific monkey font drawing routines to the wxDrawer2D header:
271 - (drawer2d.h) virtual void wxDrawer2D::DrawTextMonkey( const wxString& text, double x, double y ) { DrawTextGeneric( text, x, y, &wxDcDrawer::DrawCharMonkeyCb ); }
272 - (drawer2d.h) virtual void wxDrawer2D::DrawCharMonkey( wxChar c ) { DrawCharUnknown( c ); }
273 - (drawer2d.h) void wxDrawer2D::DrawCharMonkeyCb( wxChar c ) { DrawCharMonkey( c ); }
275 The last line is a wrapper for the DrawChar function. It seems redundant, but Iso C++ does not
276 allow anymore to call a virtual member directly.
278 The font drawing will be invokated through DrawText. To let this know of the
279 existance of the monkey font class, you should add this newly created method to:
280 - (drawer2d.cpp) void wxDrawer2D::DrawText( const wxString& text, double x, double y );
282 Now, at minimum, implement the font drawing in wxDcDrawer.
284 The drawing routines are seperated in two routines. The first parses the string and calls the
285 second one to draw each character. The first is allready supplied, but might be overruled,
286 to set text colours, etc.
289 void wxDrawer2D::DrawTextMonkey( const wxString& text, double x, double y )
294 // Call generic string parsing routine.
295 DrawTextGeneric( text, x, y, (void (wxDrawer2D::*)( wxChar )) &wxDcDrawer::DrawCharMonkeyCb );
299 The implementation of the character drawing routine can fully concentrate on drawing
300 the character. The calling routine allready took care of performing all necessary affine
301 transforms. The origin (0, 0) is set at the lower left bounding box of the character.
302 Depending on the font, scaling to size should still be performed here.
305 void wxDcDrawer::DrawCharMonkey( wxChar c )
307 // Draw the character here.
308 a2dFontStroke* mkyfont = wxDynamicCast( m_currentfont.Get(), a2dFontMonkey );
311 a2dAffineMatrix oldaffine = GetTransform();
313 // scale character to size
314 a2dAffineMatrix affine;
315 affine.Scale( mkyfont->GetSize() );
316 SetTransForm( oldaffine * affine );
318 // do actual drawing here.
321 SetTransForm( oldaffine );
325 So now finally the font should work.