htmlFont
Font class
[Previous] [Main] [Next]


Description
htmlFont encapsulates the <FONT></FONT> tags. It is used to specify the font typeface, size, and color used by web browsers to render text.  
 
htmlFont is derived from htmlGroup, and thus can contain any number of other html++ objects. Objects are added using the << operator or the Add() method, and any typeface, size, or color settings specified will apply to all inserted objects, such as tables and text.  
 
It is often convenient to create predefined htmlFont objects representing the desired color, typeface and size (e.g., styles), then use the parentheses () operator to insert text and other objects inline:  
 
  htmlFont normal_text( "Arial, Helvetica" ) ;  
  htmlFont red_text( "Arial, Helvetica",  
                     SIZE_DEFAULT, COLOR_RED);  
 
  page << normal_text( "This is normal text " )  
       << red_text( "and this is red." ) ;  
 
Declaration
#include <dcmicro/htmlpp/font.h>  
 
Hierarchy
htmlfont.gif  
 
See Also
Text Formatting Classes in the User's Guide  
 
Related Constants/Definitions
typedef enum {  
size_relative = 0,  
size_absolute  
} FontSizeType ;  
 
Constructors
htmlFont()  
Constructs an empty font object.  
 
htmlFont(ColorType color,  
   const String& text )  
Constructs a font object containing text using the specified color.  
 
htmlFont(ColorType color,  
   const htmlObject& element )  
Constructs a font object containing element using the specified color.  
 
htmlFont(const String& face,  
   int relative_size,  
   const String& text )  
Constructs a font object containing text using the specified face and relative_size.  
 
htmlFont(const String& face,  
   int relative_size,  
   const htmlObject& element )  
Constructs a font object containing element using the specified face and relative_size.  
 
htmlFont(const String& face,  
   int size = SIZE_DEFAULT,  
   ColorType color = COLOR_DEFAULT,  
   FontSizeType size_type = size_relative )  
Constructs an empty font object using the specified face, size, and color. If face is an empty string (e.g., ""), the browser's default typeface will be used.  
 
htmlFont(const String& face,  
   int size,  
   ColorType color,  
   FontSizeType size_type,  
   const htmlObject& element )  
Constructs a font object using the specified face, size, and color, inserting element into it. If face is an empty string (e.g., ""), the browser's default typeface will be used.  
 
htmlFont(const String& face,  
   int size,  
   ColorType color,  
   FontSizeType size_type,  
   const String& text )  
Constructs a font object using the specified face, size, and color, inserting kinto it. If kis an empty string (e.g., ""), the browser's default typeface will be used.  
 
Destructor
virtual ~htmlFont()  
Destroys the object.  
 
Member Methods
=   htmlFont& operator= (const htmlFont& rhs )  
Replaces the font object contents with a copy of rhs, then returns a reference to the object.  
 
=   htmlFont& operator= (const htmlObject& element )  
Replaces the font object contents with element, then returns a reference to the object.  
 
=   htmlFont& operator= (const String& text )  
Replaces the font object contents with text, then returns a reference to the object.  
 
Face   htmlFont& Face( String& face )  
Sets the font typeface to face, then returns a reference to the object. If the font does not exist on the browser host, most browsers will substitute the closest match. If face is an empty string (e.g., ""), the browser's default typeface will be used.  
 
Some browsers, including Netscape Navigator 3.0 and later, accept a comma-separated list of font faces. In such cases, the browser will use the first font in the list if it is available. Otherwise, it uses the next font in the list if it is available, and so on.  
 
Face   String Face() const  
Returns the font typeface name, or an empty string (e.g., "") if not set.  
 
Size   htmlFont& Size( int size, FontSizeType size_type )  
Sets the font size and type (size_relative or size_absolute), then returns a reference to the object. Legal values for size range from 1 (smallest) to 7 (largest). if size_absolute is used, -7 to +7 is size_relative is used.  
 
Size   String Size() const  
Returns the font size setting as a string. If the size is relative, the value will be preceded by a plus (+) or minus (-) sign.  
 
