home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / private / qfontdata_p.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  12KB  |  483 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qfontdata_p.h   3.0.0   edited Oct 9 09:14 $
  3. **
  4. ** Definition of internal QFontData struct
  5. **
  6. ** Created : 941229
  7. **
  8. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  9. **
  10. ** This file is part of the kernel module of the Qt GUI Toolkit.
  11. **
  12. ** This file may be distributed under the terms of the Q Public License
  13. ** as defined by Trolltech AS of Norway and appearing in the file
  14. ** LICENSE.QPL included in the packaging of this file.
  15. **
  16. ** This file may be distributed and/or modified under the terms of the
  17. ** GNU General Public License version 2 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.GPL included in the
  19. ** packaging of this file.
  20. **
  21. ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
  22. ** licenses may use this file in accordance with the Qt Commercial License
  23. ** Agreement provided with the Software.
  24. **
  25. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  26. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27. **
  28. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  29. **   information about Qt Commercial License Agreements.
  30. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  31. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  32. **
  33. ** Contact info@trolltech.com if any conditions of this licensing are
  34. ** not clear to you.
  35. **
  36. **********************************************************************/
  37.  
  38. #ifndef QFONTDATA_P_H
  39. #define QFONTDATA_P_H
  40.  
  41. #ifndef QT_H
  42. #include <qcache.h>
  43. #include <qobject.h>
  44. #endif // QT_H
  45.  
  46.  
  47. //
  48. //  W A R N I N G
  49. //  -------------
  50. //
  51. // This file is not part of the Qt API.  It exists for the convenience
  52. // of internal files.  This header file may change from version to version
  53. // without notice, or even be removed.
  54. //
  55. // We mean it.
  56. //
  57. //
  58.  
  59. class QPaintDevice;
  60.  
  61. #ifdef Q_WS_WIN
  62. #include <qt_windows.h>
  63. #endif
  64.  
  65. #ifdef Q_WS_X11
  66. #include <qt_x11.h>
  67.  
  68. class QCharStruct;
  69. #endif
  70.  
  71. // font description
  72. struct QFontDef {
  73.     QFontDef()
  74.     : pixelSize(0), pointSize(0), lbearing(SHRT_MIN), rbearing(SHRT_MIN),
  75.       styleStrategy(QFont::PreferDefault), styleHint(QFont::AnyStyle),
  76.       weight(0), italic(FALSE), underline(FALSE), strikeOut(FALSE),
  77.       fixedPitch(FALSE), hintSetByUser(FALSE), rawMode(FALSE), dirty(TRUE)
  78.     { ; }
  79.  
  80.     QString family;
  81.     QString addStyle;
  82.  
  83.     int pixelSize;
  84.     int pointSize;
  85.     short lbearing;
  86.     short rbearing;
  87.  
  88.     ushort styleStrategy;
  89.     uchar styleHint;
  90.     uchar weight;
  91.  
  92.     bool italic;
  93.     bool underline;
  94.     bool strikeOut;
  95.     bool fixedPitch;
  96.     bool hintSetByUser;
  97.     bool rawMode;
  98.  
  99.     bool dirty;
  100. };
  101.  
  102.  
  103. class QTextCodec;
  104.  
  105. #ifdef Q_WS_X11
  106.  
  107. // this is a shared wrapper for XFontStruct (to prevent a font being freed by
  108. // the cache while it's being used)
  109. class QFontStruct : public QShared
  110. {
  111. public:
  112.     QFontStruct(Qt::HANDLE h, Qt::HANDLE xfth, Qt::HANDLE xftp,
  113.         QCString n, QTextCodec *c, int a) :
  114.     QShared(), handle(h), xfthandle(xfth), xftpattern(xftp),
  115.     name(n), codec(c), cache_cost(a), scale( 1. )
  116.     { ; }
  117.  
  118.     ~QFontStruct();
  119.  
  120.     Qt::HANDLE handle, xfthandle, xftpattern;
  121.     QCString name;
  122.     QTextCodec *codec;
  123.     int cache_cost;
  124.     float scale; // needed for printing, to correctly scale font metrics for bitmap fonts
  125. };
  126.  
  127. #endif // Q_WS_X11
  128.  
  129.  
  130. #ifdef Q_WS_WIN
  131.  
  132. class QFontStruct : public QShared
  133. {
  134. public:
  135.     QFontStruct( const QString &key );
  136.     ~QFontStruct() { reset(); }
  137.     bool        dirty()      const { return hfont == 0; }
  138.     HDC            dc()     const;
  139.     HFONT        font()     const { return hfont; }
  140.     TEXTMETRICA       *textMetricA() const;
  141.     TEXTMETRICW       *textMetricW() const;
  142.     QString key() const  { return k; }
  143.     void        reset();
  144.  
  145.     QString    k;
  146.     HDC        hdc;
  147.     HFONT    hfont;
  148.     bool    stockFont;
  149.     union {
  150.     TEXTMETRICW    w;
  151.     TEXTMETRICA    a;
  152.     } tm;
  153.     int        lw;
  154.     int cache_cost;
  155. //    friend void QFont::initFontInfo() const;
  156. };
  157.  
  158. #endif // Q_WS_WIN
  159.  
  160. #if defined( Q_WS_MAC )
  161.  
  162. #include "qt_mac.h"
  163. class QMacFontInfo;
  164.  
  165. class QFontStruct : public QShared
  166. {
  167. public:
  168.     inline QFontStruct() :   QShared(), info(NULL), cache_cost(0), internal_fi(NULL) { }
  169.     int ascent() const { return info->ascent; }
  170.     int descent() const { return info->descent; }
  171.     int minLeftBearing() const { return 0; }
  172.     int minRightBearing() const { return 0; }
  173.     int leading() const { return info->leading; }
  174.     int maxWidth() const { return info->widMax; }
  175.  
  176.     int psize;
  177.     FontInfo *info;
  178.     int cache_cost;
  179.     QMacFontInfo *internal_fi;
  180. };
  181.  
  182. #endif
  183.  
  184. #ifdef Q_WS_QWS
  185. class QFontStruct;
  186. class QGfx;
  187. #endif
  188.  
  189. typedef QCacheIterator<QFontStruct> QFontCacheIterator;
  190. class QFontCache : public QObject, public QCache<QFontStruct>
  191. {
  192. public:
  193.     QFontCache();
  194.     ~QFontCache();
  195.  
  196.     bool insert(const QString &, const QFontStruct *, int c);
  197. #ifndef Q_WS_MAC
  198.     void deleteItem(Item d);
  199. #endif
  200.     void timerEvent(QTimerEvent *);
  201.  
  202.  
  203. protected:
  204.  
  205.  
  206. private:
  207.     int timer_id;
  208.     bool fast;
  209. };
  210.  
  211.  
  212. // QFontPrivate - holds all data on which a font operates
  213. class QFontPrivate : public QShared
  214. {
  215. public:
  216.     static QFontCache *fontCache;
  217.  
  218.  
  219. public:
  220.     QFontPrivate()
  221.     : QShared(), exactMatch(FALSE), lineWidth(1)
  222.     {
  223.  
  224. #if defined(Q_WS_WIN) || defined(Q_WS_QWS) || defined(Q_WS_MAC)
  225.     fin = 0;
  226. #endif // Q_WS_WIN || Q_WS_QWS
  227. #if defined(Q_WS_WIN)
  228.     currHDC = 0;
  229. #endif // Q_WS_WIN
  230.     paintdevice = 0;
  231.     }
  232.  
  233.     QFontPrivate(const QFontPrivate &fp)
  234.     : QShared(), request(fp.request), actual(fp.actual),
  235.       exactMatch(fp.exactMatch), lineWidth(1)
  236.     {
  237.     Q_ASSERT(!fp.paintdevice);
  238. #if defined(Q_WS_WIN) || defined(Q_WS_QWS) || defined(Q_WS_MAC)
  239.     fin = 0;
  240. #endif // Q_WS_WIN || Q_WS_QWS
  241. #if defined(Q_WS_WIN)
  242.     currHDC = 0;
  243. #endif // Q_WS_WIN
  244.     paintdevice = 0;
  245.     }
  246.  
  247.     QFontPrivate( const QFontPrivate &fp, QPaintDevice *pd )
  248.     : QShared(), request(fp.request), actual(fp.actual),
  249.       exactMatch(fp.exactMatch), lineWidth(1)
  250.     {
  251.  
  252. #if defined(Q_WS_WIN) || defined(Q_WS_QWS) || defined(Q_WS_MAC)
  253.     fin = 0;
  254. #endif // Q_WS_WIN || Q_WS_QWS
  255. #if defined(Q_WS_WIN)
  256.     currHDC = 0;
  257. #endif // Q_WS_WIN
  258.     paintdevice = pd;
  259.     }
  260.  
  261.     // requested font
  262.     QFontDef request;
  263.     // actual font
  264.     QFontDef actual;
  265.  
  266.     bool exactMatch;
  267.     int lineWidth;
  268.  
  269.     // common functions
  270.     QString defaultFamily() const;
  271.     QString lastResortFamily() const;
  272.     QString lastResortFont() const;
  273.     QString key() const;
  274.  
  275.     static int getFontWeight(const QCString &, bool = FALSE);
  276.     QRect boundingRect( const QChar &ch );
  277.  
  278.     struct TextRun {
  279.     TextRun()
  280.     {
  281.         xoff = 0;
  282.         yoff = 0;
  283.         x2off = 0;
  284.         script = QFont::NoScript;
  285.         string = 0;
  286.         length = 0;
  287.         next = 0;
  288. #ifdef Q_WS_WIN
  289.         mapped = 0;
  290. #endif
  291.     }
  292.  
  293.     ~TextRun()
  294.     {
  295.         if ( next )
  296.         delete next;
  297. #ifdef Q_WS_WIN
  298. #if !(defined(UNICODE) && !defined(Q_OS_WIN32BYTESWAP_))
  299.         delete [] mapped;
  300. #endif
  301. #endif
  302.     }
  303.  
  304.     void setParams( int x, int y, int x2, const QChar *s, int len,
  305.             QFont::Script sc = QFont::NoScript ) {
  306.         xoff = x;
  307.         yoff = y;
  308.         x2off = x2;
  309.         string = s;
  310.         length = len;
  311.         script = sc;
  312.     }
  313.     int xoff;
  314.     int yoff;
  315.     int x2off;
  316.     QFont::Script script;
  317.     const QChar *string;
  318.     int length;
  319.     TextRun *next;
  320. #ifdef Q_WS_X11
  321.     QByteArray mapped;
  322. #endif
  323. #ifdef Q_WS_WIN
  324.     TCHAR *mapped;
  325. #endif
  326.     };
  327.  
  328.     // some replacement functions for native calls. This is needed, because shaping and
  329.     // non spacing marks can change the extents of a string to draw. At the same time
  330.     // drawing needs to take care to correctly position non spacing marks.
  331.     int textWidth( const QString &str, int pos, int len );
  332.  
  333.     // returns the script a certain character is in. Needed to separate the string
  334.     // into runs of different scripts as required for X11 and opentype.
  335.     QFont::Script scriptForChar(const QChar &c);
  336.  
  337. #ifdef Q_WS_X11
  338.     QFont::Script hanHack( const QChar & c );
  339.     static char **getXFontNames(const char *, int *);
  340.     static bool fontExists(const QString &);
  341.     static bool parseXFontName(const QCString &, char **);
  342.     static bool fillFontDef(const QCString &, QFontDef *, QCString *);
  343.  
  344.     static inline bool isZero(char *x)
  345.     {
  346.     return (x[0] == '0' && x[1] == 0);
  347.  
  348.     }
  349.  
  350.     static inline bool isScalable( char **tokens )
  351.     {
  352.     return (isZero(tokens[PixelSize]) &&
  353.         isZero(tokens[PointSize]) &&
  354.         isZero(tokens[AverageWidth]));
  355.     }
  356.  
  357.     static inline bool isSmoothlyScalable( char **tokens )
  358.     {
  359.     return (isZero(tokens[ResolutionX]) && isZero(tokens[ResolutionY]));
  360.     }
  361.  
  362.     static inline bool isFixedPitch( char **tokens )
  363.     {
  364.     return (tokens[Spacing][0] == 'm' ||
  365.         tokens[Spacing][0] == 'c' ||
  366.         tokens[Spacing][0] == 'M' ||
  367.         tokens[Spacing][0] == 'C');
  368.     }
  369.  
  370.     // XLFD fields
  371.     enum FontFieldNames {
  372.     Foundry,
  373.     Family,
  374.     Weight,
  375.     Slant,
  376.     Width,
  377.     AddStyle,
  378.     PixelSize,
  379.     PointSize,
  380.     ResolutionX,
  381.     ResolutionY,
  382.     Spacing,
  383.     AverageWidth,
  384.     CharsetRegistry,
  385.     CharsetEncoding,
  386.     NFontFields
  387.     };
  388.  
  389. #ifndef QT_NO_XFTFREETYPE
  390.     XftPattern *findXftFont(const QChar &, bool *) const;
  391.     XftPattern *bestXftPattern(const QString &, const QString &) const;
  392. #endif // QT_NO_XFTFREETYPE
  393.     QCString findFont(QFont::Script, bool *) const;
  394.     QCString findXftFont(QFont::Script, bool *) const;
  395.     QCString bestFamilyMember(QFont::Script, const QString &, const QString &,
  396.                   const QString &, int *) const;
  397.     QCString bestMatch(const char *, int *, QFont::Script) const;
  398.     int fontMatchScore(const char *, QCString &, float *, int *, bool *,
  399.                bool *, QFont::Script) const;
  400.     void initFontInfo(QFont::Script);
  401.     void load(QFont::Script = QFont::NoScript, bool = TRUE);
  402.     bool loadUnicode(QFont::Script, const QChar &);
  403.     void computeLineWidth();
  404.  
  405.     int textWidth( const QString &str, int pos, int len, TextRun *cache );
  406.     void textExtents( const QString &str, int pos, int len, QCharStruct *overall );
  407.     void drawText( Display *dpy, int screen, Qt::HANDLE hd, Qt::HANDLE rendhd,
  408.            GC gc, const QColor &pen, Qt::BGMode, const QColor &bgcolor,
  409.            int x, int y, const TextRun *cache, int pdWidth );
  410.     bool inFont( const QChar &ch );
  411.  
  412.     class QFontX11Data {
  413.     public:
  414.     // X fontstruct handles for each character set
  415.     QFontStruct *fontstruct[QFont::LastPrivateScript];
  416.  
  417.     QFontX11Data()
  418.     {
  419.         for (int i = 0; i < QFont::LastPrivateScript; i++) {
  420.         fontstruct[i] = 0;
  421.         }
  422.     }
  423.  
  424.     ~QFontX11Data()
  425.     {
  426.         QFontStruct *qfs;
  427.  
  428.         for (int i = 0; i < QFont::LastPrivateScript; i++) {
  429.         qfs = fontstruct[i];
  430.         fontstruct[i] = 0;
  431.  
  432.         if (qfs && qfs != (QFontStruct *) -1) {
  433.             qfs->deref();
  434.         }
  435.         }
  436.     }
  437.     } x11data;
  438.     static QFont::Script defaultScript;
  439.  
  440. #endif // Q_WS_X11
  441.  
  442.     QPaintDevice *paintdevice;
  443.  
  444. #ifdef Q_WS_WIN
  445.     ~QFontPrivate() {
  446.     if( fin )
  447.         fin->deref();
  448.     }
  449.     void load();
  450.     void initFontInfo();
  451.     HFONT create( bool *stockFont, HDC hdc = 0, bool compatMode = FALSE );
  452.     QFontStruct *fin;
  453.     HDC currHDC;
  454.  
  455.     void buildCache( HDC hdc, const QString &str, int pos, int len, TextRun *cache );
  456.     void drawText( HDC hdc, int x, int y, TextRun *cache );
  457. #endif // Q_WS_WIN
  458.  
  459. #ifdef Q_WS_QWS
  460.     ~QFontPrivate();
  461.     void load();
  462.     QFontStruct *fin;
  463.     int textWidth( const QString &str, int pos, int len, TextRun *cache );
  464.     void drawText( QGfx *gfx, int x, int y, const TextRun *cache );
  465. #endif
  466.  
  467. #if defined( Q_WS_MAC )
  468.     ~QFontPrivate() {
  469.     if( fin && fin->deref() )
  470.         delete fin; 
  471.     }
  472.     void macSetFont(QPaintDevice *);
  473.     void drawText( int x, int y, QString s, int len );
  474.     void computeLineWidth();
  475.     void load();
  476.     QFontStruct *fin;
  477. #endif
  478.  
  479. };
  480.  
  481.  
  482. #endif // QFONTDATA_P_H
  483.