|
1 /* |
|
2 * Copyright (c) 1997-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "FONTCOMP.H" |
|
20 |
|
21 FscRead::FscRead(fstream& aFile,FontCompiler& aFontCompiler,Fxf* aFxf): |
|
22 FontRead(aFile,aFontCompiler,aFxf) |
|
23 {} |
|
24 |
|
25 int FscRead::DoCom(int aSecondPass) |
|
26 /* |
|
27 Takes a command and takes appropriate action |
|
28 Returns -ve if an error occured |
|
29 0 for OKAY |
|
30 +ve number for new current character number |
|
31 */ |
|
32 { |
|
33 int ret=0,num=0,neg=0; |
|
34 int i=0; |
|
35 for (;i<iInputBufLen;i++) |
|
36 { |
|
37 char chr=iInputBuf[i]; |
|
38 if (num==0 && chr=='-') |
|
39 neg=1; |
|
40 else if (chr>='0' && chr<='9') |
|
41 num=num*10+chr-'0'; |
|
42 } |
|
43 if (neg) |
|
44 num= -num; |
|
45 if (iInputBuf[1]=='c') |
|
46 return(num); |
|
47 if (aSecondPass) |
|
48 ret=0; |
|
49 else switch(iInputBuf[1]) |
|
50 { |
|
51 case 'd': |
|
52 iFxf->descent=num; |
|
53 break; |
|
54 case 'a': |
|
55 iFxf->nominal_ascent=num; |
|
56 break; |
|
57 case 'm': |
|
58 iFxf->max_info_width=num; |
|
59 break; |
|
60 case 'h': |
|
61 if ((iFxf->cell_height=num)>MAX_HEIGHT) |
|
62 ret=Parameter; |
|
63 if (iFxf->nominal_ascent==0) |
|
64 iFxf->nominal_ascent=iFxf->cell_height-iFxf->descent; |
|
65 break; |
|
66 case 'n': /* Font data label name */ |
|
67 { |
|
68 int pos=0; |
|
69 while(iInputBuf[pos]!=' ') |
|
70 pos++; |
|
71 while(iInputBuf[pos]==' ') |
|
72 pos++; |
|
73 if(iInputBufLen-pos>FONT_NAME_LEN) |
|
74 return(FileFormat); |
|
75 memcpy(iFxf->name,&iInputBuf[pos],iInputBufLen-pos); |
|
76 (iFxf->name)[iInputBufLen-pos]=0; |
|
77 } |
|
78 break; |
|
79 case 't': /* Font data typeface */ |
|
80 { |
|
81 int pos=0; |
|
82 while(iInputBuf[pos]!=' ') |
|
83 pos++; |
|
84 while(iInputBuf[pos]==' ') |
|
85 pos++; |
|
86 if(iInputBufLen-pos>FONT_NAME_LEN) |
|
87 return(FileFormat); |
|
88 memcpy(iFxf->typeface,&iInputBuf[pos],iInputBufLen-pos); |
|
89 (iFxf->typeface)[iInputBufLen-pos]=0; |
|
90 } |
|
91 break; |
|
92 case 'b': |
|
93 iFxf->iBold=1; |
|
94 break; |
|
95 case 'i': |
|
96 iFxf->iItalic=1; |
|
97 break; |
|
98 case 'f': |
|
99 // not a lot |
|
100 break; |
|
101 case 'v': |
|
102 iFxf->iUid=num; |
|
103 break; |
|
104 case 'o': |
|
105 iOverHang=num; |
|
106 break; |
|
107 case 'u': |
|
108 iUnderHang=num; |
|
109 break; |
|
110 default: |
|
111 ret=FileFormat; |
|
112 break; |
|
113 } |
|
114 return(ret); |
|
115 } |
|
116 |
|
117 char* FscRead::ScanLine(int& aLen) |
|
118 /* Scan the input line for the first '0' or '1' character and return a descriptor pointing to it, |
|
119 returns a zero length descriptor if not found. |
|
120 */ |
|
121 { |
|
122 aLen=0; |
|
123 if((iInputBuf[0]!='0' && iInputBuf[0]!='1') || iInputBufLen==0) |
|
124 return(NULL); |
|
125 while(iInputBuf[aLen]=='0' || iInputBuf[aLen]=='1') |
|
126 aLen++; |
|
127 return(iInputBuf); |
|
128 } |
|
129 |
|
130 |
|
131 int FscRead::ReadLine() |
|
132 { |
|
133 int pos=0; |
|
134 while(iFileBuf[iFileBufPos+pos]!='\n' && iFileBufPos+pos<iFileBufLen) |
|
135 pos++; |
|
136 if(iFileBufPos+pos==iFileBufLen) return(1); |
|
137 memcpy(iInputBuf,&iFileBuf[iFileBufPos],pos); |
|
138 iInputBufLen=pos-1; |
|
139 iFileBufPos+=pos+1; |
|
140 if(iFileBufPos>=iFileBufLen) return(1); |
|
141 return(0); |
|
142 } |
|
143 |
|
144 FontRead::FontRead(fstream& aFile,FontCompiler& aFontCompiler,Fxf* aFxf): |
|
145 iInputFile(aFile), |
|
146 iFontCompiler(&aFontCompiler), |
|
147 iFxf(aFxf) |
|
148 { |
|
149 } |
|
150 |
|
151 int FscRead::ReadFont() |
|
152 { |
|
153 iInputFile.seekg(0,ios::end); |
|
154 iFileBufLen=iInputFile.tellg(); |
|
155 iInputFile.seekg(0); |
|
156 iFileBufPos=0; |
|
157 iFileBuf=new char[iFileBufLen]; |
|
158 iInputFile.read(iFileBuf,iFileBufLen); |
|
159 int ret=Pass1(); |
|
160 if(ret) return(ret); |
|
161 return(Pass2()); |
|
162 } |
|
163 |
|
164 int FscRead::Pass1() |
|
165 { |
|
166 int n_row=0; // counts row in character picture |
|
167 int ret=0; |
|
168 int specChr=0; |
|
169 int isCharBody=0; |
|
170 int lastLine=0; |
|
171 int widthofi=0; |
|
172 int widthofM=0; |
|
173 |
|
174 iFxf->MaxChrWidth=0; |
|
175 iFxf->cell_height=0; |
|
176 iFxf->UlinePos=0; |
|
177 iFxf->UlineThickness=0; |
|
178 iFxf->nominal_ascent=0; |
|
179 iFxf->descent=0; |
|
180 iFxf->chr_seg=0; |
|
181 iFxf->FirstChr=0; |
|
182 iFxf->n_chars=0; |
|
183 iFxf->max_info_width=0; |
|
184 iFxf->flags=0; |
|
185 iFxf->special=0; |
|
186 iFxf->ByteWid=0; |
|
187 iFxf->UseWords=0; |
|
188 iFxf->iBold=0; |
|
189 iFxf->iItalic=0; |
|
190 iFxf->iProportional=0; |
|
191 iFxf->iSerif=0; |
|
192 iFxf->iSymbol=0; |
|
193 iFxf->iUid=0; |
|
194 |
|
195 iUnderHang=0; |
|
196 iOverHang=0; |
|
197 iChar=new FcmCharHead[MAX_CHARS]; |
|
198 for(int letter=0;letter<MAX_CHARS;letter++) |
|
199 iFxf->chr[letter]=NULL; |
|
200 //************************************************** |
|
201 // First pass. Read header info & character widths * |
|
202 //************************************************** |
|
203 do |
|
204 { |
|
205 int width=0; // width of current character picture |
|
206 |
|
207 lastLine=ReadLine(); |
|
208 isCharBody=0; |
|
209 if (iInputBufLen>0 && iInputBuf[0]=='*') |
|
210 { |
|
211 if ((ret=DoCom(0))<0) |
|
212 return(FileFormat); |
|
213 else if (ret) |
|
214 { |
|
215 for(;iFxf->n_chars<ret;iFxf->n_chars++) |
|
216 iFxf->chr[iFxf->n_chars]=NULL; |
|
217 specChr=iFxf->n_chars; |
|
218 } |
|
219 } |
|
220 else |
|
221 { |
|
222 int len=0; |
|
223 char* ptr=ScanLine(len); |
|
224 if (len) |
|
225 { |
|
226 isCharBody=1; |
|
227 if (iFxf->FirstChr<0) |
|
228 iFxf->FirstChr=iFxf->n_chars; |
|
229 if (n_row==0) |
|
230 { |
|
231 for (width=0;width<len && (ptr[width]=='0' || ptr[width]=='1');width++); |
|
232 if (iFxf->n_chars>255) |
|
233 return(FileFormat); |
|
234 iFxf->chr[iFxf->n_chars]=iChar; |
|
235 iFxf->n_chars++; |
|
236 iChar->xOffset= -iUnderHang; |
|
237 iChar->width=width; |
|
238 iChar->ByteWid=((iChar->width+15)>>3)&(~1); |
|
239 iChar->move=width-iUnderHang-iOverHang; |
|
240 if(iFxf->n_chars=='i') |
|
241 widthofi=iChar->move; |
|
242 else if(iFxf->n_chars=='M') |
|
243 widthofM=iChar->move; |
|
244 iUnderHang=0; |
|
245 iOverHang=0; |
|
246 if (width>iFxf->MaxChrWidth) |
|
247 iFxf->MaxChrWidth=width; |
|
248 } |
|
249 n_row++; |
|
250 } |
|
251 } |
|
252 if ((n_row!=0 && !isCharBody) || (lastLine && isCharBody)) |
|
253 { |
|
254 if (n_row>iFxf->cell_height) |
|
255 return(FileFormat); |
|
256 iChar->height=n_row; |
|
257 iChar->yOffset=iFxf->cell_height-iFxf->descent; |
|
258 specChr++; |
|
259 n_row=0; |
|
260 width=0; |
|
261 iChar++; |
|
262 } |
|
263 } while(!lastLine); |
|
264 if (iFxf->cell_height==0) |
|
265 return(FileFormat); |
|
266 if(widthofi && widthofM) |
|
267 iFxf->iProportional=(widthofi!=widthofM); |
|
268 return(NoError); |
|
269 } |
|
270 |
|
271 int FscRead::Pass2() |
|
272 /******************************************************/ |
|
273 /* Second pass. Read in actual picture data this time */ |
|
274 /******************************************************/ |
|
275 { |
|
276 unsigned int bit; // Used to set bits in pic |
|
277 int ret,chNum; |
|
278 int n_row; // counts row in character picture |
|
279 int offset=0; |
|
280 int isCharBody; |
|
281 int lastLine; |
|
282 |
|
283 iFileBufPos=0; |
|
284 n_row=0; |
|
285 chNum=0; |
|
286 do { |
|
287 lastLine=ReadLine(); |
|
288 isCharBody=0; |
|
289 if (iInputBufLen>0 && iInputBuf[0]=='*') |
|
290 { |
|
291 if ((ret=DoCom(1))>0) |
|
292 { |
|
293 if (ret<chNum) |
|
294 return(FileFormat); |
|
295 chNum=ret; |
|
296 } |
|
297 } |
|
298 else |
|
299 { |
|
300 int len=0; |
|
301 char* ptr=ScanLine(len); |
|
302 if (len) |
|
303 { |
|
304 isCharBody=1; |
|
305 if (n_row==0) |
|
306 { |
|
307 iChar=iFxf->chr[chNum]; |
|
308 chNum++; |
|
309 } |
|
310 unsigned short int* pDest=(unsigned short int*)(iFontCompiler->FontStore()+offset+iChar->ByteWid*n_row); |
|
311 bit=1; |
|
312 *pDest=0; |
|
313 for (int width=0;width<len && (ptr[width]=='0' || ptr[width]=='1');width++) |
|
314 { |
|
315 if (ptr[width]=='1') |
|
316 *pDest|=bit; |
|
317 if (bit==0x8000) |
|
318 { |
|
319 bit=1; |
|
320 pDest++; |
|
321 *pDest=0; |
|
322 } |
|
323 else |
|
324 bit<<=1; |
|
325 } |
|
326 n_row++; |
|
327 } |
|
328 } |
|
329 if ((n_row!=0 && !isCharBody) || (lastLine && isCharBody)) |
|
330 { |
|
331 iChar->offset=offset; |
|
332 offset+=iChar->ByteWid*n_row; |
|
333 n_row=0; |
|
334 } |
|
335 } while(!lastLine); |
|
336 return(NoError); |
|
337 } |