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