xref: /aoo41x/main/vcl/aqua/source/gdi/ctlayout.cxx (revision 250ab64b)
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 "ctfonts.hxx"
232822fc04SHerbert Dürr 
242822fc04SHerbert Dürr // =======================================================================
252822fc04SHerbert Dürr 
262822fc04SHerbert Dürr class CTLayout
272822fc04SHerbert Dürr :	public SalLayout
282822fc04SHerbert Dürr {
292822fc04SHerbert Dürr public:
302822fc04SHerbert Dürr 	explicit        CTLayout( const CTTextStyle* );
312822fc04SHerbert Dürr 	virtual         ~CTLayout( void );
322822fc04SHerbert Dürr 
332822fc04SHerbert Dürr 	virtual bool	LayoutText( ImplLayoutArgs& );
342822fc04SHerbert Dürr 	virtual void	AdjustLayout( ImplLayoutArgs& );
352822fc04SHerbert Dürr 	virtual void	DrawText( SalGraphics& ) const;
362822fc04SHerbert Dürr 
372822fc04SHerbert Dürr 	virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pOutGlyphIds, Point& rPos, int&,
382822fc04SHerbert Dürr 						sal_Int32* pGlyphAdvances, int* pCharIndexes ) const;
392822fc04SHerbert Dürr 
402822fc04SHerbert Dürr 	virtual long    GetTextWidth() const;
412822fc04SHerbert Dürr 	virtual long    FillDXArray( sal_Int32* pDXArray ) const;
422822fc04SHerbert Dürr 	virtual int     GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const;
432822fc04SHerbert Dürr 	virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
442822fc04SHerbert Dürr 	virtual bool    GetGlyphOutlines( SalGraphics&, PolyPolyVector& ) const;
452822fc04SHerbert Dürr 	virtual bool    GetBoundRect( SalGraphics&, Rectangle& ) const;
462822fc04SHerbert Dürr 
472822fc04SHerbert Dürr 	const ImplFontData* GetFallbackFontData( sal_GlyphId ) const;
482822fc04SHerbert Dürr 
492822fc04SHerbert Dürr 	virtual void	InitFont( void) const;
502822fc04SHerbert Dürr 	virtual void	MoveGlyph( int nStart, long nNewXPos );
512822fc04SHerbert Dürr 	virtual void	DropGlyph( int nStart );
522822fc04SHerbert Dürr 	virtual void	Simplify( bool bIsBase );
532822fc04SHerbert Dürr 
542822fc04SHerbert Dürr private:
552822fc04SHerbert Dürr 	const CTTextStyle* const	mpTextStyle;
562822fc04SHerbert Dürr 
572822fc04SHerbert Dürr 	// CoreText specific objects
582822fc04SHerbert Dürr 	CFAttributedStringRef mpAttrString;
592822fc04SHerbert Dürr 	CTLineRef mpCTLine;
602822fc04SHerbert Dürr 
612822fc04SHerbert Dürr 	int mnCharCount;		// ==mnEndCharPos-mnMinCharPos
6261277561SHerbert Dürr 	int mnTrailingSpaceCount;
6361277561SHerbert Dürr 	double mfTrailingSpaceWidth;	// preserves the width of stripped-off trailing space
642822fc04SHerbert Dürr 
652822fc04SHerbert Dürr 	// to prevent overflows
662822fc04SHerbert Dürr 	// font requests get size limited by downscaling huge fonts
672822fc04SHerbert Dürr 	// in these cases the font scale becomes something bigger than 1.0
682822fc04SHerbert Dürr 	float mfFontScale; // TODO: does CoreText have a font size limit?
692822fc04SHerbert Dürr 
702822fc04SHerbert Dürr 	// cached details about the resulting layout
712822fc04SHerbert Dürr 	// mutable members since these details are all lazy initialized
722822fc04SHerbert Dürr 	mutable double	mfCachedWidth;			// cached value of resulting typographical width
732822fc04SHerbert Dürr 
742822fc04SHerbert Dürr 	// x-offset relative to layout origin
752822fc04SHerbert Dürr 	// currently only used in RTL-layouts
762822fc04SHerbert Dürr 	mutable long	mnBaseAdv;
772822fc04SHerbert Dürr };
782822fc04SHerbert Dürr 
792822fc04SHerbert Dürr // =======================================================================
802822fc04SHerbert Dürr 
812822fc04SHerbert Dürr CTLayout::CTLayout( const CTTextStyle* pTextStyle )
822822fc04SHerbert Dürr :	mpTextStyle( pTextStyle )
832822fc04SHerbert Dürr ,	mpAttrString( NULL )
842822fc04SHerbert Dürr ,	mpCTLine( NULL )
852822fc04SHerbert Dürr ,	mnCharCount( 0 )
8661277561SHerbert Dürr ,	mnTrailingSpaceCount( 0 )
8761277561SHerbert Dürr ,	mfTrailingSpaceWidth( 0.0 )
882822fc04SHerbert Dürr ,	mfFontScale( pTextStyle->mfFontScale )
892822fc04SHerbert Dürr ,	mfCachedWidth( -1 )
902822fc04SHerbert Dürr ,	mnBaseAdv( 0 )
912822fc04SHerbert Dürr {
922822fc04SHerbert Dürr 	CFRetain( mpTextStyle->GetStyleDict() );
932822fc04SHerbert Dürr }
942822fc04SHerbert Dürr 
952822fc04SHerbert Dürr // -----------------------------------------------------------------------
962822fc04SHerbert Dürr 
972822fc04SHerbert Dürr CTLayout::~CTLayout()
982822fc04SHerbert Dürr {
992822fc04SHerbert Dürr 	if( mpCTLine )
1002822fc04SHerbert Dürr 		CFRelease( mpCTLine );
1012822fc04SHerbert Dürr 	if( mpAttrString )
1022822fc04SHerbert Dürr 		CFRelease( mpAttrString );
1032822fc04SHerbert Dürr 	CFRelease( mpTextStyle->GetStyleDict() );
1042822fc04SHerbert Dürr }
1052822fc04SHerbert Dürr 
1062822fc04SHerbert Dürr // -----------------------------------------------------------------------
1072822fc04SHerbert Dürr 
1082822fc04SHerbert Dürr bool CTLayout::LayoutText( ImplLayoutArgs& rArgs )
1092822fc04SHerbert Dürr {
1102822fc04SHerbert Dürr 	if( mpAttrString )
1112822fc04SHerbert Dürr 		CFRelease( mpAttrString );
1122822fc04SHerbert Dürr 	mpAttrString = NULL;
1132822fc04SHerbert Dürr 	if( mpCTLine )
1142822fc04SHerbert Dürr 		CFRelease( mpCTLine );
1152822fc04SHerbert Dürr 	mpCTLine = NULL;
1162822fc04SHerbert Dürr 
1172822fc04SHerbert Dürr 	SalLayout::AdjustLayout( rArgs );
1182822fc04SHerbert Dürr 	mnCharCount = mnEndCharPos - mnMinCharPos;
1192822fc04SHerbert Dürr 
1202822fc04SHerbert Dürr 	// short circuit if there is nothing to do
1212822fc04SHerbert Dürr 	if( mnCharCount <= 0 )
1222822fc04SHerbert Dürr 		return false;
1232822fc04SHerbert Dürr 
124*250ab64bSHerbert Dürr 	// prepare the string to be layouted by CoreText
1252822fc04SHerbert Dürr 	CFStringRef aCFText = CFStringCreateWithCharactersNoCopy( NULL, rArgs.mpStr + mnMinCharPos, mnCharCount, kCFAllocatorNull );
126*250ab64bSHerbert Dürr 	// #i124375# force soft-hyphen visibility to meet the expectations of Writer+EditEngine
127*250ab64bSHerbert Dürr 	if( CFStringFind( aCFText, (CFStringRef)@"\u00AD", 0).length > 0 )
128*250ab64bSHerbert Dürr 	{
129*250ab64bSHerbert Dürr 		NSString* pDashStr = [(NSString*)aCFText stringByReplacingOccurrencesOfString: @"\u00AD" withString: @"-"];
130*250ab64bSHerbert Dürr 		aCFText = CFStringCreateCopy( NULL, (CFStringRef)pDashStr );
131*250ab64bSHerbert Dürr 	}
132*250ab64bSHerbert Dürr 
133*250ab64bSHerbert Dürr 	// create the CoreText line layout using the requested text style
1342822fc04SHerbert Dürr 	mpAttrString = CFAttributedStringCreate( NULL, aCFText, mpTextStyle->GetStyleDict() );
1352822fc04SHerbert Dürr 	mpCTLine = CTLineCreateWithAttributedString( mpAttrString );
1362822fc04SHerbert Dürr 	CFRelease( aCFText);
1372822fc04SHerbert Dürr 
1382822fc04SHerbert Dürr 	// get info about trailing whitespace to prepare for text justification in AdjustLayout()
13961277561SHerbert Dürr 	mnTrailingSpaceCount = 0;
14061277561SHerbert Dürr 	for( int i = mnEndCharPos; --i >= mnMinCharPos; ++mnTrailingSpaceCount )
14161277561SHerbert Dürr 		if( !IsSpacingGlyph( rArgs.mpStr[i] | GF_ISCHAR )
14261277561SHerbert Dürr 		&&  (rArgs.mpStr[i] != 0x00A0) )
1432822fc04SHerbert Dürr 			break;
1442822fc04SHerbert Dürr 	return true;
1452822fc04SHerbert Dürr }
1462822fc04SHerbert Dürr 
1472822fc04SHerbert Dürr // -----------------------------------------------------------------------
1482822fc04SHerbert Dürr 
1492822fc04SHerbert Dürr void CTLayout::AdjustLayout( ImplLayoutArgs& rArgs )
1502822fc04SHerbert Dürr {
1512822fc04SHerbert Dürr 	if( !mpCTLine)
1522822fc04SHerbert Dürr 		return;
1532822fc04SHerbert Dürr 
1542822fc04SHerbert Dürr 	int nPixelWidth = rArgs.mnLayoutWidth;
155bca19674SHerbert Dürr 	if( rArgs.mpDXArray )
1562822fc04SHerbert Dürr 	{
1572822fc04SHerbert Dürr 		// for now we are only interested in the layout width
1582822fc04SHerbert Dürr 		// TODO: use all mpDXArray elements for layouting
159bca19674SHerbert Dürr 		nPixelWidth = rArgs.mpDXArray[ mnCharCount-1 ];
1602822fc04SHerbert Dürr 	}
16161277561SHerbert Dürr 	else if( !nPixelWidth ) // short-circuit if there is nothing to adjust
16261277561SHerbert Dürr 		return;
1632822fc04SHerbert Dürr 
1643e4cd16fSHerbert Dürr 	// short-circuit when justifying an all-whitespace string
16561277561SHerbert Dürr 	if( mnTrailingSpaceCount >= mnCharCount)
1663e4cd16fSHerbert Dürr 	{
167bca19674SHerbert Dürr 		mfCachedWidth = nPixelWidth / mfFontScale;
1683e4cd16fSHerbert Dürr 		return;
1693e4cd16fSHerbert Dürr 	}
1703e4cd16fSHerbert Dürr 
1712822fc04SHerbert Dürr 	// return early if there is nothing to do
1722822fc04SHerbert Dürr 	if( nPixelWidth <= 0 )
1732822fc04SHerbert Dürr 		return;
1742822fc04SHerbert Dürr 
1752822fc04SHerbert Dürr 	// HACK: justification requests which change the width by just one pixel are probably
1762822fc04SHerbert Dürr 	// #i86038# introduced by lossy conversions between integer based coordinate system
177bca19674SHerbert Dürr 	const int nOrigWidth = GetTextWidth();
1782822fc04SHerbert Dürr 	if( (nOrigWidth >= nPixelWidth-1) && (nOrigWidth <= nPixelWidth+1) )
1792822fc04SHerbert Dürr 		return;
1802822fc04SHerbert Dürr 
1812822fc04SHerbert Dürr 	// if the text to be justified has whitespace in it then
1822822fc04SHerbert Dürr 	// - Writer goes crazy with its HalfSpace magic
183bca19674SHerbert Dürr 	// - CoreText handles spaces specially (in particular at the text end)
18461277561SHerbert Dürr 	if( mnTrailingSpaceCount ) {
185bca19674SHerbert Dürr 		int nTrailingSpaceWidth = 0;
186bca19674SHerbert Dürr 		if( rArgs.mpDXArray) {
187bca19674SHerbert Dürr 			const int nFullPixWidth = nPixelWidth;
18861277561SHerbert Dürr 			nPixelWidth = rArgs.mpDXArray[ mnCharCount-1-mnTrailingSpaceCount ];
189bca19674SHerbert Dürr 			nTrailingSpaceWidth = nFullPixWidth - nPixelWidth;
19061277561SHerbert Dürr 			mfTrailingSpaceWidth = nTrailingSpaceWidth;
191bca19674SHerbert Dürr 		} else {
19261277561SHerbert Dürr 			if( mfTrailingSpaceWidth <= 0.0 )
19361277561SHerbert Dürr 				mfTrailingSpaceWidth = CTLineGetTrailingWhitespaceWidth( mpCTLine );
19461277561SHerbert Dürr 			nTrailingSpaceWidth = rint( mfTrailingSpaceWidth );
19561277561SHerbert Dürr 			nPixelWidth -= nTrailingSpaceWidth;
196bca19674SHerbert Dürr 		}
197bca19674SHerbert Dürr 		if( nPixelWidth <= 0 )
1982822fc04SHerbert Dürr 			return;
199bca19674SHerbert Dürr 
2002822fc04SHerbert Dürr 		// recreate the CoreText line layout without trailing spaces
2012822fc04SHerbert Dürr 		CFRelease( mpCTLine );
2022822fc04SHerbert Dürr 		CFStringRef aCFText = CFStringCreateWithCharactersNoCopy( NULL, rArgs.mpStr + mnMinCharPos,
20361277561SHerbert Dürr 			mnCharCount - mnTrailingSpaceCount, kCFAllocatorNull );
2042822fc04SHerbert Dürr 		CFAttributedStringRef pAttrStr = CFAttributedStringCreate( NULL, aCFText, mpTextStyle->GetStyleDict() );
2052822fc04SHerbert Dürr 		mpCTLine = CTLineCreateWithAttributedString( pAttrStr );
2062822fc04SHerbert Dürr 		CFRelease( aCFText);
2072822fc04SHerbert Dürr 		CFRelease( pAttrStr );
208bca19674SHerbert Dürr 
209bca19674SHerbert Dürr 		// in RTL-layouts trailing spaces are leftmost
210bca19674SHerbert Dürr 		// TODO: use BiDi-algorithm to thoroughly check this assumption
211bca19674SHerbert Dürr 		if( rArgs.mnFlags & SAL_LAYOUT_BIDI_RTL)
212bca19674SHerbert Dürr 			mnBaseAdv = nTrailingSpaceWidth;
2132822fc04SHerbert Dürr 	}
2142822fc04SHerbert Dürr 
215bca19674SHerbert Dürr 	const double fAdjustedWidth = nPixelWidth / mfFontScale;
21661277561SHerbert Dürr 	CTLineRef pNewCTLine = CTLineCreateJustifiedLine( mpCTLine, 1.0, fAdjustedWidth );
2172822fc04SHerbert Dürr 	if( !pNewCTLine ) { // CTLineCreateJustifiedLine can and does fail
2182822fc04SHerbert Dürr 		// handle failure by keeping the unjustified layout
2192822fc04SHerbert Dürr 		// TODO: a better solution such as
2202822fc04SHerbert Dürr 		// - forcing glyph overlap
2212822fc04SHerbert Dürr 		// - changing the font size
2222822fc04SHerbert Dürr 		// - changing the CTM matrix
2232822fc04SHerbert Dürr 		return;
2242822fc04SHerbert Dürr 	}
2252822fc04SHerbert Dürr 	CFRelease( mpCTLine );
2262822fc04SHerbert Dürr 	mpCTLine = pNewCTLine;
22761277561SHerbert Dürr 	mfCachedWidth = fAdjustedWidth + mfTrailingSpaceWidth;
2282822fc04SHerbert Dürr }
2292822fc04SHerbert Dürr 
2302822fc04SHerbert Dürr // -----------------------------------------------------------------------
2312822fc04SHerbert Dürr 
2322822fc04SHerbert Dürr void CTLayout::DrawText( SalGraphics& rGraphics ) const
2332822fc04SHerbert Dürr {
2342822fc04SHerbert Dürr 	AquaSalGraphics& rAquaGraphics = static_cast<AquaSalGraphics&>(rGraphics);
2352822fc04SHerbert Dürr 
2362822fc04SHerbert Dürr 	// short circuit if there is nothing to do
2372822fc04SHerbert Dürr 	if( (mnCharCount <= 0)
2382822fc04SHerbert Dürr 	||  !rAquaGraphics.CheckContext() )
2392822fc04SHerbert Dürr 		return;
2402822fc04SHerbert Dürr 
2412822fc04SHerbert Dürr 	// the view is vertically flipped => flipped glyphs
2422822fc04SHerbert Dürr 	// so apply a temporary transformation that it flips back
2432822fc04SHerbert Dürr 	// also compensate if the font was size limited
2442822fc04SHerbert Dürr 	CGContextSaveGState( rAquaGraphics.mrContext );
2452822fc04SHerbert Dürr 	CGContextScaleCTM( rAquaGraphics.mrContext, +mfFontScale, -mfFontScale );
2462822fc04SHerbert Dürr 	CGContextSetShouldAntialias( rAquaGraphics.mrContext, !rAquaGraphics.mbNonAntialiasedText );
2472822fc04SHerbert Dürr 
2482822fc04SHerbert Dürr 	// Draw the text
2492822fc04SHerbert Dürr 	const Point aVclPos = GetDrawPosition( Point(mnBaseAdv,0) );
2502822fc04SHerbert Dürr 	CGPoint aTextPos = { +aVclPos.X()/mfFontScale, -aVclPos.Y()/mfFontScale };
2512822fc04SHerbert Dürr 
2522822fc04SHerbert Dürr 	if( mpTextStyle->mfFontRotation != 0.0 )
2532822fc04SHerbert Dürr 	{
2542822fc04SHerbert Dürr 		const CGFloat fRadians = mpTextStyle->mfFontRotation;
2552822fc04SHerbert Dürr 		CGContextRotateCTM( rAquaGraphics.mrContext, +fRadians );
2562822fc04SHerbert Dürr 
2572822fc04SHerbert Dürr 		const CGAffineTransform aInvMatrix = CGAffineTransformMakeRotation( -fRadians );
2582822fc04SHerbert Dürr 		aTextPos = CGPointApplyAffineTransform( aTextPos, aInvMatrix );
2592822fc04SHerbert Dürr 	}
2602822fc04SHerbert Dürr 
2612822fc04SHerbert Dürr 	CGContextSetTextPosition( rAquaGraphics.mrContext, aTextPos.x, aTextPos.y );
2622822fc04SHerbert Dürr 	CTLineDraw( mpCTLine, rAquaGraphics.mrContext );
2632822fc04SHerbert Dürr 
2642822fc04SHerbert Dürr 	// request an update of the changed window area
2652822fc04SHerbert Dürr 	if( rAquaGraphics.IsWindowGraphics() )
2662822fc04SHerbert Dürr 	{
2672822fc04SHerbert Dürr 		const CGRect aInkRect = CTLineGetImageBounds( mpCTLine, rAquaGraphics.mrContext );
2682822fc04SHerbert Dürr 		const CGRect aRefreshRect = CGContextConvertRectToDeviceSpace( rAquaGraphics.mrContext, aInkRect );
2692822fc04SHerbert Dürr 		rAquaGraphics.RefreshRect( aRefreshRect );
2702822fc04SHerbert Dürr 	}
2712822fc04SHerbert Dürr 
2722822fc04SHerbert Dürr 	// restore the original graphic context transformations
2732822fc04SHerbert Dürr 	CGContextRestoreGState( rAquaGraphics.mrContext );
2742822fc04SHerbert Dürr }
2752822fc04SHerbert Dürr 
2762822fc04SHerbert Dürr // -----------------------------------------------------------------------
2772822fc04SHerbert Dürr 
2782822fc04SHerbert Dürr int CTLayout::GetNextGlyphs( int nLen, sal_GlyphId* pOutGlyphIds, Point& rPos, int& nStart,
2792822fc04SHerbert Dürr 	sal_Int32* pGlyphAdvances, int* pCharIndexes ) const
2802822fc04SHerbert Dürr {
2812822fc04SHerbert Dürr 	if( !mpCTLine )
2822822fc04SHerbert Dürr 		return 0;
2832822fc04SHerbert Dürr 
2842822fc04SHerbert Dürr 	if( nStart < 0 ) // first glyph requested?
2852822fc04SHerbert Dürr 		nStart = 0;
2862822fc04SHerbert Dürr 	nLen = 1; // TODO: handle nLen>1 below
2872822fc04SHerbert Dürr 
2882822fc04SHerbert Dürr 	// prepare to iterate over the glyph runs
2892822fc04SHerbert Dürr 	int nCount = 0;
2902822fc04SHerbert Dürr 	int nSubIndex = nStart;
2912822fc04SHerbert Dürr 
2922822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
2932822fc04SHerbert Dürr 	typedef std::vector<CGGlyph> CGGlyphVector;
2942822fc04SHerbert Dürr 	typedef std::vector<CGPoint> CGPointVector;
2952822fc04SHerbert Dürr 	typedef std::vector<CGSize>  CGSizeVector;
2962822fc04SHerbert Dürr 	typedef std::vector<CFIndex> CFIndexVector;
2972822fc04SHerbert Dürr 	CGGlyphVector aCGGlyphVec;
2982822fc04SHerbert Dürr 	CGPointVector aCGPointVec;
2992822fc04SHerbert Dürr 	CGSizeVector  aCGSizeVec;
3002822fc04SHerbert Dürr 	CFIndexVector aCFIndexVec;
3012822fc04SHerbert Dürr 
3022822fc04SHerbert Dürr 	// TODO: iterate over cached layout
3032822fc04SHerbert Dürr 	CFArrayRef aGlyphRuns = rCT.LineGetGlyphRuns( mpCTLine );
3042822fc04SHerbert Dürr 	const int nRunCount = CFArrayGetCount( aGlyphRuns );
3052822fc04SHerbert Dürr 	for( int nRunIndex = 0; nRunIndex < nRunCount; ++nRunIndex ) {
3062822fc04SHerbert Dürr 		CTRunRef pGlyphRun = (CTRunRef)CFArrayGetValueAtIndex( aGlyphRuns, nRunIndex );
3072822fc04SHerbert Dürr 		const CFIndex nGlyphsInRun = rCT.RunGetGlyphCount( pGlyphRun );
3082822fc04SHerbert Dürr 		// skip to the first glyph run of interest
3092822fc04SHerbert Dürr 		if( nSubIndex >= nGlyphsInRun ) {
3102822fc04SHerbert Dürr 			nSubIndex -= nGlyphsInRun;
3112822fc04SHerbert Dürr 			continue;
3122822fc04SHerbert Dürr 		}
3132822fc04SHerbert Dürr 		const CFRange aFullRange = CFRangeMake( 0, nGlyphsInRun );
3142822fc04SHerbert Dürr 
3152822fc04SHerbert Dürr 		// get glyph run details
3162822fc04SHerbert Dürr 		const CGGlyph* pCGGlyphIdx = rCT.RunGetGlyphsPtr( pGlyphRun );
3172822fc04SHerbert Dürr 		if( !pCGGlyphIdx ) {
3182822fc04SHerbert Dürr 			aCGGlyphVec.reserve( nGlyphsInRun );
3192822fc04SHerbert Dürr 			CTRunGetGlyphs( pGlyphRun, aFullRange, &aCGGlyphVec[0] );
3202822fc04SHerbert Dürr 			pCGGlyphIdx = &aCGGlyphVec[0];
3212822fc04SHerbert Dürr 		}
3222822fc04SHerbert Dürr 		const CGPoint* pCGGlyphPos = rCT.RunGetPositionsPtr( pGlyphRun );
3232822fc04SHerbert Dürr 		if( !pCGGlyphPos ) {
3242822fc04SHerbert Dürr 			aCGPointVec.reserve( nGlyphsInRun );
3252822fc04SHerbert Dürr 			CTRunGetPositions( pGlyphRun, aFullRange, &aCGPointVec[0] );
3262822fc04SHerbert Dürr 			pCGGlyphPos = &aCGPointVec[0];
3272822fc04SHerbert Dürr 		}
3282822fc04SHerbert Dürr 
3292822fc04SHerbert Dürr 		const CGSize* pCGGlyphAdvs = NULL;
3302822fc04SHerbert Dürr 		if( pGlyphAdvances) {
3312822fc04SHerbert Dürr 			pCGGlyphAdvs = rCT.RunGetAdvancesPtr( pGlyphRun );
3322822fc04SHerbert Dürr 			if( !pCGGlyphAdvs) {
3332822fc04SHerbert Dürr 				aCGSizeVec.reserve( nGlyphsInRun );
3342822fc04SHerbert Dürr 				CTRunGetAdvances( pGlyphRun, aFullRange, &aCGSizeVec[0] );
3352822fc04SHerbert Dürr 				pCGGlyphAdvs = &aCGSizeVec[0];
3362822fc04SHerbert Dürr 			}
3372822fc04SHerbert Dürr 		}
3382822fc04SHerbert Dürr 
3392822fc04SHerbert Dürr 		const CFIndex* pCGGlyphStrIdx = NULL;
3402822fc04SHerbert Dürr 		if( pCharIndexes) {
3412822fc04SHerbert Dürr 			pCGGlyphStrIdx = rCT.RunGetStringIndicesPtr( pGlyphRun );
3422822fc04SHerbert Dürr 			if( !pCGGlyphStrIdx) {
3432822fc04SHerbert Dürr 				aCFIndexVec.reserve( nGlyphsInRun );
3442822fc04SHerbert Dürr 				CTRunGetStringIndices( pGlyphRun, aFullRange, &aCFIndexVec[0] );
3452822fc04SHerbert Dürr 				pCGGlyphStrIdx = &aCFIndexVec[0];
3462822fc04SHerbert Dürr 			}
3472822fc04SHerbert Dürr 		}
3482822fc04SHerbert Dürr 
3492822fc04SHerbert Dürr 		// get the details for each interesting glyph
3502822fc04SHerbert Dürr 		// TODO: handle nLen>1
3512822fc04SHerbert Dürr 		for(; (--nLen >= 0) && (nSubIndex < nGlyphsInRun); ++nSubIndex, ++nStart )
3522822fc04SHerbert Dürr 		{
3532822fc04SHerbert Dürr 			// convert glyph details for VCL
3542822fc04SHerbert Dürr 			*(pOutGlyphIds++) = pCGGlyphIdx[ nSubIndex ];
3552822fc04SHerbert Dürr 			if( pGlyphAdvances )
3562822fc04SHerbert Dürr 				*(pGlyphAdvances++) = pCGGlyphAdvs[ nSubIndex ].width;
3572822fc04SHerbert Dürr 			if( pCharIndexes )
3582822fc04SHerbert Dürr 				*(pCharIndexes++) = pCGGlyphStrIdx[ nSubIndex] + mnMinCharPos;
3592822fc04SHerbert Dürr 			if( !nCount++ ) {
3602822fc04SHerbert Dürr 				const CGPoint& rCurPos = pCGGlyphPos[ nSubIndex ];
3612822fc04SHerbert Dürr 				rPos = GetDrawPosition( Point( mfFontScale * rCurPos.x, mfFontScale * rCurPos.y) );
3622822fc04SHerbert Dürr 			}
3632822fc04SHerbert Dürr 		}
3642822fc04SHerbert Dürr 		nSubIndex = 0; // prepare for the next glyph run
3652822fc04SHerbert Dürr 		break; // TODO: handle nLen>1
3662822fc04SHerbert Dürr 	}
3672822fc04SHerbert Dürr 
3682822fc04SHerbert Dürr 	return nCount;
3692822fc04SHerbert Dürr }
3702822fc04SHerbert Dürr 
3712822fc04SHerbert Dürr // -----------------------------------------------------------------------
3722822fc04SHerbert Dürr 
3732822fc04SHerbert Dürr long CTLayout::GetTextWidth() const
3742822fc04SHerbert Dürr {
3752822fc04SHerbert Dürr 	if( (mnCharCount <= 0) || !mpCTLine )
3762822fc04SHerbert Dürr 		return 0;
3772822fc04SHerbert Dürr 
3782822fc04SHerbert Dürr 	if( mfCachedWidth < 0.0 )
3792822fc04SHerbert Dürr 		mfCachedWidth = CTLineGetTypographicBounds( mpCTLine, NULL, NULL, NULL );
3802822fc04SHerbert Dürr 
3812822fc04SHerbert Dürr 	const long nScaledWidth = lrint( mfFontScale * mfCachedWidth );
3822822fc04SHerbert Dürr 	return nScaledWidth;
3832822fc04SHerbert Dürr }
3842822fc04SHerbert Dürr 
3852822fc04SHerbert Dürr // -----------------------------------------------------------------------
3862822fc04SHerbert Dürr 
3872822fc04SHerbert Dürr long CTLayout::FillDXArray( sal_Int32* pDXArray ) const
3882822fc04SHerbert Dürr {
3892822fc04SHerbert Dürr 	// short circuit requests which don't need full details
3902822fc04SHerbert Dürr 	if( !pDXArray )
3912822fc04SHerbert Dürr 		return GetTextWidth();
3922822fc04SHerbert Dürr 
3932822fc04SHerbert Dürr 	long nPixWidth = GetTextWidth();
3942822fc04SHerbert Dürr 	if( pDXArray ) {
395d237788cSHerbert Dürr 		// prepare the sub-pixel accurate logical-width array
396d237788cSHerbert Dürr 		::std::vector<float> aWidthVector( mnCharCount );
39761277561SHerbert Dürr 		if( mnTrailingSpaceCount && (mfTrailingSpaceWidth > 0.0) ) {
39861277561SHerbert Dürr 			const double fOneWidth = mfTrailingSpaceWidth / mnTrailingSpaceCount;
39961277561SHerbert Dürr 			for( int i = 1; i <= mnTrailingSpaceCount; ++i)
40061277561SHerbert Dürr 				aWidthVector[ mnCharCount - i ] = fOneWidth;
40161277561SHerbert Dürr 		}
40261277561SHerbert Dürr 		// measure advances in each glyph run
4032822fc04SHerbert Dürr 		CFArrayRef aGlyphRuns = CTLineGetGlyphRuns( mpCTLine );
4042822fc04SHerbert Dürr 		const int nRunCount = CFArrayGetCount( aGlyphRuns );
4052822fc04SHerbert Dürr 		typedef std::vector<CGSize> CGSizeVector;
4062822fc04SHerbert Dürr 		CGSizeVector aSizeVec;
4072822fc04SHerbert Dürr 		typedef std::vector<CFIndex> CFIndexVector;
4082822fc04SHerbert Dürr 		CFIndexVector aIndexVec;
4092822fc04SHerbert Dürr 		for( int nRunIndex = 0; nRunIndex < nRunCount; ++nRunIndex ) {
4102822fc04SHerbert Dürr 			CTRunRef pGlyphRun = (CTRunRef)CFArrayGetValueAtIndex( aGlyphRuns, nRunIndex );
4112822fc04SHerbert Dürr 			const CFIndex nGlyphCount = CTRunGetGlyphCount( pGlyphRun );
4122822fc04SHerbert Dürr 			const CFRange aFullRange = CFRangeMake( 0, nGlyphCount );
413bca19674SHerbert Dürr 			aSizeVec.resize( nGlyphCount );
414bca19674SHerbert Dürr 			aIndexVec.resize( nGlyphCount );
4152822fc04SHerbert Dürr 			CTRunGetAdvances( pGlyphRun, aFullRange, &aSizeVec[0] );
4162822fc04SHerbert Dürr 			CTRunGetStringIndices( pGlyphRun, aFullRange, &aIndexVec[0] );
4172822fc04SHerbert Dürr 			for( int i = 0; i != nGlyphCount; ++i ) {
4182822fc04SHerbert Dürr 				const int nRelIdx = aIndexVec[i];
419d237788cSHerbert Dürr 				aWidthVector[nRelIdx] += aSizeVec[i].width;
420d237788cSHerbert Dürr  			}
421d237788cSHerbert Dürr  		}
422d237788cSHerbert Dürr 
423d237788cSHerbert Dürr 		// convert the sub-pixel accurate array into classic pDXArray integers
424d237788cSHerbert Dürr 		float fWidthSum = 0.0;
425d237788cSHerbert Dürr 		sal_Int32 nOldDX = 0;
426d237788cSHerbert Dürr 		for( int i = 0; i < mnCharCount; ++i) {
427d237788cSHerbert Dürr 			const sal_Int32 nNewDX = rint( fWidthSum += aWidthVector[i]);
428d237788cSHerbert Dürr 			pDXArray[i] = nNewDX - nOldDX;
429d237788cSHerbert Dürr 			nOldDX = nNewDX;
4302822fc04SHerbert Dürr 		}
4312822fc04SHerbert Dürr 	}
4322822fc04SHerbert Dürr 
4332822fc04SHerbert Dürr 	return nPixWidth;
4342822fc04SHerbert Dürr }
4352822fc04SHerbert Dürr 
4362822fc04SHerbert Dürr // -----------------------------------------------------------------------
4372822fc04SHerbert Dürr 
4382822fc04SHerbert Dürr int CTLayout::GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const
4392822fc04SHerbert Dürr {
4402822fc04SHerbert Dürr 	if( !mpCTLine )
4412822fc04SHerbert Dürr 		return STRING_LEN;
4422822fc04SHerbert Dürr 
4432822fc04SHerbert Dürr 	CTTypesetterRef aCTTypeSetter = CTTypesetterCreateWithAttributedString( mpAttrString );
4442822fc04SHerbert Dürr 	const double fCTMaxWidth = (double)nMaxWidth / (nFactor * mfFontScale);
4452822fc04SHerbert Dürr 	CFIndex nIndex = CTTypesetterSuggestClusterBreak( aCTTypeSetter, 0, fCTMaxWidth );
4462822fc04SHerbert Dürr 	if( nIndex >= mnCharCount )
4472822fc04SHerbert Dürr 		return STRING_LEN;
4482822fc04SHerbert Dürr 
4492822fc04SHerbert Dürr 	nIndex += mnMinCharPos;
4502822fc04SHerbert Dürr 	return (int)nIndex;
4512822fc04SHerbert Dürr }
4522822fc04SHerbert Dürr 
4532822fc04SHerbert Dürr // -----------------------------------------------------------------------
4542822fc04SHerbert Dürr 
4552822fc04SHerbert Dürr void CTLayout::GetCaretPositions( int nMaxIndex, sal_Int32* pCaretXArray ) const
4562822fc04SHerbert Dürr {
4572822fc04SHerbert Dürr 	DBG_ASSERT( ((nMaxIndex>0)&&!(nMaxIndex&1)),
4582822fc04SHerbert Dürr 		"CTLayout::GetCaretPositions() : invalid number of caret pairs requested");
4592822fc04SHerbert Dürr 
4602822fc04SHerbert Dürr 	// initialize the caret positions
4612822fc04SHerbert Dürr 	for( int i = 0; i < nMaxIndex; ++i )
4622822fc04SHerbert Dürr 		pCaretXArray[ i ] = -1;
4632822fc04SHerbert Dürr 
4642822fc04SHerbert Dürr 	const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
4652822fc04SHerbert Dürr 	for( int n = 0; n <= mnCharCount; ++n )
4662822fc04SHerbert Dürr 	{
4672822fc04SHerbert Dürr 		// measure the characters cursor position
4682822fc04SHerbert Dürr 		CGFloat fPos2 = -1;
4692822fc04SHerbert Dürr 		const CGFloat fPos1 = rCT.LineGetOffsetForStringIndex( mpCTLine, n, &fPos2 );
4702822fc04SHerbert Dürr 		(void)fPos2; // TODO: split cursor at line direction change
4712822fc04SHerbert Dürr 		// update previous trailing position
4722822fc04SHerbert Dürr 		if( n > 0 )
4732822fc04SHerbert Dürr 			pCaretXArray[ 2*n-1 ] = lrint( fPos1 * mfFontScale );
4742822fc04SHerbert Dürr 		// update current leading position
4752822fc04SHerbert Dürr 		if( 2*n >= nMaxIndex )
4762822fc04SHerbert Dürr 			break;
4772822fc04SHerbert Dürr 		pCaretXArray[ 2*n+0 ] = lrint( fPos1 * mfFontScale );
4782822fc04SHerbert Dürr 	}
4792822fc04SHerbert Dürr }
4802822fc04SHerbert Dürr 
4812822fc04SHerbert Dürr // -----------------------------------------------------------------------
4822822fc04SHerbert Dürr 
4832822fc04SHerbert Dürr bool CTLayout::GetBoundRect( SalGraphics& rGraphics, Rectangle& rVCLRect ) const
4842822fc04SHerbert Dürr {
4852822fc04SHerbert Dürr 	AquaSalGraphics& rAquaGraphics = static_cast<AquaSalGraphics&>(rGraphics);
4862822fc04SHerbert Dürr 	CGRect aMacRect = CTLineGetImageBounds( mpCTLine, rAquaGraphics.mrContext );
4872822fc04SHerbert Dürr 	CGPoint aMacPos = CGContextGetTextPosition( rAquaGraphics.mrContext );
4882822fc04SHerbert Dürr 	aMacRect.origin.x -= aMacPos.x;
4892822fc04SHerbert Dürr 	aMacRect.origin.y -= aMacPos.y;
4902822fc04SHerbert Dürr 
4912822fc04SHerbert Dürr 	const Point aPos = GetDrawPosition( Point(mnBaseAdv, 0) );
4922822fc04SHerbert Dürr 
4932822fc04SHerbert Dürr 	// CoreText top-bottom are vertically flipped from a VCL aspect
4942822fc04SHerbert Dürr 	rVCLRect.Left()   = aPos.X() + mfFontScale * aMacRect.origin.x;
4952822fc04SHerbert Dürr 	rVCLRect.Right()  = aPos.X() + mfFontScale * (aMacRect.origin.x + aMacRect.size.width);
4962822fc04SHerbert Dürr 	rVCLRect.Bottom() = aPos.Y() - mfFontScale * aMacRect.origin.y;
4972822fc04SHerbert Dürr 	rVCLRect.Top()    = aPos.Y() - mfFontScale * (aMacRect.origin.y + aMacRect.size.height);
4982822fc04SHerbert Dürr 	return true;
4992822fc04SHerbert Dürr }
5002822fc04SHerbert Dürr 
5012822fc04SHerbert Dürr // =======================================================================
5022822fc04SHerbert Dürr 
5032822fc04SHerbert Dürr // glyph fallback is supported directly by Aqua
5042822fc04SHerbert Dürr // so methods used only by MultiSalLayout can be dummy implementated
5052822fc04SHerbert Dürr bool CTLayout::GetGlyphOutlines( SalGraphics&, PolyPolyVector& ) const { return false; }
5062822fc04SHerbert Dürr void CTLayout::InitFont() const {}
5072822fc04SHerbert Dürr void CTLayout::MoveGlyph( int /*nStart*/, long /*nNewXPos*/ ) {}
5082822fc04SHerbert Dürr void CTLayout::DropGlyph( int /*nStart*/ ) {}
5092822fc04SHerbert Dürr void CTLayout::Simplify( bool /*bIsBase*/ ) {}
5102822fc04SHerbert Dürr 
5112822fc04SHerbert Dürr // get the ImplFontData for a glyph fallback font
5122822fc04SHerbert Dürr // for a glyphid that was returned by CTLayout::GetNextGlyphs()
5132822fc04SHerbert Dürr const ImplFontData* CTLayout::GetFallbackFontData( sal_GlyphId /*aGlyphId*/ ) const
5142822fc04SHerbert Dürr {
5152822fc04SHerbert Dürr #if 0
5162822fc04SHerbert Dürr 	// check if any fallback fonts were needed
5172822fc04SHerbert Dürr 	if( !mpFallbackInfo )
5182822fc04SHerbert Dürr 		return NULL;
5192822fc04SHerbert Dürr 	// check if the current glyph needs a fallback font
5202822fc04SHerbert Dürr 	int nFallbackLevel = (aGlyphId & GF_FONTMASK) >> GF_FONTSHIFT;
5212822fc04SHerbert Dürr 	if( !nFallbackLevel )
5222822fc04SHerbert Dürr 		return NULL;
5232822fc04SHerbert Dürr 	pFallbackFont = mpFallbackInfo->GetFallbackFontData( nFallbackLevel );
5242822fc04SHerbert Dürr #else
5252822fc04SHerbert Dürr 	// let CoreText's font cascading handle glyph fallback
5262822fc04SHerbert Dürr 	const ImplFontData* pFallbackFont = NULL;
5272822fc04SHerbert Dürr #endif
5282822fc04SHerbert Dürr 	return pFallbackFont;
5292822fc04SHerbert Dürr }
5302822fc04SHerbert Dürr 
5312822fc04SHerbert Dürr // =======================================================================
5322822fc04SHerbert Dürr 
5332822fc04SHerbert Dürr SalLayout* CTTextStyle::GetTextLayout( void ) const
5342822fc04SHerbert Dürr {
5352822fc04SHerbert Dürr 	return new CTLayout( this);
5362822fc04SHerbert Dürr }
5372822fc04SHerbert Dürr 
5382822fc04SHerbert Dürr // =======================================================================
5392822fc04SHerbert Dürr 
540