summaryrefslogtreecommitdiffstats
path: root/khtml/rendering/font.h
blob: 714b038293331e00365d03745ecdff6f9c6f000e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * This file is part of the html renderer for KDE.
 *
 * Copyright (C) 2000-2003 Lars Knoll ([email protected])
 *           (C) 2000 Antti Koivisto ([email protected])
 *           (C) 2000 Dirk Mueller ([email protected])
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef KHTMLFONT_H
#define KHTMLFONT_H

#include <tqfont.h>
#include <tqfontmetrics.h>
#include <tqmap.h>
#include <tqpainter.h>

class TQFontDatabase;
class TQPaintDeviceMetrics;

namespace khtml
{
class RenderStyle;
class CSSStyleSelector;

class FontDef
{
public:
    FontDef()
        : size( 0 ), italic( false ), smallCaps( false ), weight( 50 ) {}
    bool operator == ( const FontDef &other ) const {
        return ( family == other.family &&
                 size == other.size &&
                 italic == other.italic &&
                 smallCaps == other.smallCaps &&
                 weight == other.weight );
    }

    TQString family;
    short int size;
    bool italic 		: 1;
    bool smallCaps 		: 1;
    unsigned int weight 		: 8;
};


class Font
{
    friend class RenderStyle;
    friend class CSSStyleSelector;

public:
    Font() : fontDef(), f(), fm( f ), scFont( 0 ), letterSpacing( 0 ), wordSpacing( 0 ) {}
    Font( const FontDef &fd )
        :  fontDef( fd ), f(), fm( f ), scFont( 0 ), letterSpacing( 0 ), wordSpacing( 0 )
        {}
    Font(const Font& o)
        : fontDef(o.fontDef), f(o.f), fm(o.fm), scFont(o.scFont), letterSpacing(o.letterSpacing), wordSpacing(o.wordSpacing) { if (o.scFont) scFont = new TQFont(*o.scFont); }
    ~Font() { delete scFont; }

    bool operator == ( const Font &other ) const {
        return (fontDef == other.fontDef &&
                letterSpacing == other.letterSpacing &&
                wordSpacing == other.wordSpacing );
    }

    const FontDef& getFontDef() const { return fontDef; }

    void update( TQPaintDeviceMetrics *devMetrics ) const;

    /**
     * Draws a piece from the given piece of text.
     * @param p painter
     * @param x x-coordinate to begin drawing, always denotes leftmost position
     * @param y y-coordinate of baseline of text
     * @param str string to draw a piece from
     * @param slen total length of string
     * @param pos zero-based offset of beginning of piece
     * @param len length of piece
     * @param width additional pixels to be distributed equally among all
     *		spaces
     * @param d text direction
     * @param from begin with this position relative to @p pos, -1 to start
     *		at @p pos
     * @param to stop before this position relative to @p pos, -1 to use full
     *		length of piece
     * @param bg if valid, fill the background of the drawn piece with this
     *		color
     * @param uy y-coordinate of top position, used for background and text
     *		decoration painting
     * @param h total height of line, only used for background and text
     *		decoration painting
     * @param deco combined text decoration (see Decoration)
     */
    void drawText( TQPainter *p, int x, int y, TQChar *str, int slen, int pos, int len, int width,
                   TQPainter::TextDirection d, int from=-1, int to=-1, TQColor bg=TQColor(),
		   int uy=-1, int h=-1, int deco=0 ) const;

    /** returns the width of the given string chunk in pixels.
     *
     * The method also considers various styles like text-align and font-variant
     * @param str pointer to string
     * @param slen total length of string
     * @param pos zero-based position in string where to start measuring
     * @param len count of characters up to which the width should be determined
     * @param start starting position of inline text box within str, only
     * used when toAdd is specified.
     * @param end ending position of inline text box within str, only
     * used when toAdd is specified.
     * @param toAdd amount of pixels to distribute evenly among all spaces of
     * str. Note that toAdd applies to all spaces within str, but only those
     * within [pos, pos+len) are counted towards the width.
     */
    int width( TQChar *str, int slen, int pos, int len, int start = 0, int end = 0, int toAdd = 0 ) const;
    /** return the width of the given char in pixels.
     *
     * The method also considers various styles like text-align and font-variant
     * @param str pointer to string
     * @param slen total length of string
     * @param pos zero-based position of char in string
     */
    int width( TQChar *str, int slen, int pos) const;

    /** Text decoration constants.
     *
     * The enumeration constant values match those of ETextDecoration, but only
     * a subset is supported.
     */
    enum Decoration { UNDERLINE = 0x1, OVERLINE = 0x2, LINE_THROUGH= 0x4 };
    // Keep in sync with ETextDecoration

    /** draws text decoration
     * @param p painter
     * @param x x-coordinate
     * @param y top y-coordinate of line box
     * @param baseline baseline
     * @param width length of decoration in pixels
     * @param height height of line box
     * @param deco decoration to be drawn (see Decoration). The enumeration
     *		constants may be combined.
     */
    void drawDecoration(TQPainter *p, int x, int y, int baseline, int width, int height, int deco) const;

    /** returns letter spacing
     */
    int getLetterSpacing() const { return letterSpacing; }
    /** returns word spacing
     */
    int getWordSpacing() const { return wordSpacing; }

private:
    mutable FontDef fontDef;
    mutable TQFont f;
    mutable TQFontMetrics fm;
    mutable TQFont *scFont;
    short letterSpacing;
    short wordSpacing;

    struct ScalKey;
    enum   ScalInfo {
        Unknown,
        No,
        Yes
    };

    static TQMap<ScalKey, ScalInfo>* scalCache;
    static TQMap<ScalKey, TQValueList<int> >* scalSizesCache;
    static bool isFontScalable(TQFontDatabase& db, const TQFont& font);
};

} // namespace

#endif