home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 12.4 KB | 381 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: SLTxtPar.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef SLTXTPAR_H
- #include "SLTxtPar.h"
- #endif
-
- #ifndef SLPRIDEB_H
- #include "SLPriDeb.h"
- #endif
-
- #ifndef SLPRIMEM_H
- #include "SLPriMem.h"
- #endif
-
- #ifdef FW_BUILD_MAC
- #include <Script.h>
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment Strings
- #endif
-
- //========================================================================================
- // struct FW_ODTradITextData
- //========================================================================================
-
- struct FW_ODTradITextData
- {
- FW_Locale fLocale;
- char fText[1]; // Variable length array!
- };
-
- //========================================================================================
- // Parameter functions for Traditional Mac Text
- //========================================================================================
-
- const size_t kTradMacTextOffset = sizeof(FW_Locale);
-
- // We declare the following functions extern "C" even though they are static
- // (and therefore private to this module) because the FW_ODITextFunctions struct
- // expects an array of "C" functions. A strict type checking compiler such as
- // SCpp will not allow you to do it otherwise. - (SFU)
-
- extern "C"
- {
- static void TradMac_GetODITextParams(ODIText* text,
- FW_ODITextParams* params,
- FW_PlatformError* error);
- static FW_Boolean TradMac_IsCharacterStart(ODIText* text,
- FW_BytePosition,
- FW_BytePosition,
- FW_PlatformError* error);
- static FW_BytePosition TradMac_GetBytePosition(ODIText* text,
- FW_CharacterPosition,
- FW_PlatformError* error);
- static void TradMac_SetCapacity(ODIText *text,
- long capacity,
- FW_Boolean deletePrevBuffer,
- FW_PlatformError* error);
- static void TradMac_SetLength(ODIText* text,
- long,
- FW_PlatformError* error);
- static void TradMac_SetLocale(ODIText* text,
- FW_Locale locale,
- FW_PlatformError* error);
- }
-
- //----------------------------------------------------------------------------------------
- // TradMac_GetODITextParams
- //----------------------------------------------------------------------------------------
-
- static void TradMac_GetODITextParams(ODIText *text,
- FW_ODITextParams* params,
- FW_PlatformError* error)
- {
- *error = 0;
- params->fTextStart = (char*) text->text._buffer + kTradMacTextOffset;
- params->fTextByteLength = text->text._length - kTradMacTextOffset;
- params->fTextByteCapacity = text->text._maximum - kTradMacTextOffset;
- #ifdef FW_BUILD_MAC
- ODByteArray *array = &text->text;
- FW_ODTradITextData *iTextData = (FW_ODTradITextData *) array->_buffer;
- params->fTextLocale = iTextData->fLocale;
- #else
- // This is a hack until OpenDoc for Windows does the right thing
- params->fTextLocale.fScriptCode = 0;
- params->fTextLocale.fLangCode = 0;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // TradMac_IsCharacterStart
- //----------------------------------------------------------------------------------------
-
- static FW_Boolean TradMac_IsCharacterStart(ODIText *text,
- FW_BytePosition knownCharacterStartPosition,
- FW_BytePosition positionToCheck,
- FW_PlatformError* error)
- {
- FW_Boolean result = true; // assume true, which will be the case for single-byte scripts
- *error = 0;
-
- #ifdef FW_BUILD_MAC
- //KVV For Windows, always assume single-byte character set
- ODByteArray *array = &text->text;
- FW_ODTradITextData *data = (FW_ODTradITextData *) array->_buffer;
-
- if (!::FW_LocaleIsSingleByte(data->fLocale))
- {
- // return true if byte is first byte of a character
- // This doesn't make a distinction between single-byte and double-byte characters
- result = ::CharacterByteType(data->fText+knownCharacterStartPosition,
- positionToCheck-knownCharacterStartPosition,
- data->fLocale.fScriptCode) != smLastByte;
- }
- #endif
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TradMac_GetBytePosition
- //----------------------------------------------------------------------------------------
-
- static FW_BytePosition TradMac_GetBytePosition(ODIText *text, FW_CharacterPosition position,
- FW_PlatformError* error)
- {
- FW_BytePosition result = position; // assume single byte characters
- *error = 0;
-
- ODByteArray *array = &text->text;
- FW_ODTradITextData *data = (FW_ODTradITextData *) array->_buffer;
-
- #ifdef FW_BUILD_MAC
- //KVV For Windows, always assume single-byte character set
- if (!::FW_LocaleIsSingleByte(data->fLocale))
- {
- char* p = data->fText;
- short offset = 0;
- short byteType;
- FW_CharacterPosition chPos = 0;
- FW_BytePosition bytePos = 0;
- while (chPos < position)
- {
- byteType = ::CharacterByteType(p, offset, data->fLocale.fScriptCode);
- bytePos++;
- if (byteType == smFirstByte)
- {
- offset = 1;
- }
- else // (byteType == smLastByte) || (byteType == smSingleByte)
- {
- chPos++;
- p += offset; // offset is 1 or 0, depending on previous byte
- p++;
- offset = 0;
- }
- }
- result = bytePos;
- }
- #endif
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TradMac_SetCapacity
- //----------------------------------------------------------------------------------------
-
- static void TradMac_SetCapacity(ODIText *text,
- long capacity,
- FW_Boolean deletePrevBuffer,
- FW_PlatformError* error)
- {
- FW_ERR_TRY
- {
- if (capacity > long(text->text._maximum - kTradMacTextOffset))
- {
- unsigned char *newBuffer = new unsigned char[capacity+kTradMacTextOffset];
- ::FW_PrimitiveCopyMemory(text->text._buffer, newBuffer, text->text._length);
- if (deletePrevBuffer)
- delete [] text->text._buffer;
- text->text._buffer = newBuffer;
- text->text._maximum = capacity+kTradMacTextOffset;
- }
- }
- FW_ERR_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // TradMac_SetLength
- //----------------------------------------------------------------------------------------
-
- static void TradMac_SetLength(ODIText *text, long length,
- FW_PlatformError* error)
- {
- *error = 0;
- FW_PRIV_ASSERT(length <= long(text->text._maximum - kTradMacTextOffset));
- text->text._length = length + kTradMacTextOffset;
- }
-
- //----------------------------------------------------------------------------------------
- // TradMac_SetLocale
- //----------------------------------------------------------------------------------------
-
- static void TradMac_SetLocale(ODIText *text, FW_Locale locale, FW_PlatformError* error)
- {
- *error = 0;
- ODByteArray *array = &text->text;
- FW_ODTradITextData *iTextData = (FW_ODTradITextData *) array->_buffer;
- iTextData->fLocale = locale;
- }
-
- //----------------------------------------------------------------------------------------
- // gTraditionalMacFunctions
- //----------------------------------------------------------------------------------------
-
- FW_ODITextFunctions gTraditionalMacFunctions =
- {
- TradMac_GetODITextParams,
- TradMac_IsCharacterStart,
- TradMac_GetBytePosition,
- TradMac_SetCapacity,
- TradMac_SetLength,
- TradMac_SetLocale
- };
-
- //========================================================================================
- // Text Format Registry Functions
- //========================================================================================
-
- struct PrivRegistryEntry
- {
- ODITextFormat fFormat;
- FW_ODITextFunctions* fFunctions;
- };
-
- static PrivRegistryEntry gRegistryMap[] =
- {
- { kODTraditionalMacText, &gTraditionalMacFunctions }
- };
-
- const int kNumberFormats = sizeof(gRegistryMap) / sizeof(PrivRegistryEntry);
-
- //----------------------------------------------------------------------------------------
- // PrivGetFormatFunctions
- //----------------------------------------------------------------------------------------
-
- static FW_ODITextFunctions* PrivGetFormatFunctions(ODITextFormat format)
- {
- // We cache the last format,functions pair used.
- // This not only will speed up the usual case where we're continually
- // just getting the FormatFunctions for the same ODIFormat (i.e. kODTraditionalMacText),
- // it will usually save us from even needing to instantiate the PrivTextFormatMap.
- // Any part that uses kODTraditionalMacText exclusively will never execute
- // PrivGetTextFormatMap, so it will never instantiate the map.
-
- static ODITextFormat gLastFormat = kODTraditionalMacText;
- static FW_ODITextFunctions* gLastFunctions = &gTraditionalMacFunctions;
-
- FW_ODITextFunctions* result = 0;
- if (format == gLastFormat)
- result = gLastFunctions;
- else
- {
- for (int i=0; i<kNumberFormats; i++)
- {
- if (gRegistryMap[i].fFormat == format)
- result = gRegistryMap[i].fFunctions;
- }
- if (result)
- {
- gLastFormat = format;
- gLastFunctions = result;
- }
- }
- return result;
- }
-
- //========================================================================================
- // ODIText Parameter Functions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_TextParams_GetParams
- //----------------------------------------------------------------------------------------
-
- void FW_TextParams_GetParams(ODIText *text,
- FW_ODITextParams* params,
- FW_PlatformError* error)
- {
- FW_ODITextFunctions* functions = PrivGetFormatFunctions(text->format);
- FW_PRIV_ASSERT(functions);
- (functions->fGetParams)(text, params, error);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TextParams_IsCharacterStart
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_TextParams_IsCharacterStart(ODIText *text,
- FW_BytePosition knownCharacterStartPosition,
- FW_BytePosition positionToCheck,
- FW_PlatformError* error)
- {
- FW_ODITextFunctions* functions = PrivGetFormatFunctions(text->format);
- FW_PRIV_ASSERT(functions);
- return (functions->fIsCharacter)(text, knownCharacterStartPosition, positionToCheck, error);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TextParams_GetBytePosition
- //----------------------------------------------------------------------------------------
-
- FW_BytePosition FW_TextParams_GetBytePosition(ODIText *text,
- FW_CharacterPosition position,
- FW_PlatformError* error)
- {
- FW_ODITextFunctions* functions = PrivGetFormatFunctions(text->format);
- FW_PRIV_ASSERT(functions);
- return (functions->fGetBytePos)(text, position, error);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TextParams_SetCapacity
- //----------------------------------------------------------------------------------------
-
- void FW_TextParams_SetCapacity(ODIText *text,
- long capacity,
- FW_Boolean deletePrevBuffer,
- FW_ODITextParams* params,
- FW_PlatformError* error)
- {
- FW_ODITextFunctions* functions = PrivGetFormatFunctions(text->format);
- FW_PRIV_ASSERT(functions);
- (functions->fSetCapacity)(text, capacity, deletePrevBuffer, error);
- if (!*error)
- (functions->fGetParams)(text, params, error);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TextParams_SetLength
- //----------------------------------------------------------------------------------------
-
- void FW_TextParams_SetLength(ODIText *text,
- long length,
- FW_ODITextParams* params,
- FW_PlatformError* error)
- {
- FW_ODITextFunctions* functions = PrivGetFormatFunctions(text->format);
- FW_PRIV_ASSERT(functions);
- (functions->fSetLength)(text, length, error);
- if (!*error)
- (functions->fGetParams)(text, params, error);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TextParams_SetLength
- //----------------------------------------------------------------------------------------
-
- void FW_TextParams_SetLocale(ODIText *text,
- FW_Locale locale,
- FW_ODITextParams* params,
- FW_PlatformError* error)
- {
- FW_ODITextFunctions* functions = PrivGetFormatFunctions(text->format);
- FW_PRIV_ASSERT(functions);
- (functions->fSetLocale)(text, locale, error);
- if (!*error)
- (functions->fGetParams)(text, params, error);
- }
-