Diffstat (limited to 'freetype/fontfactoryttf_qws.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | freetype/fontfactoryttf_qws.cpp | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/freetype/fontfactoryttf_qws.cpp b/freetype/fontfactoryttf_qws.cpp new file mode 100644 index 0000000..66fd96a --- a/dev/null +++ b/freetype/fontfactoryttf_qws.cpp | |||
@@ -0,0 +1,238 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | |||
22 | #include "fontfactoryttf_qws.h" | ||
23 | |||
24 | #ifdef QT_NO_FREETYPE | ||
25 | |||
26 | #include "qfontdata_p.h" | ||
27 | #include <string.h> | ||
28 | #include <stdio.h> | ||
29 | |||
30 | #define FLOOR(x) ((x) & -64) | ||
31 | #define CEIL(x) (((x)+63) & -64) | ||
32 | #define TRUNC(x) ((x) >> 6) | ||
33 | |||
34 | |||
35 | extern "C" { | ||
36 | #include <ft2build.h> | ||
37 | #include FT_FREETYPE_H | ||
38 | #include FT_GLYPH_H | ||
39 | #include FT_OUTLINE_H | ||
40 | #include FT_BBOX_H | ||
41 | } | ||
42 | |||
43 | class QDiskFontFT : public QDiskFontPrivate { | ||
44 | public: | ||
45 | FT_Face face; | ||
46 | }; | ||
47 | |||
48 | class QRenderedFontFT : public QRenderedFont { | ||
49 | public: | ||
50 | QRenderedFontFT(QDiskFont* f, const QFontDef &d) : | ||
51 | QRenderedFont(f,d) | ||
52 | { | ||
53 | QDiskFontFT *df = (QDiskFontFT*)(f->p); | ||
54 | myface=df->face; | ||
55 | selectThisSize(); | ||
56 | // A 1-pixel baseline is excluded in Qt/Windows/X11 fontmetrics | ||
57 | // (see QFontMetrics::height()) | ||
58 | // | ||
59 | fascent=CEIL(myface->size->metrics.ascender)/64; | ||
60 | fdescent=-FLOOR(myface->size->metrics.descender)/64-1; | ||
61 | fmaxwidth=CEIL(myface->size->metrics.max_advance)/64; | ||
62 | fleading=CEIL(myface->size->metrics.height)/64 | ||
63 | - fascent - fdescent + 1; | ||
64 | |||
65 | // FT has these in font units | ||
66 | funderlinepos = ptsize/200+1; | ||
67 | funderlinewidth = ptsize/200+1; | ||
68 | } | ||
69 | |||
70 | ~QRenderedFontFT() | ||
71 | { | ||
72 | } | ||
73 | |||
74 | bool unicode(int & i) const | ||
75 | { | ||
76 | int ret; | ||
77 | |||
78 | FT_Face theface=myface; | ||
79 | |||
80 | ret=FT_Get_Char_Index(theface,i); | ||
81 | |||
82 | if (ret==0) { | ||
83 | return FALSE; | ||
84 | } else { | ||
85 | i=ret; | ||
86 | return TRUE; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | bool inFont(QChar ch) const | ||
91 | { | ||
92 | int index = ch.unicode(); | ||
93 | return unicode(index); | ||
94 | } | ||
95 | |||
96 | QGlyph render(QChar ch) | ||
97 | { | ||
98 | selectThisSize(); | ||
99 | |||
100 | int index = ch.unicode(); | ||
101 | if ( !unicode(index) ) | ||
102 | index = 0; | ||
103 | QGlyph result; | ||
104 | |||
105 | FT_Error err; | ||
106 | |||
107 | err=FT_Load_Glyph(myface,index,FT_LOAD_DEFAULT); | ||
108 | if(err) | ||
109 | qFatal("Load glyph error %x",err); | ||
110 | |||
111 | int width,height,pitch,size = 0; | ||
112 | FT_Glyph glyph; | ||
113 | err=FT_Get_Glyph( myface->glyph, &glyph ); | ||
114 | if(err) | ||
115 | qFatal("Get glyph error %x",err); | ||
116 | |||
117 | FT_BBox bbox; | ||
118 | FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_gridfit, &bbox); | ||
119 | |||
120 | FT_Vector origin; | ||
121 | origin.x = -bbox.xMin; | ||
122 | origin.y = -bbox.yMin; | ||
123 | |||
124 | if ( FT_IS_SCALABLE(myface) ) { | ||
125 | err=FT_Glyph_To_Bitmap(&glyph, | ||
126 | smooth ? ft_render_mode_normal : ft_render_mode_mono, | ||
127 | &origin, 1); // destroy original glyph | ||
128 | if(err) | ||
129 | qWarning("Get bitmap error %d",err); | ||
130 | } | ||
131 | |||
132 | if ( !err ) { | ||
133 | FT_Bitmap bm = ((FT_BitmapGlyph)glyph)->bitmap; | ||
134 | pitch = bm.pitch; | ||
135 | size=pitch*bm.rows; | ||
136 | result.data = new uchar[size]; // XXX memory manage me | ||
137 | width=bm.width; | ||
138 | height=bm.rows; | ||
139 | if ( size ) { | ||
140 | memcpy( result.data, bm.buffer, size ); | ||
141 | } else { | ||
142 | result.data = new uchar[0]; // XXX memory manage me | ||
143 | } | ||
144 | } else { | ||
145 | result.data = new uchar[0]; // XXX memory manage me | ||
146 | } | ||
147 | |||
148 | result.metrics = new QGlyphMetrics; | ||
149 | memset((char*)result.metrics, 0, sizeof(QGlyphMetrics)); | ||
150 | result.metrics->bearingx=myface->glyph->metrics.horiBearingX/64; | ||
151 | result.metrics->advance=myface->glyph->metrics.horiAdvance/64; | ||
152 | result.metrics->bearingy=myface->glyph->metrics.horiBearingY/64; | ||
153 | |||
154 | result.metrics->linestep=pitch; | ||
155 | result.metrics->width=width; | ||
156 | result.metrics->height=height; | ||
157 | |||
158 | FT_Done_Glyph( glyph ); | ||
159 | |||
160 | return result; | ||
161 | } | ||
162 | |||
163 | FT_Face myface; | ||
164 | |||
165 | private: | ||
166 | void selectThisSize() | ||
167 | { | ||
168 | int psize=(ptsize<<6)/10; | ||
169 | |||
170 | // Assume 72 dpi for now | ||
171 | const int dpi=72; | ||
172 | FT_Error err; | ||
173 | err=FT_Set_Char_Size(myface, psize,psize,dpi,dpi); | ||
174 | if (err) { | ||
175 | if (FT_IS_SCALABLE(myface) ) { | ||
176 | qWarning("Set char size error %x for size %d",err,ptsize); | ||
177 | } else { | ||
178 | int best=-1; | ||
179 | int bdh=99; | ||
180 | for (int i=0; i<myface->num_fixed_sizes; i++) { | ||
181 | FT_Bitmap_Size& sz=myface->available_sizes[i]; | ||
182 | int dh = sz.height - ptsize*dpi/72/10; | ||
183 | dh = QABS(dh); | ||
184 | if ( dh < bdh ) { | ||
185 | bdh=dh; | ||
186 | best=i; | ||
187 | } | ||
188 | } | ||
189 | if ( best >= 0 ) | ||
190 | err=FT_Set_Pixel_Sizes(myface, | ||
191 | myface->available_sizes[best].width, | ||
192 | myface->available_sizes[best].height); | ||
193 | if ( err ) | ||
194 | qWarning("Set char size error %x for size %d",err,ptsize); | ||
195 | } | ||
196 | } | ||
197 | } | ||
198 | }; | ||
199 | |||
200 | QFontFactoryFT::QFontFactoryFT() | ||
201 | { | ||
202 | FT_Error err; | ||
203 | err=FT_Init_FreeType(&library); | ||
204 | if(err) { | ||
205 | qFatal("Couldn't initialise Freetype library"); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | QFontFactoryFT::~QFontFactoryFT() | ||
210 | { | ||
211 | } | ||
212 | |||
213 | QString QFontFactoryFT::name() | ||
214 | { | ||
215 | return "FT"; | ||
216 | } | ||
217 | |||
218 | QRenderedFont * QFontFactoryFT::get(const QFontDef & f,QDiskFont * f2) | ||
219 | { | ||
220 | return new QRenderedFontFT(f2, f); | ||
221 | } | ||
222 | |||
223 | void QFontFactoryFT::load(QDiskFont * qdf) const | ||
224 | { | ||
225 | if(qdf->loaded) | ||
226 | return; | ||
227 | QDiskFontFT *f = new QDiskFontFT; | ||
228 | qdf->p=f; | ||
229 | FT_Error err; | ||
230 | err=FT_New_Face(library,qdf->file.ascii(),0,&(f->face)); | ||
231 | if(err) { | ||
232 | qFatal("Error %d opening face",err); | ||
233 | } | ||
234 | qdf->loaded=true; | ||
235 | } | ||
236 | |||
237 | |||
238 | #endif // QT_NO_FREETYPE | ||