commands/genbmp/genbmp.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // genbmp.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/ioutils.h>
       
    14 #include <gdi.h>
       
    15 #include <fbs.h>
       
    16 #include <imageconversion.h>
       
    17 
       
    18 using namespace IoUtils;
       
    19 
       
    20 class CCmdGenbmp : public CCommandBase
       
    21 	{
       
    22 public:
       
    23 	static CCommandBase* NewLC();
       
    24 	~CCmdGenbmp();
       
    25 private:
       
    26 	CCmdGenbmp();
       
    27 private: // From CCommandBase.
       
    28 	virtual const TDesC& Name() const;
       
    29 	virtual void DoRunL();
       
    30 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    31 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    32 private: // From CActive.
       
    33 	virtual void RunL();
       
    34 private:
       
    35 	RFbsSession iFbsSession;
       
    36 	CFbsBitmap* iBitmap;
       
    37 	CFbsBitmapDevice* iBitmapDevice;
       
    38 	CBitmapContext* iBitmapContext;
       
    39 	CImageEncoder* iImageEncoder;
       
    40 	RFile iFile;
       
    41 	TFileName2 iFileName;
       
    42 	RPointerArray<HBufC> iText;
       
    43 	TSize iSize;
       
    44 	TUint32 iForegroundColor;
       
    45 	TUint32 iBackgroundColor;
       
    46 	HBufC* iTypefaceName;
       
    47 	TInt iMaxFontHeight;
       
    48 	TInt iGap;
       
    49 	};
       
    50 
       
    51 
       
    52 CCommandBase* CCmdGenbmp::NewLC()
       
    53 	{
       
    54 	CCmdGenbmp* self = new(ELeave) CCmdGenbmp();
       
    55 	CleanupStack::PushL(self);
       
    56 	self->BaseConstructL();
       
    57 	return self;
       
    58 	}
       
    59 
       
    60 CCmdGenbmp::~CCmdGenbmp()
       
    61 	{
       
    62 	delete iTypefaceName;
       
    63 	delete iImageEncoder;
       
    64 	delete iBitmapContext;
       
    65 	delete iBitmapDevice;
       
    66 	delete iBitmap;
       
    67 	iFbsSession.Disconnect();
       
    68 	iFile.Close();
       
    69 	iText.ResetAndDestroy();
       
    70 	REComSession::FinalClose();
       
    71 	}
       
    72 
       
    73 CCmdGenbmp::CCmdGenbmp()
       
    74 	: iSize(150, 150), iForegroundColor(0xffffff), iBackgroundColor(0x000000), iMaxFontHeight(15), iGap(10)
       
    75 	{
       
    76 	}
       
    77 
       
    78 const TDesC& CCmdGenbmp::Name() const
       
    79 	{
       
    80 	_LIT(KName, "genbmp");	
       
    81 	return KName;
       
    82 	}
       
    83 
       
    84 void CCmdGenbmp::DoRunL()
       
    85 	{
       
    86 	LeaveIfErr(iFbsSession.Connect(FsL()), _L("Couldn't connect to the Font & Bitmap Server"));
       
    87 	iBitmap = new(ELeave)CFbsBitmap;
       
    88 	User::LeaveIfError(iBitmap->Create(iSize, EColor16));
       
    89 	iBitmapDevice = CFbsBitmapDevice::NewL(iBitmap);
       
    90 	LeaveIfErr(iBitmapDevice->CreateBitmapContext(iBitmapContext), _L("Couldn't create bitmap context"));
       
    91 	iBitmapContext->SetPenColor(iForegroundColor);
       
    92 	iBitmapContext->SetPenStyle(CGraphicsContext::ENullPen);
       
    93 	iBitmapContext->SetBrushColor(iBackgroundColor);
       
    94 	iBitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
    95 
       
    96 	TFontSpec fontSpec;
       
    97 	fontSpec.iTypeface.iName = iTypefaceName ? *iTypefaceName : _L("Arial");
       
    98 	fontSpec.iTypeface.SetIsSymbol(EFalse);
       
    99 	CFont* font;
       
   100 	LeaveIfErr(iBitmapDevice->GetNearestFontToMaxHeightInPixels(font, fontSpec, iMaxFontHeight), _L("Couldn't get suitable font"));
       
   101 	iBitmapContext->DrawRect(iSize);
       
   102 	iBitmapContext->UseFont(font);
       
   103 
       
   104 	TPoint linePos(iGap, iGap + iMaxFontHeight);
       
   105 	const TInt numLines = iText.Count();
       
   106 	for (TInt i = 0; i < numLines; ++i)
       
   107 		{
       
   108 		iBitmapContext->DrawText(*(iText[i]), linePos);
       
   109 		linePos.iY += (iGap + iMaxFontHeight);
       
   110 		}
       
   111 
       
   112 	iBitmapContext->DiscardFont();
       
   113 
       
   114 	LeaveIfErr(iFile.Replace(FsL(), iFileName, EFileWrite), _L("Unable to open \"%S\" for writing"), &iFileName);
       
   115 	iImageEncoder = CImageEncoder::FileNewL(iFile, CImageEncoder::EOptionNone, KImageTypeBMPUid);
       
   116 	iImageEncoder->Convert(&iStatus, *iBitmap);
       
   117 	SetActive();
       
   118 	}
       
   119 
       
   120 void CCmdGenbmp::ArgumentsL(RCommandArgumentList& aArguments)
       
   121 	{
       
   122 	_LIT(KArgFileName, "file_name");
       
   123 	aArguments.AppendFileNameL(iFileName, KArgFileName);
       
   124 
       
   125 	_LIT(KArgText, "text");
       
   126 	aArguments.AppendStringL(iText, KArgText);
       
   127 	}
       
   128 
       
   129 void CCmdGenbmp::OptionsL(RCommandOptionList& aOptions)
       
   130 	{
       
   131 	_LIT(KOptForegroundColor, "foreground-color");
       
   132 	aOptions.AppendUintL((TUint&)iForegroundColor, KOptForegroundColor);
       
   133 
       
   134 	_LIT(KOptBackgroundColor, "background-color");
       
   135 	aOptions.AppendUintL((TUint&)iBackgroundColor, KOptBackgroundColor);
       
   136 
       
   137 	_LIT(KOptWidth, "width");
       
   138 	aOptions.AppendIntL(iSize.iWidth, KOptWidth);
       
   139 
       
   140 	_LIT(KOptHeight, "height");
       
   141 	aOptions.AppendIntL(iSize.iHeight, KOptHeight);
       
   142 
       
   143 	_LIT(KOptTypefaceName, "typeface");
       
   144 	aOptions.AppendStringL(iTypefaceName, KOptTypefaceName);
       
   145 
       
   146 	_LIT(KOptMaxFontHeight, "max-font-height");
       
   147 	aOptions.AppendIntL(iMaxFontHeight, KOptMaxFontHeight);
       
   148 
       
   149 	_LIT(KOptGap, "gap");
       
   150 	aOptions.AppendIntL(iGap, KOptGap);
       
   151 	}
       
   152 	
       
   153 void CCmdGenbmp::RunL()
       
   154 	{
       
   155 	LeaveIfErr(iStatus.Int(), _L("Encode to BMP failed"));
       
   156 	Complete(KErrNone);
       
   157 	}
       
   158 
       
   159 
       
   160 EXE_BOILER_PLATE(CCmdGenbmp)
       
   161