The TextStyleData Format

The REALbasic EditField provides access to its style information via its TextStyleData property for Macintosh applications. Although this property has been mostly superceded by the StyledText class, it remains useful in certain situations.

TextStyleData is a string containing binary data in the format of a ''styl'' resource. This format is notoriously obscure, so we dissect it here. The format is that of a Style Scrap Record; here are the C struct definitions from the MacOS Carbon header file TextEdit.h.

struct StScrpRec {
  short               scrpNStyles;            /*number of styles in scrap*/
  ScrpSTTable         scrpStyleTab;


typedef ScrpSTElement                   ScrpSTTable[1601];

struct ScrpSTElement {
   long          scrpStartChar;
   short         scrpHeight;
   short         scrpAscent;
   short         scrpFont;
   StyleField    scrpFace;
   short         scrpSize;
   RGBColor      scrpColor;
   };

The scrpStartChar field is the offset of the first character of the style run. The scrpHeight and scrpAscent fields hold the text height and ascent, respectively, of the text font.

The scrpFont field contains the fontID. You can recover the font name from the fontID using the function FMGetFontFamilyName (Carbon) or GetFontName (Classic).

dim systemVersion as Integer
  dim MacOSX as Boolean = System.Gestalt("sysv", systemVersion) and systemVersion >= &h1000
  if MacOSX then
    soft declare function FMGetFontFamilyName Lib "Carbon.framework" (iFontFamily as Short, oName as Ptr) as Integer
  else
   soft declare sub GetFontName Lib "InterfaceLib" (familyID as Short, name as Ptr)
  end if

FMGetFontFamilyName returns an error code. For either function, pass a 256-byte MemoryBlock in the second parameter. The name is returned in the MemoryBlock in Pascal string format. When you read it from the MemoryBlock, set the encoding to Encodings.SystemDefault.

The scrpFace parameter holds the style data. A StyleField is a two-byte field, but only the high byte is used. This byte is a bit field whose bits are interpreted in terms of REALbasic constants as follows.

Const QDBold = &h01
Const QDItalic = &h02
Const QDUnderline = &h04
Const QDOutline = &h08
Const QDShadow = &h10
Const QDCondense = &h20
Const QDExtend = &h40

The scrpSize field contains the text font size.

The scrpColor field contains the text color.

struct RGBColor {
   unsigned short    red;
   unsigned short    green;
   unsigned short    blue;
   };

To convert a color passed in this form to a REALbasic Color, divide the value of each of the RGBColor fields by 257. For example, suppose colorBlock is a six-byte MemoryBlock containing RGBColor data. Conversion to a REALbasic Color value is as follows.

theColor= RGB(colorBlock.UShort(0)\257, colorBlock.UShort(2)\257, colorBlock.UShort(4)\257)

To summarize, a TextStyleData string consists of two bytes containing the number of twenty-byte ScrpSTElement chunks to follow; each twenty-byte chunk holds the style data for the run.

The REALbasic EditField provides access to its style information via its TextStyleData property for Macintosh applications. Although this property has been mostly superceded by the StyledText class, it remains useful in certain situations.