ColorhtmlFont& Color( ColorType color )  
Sets the font foreground color to color, then returns a reference to the object.  
 
ColorColorType Color() const  
Returns the current font color, or COLOR_DEFAULT if not set.  
 
PointSizehtmlFont& PointSize( int point_size )  
Set the the exact point size of the font, and returns a reference to the object. point_size should be specified as a positive integer. The attribute is only supported by Netscape Navigator 4.0 and later.  
 
PointSizeint PointSize() const  
Returns the point size setting for the font as an integer, or zero if not set.  
 
WeighthtmlFont& Weight( int degree_of_boldness )  
Sets the the weight, or "boldness" of the font and returns a reference to the object. degree_of_boldness can range from 100 (least bold) to 900 (most bold) inclusive, in steps of 100. The attribute is only supported by Netscape Navigator 4.0 and later.  
 
Weightint Weight() const  
Returns the weight setting for the font as an integer, or zero if not set.  
 
AbsoluteSize htmlFont& AbsoluteSize( int size )  
Sets the font size to size. Legal values for size range from 1 to 7.  
 
IsAbsoluteSize Boolean IsAbsoluteSize( int& size ) const  
If the font size is absolute, sets size to the value and returns TRUE. Otherwise, FALSE is returned.  
 
RelativeSizehtmlFont& RelativeSize( int size )  
Sets the font size to size, relative to the surrounding text. Legal values for size range from -7 to +7.  
 
IsRelativeSize Boolean IsRelativeSize( int& size ) const  
If the font size is relative, sets size to the value and returns TRUE. Otherwise, FALSE is returned.  
 
ClonehtmlObject FAR * Clone() const  
Returns a base-class pointer to a deep copy of the object.  
 
Example Program
 
//  Demonstration of controlling font face,  
//  size, and color.  
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlCgi   server ;  
   htmlPage  page( "html++ example application" ) ;  
 
   int  i ;  
 
   //  Show how absolute font sizes appear  
   page << "Absolute sizes: " ;  
   for ( i = 1 ; i <= 7 ; i++ )  
   {  
      htmlFont font("", i, COLOR_RED, size_absolute);  
      font << i << " "  ;  
      page << font ;  
   }  
 
   //  Show how relative font sizes appear  
   page << htmlBreak()  
       << "Relative sizes: " ;  
   for ( i = 7 ; i >= -7 ; i-- )  
   {  
      htmlFont font("", i, COLOR_BLUE, size_relative);  
      font << i << " "  ;  
      page << font ;  
   }  
 
   //  Add a small caption  
   page << htmlBreak()   
       << htmlFont( "", -2, COLOR_DEFAULT, size_relative,  
               "Note that since there are only seven "  
               "possible font sizes, only values in the "  
               "range +4 to -2 show changes in size." )  
        << htmlParagraph() ;  
 
   //  An array of font names  
   char * font_list[] = { "Arial""Verdana""System" } ;  
 
   //  Create an empty font object  
   htmlFont  font ;  
 
   //  Loop for all fonts listed in font_list  
   for ( i = 0 ;   
         i < NUMBER_OF_ELEMENTS(font_list) ;   
         i++ )  
   {  
      //  Erase the previous contents, if any  
     font = "" ;  
 
      //  Set the typeface  
      font.Face( font_list[i] ) ;  
 
      font << "This is " << font.Face()  
           << " text." << htmlBreak() ;  
 
      page << font ;  
   }  
 
   server << page ;  
   return 0 ;  
}  
 
