ubiditransform.cpp [plain text]
#include "cmemory.h"
#include "unicode/ubidi.h"
#include "unicode/ustring.h"
#include "unicode/ushape.h"
#include "unicode/utf16.h"
#include "ustr_imp.h"
#include "unicode/ubiditransform.h"
#define LTR UBIDI_LTR
#define RTL UBIDI_RTL
#define LOGICAL UBIDI_LOGICAL
#define VISUAL UBIDI_VISUAL
#define SHAPE_LOGICAL U_SHAPE_TEXT_DIRECTION_LOGICAL
#define SHAPE_VISUAL U_SHAPE_TEXT_DIRECTION_VISUAL_LTR
#define CHECK_LEN(STR, LEN, ERROR) UPRV_BLOCK_MACRO_BEGIN { \
if (LEN == 0) return 0; \
if (LEN < -1) { *(ERROR) = U_ILLEGAL_ARGUMENT_ERROR; return 0; } \
if (LEN == -1) LEN = u_strlen(STR); \
} UPRV_BLOCK_MACRO_END
#define MAX_ACTIONS 7
typedef UBool (*UBiDiAction)(UBiDiTransform *, UErrorCode *);
typedef struct {
UBiDiLevel inLevel;
UBiDiOrder inOrder;
UBiDiLevel outLevel;
UBiDiOrder outOrder;
uint32_t digitsDir;
uint32_t lettersDir;
UBiDiLevel baseLevel;
const UBiDiAction actions[MAX_ACTIONS];
} ReorderingScheme;
struct UBiDiTransform {
UBiDi *pBidi;
const ReorderingScheme *pActiveScheme;
UChar *src;
UChar *dest;
uint32_t srcLength;
uint32_t srcSize;
uint32_t destSize;
uint32_t *pDestLength;
uint32_t reorderingOptions;
uint32_t digits;
uint32_t letters;
};
U_DRAFT UBiDiTransform* U_EXPORT2
ubiditransform_open(UErrorCode *pErrorCode)
{
UBiDiTransform *pBiDiTransform = NULL;
if (U_SUCCESS(*pErrorCode)) {
pBiDiTransform = (UBiDiTransform*) uprv_calloc(1, sizeof(UBiDiTransform));
if (pBiDiTransform == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
}
}
return pBiDiTransform;
}
U_DRAFT void U_EXPORT2
ubiditransform_close(UBiDiTransform *pBiDiTransform)
{
if (pBiDiTransform != NULL) {
if (pBiDiTransform->pBidi != NULL) {
ubidi_close(pBiDiTransform->pBidi);
}
if (pBiDiTransform->src != NULL) {
uprv_free(pBiDiTransform->src);
}
uprv_free(pBiDiTransform);
}
}
static UBool
action_resolve(UBiDiTransform *pTransform, UErrorCode *pErrorCode)
{
ubidi_setPara(pTransform->pBidi, pTransform->src, pTransform->srcLength,
pTransform->pActiveScheme->baseLevel, NULL, pErrorCode);
return FALSE;
}
static UBool
action_reorder(UBiDiTransform *pTransform, UErrorCode *pErrorCode)
{
ubidi_writeReordered(pTransform->pBidi, pTransform->dest, pTransform->destSize,
static_cast<uint16_t>(pTransform->reorderingOptions), pErrorCode);
*pTransform->pDestLength = pTransform->srcLength;
pTransform->reorderingOptions = UBIDI_REORDER_DEFAULT;
return TRUE;
}
static UBool
action_setInverse(UBiDiTransform *pTransform, UErrorCode *pErrorCode)
{
(void)pErrorCode;
ubidi_setInverse(pTransform->pBidi, TRUE);
ubidi_setReorderingMode(pTransform->pBidi, UBIDI_REORDER_INVERSE_LIKE_DIRECT);
return FALSE;
}
static UBool
action_setRunsOnly(UBiDiTransform *pTransform, UErrorCode *pErrorCode)
{
(void)pErrorCode;
ubidi_setReorderingMode(pTransform->pBidi, UBIDI_REORDER_RUNS_ONLY);
return FALSE;
}
static UBool
action_reverse(UBiDiTransform *pTransform, UErrorCode *pErrorCode)
{
ubidi_writeReverse(pTransform->src, pTransform->srcLength,
pTransform->dest, pTransform->destSize,
UBIDI_REORDER_DEFAULT, pErrorCode);
*pTransform->pDestLength = pTransform->srcLength;
return TRUE;
}
static void
updateSrc(UBiDiTransform *pTransform, const UChar *newSrc, uint32_t newLength,
uint32_t newSize, UErrorCode *pErrorCode)
{
if (newSize < newLength) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
return;
}
if (newSize > pTransform->srcSize) {
newSize += 50; if (pTransform->src != NULL) {
uprv_free(pTransform->src);
pTransform->src = NULL;
}
pTransform->src = (UChar *)uprv_malloc(newSize * sizeof(UChar));
if (pTransform->src == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
pTransform->srcSize = newSize;
}
u_strncpy(pTransform->src, newSrc, newLength);
pTransform->srcLength = u_terminateUChars(pTransform->src,
pTransform->srcSize, newLength, pErrorCode);
}
static void
doShape(UBiDiTransform *pTransform, uint32_t options, UErrorCode *pErrorCode)
{
*pTransform->pDestLength = u_shapeArabic(pTransform->src,
pTransform->srcLength, pTransform->dest, pTransform->destSize,
options, pErrorCode);
}
static UBool
action_shapeArabic(UBiDiTransform *pTransform, UErrorCode *pErrorCode)
{
if ((pTransform->letters | pTransform->digits) == 0) {
return FALSE;
}
if (pTransform->pActiveScheme->lettersDir == pTransform->pActiveScheme->digitsDir) {
doShape(pTransform, pTransform->letters | pTransform->digits | pTransform->pActiveScheme->lettersDir,
pErrorCode);
} else {
doShape(pTransform, pTransform->digits | pTransform->pActiveScheme->digitsDir, pErrorCode);
if (U_SUCCESS(*pErrorCode)) {
updateSrc(pTransform, pTransform->dest, *pTransform->pDestLength,
*pTransform->pDestLength, pErrorCode);
doShape(pTransform, pTransform->letters | pTransform->pActiveScheme->lettersDir,
pErrorCode);
}
}
return TRUE;
}
static UBool
action_mirror(UBiDiTransform *pTransform, UErrorCode *pErrorCode)
{
UChar32 c;
uint32_t i = 0, j = 0;
if (0 == (pTransform->reorderingOptions & UBIDI_DO_MIRRORING)) {
return FALSE;
}
if (pTransform->destSize < pTransform->srcLength) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
return FALSE;
}
do {
UBool isOdd = ubidi_getLevelAt(pTransform->pBidi, i) & 1;
U16_NEXT(pTransform->src, i, pTransform->srcLength, c);
U16_APPEND_UNSAFE(pTransform->dest, j, isOdd ? u_charMirror(c) : c);
} while (i < pTransform->srcLength);
*pTransform->pDestLength = pTransform->srcLength;
pTransform->reorderingOptions = UBIDI_REORDER_DEFAULT;
return TRUE;
}
static const ReorderingScheme Schemes[] =
{
{LTR, LOGICAL, LTR, VISUAL, SHAPE_LOGICAL, SHAPE_LOGICAL, LTR,
{action_shapeArabic, action_resolve, action_reorder, NULL}},
{RTL, LOGICAL, LTR, VISUAL, SHAPE_LOGICAL, SHAPE_VISUAL, RTL,
{action_resolve, action_reorder, action_shapeArabic, NULL}},
{LTR, LOGICAL, RTL, VISUAL, SHAPE_LOGICAL, SHAPE_LOGICAL, LTR,
{action_shapeArabic, action_resolve, action_reorder, action_reverse, NULL}},
{RTL, LOGICAL, RTL, VISUAL, SHAPE_LOGICAL, SHAPE_VISUAL, RTL,
{action_resolve, action_reorder, action_shapeArabic, action_reverse, NULL}},
{LTR, VISUAL, RTL, LOGICAL, SHAPE_LOGICAL, SHAPE_VISUAL, RTL,
{action_shapeArabic, action_setInverse, action_resolve, action_reorder, NULL}},
{RTL, VISUAL, RTL, LOGICAL, SHAPE_LOGICAL, SHAPE_VISUAL, RTL,
{action_reverse, action_shapeArabic, action_setInverse, action_resolve, action_reorder, NULL}},
{LTR, VISUAL, LTR, LOGICAL, SHAPE_LOGICAL, SHAPE_LOGICAL, LTR,
{action_setInverse, action_resolve, action_reorder, action_shapeArabic, NULL}},
{RTL, VISUAL, LTR, LOGICAL, SHAPE_LOGICAL, SHAPE_LOGICAL, LTR,
{action_reverse, action_setInverse, action_resolve, action_reorder, action_shapeArabic, NULL}},
{LTR, LOGICAL, RTL, LOGICAL, SHAPE_LOGICAL, SHAPE_LOGICAL, LTR,
{action_shapeArabic, action_resolve, action_mirror, action_setRunsOnly, action_resolve, action_reorder, NULL}},
{RTL, LOGICAL, LTR, LOGICAL, SHAPE_LOGICAL, SHAPE_LOGICAL, RTL,
{action_resolve, action_mirror, action_setRunsOnly, action_resolve, action_reorder, action_shapeArabic, NULL}},
{LTR, VISUAL, RTL, VISUAL, SHAPE_LOGICAL, SHAPE_VISUAL, LTR,
{action_shapeArabic, action_setInverse, action_resolve, action_mirror, action_reverse, NULL}},
{RTL, VISUAL, LTR, VISUAL, SHAPE_LOGICAL, SHAPE_VISUAL, LTR,
{action_reverse, action_shapeArabic, action_setInverse, action_resolve, action_mirror, NULL}},
{LTR, LOGICAL, LTR, LOGICAL, SHAPE_LOGICAL, SHAPE_LOGICAL, LTR,
{action_resolve, action_mirror, action_shapeArabic, NULL}},
{RTL, LOGICAL, RTL, LOGICAL, SHAPE_VISUAL, SHAPE_LOGICAL, RTL,
{action_resolve, action_mirror, action_shapeArabic, NULL}},
{LTR, VISUAL, LTR, VISUAL, SHAPE_LOGICAL, SHAPE_VISUAL, LTR,
{action_resolve, action_mirror, action_shapeArabic, NULL}},
{RTL, VISUAL, RTL, VISUAL, SHAPE_LOGICAL, SHAPE_VISUAL, LTR,
{action_reverse, action_resolve, action_mirror, action_shapeArabic, action_reverse, NULL}}
};
static const uint32_t nSchemes = sizeof(Schemes) / sizeof(*Schemes);
static void
resolveBaseDirection(const UChar *text, uint32_t length,
UBiDiLevel *pInLevel, UBiDiLevel *pOutLevel)
{
switch (*pInLevel) {
case UBIDI_DEFAULT_LTR:
case UBIDI_DEFAULT_RTL: {
UBiDiLevel level = static_cast<UBiDiLevel>(ubidi_getBaseDirection(text, length));
*pInLevel = static_cast<UBiDiLevel>(level != UBIDI_NEUTRAL) ? level
: *pInLevel == UBIDI_DEFAULT_RTL ? static_cast<UBiDiLevel>(RTL) : static_cast<UBiDiLevel>(LTR);
break;
}
default:
*pInLevel &= 1;
break;
}
switch (*pOutLevel) {
case UBIDI_DEFAULT_LTR:
case UBIDI_DEFAULT_RTL:
*pOutLevel = *pInLevel;
break;
default:
*pOutLevel &= 1;
break;
}
}
static const ReorderingScheme*
findMatchingScheme(UBiDiLevel inLevel, UBiDiLevel outLevel,
UBiDiOrder inOrder, UBiDiOrder outOrder)
{
uint32_t i;
for (i = 0; i < nSchemes; i++) {
const ReorderingScheme *pScheme = Schemes + i;
if (inLevel == pScheme->inLevel && outLevel == pScheme->outLevel
&& inOrder == pScheme->inOrder && outOrder == pScheme->outOrder) {
return pScheme;
}
}
return NULL;
}
U_DRAFT uint32_t U_EXPORT2
ubiditransform_transform(UBiDiTransform *pBiDiTransform,
const UChar *src, int32_t srcLength,
UChar *dest, int32_t destSize,
UBiDiLevel inParaLevel, UBiDiOrder inOrder,
UBiDiLevel outParaLevel, UBiDiOrder outOrder,
UBiDiMirroring doMirroring, uint32_t shapingOptions,
UErrorCode *pErrorCode)
{
uint32_t destLength = 0;
UBool textChanged = FALSE;
const UBiDiTransform *pOrigTransform = pBiDiTransform;
const UBiDiAction *action = NULL;
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (src == NULL || dest == NULL) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
CHECK_LEN(src, srcLength, pErrorCode);
CHECK_LEN(dest, destSize, pErrorCode);
if (pBiDiTransform == NULL) {
pBiDiTransform = ubiditransform_open(pErrorCode);
if (U_FAILURE(*pErrorCode)) {
return 0;
}
}
resolveBaseDirection(src, srcLength, &inParaLevel, &outParaLevel);
pBiDiTransform->pActiveScheme = findMatchingScheme(inParaLevel, outParaLevel,
inOrder, outOrder);
if (pBiDiTransform->pActiveScheme == NULL) {
goto cleanup;
}
pBiDiTransform->reorderingOptions = doMirroring ? UBIDI_DO_MIRRORING
: UBIDI_REORDER_DEFAULT;
shapingOptions &= ~U_SHAPE_TEXT_DIRECTION_MASK;
pBiDiTransform->digits = shapingOptions & ~U_SHAPE_LETTERS_MASK;
pBiDiTransform->letters = shapingOptions & ~U_SHAPE_DIGITS_MASK;
updateSrc(pBiDiTransform, src, srcLength, destSize > srcLength ? destSize : srcLength, pErrorCode);
if (U_FAILURE(*pErrorCode)) {
goto cleanup;
}
if (pBiDiTransform->pBidi == NULL) {
pBiDiTransform->pBidi = ubidi_openSized(0, 0, pErrorCode);
if (U_FAILURE(*pErrorCode)) {
goto cleanup;
}
}
pBiDiTransform->dest = dest;
pBiDiTransform->destSize = destSize;
pBiDiTransform->pDestLength = &destLength;
for (action = pBiDiTransform->pActiveScheme->actions; *action && U_SUCCESS(*pErrorCode); action++) {
if ((*action)(pBiDiTransform, pErrorCode)) {
if (action + 1) {
updateSrc(pBiDiTransform, pBiDiTransform->dest, *pBiDiTransform->pDestLength,
*pBiDiTransform->pDestLength, pErrorCode);
}
textChanged = TRUE;
}
}
ubidi_setInverse(pBiDiTransform->pBidi, FALSE);
if (!textChanged && U_SUCCESS(*pErrorCode)) {
if (destSize < srcLength) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
} else {
u_strncpy(dest, src, srcLength);
destLength = srcLength;
}
}
cleanup:
if (pOrigTransform != pBiDiTransform) {
ubiditransform_close(pBiDiTransform);
} else {
pBiDiTransform->dest = NULL;
pBiDiTransform->pDestLength = NULL;
pBiDiTransform->srcLength = 0;
pBiDiTransform->destSize = 0;
}
return U_FAILURE(*pErrorCode) ? 0 : destLength;
}