sl@0: // Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "FbsMessage.h" sl@0: #include "UTILS.H" sl@0: sl@0: const TInt KFbsGlyphDataIterCodeInvalid = -1; sl@0: sl@0: extern void Panic(TFbsPanic aPanic); sl@0: sl@0: /** sl@0: The default constructor sets the iterator to a closed and empty state. It sl@0: is the only way of constructing an iterator as instances cannot be copied by sl@0: assignment or passed by value. sl@0: */ sl@0: EXPORT_C RFbsGlyphDataIterator::RFbsGlyphDataIterator() : sl@0: iImpl(NULL) sl@0: { sl@0: } sl@0: sl@0: /** sl@0: For a given font (aFont), this method retrieves the glyph data for a list of sl@0: glyph codes (aGlyphCodes), containing a number (aCount) of codes. On success, sl@0: the iterator is initialised with the data for the first glyph in aGlyphCodes. sl@0: sl@0: The memory allocated to aGlyphCodes must not be freed or altered while the sl@0: iterator is in use (i.e. until the iterator has been closed). sl@0: sl@0: Open() may not be called on an already open iterator. In order to re-open an sl@0: iterator, it must first be closed by a call tor Close(). sl@0: sl@0: If a glyph code is passed in that is not a recognised glyph code for the sl@0: associated font, an empty-box glyph will be returned. This behaviour is sl@0: consistent with CFbsFont::GetCharacterData(). sl@0: sl@0: @pre The iterator is not already open. sl@0: sl@0: @param aFont The font to provide the glyph code data for. sl@0: @param aGlyphCodes An array of glyph codes that the iterator will sl@0: provide data for. This memory allocated for this array must not be sl@0: freed before the iterator is closed. sl@0: @param aCount The number of glyph codes in aGlyphCodes. sl@0: sl@0: @return sl@0: KErrNone, if the iterator is successfully initialised to retrieve sl@0: the glyph data; sl@0: KErrInUse, if the iterator is already open, in which case the state of sl@0: the iterator is left unchanged; sl@0: KErrNoMemory, if the iterator cannot be opened due to insufficient sl@0: system memory; sl@0: KErrNotSupported, if aFont refers to a bitmap font or an outline & shadow sl@0: font, if the required version of the hardware driver is not available sl@0: (EGL 1.3 or later is required), or if RSgImages are not supported by sl@0: the system; sl@0: KErrArgument, if aCount is negative or zero, or if aGlyphCodes is null. sl@0: */ sl@0: EXPORT_C TInt RFbsGlyphDataIterator::Open(CFbsFont& aFont, const TUint* aGlyphCodes, TInt aCount) sl@0: { sl@0: if (iImpl) sl@0: { sl@0: return KErrInUse; sl@0: } sl@0: if ((aCount <= 0) || !aGlyphCodes) sl@0: { sl@0: return KErrArgument; sl@0: } sl@0: if (!aFont.Address()->IsOpenFont()) sl@0: { sl@0: return KErrNotSupported; sl@0: } sl@0: TInt glyphBitmapType = aFont.Address()->GlyphBitmapType(); sl@0: if (!( (glyphBitmapType == EMonochromeGlyphBitmap) || (glyphBitmapType == EAntiAliasedGlyphBitmap) )) sl@0: { sl@0: //Only supported bitmap types can be used i.e. EMonochromeGlyphBitmap or EAntiAliasedGlyphBitmap sl@0: return KErrNotSupported; sl@0: } sl@0: // Check that the max width and height of the font are both no more than 2048. sl@0: // This is the smallest maximum size an RSgImage can be created with. sl@0: // This limit is arbitrarily set as it should cover nearly all use cases. sl@0: const TInt KMaxFontSizeInPixels = 2048; sl@0: TInt maxHeight = aFont.FontMaxHeight(); sl@0: TInt maxWidth = aFont.MaxCharWidthInPixels(); sl@0: if ( (KMaxFontSizeInPixels < maxHeight) || (KMaxFontSizeInPixels < maxWidth) ) sl@0: { sl@0: return KErrTooBig; sl@0: } sl@0: // Construct implementor object that holds the state for the iterator. sl@0: iImpl = new CGlyphDataIteratorImpl(aFont.iHandle, aGlyphCodes, aCount); sl@0: if (!iImpl) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: TInt err = iImpl->Initialise(); sl@0: if (err != KErrNone) sl@0: { sl@0: Close(); sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: /** sl@0: Moves the iterator to the data for the next glyph code in the array passed sl@0: into RFbsGlyphDataIterator::Open(). Data for the glyph can then be accessed sl@0: using the Image(), Rect() and Metrics() methods. sl@0: sl@0: Once Next() has been called, the references returned by Image(), Rect() and sl@0: Metrics() methods during the previous iteration should be considered invalid sl@0: and must be discarded. sl@0: sl@0: Calling Next() repeatedly will iterate through the glyph data for all the glyph sl@0: codes, until it has reached the last glyph code in the array (assuming no errors sl@0: are encountered), at which point KErrNotFound is returned, and the array of glyph sl@0: codes passed to RFbsGlyphDataIterator::Open() can safely be deleted. sl@0: sl@0: If the call was successful, KErrNone is returned. If an error is encountered an sl@0: error code will be returned and the state of the iterator is left unchanged. sl@0: sl@0: @pre The iterator has been opened by a successful call to Open(). sl@0: @post The properties of the iterator are of the glyph corresponding sl@0: to the next code passed in the array to Open(). However, if an error code sl@0: was returned, the state of the iterator is unchanged. sl@0: sl@0: @return sl@0: KErrNone, if the iterator was successfully advanced; sl@0: KErrNotFound, if the iterator is already at the last element and cannot sl@0: be advanced any further; sl@0: KErrNoMemory, if there is insufficient system memory available; sl@0: KErrNoGraphicsMemory, if there is insufficient graphics memory available. sl@0: sl@0: @panic FBSCLI 31, if the iterator is not open. sl@0: @panic FBSERV -8, if as a result of this call, communication with the sl@0: server is invoked, and the associated CFbsFont has been destroyed. sl@0: */ sl@0: EXPORT_C TInt RFbsGlyphDataIterator::Next() sl@0: { sl@0: __ASSERT_ALWAYS(iImpl, Panic(EFbsPanicGlyphDataIteratorClosed)); sl@0: return iImpl->Next(); sl@0: } sl@0: sl@0: /** sl@0: Closes the iterator and releases its internal resources. After calling, all data sl@0: retrieved by the iterator is no longer safe to use. Once closed, this iterator sl@0: can be re-opened. Calling Close() on an already closed iterator has no effect. sl@0: sl@0: Once an iterator is closed, the array of glyphs (aGlyphCodes) passed to sl@0: RFbsGlyphDataIterator::Open() can safely be deleted. sl@0: sl@0: @post The iterator is closed. sl@0: */ sl@0: EXPORT_C void RFbsGlyphDataIterator::Close() sl@0: { sl@0: delete iImpl; sl@0: iImpl = NULL; sl@0: } sl@0: sl@0: /** sl@0: Returns a reference to the RSgImage that contains the glyph for the current sl@0: iteration. The image representation of the glyph is the same as the image sl@0: returned by the existing CFbsFont::GetCharacterData() method (i.e. an alpha mask). sl@0: sl@0: The RSgImage should only be considered a temporary handle for use in this sl@0: iteration, and should not be used after a call to Next() or Close() has sl@0: been made. sl@0: sl@0: Note: For glyphs such as space which have no visible representation, Image() sl@0: will return a null image handle (i.e. RSgImage::IsNull() returns ETrue). This sl@0: cannot be used for drawing. In this case Rect() will be empty however sl@0: Metrics() will still be valid. sl@0: sl@0: @pre The iterator has been initialised by successfully calling Open(). sl@0: sl@0: @return A handle to the image where the glyph for this iteration is stored. sl@0: sl@0: @panic FBSCLI 31, if the iterator is not open. sl@0: */ sl@0: EXPORT_C const RSgImage& RFbsGlyphDataIterator::Image() const sl@0: { sl@0: __ASSERT_ALWAYS(iImpl, Panic(EFbsPanicGlyphDataIteratorClosed)); sl@0: __ASSERT_DEBUG(!iImpl->iGlyphBatch.IsEmpty(), Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: return iImpl->iGlyphBatch.First()->iImage; sl@0: } sl@0: sl@0: /** sl@0: Returns the area within the RSgImage where the glyph for the current sl@0: iteration is located. The reference returned by Rect() should be considered sl@0: temporary for use within this iteration and should not be used after a call to sl@0: Next() or Close() has been made. sl@0: sl@0: @pre The iterator has been initialised by successfully calling Open(). sl@0: sl@0: @return A rectangle representing the position and size in pixels, sl@0: of the glyph for this iteration on the RSgImage provided by Image(). sl@0: sl@0: @panic FBSCLI 31, if the iterator is not open. sl@0: */ sl@0: EXPORT_C const TRect& RFbsGlyphDataIterator::Rect() const sl@0: { sl@0: __ASSERT_ALWAYS(iImpl, Panic(EFbsPanicGlyphDataIteratorClosed)); sl@0: __ASSERT_DEBUG(!iImpl->iGlyphBatch.IsEmpty(), Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: return iImpl->iGlyphDataIterRect; sl@0: } sl@0: sl@0: /** sl@0: Returns the glyph metrics for the current iteration. The reference returned by sl@0: Metrics() should be considered temporary for use within this iteration and sl@0: should not be used after a call to Next() or Close() has been made. sl@0: sl@0: @pre The iterator has been initialised by successfully calling Open(). sl@0: sl@0: @return The metrics for the glyph at the current iteration. sl@0: sl@0: @panic FBSCLI 31, if the iterator is not open. sl@0: */ sl@0: EXPORT_C const TOpenFontCharMetrics& RFbsGlyphDataIterator::Metrics() const sl@0: { sl@0: __ASSERT_ALWAYS(iImpl, Panic(EFbsPanicGlyphDataIteratorClosed)); sl@0: __ASSERT_DEBUG(!iImpl->iGlyphBatch.IsEmpty(), Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: return iImpl->iGlyphBatch.First()->iInfo.iMetrics; sl@0: } sl@0: sl@0: /** sl@0: Returns the glyph code associated with the data for the current iteration. sl@0: sl@0: @pre The iterator has been initialised by successfully calling Open(). sl@0: sl@0: @return The glyph code of the glyph at the current iteration. sl@0: sl@0: @panic FBSCLI 31, if the iterator is not open. sl@0: */ sl@0: EXPORT_C TUint RFbsGlyphDataIterator::GlyphCode() const sl@0: { sl@0: __ASSERT_ALWAYS(iImpl, Panic(EFbsPanicGlyphDataIteratorClosed)); sl@0: __ASSERT_DEBUG(!iImpl->iGlyphBatch.IsEmpty(), Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: return iImpl->iGlyphDataIterCodes[iImpl->iGlyphDataIterCodeIndex]; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Constructs a CGlyphDataIteratorImpl. sl@0: @param aFbsFontHandle The handle of the FbsFont that the iterator is working with. sl@0: @param aGlyphCodes The array of glyph codes sent to RFbsGlyphDataIterator::Open() sl@0: @param aCount The number of glyph codes in aGlyphCodes. sl@0: */ sl@0: CGlyphDataIteratorImpl::CGlyphDataIteratorImpl(TInt aFbsFontHandle, const TUint* aGlyphCodes, TInt aCount) : sl@0: iGlyphBatch(_FOFF(TGlyphBatchItem, iLink)), sl@0: iGlyphDataIterCodes(aGlyphCodes), sl@0: iGlyphDataIterCodeCount(aCount), sl@0: iGlyphDataIterCodeIndex(KFbsGlyphDataIterCodeInvalid), sl@0: iFbsFontHandle(aFbsFontHandle) sl@0: { sl@0: } sl@0: sl@0: /** sl@0: Destructor. Releases all resources, disconnects from server and frees any sl@0: items in the list of batched items. sl@0: */ sl@0: CGlyphDataIteratorImpl::~CGlyphDataIteratorImpl() sl@0: { sl@0: if (iFbs) sl@0: { sl@0: if (iGlyphDataIterCodeIndex != KFbsGlyphDataIterCodeInvalid) sl@0: { sl@0: //Send the No-Op command to ensure that the "In Transit" RSgImage(s) are closed. sl@0: iFbs->SendCommand(EFbsMessNoOp); sl@0: } sl@0: RFbsSession::Disconnect(); sl@0: iFbs = NULL; sl@0: } sl@0: while (!iGlyphBatch.IsEmpty()) sl@0: { sl@0: TGlyphBatchItem* item = iGlyphBatch.First(); sl@0: item->iImage.Close(); sl@0: iGlyphBatch.Remove(*item); sl@0: delete item; sl@0: } sl@0: iGlyphBatch.Reset(); sl@0: } sl@0: sl@0: /** sl@0: Sets up the CGlyphDataIteratorImpl, populating the first batch of glyphs. sl@0: Should only be called once, immediately after construction. sl@0: */ sl@0: TInt CGlyphDataIteratorImpl::Initialise() sl@0: { sl@0: __ASSERT_DEBUG(iFbsFontHandle, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: __ASSERT_DEBUG(iGlyphDataIterCodes, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: __ASSERT_DEBUG(iGlyphDataIterCodeCount, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: __ASSERT_DEBUG(iGlyphDataIterCodeIndex == KFbsGlyphDataIterCodeInvalid, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: sl@0: // If the client already has a session open, this is just a reference counting exercise and should incur no performance impact. sl@0: TInt err = RFbsSession::Connect(); sl@0: if (err == KErrNone) sl@0: { sl@0: iFbs = RFbsSession::GetSession(); sl@0: err = UpdateGlyphBatch(0); sl@0: } sl@0: if (err == KErrNone) sl@0: { sl@0: iGlyphDataIterCodeIndex = 0; sl@0: UpdateGlyphRect(); sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: /** sl@0: Increments the current iteration if possible, re-sending the request sl@0: for more glyphs if the current batch of glyphs is down to the last sl@0: item. sl@0: @see RFbsGlyphDataIterator::Next() sl@0: */ sl@0: TInt CGlyphDataIteratorImpl::Next() sl@0: { sl@0: __ASSERT_DEBUG(!iGlyphBatch.IsEmpty(), Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: if ( (iGlyphDataIterCodeIndex + 1) >= iGlyphDataIterCodeCount) sl@0: { sl@0: return KErrNotFound; sl@0: } sl@0: TInt err = UpdateGlyphBatch(iGlyphDataIterCodeIndex + 1); sl@0: if (err == KErrNone) sl@0: { sl@0: ++iGlyphDataIterCodeIndex; sl@0: // Close the current image and pop the head of the batch. sl@0: TGlyphBatchItem* item = iGlyphBatch.First(); sl@0: item->iImage.Close(); sl@0: iGlyphBatch.Remove(*item); sl@0: delete item; sl@0: __ASSERT_DEBUG(!iGlyphBatch.IsEmpty(), Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: UpdateGlyphRect(); sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: /** sl@0: Checks whether a call to the server is required to get a new batch of glyph sl@0: info, and processes the response from the server as necessary. sl@0: sl@0: @param aIndex Specifies the index into the glyph array which needs to be in sl@0: the active glyph batch. If it is not there, a request is made to the server sl@0: to get it. sl@0: @return KErrNone if getting at least one glyph succeeded or a call to the sl@0: server was not necessary, otherwise one of the system wide error codes. sl@0: @panic FBSCLI 31 (debug only), if the iterator is not open sl@0: @panic FBSCLI 33 (debug only), if an unexpected number of glyphs was received sl@0: as a result of requesting glyphs from the server, or if the current batch sl@0: of glyphs is empty when there should be at least one item. sl@0: */ sl@0: TInt CGlyphDataIteratorImpl::UpdateGlyphBatch(TInt aIndex) sl@0: { sl@0: __ASSERT_DEBUG(Rng(0, aIndex, iGlyphDataIterCodeCount - 1), Panic(EFbsPanicGlyphDataIteratorIndexOutOfRange)); sl@0: sl@0: TInt err = KErrNone; sl@0: sl@0: TBool needMoreGlyphs = EFalse; sl@0: if (iGlyphBatch.IsEmpty()) sl@0: { sl@0: // Current batch is empty, must request more. Should only get here when the iterator sl@0: // is first opened, since one item should always be in the list from then on. sl@0: __ASSERT_DEBUG(aIndex == 0, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: needMoreGlyphs = ETrue; sl@0: } sl@0: else if (iGlyphBatch.IsLast(iGlyphBatch.First())) sl@0: { sl@0: // Only one item in the list. sl@0: needMoreGlyphs = ETrue; sl@0: } sl@0: sl@0: if (needMoreGlyphs) sl@0: { sl@0: // If the array of batched images is empty OR only one left, means we need to request a new batch. sl@0: // We make sure there is at least one glyph in the batch so the iterator is always usable even sl@0: // when a failure to move to the next iteration occurs. sl@0: sl@0: TBool glyphAddedToBatch = EFalse; sl@0: TUint glyphCodes[KMaxGlyphBatchSize]; sl@0: sl@0: TInt numGlyphsToRequest = Min(iGlyphDataIterCodeCount - aIndex, KMaxGlyphBatchSize); sl@0: (void)Mem::Copy(glyphCodes, &(iGlyphDataIterCodes[aIndex]), sizeof(TUint) * numGlyphsToRequest); sl@0: TPckg argGlyphCodes(glyphCodes); sl@0: sl@0: TGlyphImageInfo rcvdGlyphInfo[KMaxGlyphBatchSize]; sl@0: TPckg argGlyphInfo(rcvdGlyphInfo); sl@0: sl@0: if (numGlyphsToRequest < KMaxGlyphBatchSize) sl@0: { sl@0: argGlyphCodes.SetLength(numGlyphsToRequest * sizeof(TUint)); sl@0: argGlyphInfo.SetLength(numGlyphsToRequest * sizeof(TGlyphImageInfo)); sl@0: } sl@0: sl@0: err = iFbs->SendCommand(EFbsMessGetGlyphs, TIpcArgs(iFbsFontHandle, &argGlyphCodes, &argGlyphInfo)); sl@0: if (err == KErrNone) sl@0: { sl@0: __ASSERT_DEBUG(argGlyphInfo.Length() % sizeof(TGlyphImageInfo) == 0, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: TInt numRcvdGlyphs = argGlyphInfo.Length() / sizeof(TGlyphImageInfo); sl@0: __ASSERT_DEBUG(numRcvdGlyphs > 0, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: __ASSERT_DEBUG(numRcvdGlyphs <= KMaxGlyphBatchSize, Panic(EFbsPanicGlyphDataIteratorInvalidState)); sl@0: sl@0: // Store the received glyph data, and open the image handles so that the IDs sl@0: // will not be released by FbServ between now and the client using them. sl@0: // If a failure occurs while processing one of the recevied glyphs, sl@0: // abort the rest but keep the ones that succeeded. sl@0: for (TInt i = 0; (i < numRcvdGlyphs) && (err == KErrNone); ++i) sl@0: { sl@0: TGlyphBatchItem* glyphEntry = new TGlyphBatchItem; sl@0: if (!glyphEntry) sl@0: { sl@0: err = KErrNoMemory; sl@0: } sl@0: else sl@0: { sl@0: glyphEntry->iInfo = rcvdGlyphInfo[i]; sl@0: sl@0: RSgImage glyphImage; sl@0: if (rcvdGlyphInfo[i].iImageId != KSgNullDrawableId) sl@0: { sl@0: err = glyphEntry->iImage.Open(rcvdGlyphInfo[i].iImageId); sl@0: } sl@0: if (err == KErrNone) sl@0: { sl@0: iGlyphBatch.AddLast(*glyphEntry); sl@0: glyphAddedToBatch = ETrue; sl@0: } sl@0: else sl@0: { sl@0: delete glyphEntry; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: if (err != KErrNone && glyphAddedToBatch) sl@0: { sl@0: // There was an error adding an item to the batch. Rather than return the sl@0: // error to the client, ignore it and use what glyphs we successfully batched. sl@0: err = KErrNone; sl@0: } sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: /** sl@0: Updates the glyph rectangle member based on the current glyph metrics. sl@0: @post The iGlyphDataIterRect member is updated to reflect the position sl@0: and size of the currently active glyph. sl@0: */ sl@0: void CGlyphDataIteratorImpl::UpdateGlyphRect() sl@0: { sl@0: iGlyphDataIterRect.iTl = TPoint(iGlyphBatch.First()->iInfo.iPosX, iGlyphBatch.First()->iInfo.iPosY); sl@0: iGlyphDataIterRect.SetSize(TSize(iGlyphBatch.First()->iInfo.iMetrics.Width(), iGlyphBatch.First()->iInfo.iMetrics.Height())); sl@0: }