xref: /aoo41x/main/vcl/aqua/source/gdi/ctlayout.cxx (revision 3e4cd16f)
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 	mutable double	mfTrailingSpaceWidth;   // in Pixels
762822fc04SHerbert Dürr 
772822fc04SHerbert Dürr 	// x-offset relative to layout origin
782822fc04SHerbert Dürr 	// currently only used in RTL-layouts
792822fc04SHerbert Dürr 	mutable long	mnBaseAdv;
802822fc04SHerbert Dürr };
812822fc04SHerbert Dürr 
822822fc04SHerbert Dürr // =======================================================================
832822fc04SHerbert Dürr 
842822fc04SHerbert Dürr CTLayout::CTLayout( const CTTextStyle* pTextStyle )
852822fc04SHerbert Dürr :	mpTextStyle( pTextStyle )
862822fc04SHerbert Dürr ,	mpAttrString( NULL )
872822fc04SHerbert Dürr ,	mpCTLine( NULL )
882822fc04SHerbert Dürr ,	mnCharCount( 0 )
892822fc04SHerbert Dürr ,	mnTrailingSpaces( 0 )
902822fc04SHerbert Dürr ,	mfFontScale( pTextStyle->mfFontScale )
912822fc04SHerbert Dürr ,	mfCachedWidth( -1 )
922822fc04SHerbert Dürr ,	mfTrailingSpaceWidth( 0 )
932822fc04SHerbert Dürr ,	mnBaseAdv( 0 )
942822fc04SHerbert Dürr {
952822fc04SHerbert Dürr 	CFRetain( mpTextStyle->GetStyleDict() );
962822fc04SHerbert Dürr }
972822fc04SHerbert Dürr 
982822fc04SHerbert Dürr // -----------------------------------------------------------------------
992822fc04SHerbert Dürr 
1002822fc04SHerbert Dürr CTLayout::~CTLayout()
1012822fc04SHerbert Dürr {
1022822fc04SHerbert Dürr 	if( mpCTLine )
1032822fc04SHerbert Dürr 		CFRelease( mpCTLine );
1042822fc04SHerbert Dürr 	if( mpAttrString )
1052822fc04SHerbert Dürr 		CFRelease( mpAttrString );
1062822fc04SHerbert Dürr 	CFRelease( mpTextStyle->GetStyleDict() );
1072822fc04SHerbert Dürr }
1082822fc04SHerbert Dürr 
1092822fc04SHerbert Dürr // -----------------------------------------------------------------------
1102822fc04SHerbert Dürr 
1112822fc04SHerbert Dürr bool CTLayout::LayoutText( ImplLayoutArgs& rArgs )
1122822fc04SHerbert Dürr {
1132822fc04SHerbert Dürr 	if( mpAttrString )
1142822fc04SHerbert Dürr 		CFRelease( mpAttrString );
1152822fc04SHerbert Dürr 	mpAttrString = NULL;
1162822fc04SHerbert Dürr 	if( mpCTLine )
1172822fc04SHerbert Dürr 		CFRelease( mpCTLine );
1182822fc04SHerbert Dürr 	mpCTLine = NULL;
1192822fc04SHerbert Dürr 
1202822fc04SHerbert Dürr 	SalLayout::AdjustLayout( rArgs );
1212822fc04SHerbert Dürr 	mnCharCount = mnEndCharPos - mnMinCharPos;
1222822fc04SHerbert Dürr 
1232822fc04SHerbert Dürr 	// short circuit if there is nothing to do
1242822fc04SHerbert Dürr 	if( mnCharCount <= 0 )
1252822fc04SHerbert Dürr 		return false;
1262822fc04SHerbert Dürr 
1272822fc04SHerbert Dürr 	// create the CoreText line layout
1282822fc04SHerbert Dürr 	CFStringRef aCFText = CFStringCreateWithCharactersNoCopy( NULL, rArgs.mpStr + mnMinCharPos, mnCharCount, kCFAllocatorNull );
1292822fc04SHerbert Dürr 	mpAttrString = CFAttributedStringCreate( NULL, aCFText, mpTextStyle->GetStyleDict() );
1302822fc04SHerbert Dürr 	mpCTLine = CTLineCreateWithAttributedString( mpAttrString );
1312822fc04SHerbert Dürr 	CFRelease( aCFText);
1322822fc04SHerbert Dürr 
1332822fc04SHerbert Dürr 	// get info about trailing whitespace to prepare for text justification in AdjustLayout()
1342822fc04SHerbert Dürr 	mnTrailingSpaces = 0;
1352822fc04SHerbert Dürr 	for( int i = mnEndCharPos; --i >= mnMinCharPos; ++mnTrailingSpaces )
1362822fc04SHerbert Dürr 		if( !IsSpacingGlyph( rArgs.mpStr[i] | GF_ISCHAR ))
1372822fc04SHerbert Dürr 			break;
1382822fc04SHerbert Dürr 	return true;
1392822fc04SHerbert Dürr }
1402822fc04SHerbert Dürr 
1412822fc04SHerbert Dürr // -----------------------------------------------------------------------
1422822fc04SHerbert Dürr 
1432822fc04SHerbert Dürr void CTLayout::AdjustLayout( ImplLayoutArgs& rArgs )
1442822fc04SHerbert Dürr {
1452822fc04SHerbert Dürr 	if( !mpCTLine)
1462822fc04SHerbert Dürr 		return;
1472822fc04SHerbert Dürr 
1482822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
1492822fc04SHerbert Dürr 	// CoreText fills trailing space during justification so we have to
1502822fc04SHerbert Dürr 	// take that into account when requesting CT to justify something
1512822fc04SHerbert Dürr 	mfTrailingSpaceWidth = rCT.LineGetTrailingWhitespaceWidth( mpCTLine );
1522822fc04SHerbert Dürr 	const int nTrailingSpaceWidth = rint( mfFontScale * mfTrailingSpaceWidth );
1532822fc04SHerbert Dürr 
1542822fc04SHerbert Dürr 	int nOrigWidth = GetTextWidth();
1552822fc04SHerbert Dürr 	int nPixelWidth = rArgs.mnLayoutWidth;
1562822fc04SHerbert Dürr 	if( nPixelWidth )
1572822fc04SHerbert Dürr 	{
1582822fc04SHerbert Dürr 		nPixelWidth -= nTrailingSpaceWidth;
1592822fc04SHerbert Dürr 		if( nPixelWidth <= 0)
1602822fc04SHerbert Dürr 			return;
1612822fc04SHerbert Dürr 	}
1622822fc04SHerbert Dürr 	else if( rArgs.mpDXArray )
1632822fc04SHerbert Dürr 	{
1642822fc04SHerbert Dürr 		// for now we are only interested in the layout width
1652822fc04SHerbert Dürr 		// TODO: use all mpDXArray elements for layouting
1662822fc04SHerbert Dürr 		nPixelWidth = rArgs.mpDXArray[ mnCharCount - 1 - mnTrailingSpaces ];
1672822fc04SHerbert Dürr 	}
1682822fc04SHerbert Dürr 
169*3e4cd16fSHerbert Dürr 	// short-circuit when justifying an all-whitespace string
170*3e4cd16fSHerbert Dürr 	if( mnTrailingSpaces >= mnCharCount)
171*3e4cd16fSHerbert Dürr 	{
172*3e4cd16fSHerbert Dürr 		mfCachedWidth = mfTrailingSpaceWidth = nPixelWidth / mfFontScale;
173*3e4cd16fSHerbert Dürr 		return;
174*3e4cd16fSHerbert Dürr 	}
175*3e4cd16fSHerbert Dürr 
1762822fc04SHerbert Dürr 	// in RTL-layouts trailing spaces are leftmost
1772822fc04SHerbert Dürr 	// TODO: use BiDi-algorithm to thoroughly check this assumption
1782822fc04SHerbert Dürr 	if( rArgs.mnFlags & SAL_LAYOUT_BIDI_RTL)
1792822fc04SHerbert Dürr 		mnBaseAdv = nTrailingSpaceWidth;
1802822fc04SHerbert Dürr 
1812822fc04SHerbert Dürr 	// return early if there is nothing to do
1822822fc04SHerbert Dürr 	if( nPixelWidth <= 0 )
1832822fc04SHerbert Dürr 		return;
1842822fc04SHerbert Dürr 
1852822fc04SHerbert Dürr 	// HACK: justification requests which change the width by just one pixel are probably
1862822fc04SHerbert Dürr 	// #i86038# introduced by lossy conversions between integer based coordinate system
1872822fc04SHerbert Dürr 	if( (nOrigWidth >= nPixelWidth-1) && (nOrigWidth <= nPixelWidth+1) )
1882822fc04SHerbert Dürr 		return;
1892822fc04SHerbert Dürr 
1902822fc04SHerbert Dürr 	// if the text to be justified has whitespace in it then
1912822fc04SHerbert Dürr 	// - Writer goes crazy with its HalfSpace magic
1922822fc04SHerbert Dürr 	// - LayoutEngine handles spaces specially (in particular at the text start or end)
1932822fc04SHerbert Dürr 	if( mnTrailingSpaces ) {
1942822fc04SHerbert Dürr 		// adjust for Writer's SwFntObj::DrawText() Halfspace magic at the text end
1952822fc04SHerbert Dürr 		std::vector<sal_Int32> aOrigDXAry;
1962822fc04SHerbert Dürr 		aOrigDXAry.resize( mnCharCount);
1972822fc04SHerbert Dürr 		FillDXArray( &aOrigDXAry[0] );
1982822fc04SHerbert Dürr 		int nLastCharSpace = rArgs.mpDXArray[ mnCharCount-1-mnTrailingSpaces ]
1992822fc04SHerbert Dürr 			- aOrigDXAry[ mnCharCount-1-mnTrailingSpaces ];
2002822fc04SHerbert Dürr 		nPixelWidth -= nLastCharSpace;
2012822fc04SHerbert Dürr 		if( nPixelWidth < 0 )
2022822fc04SHerbert Dürr 			return;
2032822fc04SHerbert Dürr 		// recreate the CoreText line layout without trailing spaces
2042822fc04SHerbert Dürr 		CFRelease( mpCTLine );
2052822fc04SHerbert Dürr 		CFStringRef aCFText = CFStringCreateWithCharactersNoCopy( NULL, rArgs.mpStr + mnMinCharPos,
2062822fc04SHerbert Dürr 			mnCharCount - mnTrailingSpaces, kCFAllocatorNull );
2072822fc04SHerbert Dürr 		CFAttributedStringRef pAttrStr = CFAttributedStringCreate( NULL, aCFText, mpTextStyle->GetStyleDict() );
2082822fc04SHerbert Dürr 		mpCTLine = CTLineCreateWithAttributedString( pAttrStr );
2092822fc04SHerbert Dürr 		CFRelease( aCFText);
2102822fc04SHerbert Dürr 		CFRelease( pAttrStr );
2112822fc04SHerbert Dürr 	}
2122822fc04SHerbert Dürr 
2132822fc04SHerbert Dürr 	CTLineRef pNewCTLine = rCT.LineCreateJustifiedLine( mpCTLine, 1.0, nPixelWidth / mfFontScale );
2142822fc04SHerbert Dürr 	if( !pNewCTLine ) { // CTLineCreateJustifiedLine can and does fail
2152822fc04SHerbert Dürr 		// handle failure by keeping the unjustified layout
2162822fc04SHerbert Dürr 		// TODO: a better solution such as
2172822fc04SHerbert Dürr 		// - forcing glyph overlap
2182822fc04SHerbert Dürr 		// - changing the font size
2192822fc04SHerbert Dürr 		// - changing the CTM matrix
2202822fc04SHerbert Dürr 		return;
2212822fc04SHerbert Dürr 	}
2222822fc04SHerbert Dürr 	CFRelease( mpCTLine );
2232822fc04SHerbert Dürr 	mpCTLine = pNewCTLine;
2242822fc04SHerbert Dürr 	mfCachedWidth = -1; // TODO: can we set it directly to target width we requested? For now we re-measure
2252822fc04SHerbert Dürr 	mfTrailingSpaceWidth = 0;
2262822fc04SHerbert Dürr }
2272822fc04SHerbert Dürr 
2282822fc04SHerbert Dürr // -----------------------------------------------------------------------
2292822fc04SHerbert Dürr 
2302822fc04SHerbert Dürr void CTLayout::DrawText( SalGraphics& rGraphics ) const
2312822fc04SHerbert Dürr {
2322822fc04SHerbert Dürr 	AquaSalGraphics& rAquaGraphics = static_cast<AquaSalGraphics&>(rGraphics);
2332822fc04SHerbert Dürr 
2342822fc04SHerbert Dürr 	// short circuit if there is nothing to do
2352822fc04SHerbert Dürr 	if( (mnCharCount <= 0)
2362822fc04SHerbert Dürr 	||  !rAquaGraphics.CheckContext() )
2372822fc04SHerbert Dürr 		return;
2382822fc04SHerbert Dürr 
2392822fc04SHerbert Dürr 	// the view is vertically flipped => flipped glyphs
2402822fc04SHerbert Dürr 	// so apply a temporary transformation that it flips back
2412822fc04SHerbert Dürr 	// also compensate if the font was size limited
2422822fc04SHerbert Dürr 	CGContextSaveGState( rAquaGraphics.mrContext );
2432822fc04SHerbert Dürr 	CGContextScaleCTM( rAquaGraphics.mrContext, +mfFontScale, -mfFontScale );
2442822fc04SHerbert Dürr 	CGContextSetShouldAntialias( rAquaGraphics.mrContext, !rAquaGraphics.mbNonAntialiasedText );
2452822fc04SHerbert Dürr 
2462822fc04SHerbert Dürr 	// Draw the text
2472822fc04SHerbert Dürr 	const Point aVclPos = GetDrawPosition( Point(mnBaseAdv,0) );
2482822fc04SHerbert Dürr 	CGPoint aTextPos = { +aVclPos.X()/mfFontScale, -aVclPos.Y()/mfFontScale };
2492822fc04SHerbert Dürr 
2502822fc04SHerbert Dürr 	if( mpTextStyle->mfFontRotation != 0.0 )
2512822fc04SHerbert Dürr 	{
2522822fc04SHerbert Dürr 		const CGFloat fRadians = mpTextStyle->mfFontRotation;
2532822fc04SHerbert Dürr 		CGContextRotateCTM( rAquaGraphics.mrContext, +fRadians );
2542822fc04SHerbert Dürr 
2552822fc04SHerbert Dürr 		const CGAffineTransform aInvMatrix = CGAffineTransformMakeRotation( -fRadians );
2562822fc04SHerbert Dürr 		aTextPos = CGPointApplyAffineTransform( aTextPos, aInvMatrix );
2572822fc04SHerbert Dürr 	}
2582822fc04SHerbert Dürr 
2592822fc04SHerbert Dürr 	CGContextSetTextPosition( rAquaGraphics.mrContext, aTextPos.x, aTextPos.y );
2602822fc04SHerbert Dürr 	CTLineDraw( mpCTLine, rAquaGraphics.mrContext );
2612822fc04SHerbert Dürr 
2622822fc04SHerbert Dürr 	// request an update of the changed window area
2632822fc04SHerbert Dürr 	if( rAquaGraphics.IsWindowGraphics() )
2642822fc04SHerbert Dürr 	{
2652822fc04SHerbert Dürr 		const CGRect aInkRect = CTLineGetImageBounds( mpCTLine, rAquaGraphics.mrContext );
2662822fc04SHerbert Dürr 		const CGRect aRefreshRect = CGContextConvertRectToDeviceSpace( rAquaGraphics.mrContext, aInkRect );
2672822fc04SHerbert Dürr 		rAquaGraphics.RefreshRect( aRefreshRect );
2682822fc04SHerbert Dürr 	}
2692822fc04SHerbert Dürr 
2702822fc04SHerbert Dürr 	// restore the original graphic context transformations
2712822fc04SHerbert Dürr 	CGContextRestoreGState( rAquaGraphics.mrContext );
2722822fc04SHerbert Dürr }
2732822fc04SHerbert Dürr 
2742822fc04SHerbert Dürr // -----------------------------------------------------------------------
2752822fc04SHerbert Dürr 
2762822fc04SHerbert Dürr int CTLayout::GetNextGlyphs( int nLen, sal_GlyphId* pOutGlyphIds, Point& rPos, int& nStart,
2772822fc04SHerbert Dürr 	sal_Int32* pGlyphAdvances, int* pCharIndexes ) const
2782822fc04SHerbert Dürr {
2792822fc04SHerbert Dürr 	if( !mpCTLine )
2802822fc04SHerbert Dürr 		return 0;
2812822fc04SHerbert Dürr 
2822822fc04SHerbert Dürr 	if( nStart < 0 ) // first glyph requested?
2832822fc04SHerbert Dürr 		nStart = 0;
2842822fc04SHerbert Dürr 	nLen = 1; // TODO: handle nLen>1 below
2852822fc04SHerbert Dürr 
2862822fc04SHerbert Dürr 	// prepare to iterate over the glyph runs
2872822fc04SHerbert Dürr 	int nCount = 0;
2882822fc04SHerbert Dürr 	int nSubIndex = nStart;
2892822fc04SHerbert Dürr 
2902822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
2912822fc04SHerbert Dürr 	typedef std::vector<CGGlyph> CGGlyphVector;
2922822fc04SHerbert Dürr 	typedef std::vector<CGPoint> CGPointVector;
2932822fc04SHerbert Dürr 	typedef std::vector<CGSize>  CGSizeVector;
2942822fc04SHerbert Dürr 	typedef std::vector<CFIndex> CFIndexVector;
2952822fc04SHerbert Dürr 	CGGlyphVector aCGGlyphVec;
2962822fc04SHerbert Dürr 	CGPointVector aCGPointVec;
2972822fc04SHerbert Dürr 	CGSizeVector  aCGSizeVec;
2982822fc04SHerbert Dürr 	CFIndexVector aCFIndexVec;
2992822fc04SHerbert Dürr 
3002822fc04SHerbert Dürr 	// TODO: iterate over cached layout
3012822fc04SHerbert Dürr 	CFArrayRef aGlyphRuns = rCT.LineGetGlyphRuns( mpCTLine );
3022822fc04SHerbert Dürr 	const int nRunCount = CFArrayGetCount( aGlyphRuns );
3032822fc04SHerbert Dürr 	for( int nRunIndex = 0; nRunIndex < nRunCount; ++nRunIndex ) {
3042822fc04SHerbert Dürr 		CTRunRef pGlyphRun = (CTRunRef)CFArrayGetValueAtIndex( aGlyphRuns, nRunIndex );
3052822fc04SHerbert Dürr 		const CFIndex nGlyphsInRun = rCT.RunGetGlyphCount( pGlyphRun );
3062822fc04SHerbert Dürr 		// skip to the first glyph run of interest
3072822fc04SHerbert Dürr 		if( nSubIndex >= nGlyphsInRun ) {
3082822fc04SHerbert Dürr 			nSubIndex -= nGlyphsInRun;
3092822fc04SHerbert Dürr 			continue;
3102822fc04SHerbert Dürr 		}
3112822fc04SHerbert Dürr 		const CFRange aFullRange = CFRangeMake( 0, nGlyphsInRun );
3122822fc04SHerbert Dürr 
3132822fc04SHerbert Dürr 		// get glyph run details
3142822fc04SHerbert Dürr 		const CGGlyph* pCGGlyphIdx = rCT.RunGetGlyphsPtr( pGlyphRun );
3152822fc04SHerbert Dürr 		if( !pCGGlyphIdx ) {
3162822fc04SHerbert Dürr 			aCGGlyphVec.reserve( nGlyphsInRun );
3172822fc04SHerbert Dürr 			CTRunGetGlyphs( pGlyphRun, aFullRange, &aCGGlyphVec[0] );
3182822fc04SHerbert Dürr 			pCGGlyphIdx = &aCGGlyphVec[0];
3192822fc04SHerbert Dürr 		}
3202822fc04SHerbert Dürr 		const CGPoint* pCGGlyphPos = rCT.RunGetPositionsPtr( pGlyphRun );
3212822fc04SHerbert Dürr 		if( !pCGGlyphPos ) {
3222822fc04SHerbert Dürr 			aCGPointVec.reserve( nGlyphsInRun );
3232822fc04SHerbert Dürr 			CTRunGetPositions( pGlyphRun, aFullRange, &aCGPointVec[0] );
3242822fc04SHerbert Dürr 			pCGGlyphPos = &aCGPointVec[0];
3252822fc04SHerbert Dürr 		}
3262822fc04SHerbert Dürr 
3272822fc04SHerbert Dürr 		const CGSize* pCGGlyphAdvs = NULL;
3282822fc04SHerbert Dürr 		if( pGlyphAdvances) {
3292822fc04SHerbert Dürr 			pCGGlyphAdvs = rCT.RunGetAdvancesPtr( pGlyphRun );
3302822fc04SHerbert Dürr 			if( !pCGGlyphAdvs) {
3312822fc04SHerbert Dürr 				aCGSizeVec.reserve( nGlyphsInRun );
3322822fc04SHerbert Dürr 				CTRunGetAdvances( pGlyphRun, aFullRange, &aCGSizeVec[0] );
3332822fc04SHerbert Dürr 				pCGGlyphAdvs = &aCGSizeVec[0];
3342822fc04SHerbert Dürr 			}
3352822fc04SHerbert Dürr 		}
3362822fc04SHerbert Dürr 
3372822fc04SHerbert Dürr 		const CFIndex* pCGGlyphStrIdx = NULL;
3382822fc04SHerbert Dürr 		if( pCharIndexes) {
3392822fc04SHerbert Dürr 			pCGGlyphStrIdx = rCT.RunGetStringIndicesPtr( pGlyphRun );
3402822fc04SHerbert Dürr 			if( !pCGGlyphStrIdx) {
3412822fc04SHerbert Dürr 				aCFIndexVec.reserve( nGlyphsInRun );
3422822fc04SHerbert Dürr 				CTRunGetStringIndices( pGlyphRun, aFullRange, &aCFIndexVec[0] );
3432822fc04SHerbert Dürr 				pCGGlyphStrIdx = &aCFIndexVec[0];
3442822fc04SHerbert Dürr 			}
3452822fc04SHerbert Dürr 		}
3462822fc04SHerbert Dürr 
3472822fc04SHerbert Dürr 		// get the details for each interesting glyph
3482822fc04SHerbert Dürr 		// TODO: handle nLen>1
3492822fc04SHerbert Dürr 		for(; (--nLen >= 0) && (nSubIndex < nGlyphsInRun); ++nSubIndex, ++nStart )
3502822fc04SHerbert Dürr 		{
3512822fc04SHerbert Dürr 			// convert glyph details for VCL
3522822fc04SHerbert Dürr 			*(pOutGlyphIds++) = pCGGlyphIdx[ nSubIndex ];
3532822fc04SHerbert Dürr 			if( pGlyphAdvances )
3542822fc04SHerbert Dürr 				*(pGlyphAdvances++) = pCGGlyphAdvs[ nSubIndex ].width;
3552822fc04SHerbert Dürr 			if( pCharIndexes )
3562822fc04SHerbert Dürr 				*(pCharIndexes++) = pCGGlyphStrIdx[ nSubIndex] + mnMinCharPos;
3572822fc04SHerbert Dürr 			if( !nCount++ ) {
3582822fc04SHerbert Dürr 				const CGPoint& rCurPos = pCGGlyphPos[ nSubIndex ];
3592822fc04SHerbert Dürr 				rPos = GetDrawPosition( Point( mfFontScale * rCurPos.x, mfFontScale * rCurPos.y) );
3602822fc04SHerbert Dürr 			}
3612822fc04SHerbert Dürr 		}
3622822fc04SHerbert Dürr 		nSubIndex = 0; // prepare for the next glyph run
3632822fc04SHerbert Dürr 		break; // TODO: handle nLen>1
3642822fc04SHerbert Dürr 	}
3652822fc04SHerbert Dürr 
3662822fc04SHerbert Dürr 	return nCount;
3672822fc04SHerbert Dürr }
3682822fc04SHerbert Dürr 
3692822fc04SHerbert Dürr // -----------------------------------------------------------------------
3702822fc04SHerbert Dürr 
3712822fc04SHerbert Dürr long CTLayout::GetTextWidth() const
3722822fc04SHerbert Dürr {
3732822fc04SHerbert Dürr 	if( (mnCharCount <= 0) || !mpCTLine )
3742822fc04SHerbert Dürr 		return 0;
3752822fc04SHerbert Dürr 
3762822fc04SHerbert Dürr 	if( mfCachedWidth < 0.0 )
3772822fc04SHerbert Dürr 		mfCachedWidth = CTLineGetTypographicBounds( mpCTLine, NULL, NULL, NULL );
3782822fc04SHerbert Dürr 
3792822fc04SHerbert Dürr 	const long nScaledWidth = lrint( mfFontScale * mfCachedWidth );
3802822fc04SHerbert Dürr 	return nScaledWidth;
3812822fc04SHerbert Dürr }
3822822fc04SHerbert Dürr 
3832822fc04SHerbert Dürr // -----------------------------------------------------------------------
3842822fc04SHerbert Dürr 
3852822fc04SHerbert Dürr long CTLayout::FillDXArray( sal_Int32* pDXArray ) const
3862822fc04SHerbert Dürr {
3872822fc04SHerbert Dürr 	// short circuit requests which don't need full details
3882822fc04SHerbert Dürr 	if( !pDXArray )
3892822fc04SHerbert Dürr 		return GetTextWidth();
3902822fc04SHerbert Dürr 
3912822fc04SHerbert Dürr 	long nPixWidth = GetTextWidth();
3922822fc04SHerbert Dürr 	if( pDXArray ) {
3932822fc04SHerbert Dürr 		// initialize the result array
3942822fc04SHerbert Dürr 		for( int i = 0; i < mnCharCount; ++i)
3952822fc04SHerbert Dürr 			pDXArray[i] = 0;
3962822fc04SHerbert Dürr 		// handle each glyph run
3972822fc04SHerbert Dürr 		CFArrayRef aGlyphRuns = CTLineGetGlyphRuns( mpCTLine );
3982822fc04SHerbert Dürr 		const int nRunCount = CFArrayGetCount( aGlyphRuns );
3992822fc04SHerbert Dürr 		typedef std::vector<CGSize> CGSizeVector;
4002822fc04SHerbert Dürr 		CGSizeVector aSizeVec;
4012822fc04SHerbert Dürr 		typedef std::vector<CFIndex> CFIndexVector;
4022822fc04SHerbert Dürr 		CFIndexVector aIndexVec;
4032822fc04SHerbert Dürr 		for( int nRunIndex = 0; nRunIndex < nRunCount; ++nRunIndex ) {
4042822fc04SHerbert Dürr 			CTRunRef pGlyphRun = (CTRunRef)CFArrayGetValueAtIndex( aGlyphRuns, nRunIndex );
4052822fc04SHerbert Dürr 			const CFIndex nGlyphCount = CTRunGetGlyphCount( pGlyphRun );
4062822fc04SHerbert Dürr 			const CFRange aFullRange = CFRangeMake( 0, nGlyphCount );
4072822fc04SHerbert Dürr 			aSizeVec.reserve( nGlyphCount );
4082822fc04SHerbert Dürr 			aIndexVec.reserve( nGlyphCount );
4092822fc04SHerbert Dürr 			CTRunGetAdvances( pGlyphRun, aFullRange, &aSizeVec[0] );
4102822fc04SHerbert Dürr 			CTRunGetStringIndices( pGlyphRun, aFullRange, &aIndexVec[0] );
4112822fc04SHerbert Dürr 			for( int i = 0; i != nGlyphCount; ++i ) {
4122822fc04SHerbert Dürr 				const int nRelIdx = aIndexVec[i];
4132822fc04SHerbert Dürr 				pDXArray[ nRelIdx ] += aSizeVec[i].width;
4142822fc04SHerbert Dürr 			}
4152822fc04SHerbert Dürr 		}
4162822fc04SHerbert Dürr 	}
4172822fc04SHerbert Dürr 
4182822fc04SHerbert Dürr 	return nPixWidth;
4192822fc04SHerbert Dürr }
4202822fc04SHerbert Dürr 
4212822fc04SHerbert Dürr // -----------------------------------------------------------------------
4222822fc04SHerbert Dürr 
4232822fc04SHerbert Dürr int CTLayout::GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const
4242822fc04SHerbert Dürr {
4252822fc04SHerbert Dürr 	if( !mpCTLine )
4262822fc04SHerbert Dürr 		return STRING_LEN;
4272822fc04SHerbert Dürr 
4282822fc04SHerbert Dürr 	CTTypesetterRef aCTTypeSetter = CTTypesetterCreateWithAttributedString( mpAttrString );
4292822fc04SHerbert Dürr 	const double fCTMaxWidth = (double)nMaxWidth / (nFactor * mfFontScale);
4302822fc04SHerbert Dürr 	CFIndex nIndex = CTTypesetterSuggestClusterBreak( aCTTypeSetter, 0, fCTMaxWidth );
4312822fc04SHerbert Dürr 	if( nIndex >= mnCharCount )
4322822fc04SHerbert Dürr 		return STRING_LEN;
4332822fc04SHerbert Dürr 
4342822fc04SHerbert Dürr 	nIndex += mnMinCharPos;
4352822fc04SHerbert Dürr 	return (int)nIndex;
4362822fc04SHerbert Dürr }
4372822fc04SHerbert Dürr 
4382822fc04SHerbert Dürr // -----------------------------------------------------------------------
4392822fc04SHerbert Dürr 
4402822fc04SHerbert Dürr void CTLayout::GetCaretPositions( int nMaxIndex, sal_Int32* pCaretXArray ) const
4412822fc04SHerbert Dürr {
4422822fc04SHerbert Dürr 	DBG_ASSERT( ((nMaxIndex>0)&&!(nMaxIndex&1)),
4432822fc04SHerbert Dürr 		"CTLayout::GetCaretPositions() : invalid number of caret pairs requested");
4442822fc04SHerbert Dürr 
4452822fc04SHerbert Dürr 	// initialize the caret positions
4462822fc04SHerbert Dürr 	for( int i = 0; i < nMaxIndex; ++i )
4472822fc04SHerbert Dürr 		pCaretXArray[ i ] = -1;
4482822fc04SHerbert Dürr 
4492822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
4502822fc04SHerbert Dürr 	for( int n = 0; n <= mnCharCount; ++n )
4512822fc04SHerbert Dürr 	{
4522822fc04SHerbert Dürr 		// measure the characters cursor position
4532822fc04SHerbert Dürr 		CGFloat fPos2 = -1;
4542822fc04SHerbert Dürr 		const CGFloat fPos1 = rCT.LineGetOffsetForStringIndex( mpCTLine, n, &fPos2 );
4552822fc04SHerbert Dürr 		(void)fPos2; // TODO: split cursor at line direction change
4562822fc04SHerbert Dürr 		// update previous trailing position
4572822fc04SHerbert Dürr 		if( n > 0 )
4582822fc04SHerbert Dürr 			pCaretXArray[ 2*n-1 ] = lrint( fPos1 * mfFontScale );
4592822fc04SHerbert Dürr 		// update current leading position
4602822fc04SHerbert Dürr 		if( 2*n >= nMaxIndex )
4612822fc04SHerbert Dürr 			break;
4622822fc04SHerbert Dürr 		pCaretXArray[ 2*n+0 ] = lrint( fPos1 * mfFontScale );
4632822fc04SHerbert Dürr 	}
4642822fc04SHerbert Dürr }
4652822fc04SHerbert Dürr 
4662822fc04SHerbert Dürr // -----------------------------------------------------------------------
4672822fc04SHerbert Dürr 
4682822fc04SHerbert Dürr bool CTLayout::GetBoundRect( SalGraphics& rGraphics, Rectangle& rVCLRect ) const
4692822fc04SHerbert Dürr {
4702822fc04SHerbert Dürr 	AquaSalGraphics& rAquaGraphics = static_cast<AquaSalGraphics&>(rGraphics);
4712822fc04SHerbert Dürr 	CGRect aMacRect = CTLineGetImageBounds( mpCTLine, rAquaGraphics.mrContext );
4722822fc04SHerbert Dürr 	CGPoint aMacPos = CGContextGetTextPosition( rAquaGraphics.mrContext );
4732822fc04SHerbert Dürr 	aMacRect.origin.x -= aMacPos.x;
4742822fc04SHerbert Dürr 	aMacRect.origin.y -= aMacPos.y;
4752822fc04SHerbert Dürr 
4762822fc04SHerbert Dürr 	const Point aPos = GetDrawPosition( Point(mnBaseAdv, 0) );
4772822fc04SHerbert Dürr 
4782822fc04SHerbert Dürr 	// CoreText top-bottom are vertically flipped from a VCL aspect
4792822fc04SHerbert Dürr 	rVCLRect.Left()   = aPos.X() + mfFontScale * aMacRect.origin.x;
4802822fc04SHerbert Dürr 	rVCLRect.Right()  = aPos.X() + mfFontScale * (aMacRect.origin.x + aMacRect.size.width);
4812822fc04SHerbert Dürr 	rVCLRect.Bottom() = aPos.Y() - mfFontScale * aMacRect.origin.y;
4822822fc04SHerbert Dürr 	rVCLRect.Top()    = aPos.Y() - mfFontScale * (aMacRect.origin.y + aMacRect.size.height);
4832822fc04SHerbert Dürr 	return true;
4842822fc04SHerbert Dürr }
4852822fc04SHerbert Dürr 
4862822fc04SHerbert Dürr // =======================================================================
4872822fc04SHerbert Dürr 
4882822fc04SHerbert Dürr // glyph fallback is supported directly by Aqua
4892822fc04SHerbert Dürr // so methods used only by MultiSalLayout can be dummy implementated
4902822fc04SHerbert Dürr bool CTLayout::GetGlyphOutlines( SalGraphics&, PolyPolyVector& ) const { return false; }
4912822fc04SHerbert Dürr void CTLayout::InitFont() const {}
4922822fc04SHerbert Dürr void CTLayout::MoveGlyph( int /*nStart*/, long /*nNewXPos*/ ) {}
4932822fc04SHerbert Dürr void CTLayout::DropGlyph( int /*nStart*/ ) {}
4942822fc04SHerbert Dürr void CTLayout::Simplify( bool /*bIsBase*/ ) {}
4952822fc04SHerbert Dürr 
4962822fc04SHerbert Dürr // get the ImplFontData for a glyph fallback font
4972822fc04SHerbert Dürr // for a glyphid that was returned by CTLayout::GetNextGlyphs()
4982822fc04SHerbert Dürr const ImplFontData* CTLayout::GetFallbackFontData( sal_GlyphId /*aGlyphId*/ ) const
4992822fc04SHerbert Dürr {
5002822fc04SHerbert Dürr #if 0
5012822fc04SHerbert Dürr 	// check if any fallback fonts were needed
5022822fc04SHerbert Dürr 	if( !mpFallbackInfo )
5032822fc04SHerbert Dürr 		return NULL;
5042822fc04SHerbert Dürr 	// check if the current glyph needs a fallback font
5052822fc04SHerbert Dürr 	int nFallbackLevel = (aGlyphId & GF_FONTMASK) >> GF_FONTSHIFT;
5062822fc04SHerbert Dürr 	if( !nFallbackLevel )
5072822fc04SHerbert Dürr 		return NULL;
5082822fc04SHerbert Dürr 	pFallbackFont = mpFallbackInfo->GetFallbackFontData( nFallbackLevel );
5092822fc04SHerbert Dürr #else
5102822fc04SHerbert Dürr 	// let CoreText's font cascading handle glyph fallback
5112822fc04SHerbert Dürr 	const ImplFontData* pFallbackFont = NULL;
5122822fc04SHerbert Dürr #endif
5132822fc04SHerbert Dürr 	return pFallbackFont;
5142822fc04SHerbert Dürr }
5152822fc04SHerbert Dürr 
5162822fc04SHerbert Dürr // =======================================================================
5172822fc04SHerbert Dürr 
5182822fc04SHerbert Dürr SalLayout* CTTextStyle::GetTextLayout( void ) const
5192822fc04SHerbert Dürr {
5202822fc04SHerbert Dürr 	return new CTLayout( this);
5212822fc04SHerbert Dürr }
5222822fc04SHerbert Dürr 
5232822fc04SHerbert Dürr // =======================================================================
5242822fc04SHerbert Dürr 
525