xref: /aoo41x/main/vcl/aqua/source/gdi/ctlayout.cxx (revision bca19674)
12822fc04SHerbert Dürr /**************************************************************
22822fc04SHerbert Dürr  *
32822fc04SHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
42822fc04SHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
52822fc04SHerbert Dürr  * distributed with this work for additional information
62822fc04SHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
72822fc04SHerbert Dürr  * to you under the Apache License, Version 2.0 (the
82822fc04SHerbert Dürr  * "License"); you may not use this file except in compliance
92822fc04SHerbert Dürr  * with the License.  You may obtain a copy of the License at
102822fc04SHerbert Dürr  *
112822fc04SHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
122822fc04SHerbert Dürr  *
132822fc04SHerbert Dürr  * Unless required by applicable law or agreed to in writing,
142822fc04SHerbert Dürr  * software distributed under the License is distributed on an
152822fc04SHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162822fc04SHerbert Dürr  * KIND, either express or implied.  See the License for the
172822fc04SHerbert Dürr  * specific language governing permissions and limitations
182822fc04SHerbert Dürr  * under the License.
192822fc04SHerbert Dürr  *
202822fc04SHerbert Dürr  *************************************************************/
212822fc04SHerbert Dürr 
222822fc04SHerbert Dürr //#include "salgdi.hxx"
232822fc04SHerbert Dürr #include "tools/debug.hxx"
242822fc04SHerbert Dürr 
252822fc04SHerbert Dürr #include "ctfonts.hxx"
262822fc04SHerbert Dürr 
272822fc04SHerbert Dürr // =======================================================================
282822fc04SHerbert Dürr 
292822fc04SHerbert Dürr class CTLayout
302822fc04SHerbert Dürr :	public SalLayout
312822fc04SHerbert Dürr {
322822fc04SHerbert Dürr public:
332822fc04SHerbert Dürr 	explicit        CTLayout( const CTTextStyle* );
342822fc04SHerbert Dürr 	virtual         ~CTLayout( void );
352822fc04SHerbert Dürr 
362822fc04SHerbert Dürr 	virtual bool	LayoutText( ImplLayoutArgs& );
372822fc04SHerbert Dürr 	virtual void	AdjustLayout( ImplLayoutArgs& );
382822fc04SHerbert Dürr 	virtual void	DrawText( SalGraphics& ) const;
392822fc04SHerbert Dürr 
402822fc04SHerbert Dürr 	virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pOutGlyphIds, Point& rPos, int&,
412822fc04SHerbert Dürr 						sal_Int32* pGlyphAdvances, int* pCharIndexes ) const;
422822fc04SHerbert Dürr 
432822fc04SHerbert Dürr 	virtual long    GetTextWidth() const;
442822fc04SHerbert Dürr 	virtual long    FillDXArray( sal_Int32* pDXArray ) const;
452822fc04SHerbert Dürr 	virtual int     GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const;
462822fc04SHerbert Dürr 	virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
472822fc04SHerbert Dürr 	virtual bool    GetGlyphOutlines( SalGraphics&, PolyPolyVector& ) const;
482822fc04SHerbert Dürr 	virtual bool    GetBoundRect( SalGraphics&, Rectangle& ) const;
492822fc04SHerbert Dürr 
502822fc04SHerbert Dürr 	const ImplFontData* GetFallbackFontData( sal_GlyphId ) const;
512822fc04SHerbert Dürr 
522822fc04SHerbert Dürr 	virtual void	InitFont( void) const;
532822fc04SHerbert Dürr 	virtual void	MoveGlyph( int nStart, long nNewXPos );
542822fc04SHerbert Dürr 	virtual void	DropGlyph( int nStart );
552822fc04SHerbert Dürr 	virtual void	Simplify( bool bIsBase );
562822fc04SHerbert Dürr 
572822fc04SHerbert Dürr private:
582822fc04SHerbert Dürr 	const CTTextStyle* const	mpTextStyle;
592822fc04SHerbert Dürr 
602822fc04SHerbert Dürr 	// CoreText specific objects
612822fc04SHerbert Dürr 	CFAttributedStringRef mpAttrString;
622822fc04SHerbert Dürr 	CTLineRef mpCTLine;
632822fc04SHerbert Dürr 
642822fc04SHerbert Dürr 	int mnCharCount;		// ==mnEndCharPos-mnMinCharPos
652822fc04SHerbert Dürr 	int mnTrailingSpaces;
662822fc04SHerbert Dürr 
672822fc04SHerbert Dürr 	// to prevent overflows
682822fc04SHerbert Dürr 	// font requests get size limited by downscaling huge fonts
692822fc04SHerbert Dürr 	// in these cases the font scale becomes something bigger than 1.0
702822fc04SHerbert Dürr 	float mfFontScale; // TODO: does CoreText have a font size limit?
712822fc04SHerbert Dürr 
722822fc04SHerbert Dürr 	// cached details about the resulting layout
732822fc04SHerbert Dürr 	// mutable members since these details are all lazy initialized
742822fc04SHerbert Dürr 	mutable double	mfCachedWidth;			// cached value of resulting typographical width
752822fc04SHerbert Dürr 
762822fc04SHerbert Dürr 	// x-offset relative to layout origin
772822fc04SHerbert Dürr 	// currently only used in RTL-layouts
782822fc04SHerbert Dürr 	mutable long	mnBaseAdv;
792822fc04SHerbert Dürr };
802822fc04SHerbert Dürr 
812822fc04SHerbert Dürr // =======================================================================
822822fc04SHerbert Dürr 
832822fc04SHerbert Dürr CTLayout::CTLayout( const CTTextStyle* pTextStyle )
842822fc04SHerbert Dürr :	mpTextStyle( pTextStyle )
852822fc04SHerbert Dürr ,	mpAttrString( NULL )
862822fc04SHerbert Dürr ,	mpCTLine( NULL )
872822fc04SHerbert Dürr ,	mnCharCount( 0 )
882822fc04SHerbert Dürr ,	mnTrailingSpaces( 0 )
892822fc04SHerbert Dürr ,	mfFontScale( pTextStyle->mfFontScale )
902822fc04SHerbert Dürr ,	mfCachedWidth( -1 )
912822fc04SHerbert Dürr ,	mnBaseAdv( 0 )
922822fc04SHerbert Dürr {
932822fc04SHerbert Dürr 	CFRetain( mpTextStyle->GetStyleDict() );
942822fc04SHerbert Dürr }
952822fc04SHerbert Dürr 
962822fc04SHerbert Dürr // -----------------------------------------------------------------------
972822fc04SHerbert Dürr 
982822fc04SHerbert Dürr CTLayout::~CTLayout()
992822fc04SHerbert Dürr {
1002822fc04SHerbert Dürr 	if( mpCTLine )
1012822fc04SHerbert Dürr 		CFRelease( mpCTLine );
1022822fc04SHerbert Dürr 	if( mpAttrString )
1032822fc04SHerbert Dürr 		CFRelease( mpAttrString );
1042822fc04SHerbert Dürr 	CFRelease( mpTextStyle->GetStyleDict() );
1052822fc04SHerbert Dürr }
1062822fc04SHerbert Dürr 
1072822fc04SHerbert Dürr // -----------------------------------------------------------------------
1082822fc04SHerbert Dürr 
1092822fc04SHerbert Dürr bool CTLayout::LayoutText( ImplLayoutArgs& rArgs )
1102822fc04SHerbert Dürr {
1112822fc04SHerbert Dürr 	if( mpAttrString )
1122822fc04SHerbert Dürr 		CFRelease( mpAttrString );
1132822fc04SHerbert Dürr 	mpAttrString = NULL;
1142822fc04SHerbert Dürr 	if( mpCTLine )
1152822fc04SHerbert Dürr 		CFRelease( mpCTLine );
1162822fc04SHerbert Dürr 	mpCTLine = NULL;
1172822fc04SHerbert Dürr 
1182822fc04SHerbert Dürr 	SalLayout::AdjustLayout( rArgs );
1192822fc04SHerbert Dürr 	mnCharCount = mnEndCharPos - mnMinCharPos;
1202822fc04SHerbert Dürr 
1212822fc04SHerbert Dürr 	// short circuit if there is nothing to do
1222822fc04SHerbert Dürr 	if( mnCharCount <= 0 )
1232822fc04SHerbert Dürr 		return false;
1242822fc04SHerbert Dürr 
1252822fc04SHerbert Dürr 	// create the CoreText line layout
1262822fc04SHerbert Dürr 	CFStringRef aCFText = CFStringCreateWithCharactersNoCopy( NULL, rArgs.mpStr + mnMinCharPos, mnCharCount, kCFAllocatorNull );
1272822fc04SHerbert Dürr 	mpAttrString = CFAttributedStringCreate( NULL, aCFText, mpTextStyle->GetStyleDict() );
1282822fc04SHerbert Dürr 	mpCTLine = CTLineCreateWithAttributedString( mpAttrString );
1292822fc04SHerbert Dürr 	CFRelease( aCFText);
1302822fc04SHerbert Dürr 
1312822fc04SHerbert Dürr 	// get info about trailing whitespace to prepare for text justification in AdjustLayout()
1322822fc04SHerbert Dürr 	mnTrailingSpaces = 0;
1332822fc04SHerbert Dürr 	for( int i = mnEndCharPos; --i >= mnMinCharPos; ++mnTrailingSpaces )
1342822fc04SHerbert Dürr 		if( !IsSpacingGlyph( rArgs.mpStr[i] | GF_ISCHAR ))
1352822fc04SHerbert Dürr 			break;
1362822fc04SHerbert Dürr 	return true;
1372822fc04SHerbert Dürr }
1382822fc04SHerbert Dürr 
1392822fc04SHerbert Dürr // -----------------------------------------------------------------------
1402822fc04SHerbert Dürr 
1412822fc04SHerbert Dürr void CTLayout::AdjustLayout( ImplLayoutArgs& rArgs )
1422822fc04SHerbert Dürr {
1432822fc04SHerbert Dürr 	if( !mpCTLine)
1442822fc04SHerbert Dürr 		return;
1452822fc04SHerbert Dürr 
1462822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
1472822fc04SHerbert Dürr 
1482822fc04SHerbert Dürr 	int nPixelWidth = rArgs.mnLayoutWidth;
149*bca19674SHerbert Dürr 	if( rArgs.mpDXArray )
1502822fc04SHerbert Dürr 	{
1512822fc04SHerbert Dürr 		// for now we are only interested in the layout width
1522822fc04SHerbert Dürr 		// TODO: use all mpDXArray elements for layouting
153*bca19674SHerbert Dürr 		nPixelWidth = rArgs.mpDXArray[ mnCharCount-1 ];
1542822fc04SHerbert Dürr 	}
1552822fc04SHerbert Dürr 
1563e4cd16fSHerbert Dürr 	// short-circuit when justifying an all-whitespace string
1573e4cd16fSHerbert Dürr 	if( mnTrailingSpaces >= mnCharCount)
1583e4cd16fSHerbert Dürr 	{
159*bca19674SHerbert Dürr 		mfCachedWidth = nPixelWidth / mfFontScale;
1603e4cd16fSHerbert Dürr 		return;
1613e4cd16fSHerbert Dürr 	}
1623e4cd16fSHerbert Dürr 
1632822fc04SHerbert Dürr 	// return early if there is nothing to do
1642822fc04SHerbert Dürr 	if( nPixelWidth <= 0 )
1652822fc04SHerbert Dürr 		return;
1662822fc04SHerbert Dürr 
1672822fc04SHerbert Dürr 	// HACK: justification requests which change the width by just one pixel are probably
1682822fc04SHerbert Dürr 	// #i86038# introduced by lossy conversions between integer based coordinate system
169*bca19674SHerbert Dürr 	const int nOrigWidth = GetTextWidth();
1702822fc04SHerbert Dürr 	if( (nOrigWidth >= nPixelWidth-1) && (nOrigWidth <= nPixelWidth+1) )
1712822fc04SHerbert Dürr 		return;
1722822fc04SHerbert Dürr 
1732822fc04SHerbert Dürr 	// if the text to be justified has whitespace in it then
1742822fc04SHerbert Dürr 	// - Writer goes crazy with its HalfSpace magic
175*bca19674SHerbert Dürr 	// - CoreText handles spaces specially (in particular at the text end)
1762822fc04SHerbert Dürr 	if( mnTrailingSpaces ) {
177*bca19674SHerbert Dürr 		int nTrailingSpaceWidth = 0;
178*bca19674SHerbert Dürr 		if( rArgs.mpDXArray) {
179*bca19674SHerbert Dürr 			const int nFullPixWidth = nPixelWidth;
180*bca19674SHerbert Dürr 			nPixelWidth = rArgs.mpDXArray[ mnCharCount-1-mnTrailingSpaces ];
181*bca19674SHerbert Dürr 			nTrailingSpaceWidth = nFullPixWidth - nPixelWidth;
182*bca19674SHerbert Dürr 		} else {
183*bca19674SHerbert Dürr 			const double fTrailingSpaceWidth = rCT.LineGetTrailingWhitespaceWidth( mpCTLine );
184*bca19674SHerbert Dürr 			nTrailingSpaceWidth = rint(fTrailingSpaceWidth);
185*bca19674SHerbert Dürr 		}
186*bca19674SHerbert Dürr 		nPixelWidth -= nTrailingSpaceWidth;
187*bca19674SHerbert Dürr 		if( nPixelWidth <= 0 )
1882822fc04SHerbert Dürr 			return;
189*bca19674SHerbert Dürr 
1902822fc04SHerbert Dürr 		// recreate the CoreText line layout without trailing spaces
1912822fc04SHerbert Dürr 		CFRelease( mpCTLine );
1922822fc04SHerbert Dürr 		CFStringRef aCFText = CFStringCreateWithCharactersNoCopy( NULL, rArgs.mpStr + mnMinCharPos,
1932822fc04SHerbert Dürr 			mnCharCount - mnTrailingSpaces, kCFAllocatorNull );
1942822fc04SHerbert Dürr 		CFAttributedStringRef pAttrStr = CFAttributedStringCreate( NULL, aCFText, mpTextStyle->GetStyleDict() );
1952822fc04SHerbert Dürr 		mpCTLine = CTLineCreateWithAttributedString( pAttrStr );
1962822fc04SHerbert Dürr 		CFRelease( aCFText);
1972822fc04SHerbert Dürr 		CFRelease( pAttrStr );
198*bca19674SHerbert Dürr 
199*bca19674SHerbert Dürr 		// in RTL-layouts trailing spaces are leftmost
200*bca19674SHerbert Dürr 		// TODO: use BiDi-algorithm to thoroughly check this assumption
201*bca19674SHerbert Dürr 		if( rArgs.mnFlags & SAL_LAYOUT_BIDI_RTL)
202*bca19674SHerbert Dürr 			mnBaseAdv = nTrailingSpaceWidth;
2032822fc04SHerbert Dürr 	}
2042822fc04SHerbert Dürr 
205*bca19674SHerbert Dürr 	const double fAdjustedWidth = nPixelWidth / mfFontScale;
206*bca19674SHerbert Dürr 	CTLineRef pNewCTLine = rCT.LineCreateJustifiedLine( mpCTLine, 1.0, fAdjustedWidth );
2072822fc04SHerbert Dürr 	if( !pNewCTLine ) { // CTLineCreateJustifiedLine can and does fail
2082822fc04SHerbert Dürr 		// handle failure by keeping the unjustified layout
2092822fc04SHerbert Dürr 		// TODO: a better solution such as
2102822fc04SHerbert Dürr 		// - forcing glyph overlap
2112822fc04SHerbert Dürr 		// - changing the font size
2122822fc04SHerbert Dürr 		// - changing the CTM matrix
2132822fc04SHerbert Dürr 		return;
2142822fc04SHerbert Dürr 	}
2152822fc04SHerbert Dürr 	CFRelease( mpCTLine );
2162822fc04SHerbert Dürr 	mpCTLine = pNewCTLine;
217*bca19674SHerbert Dürr 	mfCachedWidth = fAdjustedWidth;
2182822fc04SHerbert Dürr }
2192822fc04SHerbert Dürr 
2202822fc04SHerbert Dürr // -----------------------------------------------------------------------
2212822fc04SHerbert Dürr 
2222822fc04SHerbert Dürr void CTLayout::DrawText( SalGraphics& rGraphics ) const
2232822fc04SHerbert Dürr {
2242822fc04SHerbert Dürr 	AquaSalGraphics& rAquaGraphics = static_cast<AquaSalGraphics&>(rGraphics);
2252822fc04SHerbert Dürr 
2262822fc04SHerbert Dürr 	// short circuit if there is nothing to do
2272822fc04SHerbert Dürr 	if( (mnCharCount <= 0)
2282822fc04SHerbert Dürr 	||  !rAquaGraphics.CheckContext() )
2292822fc04SHerbert Dürr 		return;
2302822fc04SHerbert Dürr 
2312822fc04SHerbert Dürr 	// the view is vertically flipped => flipped glyphs
2322822fc04SHerbert Dürr 	// so apply a temporary transformation that it flips back
2332822fc04SHerbert Dürr 	// also compensate if the font was size limited
2342822fc04SHerbert Dürr 	CGContextSaveGState( rAquaGraphics.mrContext );
2352822fc04SHerbert Dürr 	CGContextScaleCTM( rAquaGraphics.mrContext, +mfFontScale, -mfFontScale );
2362822fc04SHerbert Dürr 	CGContextSetShouldAntialias( rAquaGraphics.mrContext, !rAquaGraphics.mbNonAntialiasedText );
2372822fc04SHerbert Dürr 
2382822fc04SHerbert Dürr 	// Draw the text
2392822fc04SHerbert Dürr 	const Point aVclPos = GetDrawPosition( Point(mnBaseAdv,0) );
2402822fc04SHerbert Dürr 	CGPoint aTextPos = { +aVclPos.X()/mfFontScale, -aVclPos.Y()/mfFontScale };
2412822fc04SHerbert Dürr 
2422822fc04SHerbert Dürr 	if( mpTextStyle->mfFontRotation != 0.0 )
2432822fc04SHerbert Dürr 	{
2442822fc04SHerbert Dürr 		const CGFloat fRadians = mpTextStyle->mfFontRotation;
2452822fc04SHerbert Dürr 		CGContextRotateCTM( rAquaGraphics.mrContext, +fRadians );
2462822fc04SHerbert Dürr 
2472822fc04SHerbert Dürr 		const CGAffineTransform aInvMatrix = CGAffineTransformMakeRotation( -fRadians );
2482822fc04SHerbert Dürr 		aTextPos = CGPointApplyAffineTransform( aTextPos, aInvMatrix );
2492822fc04SHerbert Dürr 	}
2502822fc04SHerbert Dürr 
2512822fc04SHerbert Dürr 	CGContextSetTextPosition( rAquaGraphics.mrContext, aTextPos.x, aTextPos.y );
2522822fc04SHerbert Dürr 	CTLineDraw( mpCTLine, rAquaGraphics.mrContext );
2532822fc04SHerbert Dürr 
2542822fc04SHerbert Dürr 	// request an update of the changed window area
2552822fc04SHerbert Dürr 	if( rAquaGraphics.IsWindowGraphics() )
2562822fc04SHerbert Dürr 	{
2572822fc04SHerbert Dürr 		const CGRect aInkRect = CTLineGetImageBounds( mpCTLine, rAquaGraphics.mrContext );
2582822fc04SHerbert Dürr 		const CGRect aRefreshRect = CGContextConvertRectToDeviceSpace( rAquaGraphics.mrContext, aInkRect );
2592822fc04SHerbert Dürr 		rAquaGraphics.RefreshRect( aRefreshRect );
2602822fc04SHerbert Dürr 	}
2612822fc04SHerbert Dürr 
2622822fc04SHerbert Dürr 	// restore the original graphic context transformations
2632822fc04SHerbert Dürr 	CGContextRestoreGState( rAquaGraphics.mrContext );
2642822fc04SHerbert Dürr }
2652822fc04SHerbert Dürr 
2662822fc04SHerbert Dürr // -----------------------------------------------------------------------
2672822fc04SHerbert Dürr 
2682822fc04SHerbert Dürr int CTLayout::GetNextGlyphs( int nLen, sal_GlyphId* pOutGlyphIds, Point& rPos, int& nStart,
2692822fc04SHerbert Dürr 	sal_Int32* pGlyphAdvances, int* pCharIndexes ) const
2702822fc04SHerbert Dürr {
2712822fc04SHerbert Dürr 	if( !mpCTLine )
2722822fc04SHerbert Dürr 		return 0;
2732822fc04SHerbert Dürr 
2742822fc04SHerbert Dürr 	if( nStart < 0 ) // first glyph requested?
2752822fc04SHerbert Dürr 		nStart = 0;
2762822fc04SHerbert Dürr 	nLen = 1; // TODO: handle nLen>1 below
2772822fc04SHerbert Dürr 
2782822fc04SHerbert Dürr 	// prepare to iterate over the glyph runs
2792822fc04SHerbert Dürr 	int nCount = 0;
2802822fc04SHerbert Dürr 	int nSubIndex = nStart;
2812822fc04SHerbert Dürr 
2822822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
2832822fc04SHerbert Dürr 	typedef std::vector<CGGlyph> CGGlyphVector;
2842822fc04SHerbert Dürr 	typedef std::vector<CGPoint> CGPointVector;
2852822fc04SHerbert Dürr 	typedef std::vector<CGSize>  CGSizeVector;
2862822fc04SHerbert Dürr 	typedef std::vector<CFIndex> CFIndexVector;
2872822fc04SHerbert Dürr 	CGGlyphVector aCGGlyphVec;
2882822fc04SHerbert Dürr 	CGPointVector aCGPointVec;
2892822fc04SHerbert Dürr 	CGSizeVector  aCGSizeVec;
2902822fc04SHerbert Dürr 	CFIndexVector aCFIndexVec;
2912822fc04SHerbert Dürr 
2922822fc04SHerbert Dürr 	// TODO: iterate over cached layout
2932822fc04SHerbert Dürr 	CFArrayRef aGlyphRuns = rCT.LineGetGlyphRuns( mpCTLine );
2942822fc04SHerbert Dürr 	const int nRunCount = CFArrayGetCount( aGlyphRuns );
2952822fc04SHerbert Dürr 	for( int nRunIndex = 0; nRunIndex < nRunCount; ++nRunIndex ) {
2962822fc04SHerbert Dürr 		CTRunRef pGlyphRun = (CTRunRef)CFArrayGetValueAtIndex( aGlyphRuns, nRunIndex );
2972822fc04SHerbert Dürr 		const CFIndex nGlyphsInRun = rCT.RunGetGlyphCount( pGlyphRun );
2982822fc04SHerbert Dürr 		// skip to the first glyph run of interest
2992822fc04SHerbert Dürr 		if( nSubIndex >= nGlyphsInRun ) {
3002822fc04SHerbert Dürr 			nSubIndex -= nGlyphsInRun;
3012822fc04SHerbert Dürr 			continue;
3022822fc04SHerbert Dürr 		}
3032822fc04SHerbert Dürr 		const CFRange aFullRange = CFRangeMake( 0, nGlyphsInRun );
3042822fc04SHerbert Dürr 
3052822fc04SHerbert Dürr 		// get glyph run details
3062822fc04SHerbert Dürr 		const CGGlyph* pCGGlyphIdx = rCT.RunGetGlyphsPtr( pGlyphRun );
3072822fc04SHerbert Dürr 		if( !pCGGlyphIdx ) {
3082822fc04SHerbert Dürr 			aCGGlyphVec.reserve( nGlyphsInRun );
3092822fc04SHerbert Dürr 			CTRunGetGlyphs( pGlyphRun, aFullRange, &aCGGlyphVec[0] );
3102822fc04SHerbert Dürr 			pCGGlyphIdx = &aCGGlyphVec[0];
3112822fc04SHerbert Dürr 		}
3122822fc04SHerbert Dürr 		const CGPoint* pCGGlyphPos = rCT.RunGetPositionsPtr( pGlyphRun );
3132822fc04SHerbert Dürr 		if( !pCGGlyphPos ) {
3142822fc04SHerbert Dürr 			aCGPointVec.reserve( nGlyphsInRun );
3152822fc04SHerbert Dürr 			CTRunGetPositions( pGlyphRun, aFullRange, &aCGPointVec[0] );
3162822fc04SHerbert Dürr 			pCGGlyphPos = &aCGPointVec[0];
3172822fc04SHerbert Dürr 		}
3182822fc04SHerbert Dürr 
3192822fc04SHerbert Dürr 		const CGSize* pCGGlyphAdvs = NULL;
3202822fc04SHerbert Dürr 		if( pGlyphAdvances) {
3212822fc04SHerbert Dürr 			pCGGlyphAdvs = rCT.RunGetAdvancesPtr( pGlyphRun );
3222822fc04SHerbert Dürr 			if( !pCGGlyphAdvs) {
3232822fc04SHerbert Dürr 				aCGSizeVec.reserve( nGlyphsInRun );
3242822fc04SHerbert Dürr 				CTRunGetAdvances( pGlyphRun, aFullRange, &aCGSizeVec[0] );
3252822fc04SHerbert Dürr 				pCGGlyphAdvs = &aCGSizeVec[0];
3262822fc04SHerbert Dürr 			}
3272822fc04SHerbert Dürr 		}
3282822fc04SHerbert Dürr 
3292822fc04SHerbert Dürr 		const CFIndex* pCGGlyphStrIdx = NULL;
3302822fc04SHerbert Dürr 		if( pCharIndexes) {
3312822fc04SHerbert Dürr 			pCGGlyphStrIdx = rCT.RunGetStringIndicesPtr( pGlyphRun );
3322822fc04SHerbert Dürr 			if( !pCGGlyphStrIdx) {
3332822fc04SHerbert Dürr 				aCFIndexVec.reserve( nGlyphsInRun );
3342822fc04SHerbert Dürr 				CTRunGetStringIndices( pGlyphRun, aFullRange, &aCFIndexVec[0] );
3352822fc04SHerbert Dürr 				pCGGlyphStrIdx = &aCFIndexVec[0];
3362822fc04SHerbert Dürr 			}
3372822fc04SHerbert Dürr 		}
3382822fc04SHerbert Dürr 
3392822fc04SHerbert Dürr 		// get the details for each interesting glyph
3402822fc04SHerbert Dürr 		// TODO: handle nLen>1
3412822fc04SHerbert Dürr 		for(; (--nLen >= 0) && (nSubIndex < nGlyphsInRun); ++nSubIndex, ++nStart )
3422822fc04SHerbert Dürr 		{
3432822fc04SHerbert Dürr 			// convert glyph details for VCL
3442822fc04SHerbert Dürr 			*(pOutGlyphIds++) = pCGGlyphIdx[ nSubIndex ];
3452822fc04SHerbert Dürr 			if( pGlyphAdvances )
3462822fc04SHerbert Dürr 				*(pGlyphAdvances++) = pCGGlyphAdvs[ nSubIndex ].width;
3472822fc04SHerbert Dürr 			if( pCharIndexes )
3482822fc04SHerbert Dürr 				*(pCharIndexes++) = pCGGlyphStrIdx[ nSubIndex] + mnMinCharPos;
3492822fc04SHerbert Dürr 			if( !nCount++ ) {
3502822fc04SHerbert Dürr 				const CGPoint& rCurPos = pCGGlyphPos[ nSubIndex ];
3512822fc04SHerbert Dürr 				rPos = GetDrawPosition( Point( mfFontScale * rCurPos.x, mfFontScale * rCurPos.y) );
3522822fc04SHerbert Dürr 			}
3532822fc04SHerbert Dürr 		}
3542822fc04SHerbert Dürr 		nSubIndex = 0; // prepare for the next glyph run
3552822fc04SHerbert Dürr 		break; // TODO: handle nLen>1
3562822fc04SHerbert Dürr 	}
3572822fc04SHerbert Dürr 
3582822fc04SHerbert Dürr 	return nCount;
3592822fc04SHerbert Dürr }
3602822fc04SHerbert Dürr 
3612822fc04SHerbert Dürr // -----------------------------------------------------------------------
3622822fc04SHerbert Dürr 
3632822fc04SHerbert Dürr long CTLayout::GetTextWidth() const
3642822fc04SHerbert Dürr {
3652822fc04SHerbert Dürr 	if( (mnCharCount <= 0) || !mpCTLine )
3662822fc04SHerbert Dürr 		return 0;
3672822fc04SHerbert Dürr 
3682822fc04SHerbert Dürr 	if( mfCachedWidth < 0.0 )
3692822fc04SHerbert Dürr 		mfCachedWidth = CTLineGetTypographicBounds( mpCTLine, NULL, NULL, NULL );
3702822fc04SHerbert Dürr 
3712822fc04SHerbert Dürr 	const long nScaledWidth = lrint( mfFontScale * mfCachedWidth );
3722822fc04SHerbert Dürr 	return nScaledWidth;
3732822fc04SHerbert Dürr }
3742822fc04SHerbert Dürr 
3752822fc04SHerbert Dürr // -----------------------------------------------------------------------
3762822fc04SHerbert Dürr 
3772822fc04SHerbert Dürr long CTLayout::FillDXArray( sal_Int32* pDXArray ) const
3782822fc04SHerbert Dürr {
3792822fc04SHerbert Dürr 	// short circuit requests which don't need full details
3802822fc04SHerbert Dürr 	if( !pDXArray )
3812822fc04SHerbert Dürr 		return GetTextWidth();
3822822fc04SHerbert Dürr 
3832822fc04SHerbert Dürr 	long nPixWidth = GetTextWidth();
3842822fc04SHerbert Dürr 	if( pDXArray ) {
3852822fc04SHerbert Dürr 		// initialize the result array
3862822fc04SHerbert Dürr 		for( int i = 0; i < mnCharCount; ++i)
3872822fc04SHerbert Dürr 			pDXArray[i] = 0;
3882822fc04SHerbert Dürr 		// handle each glyph run
3892822fc04SHerbert Dürr 		CFArrayRef aGlyphRuns = CTLineGetGlyphRuns( mpCTLine );
3902822fc04SHerbert Dürr 		const int nRunCount = CFArrayGetCount( aGlyphRuns );
3912822fc04SHerbert Dürr 		typedef std::vector<CGSize> CGSizeVector;
3922822fc04SHerbert Dürr 		CGSizeVector aSizeVec;
3932822fc04SHerbert Dürr 		typedef std::vector<CFIndex> CFIndexVector;
3942822fc04SHerbert Dürr 		CFIndexVector aIndexVec;
3952822fc04SHerbert Dürr 		for( int nRunIndex = 0; nRunIndex < nRunCount; ++nRunIndex ) {
3962822fc04SHerbert Dürr 			CTRunRef pGlyphRun = (CTRunRef)CFArrayGetValueAtIndex( aGlyphRuns, nRunIndex );
3972822fc04SHerbert Dürr 			const CFIndex nGlyphCount = CTRunGetGlyphCount( pGlyphRun );
3982822fc04SHerbert Dürr 			const CFRange aFullRange = CFRangeMake( 0, nGlyphCount );
399*bca19674SHerbert Dürr 			aSizeVec.resize( nGlyphCount );
400*bca19674SHerbert Dürr 			aIndexVec.resize( nGlyphCount );
4012822fc04SHerbert Dürr 			CTRunGetAdvances( pGlyphRun, aFullRange, &aSizeVec[0] );
4022822fc04SHerbert Dürr 			CTRunGetStringIndices( pGlyphRun, aFullRange, &aIndexVec[0] );
4032822fc04SHerbert Dürr 			for( int i = 0; i != nGlyphCount; ++i ) {
4042822fc04SHerbert Dürr 				const int nRelIdx = aIndexVec[i];
4052822fc04SHerbert Dürr 				pDXArray[ nRelIdx ] += aSizeVec[i].width;
4062822fc04SHerbert Dürr 			}
4072822fc04SHerbert Dürr 		}
4082822fc04SHerbert Dürr 	}
4092822fc04SHerbert Dürr 
4102822fc04SHerbert Dürr 	return nPixWidth;
4112822fc04SHerbert Dürr }
4122822fc04SHerbert Dürr 
4132822fc04SHerbert Dürr // -----------------------------------------------------------------------
4142822fc04SHerbert Dürr 
4152822fc04SHerbert Dürr int CTLayout::GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const
4162822fc04SHerbert Dürr {
4172822fc04SHerbert Dürr 	if( !mpCTLine )
4182822fc04SHerbert Dürr 		return STRING_LEN;
4192822fc04SHerbert Dürr 
4202822fc04SHerbert Dürr 	CTTypesetterRef aCTTypeSetter = CTTypesetterCreateWithAttributedString( mpAttrString );
4212822fc04SHerbert Dürr 	const double fCTMaxWidth = (double)nMaxWidth / (nFactor * mfFontScale);
4222822fc04SHerbert Dürr 	CFIndex nIndex = CTTypesetterSuggestClusterBreak( aCTTypeSetter, 0, fCTMaxWidth );
4232822fc04SHerbert Dürr 	if( nIndex >= mnCharCount )
4242822fc04SHerbert Dürr 		return STRING_LEN;
4252822fc04SHerbert Dürr 
4262822fc04SHerbert Dürr 	nIndex += mnMinCharPos;
4272822fc04SHerbert Dürr 	return (int)nIndex;
4282822fc04SHerbert Dürr }
4292822fc04SHerbert Dürr 
4302822fc04SHerbert Dürr // -----------------------------------------------------------------------
4312822fc04SHerbert Dürr 
4322822fc04SHerbert Dürr void CTLayout::GetCaretPositions( int nMaxIndex, sal_Int32* pCaretXArray ) const
4332822fc04SHerbert Dürr {
4342822fc04SHerbert Dürr 	DBG_ASSERT( ((nMaxIndex>0)&&!(nMaxIndex&1)),
4352822fc04SHerbert Dürr 		"CTLayout::GetCaretPositions() : invalid number of caret pairs requested");
4362822fc04SHerbert Dürr 
4372822fc04SHerbert Dürr 	// initialize the caret positions
4382822fc04SHerbert Dürr 	for( int i = 0; i < nMaxIndex; ++i )
4392822fc04SHerbert Dürr 		pCaretXArray[ i ] = -1;
4402822fc04SHerbert Dürr 
4412822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
4422822fc04SHerbert Dürr 	for( int n = 0; n <= mnCharCount; ++n )
4432822fc04SHerbert Dürr 	{
4442822fc04SHerbert Dürr 		// measure the characters cursor position
4452822fc04SHerbert Dürr 		CGFloat fPos2 = -1;
4462822fc04SHerbert Dürr 		const CGFloat fPos1 = rCT.LineGetOffsetForStringIndex( mpCTLine, n, &fPos2 );
4472822fc04SHerbert Dürr 		(void)fPos2; // TODO: split cursor at line direction change
4482822fc04SHerbert Dürr 		// update previous trailing position
4492822fc04SHerbert Dürr 		if( n > 0 )
4502822fc04SHerbert Dürr 			pCaretXArray[ 2*n-1 ] = lrint( fPos1 * mfFontScale );
4512822fc04SHerbert Dürr 		// update current leading position
4522822fc04SHerbert Dürr 		if( 2*n >= nMaxIndex )
4532822fc04SHerbert Dürr 			break;
4542822fc04SHerbert Dürr 		pCaretXArray[ 2*n+0 ] = lrint( fPos1 * mfFontScale );
4552822fc04SHerbert Dürr 	}
4562822fc04SHerbert Dürr }
4572822fc04SHerbert Dürr 
4582822fc04SHerbert Dürr // -----------------------------------------------------------------------
4592822fc04SHerbert Dürr 
4602822fc04SHerbert Dürr bool CTLayout::GetBoundRect( SalGraphics& rGraphics, Rectangle& rVCLRect ) const
4612822fc04SHerbert Dürr {
4622822fc04SHerbert Dürr 	AquaSalGraphics& rAquaGraphics = static_cast<AquaSalGraphics&>(rGraphics);
4632822fc04SHerbert Dürr 	CGRect aMacRect = CTLineGetImageBounds( mpCTLine, rAquaGraphics.mrContext );
4642822fc04SHerbert Dürr 	CGPoint aMacPos = CGContextGetTextPosition( rAquaGraphics.mrContext );
4652822fc04SHerbert Dürr 	aMacRect.origin.x -= aMacPos.x;
4662822fc04SHerbert Dürr 	aMacRect.origin.y -= aMacPos.y;
4672822fc04SHerbert Dürr 
4682822fc04SHerbert Dürr 	const Point aPos = GetDrawPosition( Point(mnBaseAdv, 0) );
4692822fc04SHerbert Dürr 
4702822fc04SHerbert Dürr 	// CoreText top-bottom are vertically flipped from a VCL aspect
4712822fc04SHerbert Dürr 	rVCLRect.Left()   = aPos.X() + mfFontScale * aMacRect.origin.x;
4722822fc04SHerbert Dürr 	rVCLRect.Right()  = aPos.X() + mfFontScale * (aMacRect.origin.x + aMacRect.size.width);
4732822fc04SHerbert Dürr 	rVCLRect.Bottom() = aPos.Y() - mfFontScale * aMacRect.origin.y;
4742822fc04SHerbert Dürr 	rVCLRect.Top()    = aPos.Y() - mfFontScale * (aMacRect.origin.y + aMacRect.size.height);
4752822fc04SHerbert Dürr 	return true;
4762822fc04SHerbert Dürr }
4772822fc04SHerbert Dürr 
4782822fc04SHerbert Dürr // =======================================================================
4792822fc04SHerbert Dürr 
4802822fc04SHerbert Dürr // glyph fallback is supported directly by Aqua
4812822fc04SHerbert Dürr // so methods used only by MultiSalLayout can be dummy implementated
4822822fc04SHerbert Dürr bool CTLayout::GetGlyphOutlines( SalGraphics&, PolyPolyVector& ) const { return false; }
4832822fc04SHerbert Dürr void CTLayout::InitFont() const {}
4842822fc04SHerbert Dürr void CTLayout::MoveGlyph( int /*nStart*/, long /*nNewXPos*/ ) {}
4852822fc04SHerbert Dürr void CTLayout::DropGlyph( int /*nStart*/ ) {}
4862822fc04SHerbert Dürr void CTLayout::Simplify( bool /*bIsBase*/ ) {}
4872822fc04SHerbert Dürr 
4882822fc04SHerbert Dürr // get the ImplFontData for a glyph fallback font
4892822fc04SHerbert Dürr // for a glyphid that was returned by CTLayout::GetNextGlyphs()
4902822fc04SHerbert Dürr const ImplFontData* CTLayout::GetFallbackFontData( sal_GlyphId /*aGlyphId*/ ) const
4912822fc04SHerbert Dürr {
4922822fc04SHerbert Dürr #if 0
4932822fc04SHerbert Dürr 	// check if any fallback fonts were needed
4942822fc04SHerbert Dürr 	if( !mpFallbackInfo )
4952822fc04SHerbert Dürr 		return NULL;
4962822fc04SHerbert Dürr 	// check if the current glyph needs a fallback font
4972822fc04SHerbert Dürr 	int nFallbackLevel = (aGlyphId & GF_FONTMASK) >> GF_FONTSHIFT;
4982822fc04SHerbert Dürr 	if( !nFallbackLevel )
4992822fc04SHerbert Dürr 		return NULL;
5002822fc04SHerbert Dürr 	pFallbackFont = mpFallbackInfo->GetFallbackFontData( nFallbackLevel );
5012822fc04SHerbert Dürr #else
5022822fc04SHerbert Dürr 	// let CoreText's font cascading handle glyph fallback
5032822fc04SHerbert Dürr 	const ImplFontData* pFallbackFont = NULL;
5042822fc04SHerbert Dürr #endif
5052822fc04SHerbert Dürr 	return pFallbackFont;
5062822fc04SHerbert Dürr }
5072822fc04SHerbert Dürr 
5082822fc04SHerbert Dürr // =======================================================================
5092822fc04SHerbert Dürr 
5102822fc04SHerbert Dürr SalLayout* CTTextStyle::GetTextLayout( void ) const
5112822fc04SHerbert Dürr {
5122822fc04SHerbert Dürr 	return new CTLayout( this);
5132822fc04SHerbert Dürr }
5142822fc04SHerbert Dürr 
5152822fc04SHerbert Dürr // =======================================================================
5162822fc04SHerbert Dürr 
517