Salatslayout.cxx
From OpenOffice.org Wiki
// sallayout.cxx : comments
// ericb 2006-05-15
#ifndef _SV_SALGDI_HXX
#include <salgdi.hxx> // sal graphic display interface
#endif
#ifndef _SV_SALDATA_HXX
#include <saldata.hxx> // sal datas
#endif
#ifndef _SV_SALGDIUTILS_HXX
#include <salgdiutils.hxx> // some utilities for salgraphics
#endif
#ifndef _SV_SALLAYOUT_HXX
#include <sallayout.hxx>
#endif
/*
[FIXME]
premac.h and postmac.h should now be useless
*/
#include <premac.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
#include <postmac.h>
/* No other Mac specific header? */
/*
What is Float32 type ?
the only reference I found was IEEE scientiific format. If so means a conversion from
scientific format to integer, exactly the next integer -> similar to ceil()
*/
inline int Float32ToInt( Float32 f ) { return (int)(f+0.5); }
/*
Class ATSLayout
this class is intended to assume the correspondiance between Carbon API and OpenOffice.org framework
everything is virtual : real implementation is in aqua part -> libvcl will be linked libvclplug_aqua
this last lib will contain the real implementation
ATS means Apple Type Server ( of Unicode Imagery when ATSUI is used)
*/
class ATSLayout : public SalLayout
{
public:
/* ctor. Needs salgdi */
ATSLayout( SalGraphicsData *pGraphicsData );
/* dtor */
~ATSLayout();
/* described later */
virtual bool LayoutText( ImplLayoutArgs& );
/* described later */
virtual void AdjustLayout( ImplLayoutArgs& );
/* described later */
virtual void DrawText( SalGraphics& ) const;
/* described later. Pure virtual function, with all needed parmaters to exactly define the concerned glyph */
virtual int GetNextGlyphs( int nLen, long* pGlyphs, Point& rPos, int&,
long* pGlyphAdvances, int* pCharIndexes ) const;
/* Described later. Looks like the text width needs some management */
virtual long GetTextWidth() const;
/* pure virtual function VERY IMPORTANT OBJECT pDXArray */
virtual long FillDXArray( long* pDXArray ) const;
/* pure virtual function */
virtual int GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const;
/* pure virtual function. does return Caret size, lengh */
virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const;
/* pure virtual function. not sure, but looks extra attributes. */
virtual bool GetGlyphOutlines( SalGraphics&, PolyPolyVector& ) const;
/* IMPORTANT : BoundRect coordinates needs to be known and accessible */
virtual bool GetBoundRect( SalGraphics&, Rectangle& ) const;
/* for glyph+font+script fallback */
/* ? */
virtual void InitFont();
virtual void MoveGlyph( int nStart, long nNewXPos );
virtual void DropGlyph( int nStart );
virtual void Simplify( bool bIsBase );
/* mpGraphicsData is defined in aqua/inc/salgdi.h, and is an object with structure type */
private:
/* !!!! Some Carbon bindings */
SalGraphicsData * mpGraphicsData;
/* Carbon Represents a reference to an opaque structure that contains information about a style object. typedef struct OpaqueATSUStyle *ATSUStyle; commented : not implemented ? */
// ATSUStyle maATSUIStyle;
/*
Carbon
Represents a reference to an opaque text layout structure that contains information about a text layout.
typedef struct OpaqueATSUTextLayout* ATSUTextLayout;
- /
ATSUTextLayout maATSULayout;
/*
Carbon
Represents the boundary between two characters.
typedef UInt32 UniCharArrayOffset;
- /
UniCharArrayOffset mnTextOfs;
/* Not Carbon */
UniCharCount mnTextLen;
int mnBaseAdv;
private:
/* ? */
bool InitGIA() const;
/*
Carbon
Represents a reference to an opaque text layout structure that contains information about a text layout.
typedef struct OpaqueATSUTextLayout* ATSUTextLayout;
Discussion : The basic building block upon which ATSUI operates is a text layout object (ATSUTextLayout).
A text layout object ties one or more paragraphs of text together with style attributes that may apply to characters,
lines, or the entire layout. The text layout object itself contains information about line and layout attributes,
including justification, rotation, direction, and others. Character style information is contained in a style object,
which is only associated with, not contained by, a text layout object. For more information on text layout objects,
see Inside Mac OS X: Rendering Unicode Text With ATSUI.
*/
mutable ATSUGlyphInfoArray * mpGIA; };
// =======================================================================
/*
SalGraphicsData is structure pointing on a pointer pGraphicsData
[FIXME] verify the description
*/
ATSLayout::ATSLayout( SalGraphicsData *pGraphicsData )
- mpGraphicsData( pGraphicsData ),
/*
pure virtual method -> Initialisation with all values NULL or 0
seems to contain the offset, length of the text
[FIXME] what are mnBaseAvd and mpGIA ?
*/
maATSULayout( NULL ),
mnTextOfs( 0 ),
mnTextLen( 0 ),
mnBaseAdv( 0 ),
mpGIA( NULL ) {}
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
/* Dtor */
ATSLayout::~ATSLayout()
{
//
if ( maATSULayout )
/*
Carbon
From Apple documentation :
The ATSUDisposeTextLayout function frees the memory associated with the specified text layout object and its internal structures,
including line and layout control attributes, style runs, and soft line breaks. ATSUDisposeTextLayout does not dispose of any memory
that may be allocated for reference constants or style objects associated with the text layout object. You are responsible for doing so.
*/
ATSUDisposeTextLayout( maATSULayout ); }
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
/* method LayoutText takes a reference as argument */
bool ATSLayout::LayoutText( ImplLayoutArgs& rArgs )
{
/* be sure previous ATSULyaout is freed */
if( maATSULayout )
ATSUDisposeTextLayout( maATSULayout );
/* At some point we actually need to do layout... */
/* looks a bit buggy */
#if 0 // why doesn't ATSUCreateTextLayoutWithTextPtr always fails with mnTextOfs!=0 ???
mnTextOfs = rArgs.mnMinCharPos;
mnTextLen = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
UniCharCount nRunLength = kATSUToTextEnd;
OSStatus theErr = ATSUCreateTextLayoutWithTextPtr( rArgs.mpStr,
mnTextOfs, mnTextLen, rArgs.mnLength, 1, &nRunLength,
&(mpGraphicsData->maATSUIStyle), &maATSULayout );
/* temporary workaround */
#else // workaround for problem above => expect small problems in complex text
/* Text length computation */
mnTextOfs = 0;
mnTextLen = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
/*
[FIXME]
Carbon
kATSUToTextEnd
Indicates that the range of text to be operated on should span to the end of the text layout object’s text buffer.
*/
UniCharCount nRunLength = kATSUToTextEnd;
/*
Creates an opaque text layout object containing default text layout attributes as well as associated text and text styles.
OSStatus ATSUCreateTextLayoutWithTextPtr (
ConstUniCharArrayPtr iText,
UniCharArrayOffset iTextOffset,
UniCharCount iTextLength,
UniCharCount iTextTotalLength,
ItemCount iNumberOfRuns,
const UniCharCount *iRunLengths,
ATSUStyle *iStyles,
ATSUTextLayout *oTextLayout
);
Parameters
iText
A pointer of type ConstUniCharArrayPtr, referring to a text buffer containing UTF-16–encoded text.
ATSUI associates this buffer with the new text layout object and analyzes the complete text of the buffer
when obtaining the layout context for the current text range. Thus, for paragraph-format text, if you specify
a buffer containing less than a complete paragraph, some of ATSUI’s layout results are not guaranteed to be accurate.
For example, with a buffer of less than a full paragraph, ATSUI can neither reliably obtain the context for
bidirectional processing nor reliably generate accent attachments and ligature formations for Roman text.
iTextOffset
A UniCharArrayOffset value specifying the offset from the beginning of the text buffer to the first character of the range
to include in the layout. To indicate that the specified text range starts at the beginning of the text buffer,
you can pass the constant kATSUFromTextBeginning. To specify the entire text buffer, pass kATSUFromTextBeginning in this parameter
and kATSUToTextEnd in the iTextLength parameter.
iTextLength
A UniCharCount value specifying the length of the text range. Note that iTextOffset + iTextLength must be less than or equal
to the value of the iTextTotalLength parameter. If you want the range of text to extend to the end of the text buffer,
you can pass the constant kATSUToTextEnd.
iTextTotalLength
A UniCharCount value specifying the length of the entire text buffer. This value should be greater than or equal to the range
of text defined by the iTextLength parameter.
iNumberOfRuns
An ItemCount value specifying the number of text style runs you want to define within the overall text range.
The number of style objects and style run lengths passed in the iStyles and iRunLengths parameters, respectively,
should be equal to the number of runs specified here.
iRunLengths
A pointer to the first element in a UniCharCount array. This array provides ATSUI with the lengths of each of the text’s style runs.
You can pass kATSUToTextEnd for the last style run length if you want the style run to extend to the end of the text range.
If the sum of the style run lengths is less than the total length of the text range, the remaining characters are assigned to the last style run.
iStyles
A pointer to the first element in an ATSUStyle array. Each element in the array must contain a valid style object that corresponds to a style
run defined by the iRunLengths array.
oTextLayout
A valid pointer to an ATSUTextLayout value. On return, the value refers to the newly created text layout object.
*/
// ---------------------------------------------------------------
OSStatus theErr = ATSUCreateTextLayoutWithTextPtr( rArgs.mpStr + rArgs.mnMinCharPos,
mnTextOfs, mnTextLen, rArgs.mnLength-rArgs.mnMinCharPos, 1, &nRunLength,
&(mpGraphicsData->maATSUIStyle), &maATSULayout );
- endif
/*
problem : create ATSUI text layout is not possible.
no idea on how this problem is solved yet.
*/
if( theErr != noErr ) { fprintf( stderr, "ATSLayout::LayoutText() : "
"Unable to create ATSUI text layout! "
"nTextOfs=%d, nTextLen=%d, nParaLen=%d => err = %d\n",
mnTextOfs, mnTextLen, rArgs.mnLength, theErr );
return false; }
return true; }
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
//
void ATSLayout::AdjustLayout( ImplLayoutArgs& rArgs )
{
int nPixelWidth = rArgs.mnLayoutWidth;
if( !nPixelWidth && rArgs.mpDXArray ) {
// for now we are only interested in the layout width
// TODO: account for individual logical widths
// nPixelWidth = rArgs.mpDXArray[ mnTextLen - 1 ];
}
// return early if there is nothing to do
if( !nPixelWidth )
return;
/*
Carbon
Specify attributes that can be applied to a style object, a text layout object, or a line in a text layout object.
typedef UInt32 ATSUAttributeTag;
enum {
(lot of possibilities ! )
e.g. :
kATSULineWidthTag = 1L,
kATSULineRotationTag = 2L,
kATSULineDirectionTag = 3L,
...
}
all values are xxL ( UInt32 )
this is an array
*/
ATSUAttributeTag nTags[2];
/*
Represents a pointer to a style run or text layout attribute value of unknown size. */
typedef void* ATSUAttributeValuePtr;
Discussion
Each attribute value pointed to by ATSUAttributeValuePtr is identified by an attribute tag and the size (in bytes) of the attribute value.
*/
ATSUAttributeValuePtr nVals[2];
/* Array with 2 values [FIXME] does probably contain the maximal length ofa line */
ByteCount nBytes[2];
/* What does Long2Fix() ? */
/*seems to change a Long value in a fixed size value. What means 'fix' */
Fixed nFixedWidth = Long2Fix( nPixelWidth );
Fixed nFixedOne = Long2Fix( 1 );
/*
Carbon
Constants
kATSULineWidthTag
Specifies the desired width of a line of text,
in typographic points, of the line when drawn as justified or right-aligned text.
The associated value is of type ATSUTextMeasurement and has a default value of 0.
*/
nTags[0] = kATSULineWidthTag;
nBytes[0] = sizeof( Fixed );
nVals[0] = &nFixedWidth;
/*
Carbon
Constants
kATSULineJustificationFactorTag
Specifies how ATSUI should typographically fit a line of text to a given width (or height, in the case of vertical text).
The associated value is a Fract value between 0 and 1 and has a default value of kATSUNoJustification.
See “Line Justification Selectors” for information on the values that can be associated with this tag.
*/
nTags[1] = kATSULineJustificationFactorTag;
nBytes[1] = sizeof( Fixed );
nVals[1] = &nFixedOne;
/*
Carbon
Sets layout control attribute values in a text layout object.
OSStatus ATSUSetLayoutControls (
ATSUTextLayout iTextLayout,
ItemCount iAttributeCount,
const ATSUAttributeTag *iTag,
const ByteCount *iValueSize,
const ATSUAttributeValuePtr *iValue
);
Parameters
iTextLayout
An ATSUTextLayout value specifying the text layout object for which to set layout control attributes.
iAttributeCount
An ItemCount value specifying the number of attributes to set. This value should correspond to the number of elements in the iTag and iValueSize arrays.
iTag
A pointer to the initial ATSUAttributeTag value in an array of layout control attribute tags.
Each element in the array must contain a valid tag that corresponds to the layout control attribute to set. See “Attribute Tags” for a description of the Apple-
defined layout control attribute tag constants.
iValueSize
A pointer to the initial ByteCount value in an array of attribute value sizes.
Each element in the array must contain the size (in bytes) of the corresponding layout control attribute being set.
ATSUSetLayoutControls sets layout attributes after confirming the sizes in the array.
iValue
A pointer to the initial ATSUAttributeValuePtr value in an array of attribute value pointers.
Each value in the array must correspond to a tag in the iTag array and be a legal value for that tag.
*/
ATSUSetLayoutControls( maATSULayout, 2, nTags, nBytes, nVals ); }
// -----------------------------------------------------------------------
[TO BE CONTINUED ]
ericb 2006-05-15

