home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IFONT.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  24KB  |  433 lines

  1. #ifndef _IFONT_
  2.   #define _IFONT_
  3. /*******************************************************************************
  4. * FILE NAME: IFONT.HPP                                                         *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     IFont - Class to manage a font.                                          *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   (C) Copyright IBM Corporation 1992                                         *
  12. *   All Rights Reserved                                                        *
  13. *   Licensed Materials * Property of IBM                                       *
  14. *                                                                              *
  15. * HISTORY:                                                                     *
  16. *******************************************************************************/
  17. #ifndef _IVBASE_
  18.   #include <ivbase.hpp>
  19. #endif
  20. #ifndef _IHANDLE_
  21.   #include <ihandle.hpp>
  22. #endif
  23. #ifndef _IPOINT_
  24.   #include <ipoint.hpp>
  25. #endif
  26. #ifndef _ICONTROL_
  27.   #include <icontrol.hpp>
  28. #endif
  29.  
  30. /*----------------------------------------------------------------------------*/
  31. /* Align classes on four byte boundary.                                       */
  32. /*----------------------------------------------------------------------------*/
  33. #pragma pack(4)
  34.  
  35. // Forward declarations for other classes:
  36. class   IString;
  37. class   IWindow;
  38. class   IFontDialog;
  39.  
  40. struct _FATTRS;
  41. struct _FONTMETRICS;
  42.  
  43. class IFont : public IVBase {
  44. typedef IVBase Inherited;
  45. /*******************************************************************************
  46. * The IFont class manages the use of fonts.  You can use it to select a font   *
  47. * through its functions.  You can also use the font dialog to get font         *
  48. * information and set the font when drawing text.                              *
  49. *                                                                              *
  50. * The font attempts to match the requested font.  If an exact match cannot be  *
  51. * found, the font uses the nearest match.                                      *
  52. *                                                                              *
  53. * NOTE: Many of the set functions (such as setName and setPoint) have an       *
  54. *       optional final argument of type IPresSpaceHandle.  You should supply   *
  55. *       this argument if you want IFont to use a presentation space other      *
  56. *       than that associated with the desktop.  This is needed when the        *
  57. *       instantiated IFont object is for handling printer fonts.               *
  58. *                                                                              *
  59. * Example:                                                                     *
  60. *    // To create the font                                                     *
  61. *    pFont = new IFont("Tms Rmn", 24);                                         *
  62. *         // Creates IFont set to 24 point, Times Roman font.                  *
  63. *    pFont = new IFont(myWindow);                                              *
  64. *         // Creates IFont set to same font as used by whMyWindow              *
  65. *    pFont = new IFont;                                                        *
  66. *         // Creates IFont set to the system default font.                     *
  67. *                                                                              *
  68. *    // To draw using the font inside an IPaintHandler                         *
  69. *                                                                              *
  70. *    MyPaintHandler::paintWindow(IPaintEvt& evt)                               *
  71. *    {                                                                         *
  72. *    pFont->beginUsingFont(evt.presSpaceHandle());                             *
  73. *    evt.drawText("Hello", IPoint(10, 50));                                    *
  74. *    pFont->endUsingFont(evt.presSpaceHandle());                               *
  75. *    }                                                                         *
  76. *                                                                              *
  77. *******************************************************************************/
  78. public :
  79.  
  80. /*------------------------ Constructors/Destructor -----------------------------
  81. | You can construct instances of this class in the following ways:             |
  82. |    - Using a window's font.  This constructs a font with the same font that  |
  83. |      is being used in the window.  If no window is specified, then the       |
  84. |      system default font is used.                                            |
  85. |    - Using a specific font.  This constructs a font specified in the         |
  86. |      arguments.                                                              |
  87. |    - From a presentation space.  This constructs a font that corresponds to  |
  88. |      the one being used in the passed-in presentation space.                 |
  89. |    - From a copy of another font.  This constructs a font with the same      |
  90. |      specification as the font that is passed in.                            |
  91. ------------------------------------------------------------------------------*/
  92.     IFont         (const IWindow* window = 0);
  93.     IFont         (const char* faceName,
  94.                    unsigned long ulPointSize = 0,
  95.                    Boolean useFixedFont = false,
  96.                    Boolean useVectorFont = false,
  97.                    IPresSpaceHandle hpsInit = 0);
  98.     IFont         (const IPresSpaceHandle& presSpace);
  99.     IFont         (const IFont& fntCopy);
  100.  
  101. virtual
  102.     ~IFont        ();
  103.  
  104. /*--------------------- Enumerator ---------------------------------------------
  105. | Direction - Returns the direction in which the font is drawn.  This value    |
  106. |             is used as input to the setDirection function.  The valid        |
  107. |             directions are:                                                  |
  108. |               - defaultDir (the default direction)                           |
  109. |               - leftRight (left-to-right)                                    |
  110. |               - topBottom (top-to-bottom)                                    |
  111. |               - rightLeft (right-to-left)                                    |
  112. |               - bottomTop (bottom-to-top)                                    |
  113. ------------------------------------------------------------------------------*/
  114.  
  115. enum Direction {defaultDir, leftRight, topBottom, rightLeft, bottomTop};
  116. /*--------------------- Setting General Font Attributes ------------------------
  117. | The following functions are used to set the attributes of any font being     |
  118. | managed by the IFont class:                                                  |
  119. |   setDirection - Sets the direction in which to draw the font.  Valid        |
  120. |                  values, as specified by the Direction enumeration, are:     |
  121. |                    - defaultDir (the default direction)                      |
  122. |                    - leftRight (left-to-right)                               |
  123. |                    - topBottom (top-to-bottom)                               |
  124. |                    - rightLeft (right-to-left)                               |
  125. |                    - bottomTop (bottom-to-top)                               |
  126. |   setName      - Sets the font's face name.                                  |
  127. |   setPointSize - Sets the font's point size.                                 |
  128. ------------------------------------------------------------------------------*/
  129. IFont
  130.    &setDirection     (Direction direction),
  131.    &setName          (const char* name,
  132.                       IPresSpaceHandle hpsInit = 0),
  133.    &setPointSize     (unsigned long size,
  134.                       IPresSpaceHandle hpsInit = 0);
  135.  
  136.  
  137. /*------------------- Setting Font Style ---------------------------------------
  138. | The following functions are used to change the appearance of a font:         |
  139. |   setBold        - Changes to bold font.                                     |
  140. |   setItalic      - Changes to italic font.                                   |
  141. |   setUnderscore  - Causes a line to appear under the characters.             |
  142. |   setStrikeout   - Puts a line through the characters.                       |
  143. |   setOutline     - Causes the font to appear hollow.                         |
  144. |   setAllEmphasis - If true is passed in, all of the styles are set on.       |
  145. |                    If false is passed in, all of the styles are set off.     |
  146. ------------------------------------------------------------------------------*/
  147.  
  148. IFont
  149.    &setBold          (Boolean bold = true),
  150.    &setItalic        (Boolean italics = true),
  151.    &setUnderscore    (Boolean underscore = true),
  152.    &setStrikeout     (Boolean strikeout = true),
  153.    &setOutline       (Boolean outline = true),
  154.    &setAllEmphasis   (Boolean turnOn = true);
  155.  
  156.  
  157. /*------------------- Getting Font Style ---------------------------------------
  158. | The following functions are used to query the appearance of a font:          |
  159. |    isBold       - Queries whether the font is bold.                          |
  160. |    isItalic     - Queries whether the font is italic.                        |
  161. |    isUnderscore - Queries whether a line appears under the characters.       |
  162. |    isStrikeout  - Queries whether a line is drawn through the characters.    |
  163. |    isOutline    - Queries whether the font appears hollow.                   |
  164. ------------------------------------------------------------------------------*/
  165. Boolean
  166.    isBold            ( ) const,
  167.    isItalic          ( ) const,
  168.    isUnderscore      ( ) const,
  169.    isStrikeout       ( ) const,
  170.    isOutline         ( ) const;
  171.  
  172.  
  173. /*------------------------- Getting Font Attributes ----------------------------
  174. | The following functions are used to get the attributes of the current font:  |
  175. |   isBitmap  - Returns true for a bit-map font, and false for a vector font.  |
  176. |   isFixed   - Returns true if the font is fixed size (non-proportional).     |
  177. |   name      - Returns the face name of the font.                             |
  178. |   pointSize - Returns the point size of the font.                            |
  179. ------------------------------------------------------------------------------*/
  180. Boolean
  181.    isBitmap         () const,
  182.    isFixed          () const;
  183.  
  184. IString
  185.    name             () const;
  186.  
  187. unsigned long
  188.    pointSize        () const;
  189.  
  190. /*------------------------ Geometry Accessors-----------------------------------
  191. | The following functions return information about size of the font:           |
  192. |   maxSize               - Returns the maximum width and height.              |
  193. |   maxCharHeight         - Returns the height portion of maxSize.             |
  194. |   maxUppercaseSize      - Returns the maximum size for an uppercase          |
  195. |                           character.                                         |
  196. |   superscriptSize       - Returns the recommended size for superscripts.     |
  197. |   subscriptSize         - Returns the recommended size for subscripts.       |
  198. |   superscriptOffset     - Returns the recommended size of the baseline X-Y   |
  199. |                           offset for superscripts.                           |
  200. |   subscriptOffset       - Returns the recommended size of the baseline X-Y   |
  201. |                           offset for subscripts.                             |
  202. |   avgCharWidth          - Returns the average character's width.             |
  203. |   avgLowercase          - Returns the nominal height above the baseline for  |
  204. |                           lowercase characters.                              |
  205. |   avgUppercase          - Returns the height of the Em square.  This         |
  206. |                           corresponds to the point size.                     |
  207. |   maxAscender           - Returns the height of the largest ascender above   |
  208. |                           the baseline.                                      |
  209. |   maxDescender          - Returns the height of the largest descender below  |
  210. |                           the baseline.                                      |
  211. |   maxLowercaseAscender  - Returns the height of the largest lowercase        |
  212. |                           ascender.                                          |
  213. |   maxLowercaseDescender - Returns the height of the largest lowercase        |
  214. |                           descender.                                         |
  215. |   internalLeading       - Returns the approximate visual top to a row of     |
  216. |                           characters.  The recommended use of this value is  |
  217. |                           to position the first line of a block of text by   |
  218. |                           subtracting it from the value returned by the      |
  219. |                           maxAscender function and positioning the baseline  |
  220. |                           that distance below whatever is above the text.    |
  221. |   externalLeading       - Returns the amount of white space that should      |
  222. |                           appear between adjacent rows of text.              |
  223. |   charWidth             - Returns the width of a specific single-byte        |
  224. |                           character.  The c argument can only be an SBCS     |
  225. |                           character.  This member function is not            |
  226. |                           DBCS-enabled.                                      |
  227. |   textWidth             - Returns the width of a specific string.  The text  |
  228. |                           argument can only point to an SBCS string.  This   |
  229. |                           member function is not DBCS-enabled.               |
  230. |   textLines             - Returns the number of lines required to fit the    |
  231. |                           specified text, given the maximum width.  The      |
  232. |                           text argument can only point to an SBCS string.    |
  233. |                           This member function is not DBCS-enabled.          |
  234. |   minTextWidth          - Returns the width of the widest word.  The line    |
  235. |                           argument can only point to an SBCS string.  This   |
  236. |                           member function is not DBCS-enabled.               |
  237. ------------------------------------------------------------------------------*/
  238.  
  239. ISize
  240.    maxSize           ( ) const,
  241.    maxUppercaseSize  ( ) const,
  242.    superscriptSize   ( ) const,
  243.    subscriptSize     ( ) const,
  244.    superscriptOffset ( ) const,
  245.    subscriptOffset   ( ) const;
  246.  
  247. unsigned long
  248.    maxCharHeight     ( ) const,
  249.    avgCharWidth     ( ) const,
  250.  
  251.    avgLowercase     ( ) const,
  252.    avgUppercase     ( ) const,
  253.    maxAscender      () const,
  254.    maxDescender     () const,
  255.    maxLowercaseAscender ( ) const,
  256.    maxLowercaseDescender ( ) const,
  257.    internalLeading  ( ) const,
  258.    externalLeading  () const,
  259.  
  260.    charWidth        (char c) const,
  261.    textWidth        (const char* text) const,
  262.    textLines        (const char* text,
  263.                      unsigned long lineWidth) const,
  264.    minTextWidth     (const char* line) const;
  265.  
  266.  
  267.  
  268. /*-------------- Applying the Current Font to a Window -------------------------
  269. | The following function is used to apply the current font:                    |
  270. |   setWindowFont - Sets the specified IWindow's font to the current font.     |
  271. |                   This returns true if the font was successfully changed.    |
  272. ------------------------------------------------------------------------------*/
  273.  
  274. Boolean
  275.    setWindowFont    (IWindow* window) const;
  276.  
  277.  
  278. /*--------------------- Setting Vector Font Attributes -------------------------
  279. | The following functions are used to set the attributes of vector fonts.      |
  280. | These functions have no effect when applied to a bit-map font.               |
  281. |   setCharWidth  - Sets the width of the characters.                          |
  282. |   setCharHeight - Sets the height of the characters.                         |
  283. |   setCharSize   - Sets the size of the characters.                           |
  284. |   setFontAngle  - Sets the angle with which to draw the font.                |
  285. |   setFontShear  - Sets how much shear should be applied to the font.         |
  286. ------------------------------------------------------------------------------*/
  287. IFont
  288.    &setCharWidth     (unsigned long width,
  289.                       IPresSpaceHandle hpsInit = 0),
  290.    &setCharHeight    (unsigned long height,
  291.                       IPresSpaceHandle hpsInit = 0),
  292.    &setCharSize      (const ISize& size,
  293.                       IPresSpaceHandle hpsInit = 0),
  294.    &setFontAngle     (const IPoint& point,
  295.                       IPresSpaceHandle hpsInit = 0),
  296.    &setFontShear     (const IPoint& point,
  297.                       IPresSpaceHandle hpsInit = 0);
  298.  
  299.  
  300. /*--------------------- Drawing Functions --------------------------------------
  301. | The following functions are used to draw text using the font:                |
  302. |   beginUsingFont - Sets the presentation space to use the font.              |
  303. |   endUsingFont   - Restores the presentation space to the default font.      |
  304. ------------------------------------------------------------------------------*/
  305. IFont
  306.    &beginUsingFont   (IPresSpaceHandle hps),
  307.    &endUsingFont     (IPresSpaceHandle hps);
  308.  
  309. /*-------------------------------- Font Types ----------------------------------
  310. | These functions provide means of getting and setting the types of fonts      |
  311. | used by the IFont object:                                                    |
  312. |   useNonPropOnly - Sets whether the IFont object should use only             |
  313. |                    nonproportional fonts.                                    |
  314. |   useBitmapOnly  - Sets whether the IFont object should use only bit-map     |
  315. |                    fonts.                                                    |
  316. |   useVectorOnly  - Sets whether the IFont object should use only vector      |
  317. |                    fonts.                                                    |
  318. |   isNonPropOnly  - Returns true if the IFont object uses only                |
  319. |                    nonproportional fonts.                                    |
  320. |   isBitmapOnly   - Returns true if the IFont object uses only bit-map fonts. |
  321. |   isVectorOnly   - Returns true if the IFont object uses only vector fonts.  |
  322. ------------------------------------------------------------------------------*/
  323. IFont
  324.    &useNonPropOnly   (Boolean fDoIt = true,
  325.                       IPresSpaceHandle hpsInit = 0),
  326.    &useBitmapOnly    (Boolean fDoIt = true,
  327.                       IPresSpaceHandle hpsInit = 0),
  328.    &useVectorOnly    (Boolean fDoIt = true,
  329.                       IPresSpaceHandle hpsInit = 0);
  330.  
  331. Boolean
  332.    isNonPropOnly    () const,
  333.    isBitmapOnly     () const,
  334.    isVectorOnly     () const;
  335.  
  336. /*---------- Accessing Presentation Manager Structures Related to Font ---------
  337. | The following functions are used to access Presentation Manager structures:  |
  338. |   fattrs       - Returns a pointer to the font's Presentation Manager FATTRS |
  339. |                  structure.                                                  |
  340. |   fontmetrics  - Returns a pointer to the font's Presentation Manager        |
  341. |                  FONTMETRICS structure.                                      |
  342. ------------------------------------------------------------------------------*/
  343. const struct _FATTRS*
  344.    fattrs           () const;
  345.  
  346. const struct _FONTMETRICS*
  347.    fontmetrics () const;
  348.  
  349. private : /*------------------------------------------------------------------*/
  350.  
  351. // calls setFontFromDialog()
  352. friend  void* _System
  353.   _IFontDlgProc(unsigned long hwnd, unsigned long ulMsg, void* mp1, void* mp2);
  354. /*----------------------------- Implementation ---------------------------------
  355. | The following functions are used by the font:                                |
  356. |   copyWidthsArray - used by IControl to cache widths array. Helps improve    |
  357. |                     canvas performance.                                      |
  358. |   makeFontMetrics - Loads the font metrics for the selected font.            |
  359. |   makeCharWidths - Loads the widths of all the characters in the font.       |
  360. |   checkForMatch - Finds the best match for the desired font.                 |
  361. |   initialize - Initializes all the font's variables.                         |
  362. |   fontChanged - Clears all information tied to a particular font.            |
  363. |   mustUseVector - Signals that the font must use a vector font               |
  364. |                   to do what it needs to do.                                 |
  365. ------------------------------------------------------------------------------*/
  366. IFont
  367.    ©WidthsArray  (char * buf, unsigned long length),
  368.    &setFontFromDialog (IFontDialog& fontDlg,
  369.                        IPresSpaceHandle hpsInit = 0),
  370.    &makeFontMetrics  (IPresSpaceHandle hpsInit = 0),
  371.    &makeCharWidths   (IPresSpaceHandle hpsInit = 0),
  372.    &checkForMatch    (IPresSpaceHandle hpsInit = 0),
  373.    &initialize       (IPresSpaceHandle hpsInit = 0),
  374.    &fontChanged      (),
  375.    &mustUseVector    (Boolean fDoIt = true),
  376.    &setWidthHeight   (unsigned long ulPointSize,
  377.                       IPresSpaceHandle hpsInit = 0),
  378.    &setSizeFromHps    (unsigned long ulSize,
  379.                       IPresSpaceHandle hpsInit = 0);
  380.  
  381. /*------------------------------ Data Members ----------------------------------
  382. |  The following data members are used to different font values:               |
  383. |   pfontmetCl - Holds the font metrics for the font.                          |
  384. |   plClCharWidths - Holds the array of character widths for the font.         |
  385. |   pfattrsCl - Holds the font attributes structure.                           |
  386. |   fxClPointSize - Holds the point size for the font.                         |
  387. |   fxClHeight - Holds the character height.                                   |
  388. |   fxClWidth - Holds the character width.                                     |
  389. |   lClFontDir - Holds the direction to draw the font.                         |
  390. |   lClXAngle - Holds the X portion of the font angle.                         |
  391. |   lClYAngle - Holds the Y portion of the font angle.                         |
  392. |   ptlClShear - Holds the font shear.                                         |
  393. |   lClLCId - Holds the ID uses for the logical font.                          |
  394. ------------------------------------------------------------------------------*/
  395.  
  396. unsigned long
  397.    flClStyles;
  398.  
  399. struct _FONTMETRICS
  400.    *pfontmetCl;
  401.  
  402. struct _FATTRS
  403.    *pfattrsCl;
  404.  
  405.  
  406. IPoint
  407.    pntClAngle,
  408.    pntClShear;
  409.  
  410. long
  411.    *plClCharWidths,
  412.    fxClPointSize,
  413.    fxClHeight,
  414.    fxClWidth,
  415.    lClFontDir,
  416.    lClLCId;
  417.  
  418. };
  419.  
  420. /*----------------------------------------------------------------------------*/
  421. /* Resume compiler default packing.                                           */
  422. /*----------------------------------------------------------------------------*/
  423. #pragma pack()
  424.  
  425. // Because of ToolKit dependencies, none of the inlineable stuff can be
  426. // inlined right now...
  427. #ifndef I_NO_INLINES
  428.   #include <ifont.inl>
  429. #endif
  430.  
  431.  
  432. #endif /* _IFONT_ */
  433.