Example Output
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>html++ example application</TITLE>  
</HEAD>  
<BODY>  
Absolute sizes: <FONT SIZE="1" COLOR="ff0000">1 </FONT>  
<FONT SIZE="2" COLOR="ff0000">2 </FONT>  
<FONT SIZE="3" COLOR="ff0000">3 </FONT>  
<FONT SIZE="4" COLOR="ff0000">4 </FONT>  
<FONT SIZE="5" COLOR="ff0000">5 </FONT>  
<FONT SIZE="6" COLOR="ff0000">6 </FONT>  
<FONT SIZE="7" COLOR="ff0000">7 </FONT><BR>  
Relative sizes: <FONT SIZE="+7" COLOR="0000ff">7 </FONT>  
<FONT SIZE="+6" COLOR="0000ff">6 </FONT>  
<FONT SIZE="+5" COLOR="0000ff">5 </FONT>  
<FONT SIZE="+4" COLOR="0000ff">4 </FONT>  
<FONT SIZE="+3" COLOR="0000ff">3 </FONT>  
<FONT SIZE="+2" COLOR="0000ff">2 </FONT>  
<FONT SIZE="+1" COLOR="0000ff">1 </FONT>  
<FONT COLOR="0000ff">0 </FONT>  
<FONT SIZE="-1" COLOR="0000ff">-1 </FONT>  
<FONT SIZE="-2" COLOR="0000ff">-2 </FONT>  
<FONT SIZE="-3" COLOR="0000ff">-3 </FONT>  
<FONT SIZE="-4" COLOR="0000ff">-4 </FONT>  
<FONT SIZE="-5" COLOR="0000ff">-5 </FONT>  
<FONT SIZE="-6" COLOR="0000ff">-6 </FONT>  
<FONT SIZE="-7" COLOR="0000ff">-7 </FONT><BR>  
<FONT SIZE="-2">Note that since there are only seven possible font sizes, only values in the range +4 to -2 show changes in size.</FONT><P></P>  
<FONT FACE="Arial">This is Arial text.<BR>  
</FONT><FONT FACE="Verdana">This is Verdana text.<BR>  
</FONT><FONT FACE="System">This is System text.<BR>  
</FONT>  
</BODY>  
</HTML>  
 
 
font1.gif  


Example Two
 
//  Example of using htmlFont to produce an  
//  htmlText-derived class that automatically  
//  enlarges the first character.  
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
//  
//  Definition of our new class. Note how the  
//  constructors are all inline functions that  
//  simply call the parent-class constructors.  
//  
class FancyText : public htmlText  
{  
public :  
   FancyText() : htmlText() {} ;  
   FancyText( const String& text ) : htmlText(text) {} ;  
   FancyText( const htmlText& rhs ) : htmlText( rhs ) {} ;  
 
   void Print( ostream& os ) const ;  
   htmlObject FAR * Clone() const { return new FancyText(*this) ; }  
} ;  
 
//  
//  Our overriden Print() method does  
//  all the work.  
//  
void FancyText :: Print( ostream &os ) const  
{  
   PrintStart( os ) ;  
 
    //  Get the string that would normally  
    //  be output.  
   String  s = EncodedText() ;  
 
   //  Get the first character and insert it  
   //  into the large font object.  
   htmlFont   large( "", +2 ) ;  
   large = s.Left(1) ;  
 
   //  Output the large character followed  
   //  by the rest of the text.  
   os << large << s.Right( s.GetLength() - 1 ) ;  
 
   PrintEnd( os ) ;  
}  
 
 
int main( void )  
{  
   htmlPage   page( "html++ example application" ) ;  
 
   //  Our FancyText class can be used anywhere  
   //  that htmlText can.  
   FancyText  t1( "Four score and seven years ago..." ) ;  
 
   FancyText  t2( "In the beginning, God created the heavans and the earth..." ) ;  
 
   page << htmlParagraph( t1 )   
       << htmlParagraph( t2 ) ;  
   cout << page ;  
 
   return 0 ;  
}  
 
Example Two Output
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>html++ example application</TITLE>  
</HEAD>  
<BODY>  
<P><FONT SIZE="+2">F</FONT>our score and seven years ago...</P>  
<P><FONT SIZE="+2">I</FONT>n the beginning, God created the heavens and the earth...</P>  
</BODY>  
</HTML>  
 
fancyfont.gif  



©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.