PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PFont.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/objects/PFont.cpp
4// Purpose: Contains generic specification for a font
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#include "PFont.h"
28#include "PColor.h"
29#include "../base/Variant.h"
30#include "../base/PComplexData.h"
31#include "../utility/rc_ptrs.h"
32
33
34#include <iostream>
35using std::cout;
36using std::endl;
38
40 mFontFileName("Vera.ttf"),
41 mFontStyle(PFS_Normal),
42 mFontSize(16),
43 mAntiAliased(false)
44
45{
46 mCDT = CDT_FONT;
47
53 InitializeProperty("NAME", Variant("<FONT>"));
54
55 // Store colors ONLY in property map (like PDrawObject, PCanvas, PWindow)
56 // Create new color objects for property storage
57 counted_ptr<PEBLObjectBase> fgColor(new PColor(0,0,0,255));
58 PComplexData * pcd = new PComplexData(fgColor);
59 Variant col = Variant(pcd);
60 delete pcd;
61 pcd=NULL;
62 InitializeProperty("FGCOLOR", col);
63
64 counted_ptr<PEBLObjectBase> bgColor(new PColor(0,0,0,255));
65 pcd = new PComplexData(bgColor);
66 col = Variant(pcd);
67 delete pcd;
68 pcd=NULL;
69 InitializeProperty("BGCOLOR", col);
70
72}
73
74
76PFont::PFont(const std::string & filename, int style, int size, PColor fgcolor, PColor bgcolor, bool aa):
77 mFontFileName(filename),
78 mFontStyle(style),
79 mFontSize(size),
80 mAntiAliased(aa)
81{
82 mCDT = CDT_FONT;
83
84 InitializeProperty("NAME", Variant("<FONT>"));
90
91 // Store colors ONLY in property map
92 counted_ptr<PEBLObjectBase> fgColorPtr(new PColor(fgcolor));
93 PComplexData * pcd = new PComplexData(fgColorPtr);
94 Variant col = Variant(pcd);
95 delete pcd;
96 pcd=NULL;
97 InitializeProperty("FGCOLOR", col);
98
99 counted_ptr<PEBLObjectBase> bgColorPtr(new PColor(bgcolor));
100 pcd = new PComplexData(bgColorPtr);
101 col = Variant(pcd);
102 delete pcd;
103 pcd=NULL;
104 InitializeProperty("BGCOLOR", col);
105
106 InitializeProperty("ANTIALIASED", Variant(mAntiAliased));
107}
108
109
111PFont::PFont(const PFont & font)
112
113{
114 mCDT = CDT_FONT;
115
117 mFontStyle = font.GetFontStyle();
118 mFontSize = font.GetFontSize();
120
126 InitializeProperty("NAME", Variant("<FONT>"));
127
128 // Store colors ONLY in property map - copy from source font
129 PColor fgcolor = font.GetFontColor();
130 PColor bgcolor = font.GetBackgroundColor();
131
132 counted_ptr<PEBLObjectBase> fgColorPtr(new PColor(fgcolor));
133 PComplexData * pcd = new PComplexData(fgColorPtr);
134 Variant col = Variant(pcd);
135 delete pcd;
136 pcd=NULL;
137 InitializeProperty("FGCOLOR", col);
138
139 counted_ptr<PEBLObjectBase> bgColorPtr(new PColor(bgcolor));
140 pcd = new PComplexData(bgColorPtr);
141 col = Variant(pcd);
142 delete pcd;
143 pcd=NULL;
144 InitializeProperty("BGCOLOR", col);
145
146 InitializeProperty("ANTIALIASED", Variant(mAntiAliased));
147}
148
149
152{
153
154}
155
156
157//overloaded generic PEBLObjectBase methods
158bool PFont::SetProperty(std::string name, Variant v)
159{
160 if(name == "FILENAME") SetFontFileName(v);
161 else if (name == "BOLD" )
162 {
163 int b,u,i;
164 if(v.GetInteger()) b = PFS_Bold;
165 else b = PFS_Normal;
166
167 if(IsItalicFont()) i = PFS_Italic;
168 else i = PFS_Normal;
169
171 else u = PFS_Normal;
172
173 SetFontStyle( b | i | u);
174
175 }
176 else if (name == "ITALIC" )
177 {
178 int b,u,i;
179 if(IsBoldFont()) b = PFS_Bold;
180 else b = PFS_Normal;
181
182 if(v.GetInteger()) i = PFS_Italic;
183 else i = PFS_Normal;
184
186 else u = PFS_Normal;
187
188 SetFontStyle( b | i | u);
189
190 }
191 else if (name == "UNDERLINE" )
192 {
193 int b,u,i;
194 if(IsBoldFont()) b = PFS_Bold;
195 else b = PFS_Normal;
196
197 if(IsItalicFont()) i = PFS_Italic;
198 else i = PFS_Normal;
199
200 if(v.GetInteger()) u = PFS_Underline;
201 else u = PFS_Normal;
202
203 SetFontStyle( b | i | u);
204 }
205 else if (name == "SIZE") SetFontSize(v) ;
206
207
208 else if (name == "FGCOLOR")
209 {
210 // Extract the new color and update our internal color object
211 // Don't call PEBLObjectBase::SetProperty - the property map already points to mFontColor
212 PColor newColor = *(dynamic_cast<PColor*>(v.GetComplexData()->GetObject().get()));
213 SetFontColor(newColor);
214 }
215 else if (name == "BGCOLOR")
216 {
217 // Extract the new color and update our internal color object
218 // Don't call PEBLObjectBase::SetProperty - the property map already points to mBackgroundColor
219 PColor newColor = *(dynamic_cast<PColor*>(v.GetComplexData()->GetObject().get()));
220 SetBackgroundColor(newColor);
221 }
222 else if (name == "ANTIALIASED")SetAntiAliased(v.GetInteger());
223 else return false;
224
225 return true;
226}
227
228Variant PFont::GetProperty(std::string name)const
229{
230 return PEBLObjectBase::GetProperty(name);
231}
232
233
235{
236 return ValidateProperty(name);
237}
239{
240 return OVE_SUCCESS;
241}
242
243std::string PFont::ObjectName()const
244{
245 return "PFont";
246}
247
248
249
251void PFont::SetFontFileName(const std::string & name)
252{
253 mFontFileName = name;
255}
256
264
265void PFont::SetFontSize(const int size)
266{
267 mFontSize = size;
269}
270
271// Helper methods to get color pointers from property system (like PDrawObject)
273{
275 if(v.GetComplexData())
276 return dynamic_cast<PColor*>(v.GetComplexData()->GetObject().get());
277 return nullptr;
278}
279
281{
283 if(v.GetComplexData())
284 return dynamic_cast<PColor*>(v.GetComplexData()->GetObject().get());
285 return nullptr;
286}
287
288// Get methods that return by value
290{
291 PColor* ptr = GetFontColorPtr();
292 if(ptr) return *ptr;
293 return PColor(0,0,0,255); // default black
294}
295
297{
299 if(ptr) return *ptr;
300 return PColor(0,0,0,255); // default black
301}
302
304{
305 // Update individual color components to avoid problematic copy assignment
306 PColor* ptr = GetFontColorPtr();
307 if(ptr) {
308 ptr->SetRed(color.GetRed());
309 ptr->SetGreen(color.GetGreen());
310 ptr->SetBlue(color.GetBlue());
311 ptr->SetAlpha(color.GetAlpha());
312 }
313}
314
316{
317 // Update individual color components to avoid problematic copy assignment
319 if(ptr) {
320 ptr->SetRed(color.GetRed());
321 ptr->SetGreen(color.GetGreen());
322 ptr->SetBlue(color.GetBlue());
323 ptr->SetAlpha(color.GetAlpha());
324 }
325}
326
327void PFont::SetAntiAliased(const bool aa)
328{
329 mAntiAliased = aa;
331}
332
333
334
336std::ostream & PFont::SendToStream(std::ostream& out) const
337{
338 out << "<PFont: Name: [" << mFontFileName << "]\n";
339 out << " Style: ["<< mFontStyle<< "]\n";
340 out << " Size: ["<< mFontSize << "]\n";
341
342 PColor* fgColor = GetFontColorPtr();
343 PColor* bgColor = GetBackgroundColorPtr();
344
345 if(fgColor)
346 out << " Color: ["<< *fgColor << "]\n";
347 else
348 out << " Color: [NULL]\n";
349
350 if(bgColor)
351 out << " BGColor: ["<< *bgColor<< "]\n";
352 else
353 out << " BGColor: [NULL]\n";
354
355 out << " Antialiased: ["<< mAntiAliased << "]>\n";
356
357 return out;
358}
359
360
361//These get different styles
363{
364 return mFontStyle == 0;
365}
366
368{
369 //The PFS_Bold acts as a bit filter, if it isn't bold the & should be 0.
370 return (mFontStyle & PFS_Bold);
371}
372
374{
375 //The PFS_Italic acts as a bit filter, if it isn't bold the & should be 0.
376 return (mFontStyle & PFS_Italic);
377}
378
380{
381 //The PFS_Underline acts as a bit filter, if it isn't bold the & should be 0.
382 return (mFontStyle & PFS_Underline);
383}
384
#define NULL
Definition BinReloc.cpp:317
ObjectValidationError
Definition PEBLObject.h:37
@ OVE_SUCCESS
Definition PEBLObject.h:38
@ CDT_FONT
Definition PEBLObject.h:52
@ PFS_Underline
Definition PFont.h:48
@ PFS_Normal
Definition PFont.h:45
@ PFS_Bold
Definition PFont.h:46
@ PFS_Italic
Definition PFont.h:47
void SetRed(int color)
Definition PColor.cpp:197
int GetRed() const
Definition PColor.cpp:226
int GetAlpha() const
Definition PColor.cpp:229
int GetBlue() const
Definition PColor.cpp:228
int GetGreen() const
Definition PColor.cpp:227
void SetGreen(int color)
Definition PColor.cpp:204
void SetBlue(int color)
Definition PColor.cpp:211
void SetAlpha(int color)
Definition PColor.cpp:218
counted_ptr< PEBLObjectBase > GetObject() const
virtual bool InitializeProperty(std::string name, Variant v)
virtual bool SetProperty(std::string name, Variant v)
ComplexDataType mCDT
Definition PEBLObject.h:109
Variant GetProperty(std::string) const
Definition PFont.h:53
virtual void SetFontColor(PColor color)
Definition PFont.cpp:303
PColor * GetBackgroundColorPtr() const
Definition PFont.cpp:280
PColor * GetFontColorPtr() const
Definition PFont.cpp:272
int mFontStyle
Definition PFont.h:105
virtual PColor GetBackgroundColor() const
Definition PFont.cpp:296
virtual Variant GetProperty(std::string) const
Definition PFont.cpp:228
virtual void SetFontStyle(const int style)
Definition PFont.cpp:257
virtual int GetFontStyle() const
Definition PFont.h:84
virtual void SetBackgroundColor(PColor color)
Definition PFont.cpp:315
int mFontSize
Definition PFont.h:106
virtual ~PFont()
Copy constructor.
Definition PFont.cpp:151
PFont()
Standard constructors:
Definition PFont.cpp:39
virtual bool IsItalicFont() const
Definition PFont.cpp:373
virtual void SetFontSize(const int size)
Definition PFont.cpp:265
virtual PColor GetFontColor() const
Definition PFont.cpp:289
virtual void SetAntiAliased(const bool aa)
Definition PFont.cpp:327
virtual std::string GetFontFileName() const
Definition PFont.h:83
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
Definition PFont.cpp:234
virtual std::string ObjectName() const
Definition PFont.cpp:243
std::string mFontFileName
Definition PFont.h:104
virtual bool IsUnderlineFont() const
Definition PFont.cpp:379
virtual bool IsBoldFont() const
Definition PFont.cpp:367
virtual bool GetAntiAliased() const
Definition PFont.h:88
bool mAntiAliased
Definition PFont.h:110
virtual int GetFontSize() const
Definition PFont.h:85
virtual std::ostream & SendToStream(std::ostream &out) const
This sends the font descriptions to the specified stream.
Definition PFont.cpp:336
virtual void SetFontFileName(const std::string &name)
Set methods for all of the data in font.
Definition PFont.cpp:251
virtual bool SetProperty(std::string, Variant v)
Standard Destructors.
Definition PFont.cpp:158
virtual bool IsNormalFont() const
Definition PFont.cpp:362
pInt GetInteger() const
Definition Variant.cpp:997
PComplexData * GetComplexData() const
Definition Variant.cpp:1299
X * get() const
Definition rc_ptrs.h:110