Update contrib.
2 * Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Chris Cooper, 09 March 1999
16 * This application loads a scaleable open font and extracts a particular
17 * user-specified size from it which it then outputs as a BDF format font
18 * file. This can then be turned into an EPOC bitmap font using other tools.
19 * At the moment the required font name and PPEM size are specified in a
20 * program statement, so this utility must be recompiled. It is intended to
21 * change this to obtain the input from a text file - and to allow multiple
22 * conversions to be specified at once.
23 * This application has to be a Unicode build to function.
24 * If this #define is enabled than, instead of getting a useable BDF file,
25 * the hex char definitions are replaced by visible bitmaps of the chars.
26 * It should, therefore, be commented out in normal use.
30 //#define SHOW_FONT_PICTURES_INSTEAD_OF_HEX
42 TPtrC CBDFHarness::iFacename(_L("DejaVu Sans Condensed"));
43 TInt CBDFHarness::iPixelSize = 8;
44 TPtrC CBDFHarness::iFilename(_L("Swi8br"));
45 TPtrC CBDFHarness::iUid(_L("268457817"));
47 TFontPosture CBDFHarness::iPosture = EPostureUpright;
48 //TFontPosture CBDFHarness::iPosture = EPostureItalic;
50 //TFontStrokeWeight CBDFHarness::iStrokeWeight = EStrokeWeightNormal;
51 TFontStrokeWeight CBDFHarness::iStrokeWeight = EStrokeWeightBold;
53 TFontPrintPosition CBDFHarness::iPrintPosition = EPrintPosNormal;
54 //TFontPrintPosition CBDFHarness::iPrintPosition = EPrintPosSuperscript;
55 //TFontPrintPosition CBDFHarness::iPrintPosition = EPrintPosSubscript;
59 // this function is a hack to take a Unicode string containing
60 // only chars <= 255 and put out the 8 bit version to the file
61 void FileWrite8(RFile& aFile, const TDesC& aText)
63 // It's Unicode so we've got to step down from 16 bit desc to 8 bit file
64 TInt length = aText.Length();
65 HBufC8* buffer8 = HBufC8::NewMaxL(length);
66 TPtr8 p = buffer8->Des();
71 for (int r = 0; r < length; r++)
77 aFile.Write(*buffer8);
82 CBDFHarness::CBDFHarness(const TDesC &aTitle, const TDesC &aHeading): iTest(aTitle)
85 iTest.Start(aHeading);
89 CBDFHarness* CBDFHarness::NewL(const TDesC &aTitle, const TDesC &aHeading)
91 CBDFHarness* t = new (ELeave)CBDFHarness(aTitle, aHeading);
92 CleanupStack::PushL(t);
99 void CBDFHarness::ConstructL()
101 User::LeaveIfError(RFbsSession::Connect());
102 iFbs = RFbsSession::GetSession();
104 User::Leave(KErrGeneral);
105 iDev = CFbsScreenDevice::NewL(_L("scdv"),EGray4);
106 iDev->ChangeScreenDevice(NULL);
107 iDev->SetAutoUpdate(ETrue);
108 iDev->CreateContext(iGc);
110 TFontSpec fs(iFacename, iPixelSize);
111 fs.iFontStyle.SetPosture(iPosture);
112 fs.iFontStyle.SetStrokeWeight(iStrokeWeight);
113 fs.iFontStyle.SetPrintPosition(iPrintPosition);
114 TInt error = iDev->GetNearestFontInPixels((CFont*&)iStandardFont,fs);
116 User::Panic(_L("Could not create this font"),error);
120 CBDFHarness::~CBDFHarness()
123 iDev->ReleaseFont(iStandardFont);
126 RFbsSession::Disconnect();
131 void CBDFHarness::MakeBDFFontL()
133 TOpenFontFaceAttrib attrib;
134 iStandardFont->GetFaceAttrib(attrib);
135 TPtrC familyName = attrib.FamilyName();
136 TPtrC fullName = attrib.LocalFullName();
137 TOpenFontMetrics fMetrics;
138 iStandardFont->GetFontMetrics(fMetrics);
139 iPPEM = fMetrics.Size();
143 User::LeaveIfError(file_session.Connect());
144 file_session.MkDir(_L("\\BDFfonts\\"));
147 buffer.Format(_L("\\BDFfonts\\%S %d.bdf"), &fullName, iPPEM);
148 file.Replace(file_session, buffer, EFileShareAny);
149 // Extract global info
151 buffer.Format(_L("STARTFONT 2.2\r\n"));
152 FileWrite8(file, buffer);
153 // COMMENT <font name> [(Bold) ][(Italic) ]at <nn> pixels per em
154 buffer.Format(_L("COMMENT %S "), &familyName);
155 FileWrite8(file, buffer);
156 if (iStandardFont->FontSpecInTwips().iFontStyle.StrokeWeight() == EStrokeWeightBold)
158 buffer.Format(_L("(Bold) "));
159 FileWrite8(file, buffer);
161 if (iStandardFont->FontSpecInTwips().iFontStyle.Posture() == EPostureItalic)
163 buffer.Format(_L("(Italic) "));
164 FileWrite8(file, buffer);
166 buffer.Format(_L("at %d pixels per em\r\n"), iPPEM);
167 FileWrite8(file, buffer);
168 // COMMENT Generated by the EPOC BDF creator
169 buffer.Format(_L("COMMENT Generated by the EPOC BDF creator\r\n"));
170 FileWrite8(file, buffer);
172 buffer.Format(_L("FONT %S\r\n"), &iFilename);
173 FileWrite8(file, buffer);
175 buffer.Format(_L("SIZE %d 72 72\r\n"), iPPEM);
176 FileWrite8(file, buffer);
177 // FONTBOUNDINGBOX <bbox pixel width> <bbox pixel height> <bbox x pixel offset> <bbox y pixel offset>
178 TInt bbw = fMetrics.MaxWidth();
179 TInt bbh = fMetrics.MaxHeight() + fMetrics.MaxDepth();
181 TInt bbyo = -1 * fMetrics.MaxDepth();
182 buffer.Format(_L("FONTBOUNDINGBOX %d %d %d %d\r\n"), bbw, bbh, bbxo, bbyo);
183 FileWrite8(file, buffer);
185 TInt numproperties = 5;
186 if (iStandardFont->FontSpecInTwips().iFontStyle.StrokeWeight() == EStrokeWeightBold)
188 if (iStandardFont->FontSpecInTwips().iFontStyle.Posture() == EPostureItalic)
190 buffer.Format(_L("STARTPROPERTIES %d\r\n"), numproperties);
191 FileWrite8(file, buffer);
192 buffer.Format(_L("Uid %S\r\n"), &iUid);
193 FileWrite8(file, buffer);
194 buffer.Format(_L("MaxNormalCharWidth "));
195 FileWrite8(file, buffer);
197 buffer.Format(_L("%d\r\n"), maxWidth);
198 FileWrite8(file, buffer);
199 buffer.Format(_L("MaxConsecutiveFillChars 5\r\n"));
200 FileWrite8(file, buffer);
201 if (iStandardFont->FontSpecInTwips().iFontStyle.StrokeWeight() == EStrokeWeightBold)
203 buffer.Format(_L("Bold 1\r\n"));
204 FileWrite8(file, buffer);
206 if (iStandardFont->FontSpecInTwips().iFontStyle.Posture() == EPostureItalic)
208 buffer.Format(_L("Italic 1\r\n"));
209 FileWrite8(file, buffer);
212 buffer.Format(_L("FONT_ASCENT %d\r\n"), fMetrics.Ascent());
213 FileWrite8(file, buffer);
215 buffer.Format(_L("FONT_DESCENT %d\r\n"), fMetrics.Descent());
216 FileWrite8(file, buffer);
218 buffer.Format(_L("ENDPROPERTIES\r\n"));
219 FileWrite8(file, buffer);
220 // CHARS <number of glyphs>
221 // Work out how many chars in font
224 for ( i = 0; i <= 0xFFFF; i++ )
226 if ( iStandardFont->HasCharacter(i) )
229 buffer.Format(_L("CHARS %d\r\n"), charCount);
230 FileWrite8(file, buffer);
232 for ( i = 0; i <= 0xFFFF; i++ )
234 if ( iStandardFont->HasCharacter(i) )
236 // STARTCHAR <Unn in hex>
237 buffer.Format(_L("STARTCHAR U%x\r\n"), i);
238 FileWrite8(file, buffer);
239 // ENCODING <usually nn decimal>
240 buffer.Format(_L("ENCODING %d\r\n"), i);
241 FileWrite8(file, buffer);
243 // DWIDTH <swidth * (pointsize / 1000) * (device resolution / 72)> 0
244 // DWIDTH <swidth * (ppem / 1000) * (72 / 72)> 0
245 // but DWIDTH = <advance in pixels> which we know
246 // so SWIDTH = (DWIDTH * 1000) / ppem
247 TOpenFontCharMetrics cMetrics;
248 const TUint8* bitmapPtr;
250 iStandardFont->GetCharacterData(i, cMetrics, bitmapPtr, dummy);
251 TInt dwidth = cMetrics.HorizAdvance();
252 TReal dwidthReal = dwidth;
253 TInt swidth = (TInt)(((dwidthReal * 1000) / iPPEM) + 0.5);
254 buffer.Format(_L("SWIDTH %d 0\r\n"), swidth);
255 FileWrite8(file, buffer);
256 buffer.Format(_L("DWIDTH %d 0\r\n"), dwidth);
257 FileWrite8(file, buffer);
258 // BBX <bmap pixel width> <bmap pixel height> <bmap x pixel offset> <bmap y pixel offset>
259 TInt bpw = cMetrics.Width();
260 TInt bph = cMetrics.Height();
261 TInt bxo = cMetrics.HorizBearingX();
262 TInt byo = cMetrics.HorizBearingY() - bph; // Because openfont offsets are to top left and bdf is to bottom left
263 buffer.Format(_L("BBX %d %d %d %d\r\n"), bpw, bph, bxo, byo);
264 FileWrite8(file, buffer);
266 buffer.Format(_L("BITMAP\r\n"));
267 FileWrite8(file, buffer);
268 TInt paddedWidth = ((bpw + 7) / 8);
269 if (bph * paddedWidth)
271 TUint8* bitmap = new TUint8 [sizeof(TUint8) * bph * paddedWidth];
272 DecodeBitmap(bpw, paddedWidth, bph, bitmapPtr, bitmap);
273 for (TInt j = 0; j < bph; j++)
275 // Output one line of bitmap
276 #ifndef SHOW_FONT_PICTURES_INSTEAD_OF_HEX
277 // This branch is the standard version that produces useable BDF files
278 for (TInt k = paddedWidth - 1; k >= 0; k--)
280 TInt byte = *(bitmap + (j * paddedWidth) + k);
283 FileWrite8(file, buffer);
285 FileWrite8(file, buffer);
287 buffer.Format(_L("\r\n"));
288 FileWrite8(file, buffer);
290 // This branch shows the char bitmaps but does not produce useable BDF files
291 buffer.Format(_L("["));
292 FileWrite8(file, buffer);
293 iBitNum = ((j + 1) * paddedWidth * 8) - 1;
296 for (TInt k = 0; k < bpw; k++)
299 buffer.Format(_L("@"));
301 buffer.Format(_L(" "));
302 FileWrite8(file, buffer);
304 for (k = bpw; k < 8 * paddedWidth; k++)
306 buffer.Format(_L("."));
307 FileWrite8(file, buffer);
309 buffer.Format(_L("]\r\n"));
310 FileWrite8(file, buffer);
316 buffer.Format(_L("ENDCHAR\r\n"));
317 FileWrite8(file, buffer);
321 buffer.Format(_L("ENDFONT\r\n"));
322 FileWrite8(file, buffer);
326 file_session.Close();
330 TInt CBDFHarness::ReadBit()
333 for (TInt i = 0; i < (iBitNum % 8); i++)
335 val = iBitMap[iBitNum / 8] & val;
340 TInt CBDFHarness::ReadBitBack()
343 for (TInt i = 0; i < (iBitNum % 8); i++)
345 val = iBitMap[iBitNum / 8] & val;
350 void CBDFHarness::OutHex(TInt aOneHexDigit)
352 if (aOneHexDigit <= 9)
353 buffer.Format(_L("%c"), '0' + aOneHexDigit);
355 buffer.Format(_L("%c"), 'a' + aOneHexDigit - 10);
358 void CBDFHarness::DecodeBitmap(TInt aWidth, TInt aPaddedWidth, TInt aHeight, const TUint8* aCodedBitmap, TUint8* aBitMap)
361 for (TInt i = 0; i < aPaddedWidth * aHeight; i++)
365 iBitMap = aCodedBitmap;
366 for (TInt linesRead = 0; linesRead < aHeight; linesRead += repeats)
368 TInt repeating = !(ReadBit());
370 // Intended behavior is for the line of code below to be evaluated left to right.
371 // repeats = (1 * ReadBit()) + (2 * ReadBit()) + (4 * ReadBit()) + (8 * ReadBit());
372 // However, the order in which calls to ReadBit() above are evaluated is undefined,
373 // and is compiler dependent.
374 // The following code ensures that the correct code is used:
375 TInt bit1 = ReadBit();
376 TInt bit2 = ReadBit();
377 TInt bit3 = ReadBit();
378 TInt bit4 = ReadBit();
379 repeats = (1 * bit1) + (2 * bit2) + (4 * bit3) + (8 * bit4);
381 TInt padVal = (aPaddedWidth * 8) - aWidth;
384 for (TInt j = padVal + aWidth - 1; j >= padVal; j--)
386 TInt value = ReadBit();
387 for (TInt k = 0; k < (j % 8); k++)
389 for (TInt l = 0; l < repeats; l++)
391 aBitMap[((linesRead + l) * aPaddedWidth) + (j / 8)] =
392 TUint8(aBitMap[((linesRead + l) * aPaddedWidth) + (j / 8)] + value);
398 for (TInt l = 0; l < repeats; l++)
400 for (TInt j = padVal + aWidth - 1; j >= padVal; j--)
402 TInt value = ReadBit();
403 for (TInt k = 0; k < (j % 8); k++)
405 aBitMap[((linesRead + l) * aPaddedWidth) + (j / 8)] =
406 TUint8(aBitMap[((linesRead + l) * aPaddedWidth) + (j / 8)] + value);