/* Used for numbering the chapters */
static unsigned int auiHdrCounter[9];
/*
* vString2Diagram - put a string into a diagram
*/
static void
vString2Diagram(diagram_type *pDiag, output_type *pAnchor)
{
output_type *pOutput;
long lWidth;
USHORT usMaxFontSize;
TRACE_MSG("vString2Diagram");
fail(pDiag == NULL);
fail(pAnchor == NULL);
/* Compute the maximum fontsize in this string */
usMaxFontSize = MIN_FONT_SIZE;
for (pOutput = pAnchor; pOutput != NULL; pOutput = pOutput->pNext) {
if (pOutput->usFontSize > usMaxFontSize) {
usMaxFontSize = pOutput->usFontSize;
}
}
/* Goto the next line */
vMove2NextLine(pDiag, pAnchor->tFontRef, usMaxFontSize);
/* Goto the start of the line */
pDiag->lXleft = 0;
TRACE_MSG("leaving vString2Diagram");
} /* end of vString2Diagram */
/*
* vSetLeftIndentation - set the left indentation of the specified diagram
*/
void
vSetLeftIndentation(diagram_type *pDiag, long lLeftIndentation)
{
long lX;
TRACE_MSG("vSetLeftIndentation");
fail(pDiag == NULL);
fail(lLeftIndentation < 0);
lX = lMilliPoints2DrawUnits(lLeftIndentation);
if (lX > 0) {
pDiag->lXleft = lX;
} else {
pDiag->lXleft = 0;
}
} /* end of vSetLeftIndentation */
/*
* lComputeNetWidth - compute the net string width
*/
static long
lComputeNetWidth(output_type *pAnchor)
{
output_type *pTmp;
long lNetWidth;
TRACE_MSG("lComputeNetWidth");
fail(pAnchor == NULL);
/* Step 1: Count all but the last sub-string */
lNetWidth = 0;
for (pTmp = pAnchor; pTmp->pNext != NULL; pTmp = pTmp->pNext) {
fail(pTmp->lStringWidth < 0);
lNetWidth += pTmp->lStringWidth;
}
fail(pTmp == NULL);
fail(pTmp->pNext != NULL);
/* Step 2: remove the white-space from the end of the string */
while (pTmp->tNextFree != 0 &&
isspace((int)(UCHAR)pTmp->szStorage[pTmp->tNextFree - 1])) {
pTmp->szStorage[pTmp->tNextFree - 1] = '\0';
pTmp->tNextFree--;
NO_DBG_DEC(pTmp->lStringWidth);
pTmp->lStringWidth = lComputeStringWidth(
pTmp->szStorage,
pTmp->tNextFree,
pTmp->tFontRef,
pTmp->usFontSize);
NO_DBG_DEC(pTmp->lStringWidth);
}
/* Step 3: Count the last sub-string */
lNetWidth += pTmp->lStringWidth;
return lNetWidth;
} /* end of lComputeNetWidth */
/*
* iComputeHoles - compute number of holes
* (A hole is a number of whitespace characters followed by a
* non-whitespace character)
*/
static int
iComputeHoles(output_type *pAnchor)
{
output_type *pTmp;
size_t tIndex;
int iCounter;
BOOL bWasSpace, bIsSpace;
TRACE_MSG("iComputeHoles");
fail(pAnchor == NULL);
iCounter = 0;
bIsSpace = FALSE;
/* Count the holes */
for (pTmp = pAnchor; pTmp != NULL; pTmp = pTmp->pNext) {
fail(pTmp->tNextFree != strlen(pTmp->szStorage));
for (tIndex = 0; tIndex <= pTmp->tNextFree; tIndex++) {
bWasSpace = bIsSpace;
bIsSpace = isspace((int)(UCHAR)pTmp->szStorage[tIndex]);
if (bWasSpace && !bIsSpace) {
iCounter++;
}
}
}
return iCounter;
} /* end of iComputeHoles */
/*
* vAlign2Window - Align a string and insert it into the text
*/
void
vAlign2Window(diagram_type *pDiag, output_type *pAnchor,
long lScreenWidth, UCHAR ucAlignment)
{
long lNetWidth, lLeftIndentation;
if (lScreenWidth > lChar2MilliPoints(MAX_SCREEN_WIDTH) ||
lNetWidth <= 0) {
/*
* Screenwidth is "infinite", so no alignment is possible
* Don't bother to align an empty line
*/
vString2Diagram(pDiag, pAnchor);
TRACE_MSG("leaving vAlign2Window #1");
return;
}
switch (ucAlignment) {
case ALIGNMENT_CENTER:
lLeftIndentation = (lScreenWidth - lNetWidth) / 2;
DBG_DEC_C(lLeftIndentation < 0, lLeftIndentation);
if (lLeftIndentation > 0) {
vSetLeftIndentation(pDiag, lLeftIndentation);
}
break;
case ALIGNMENT_RIGHT:
lLeftIndentation = lScreenWidth - lNetWidth;
DBG_DEC_C(lLeftIndentation < 0, lLeftIndentation);
if (lLeftIndentation > 0) {
vSetLeftIndentation(pDiag, lLeftIndentation);
}
break;
case ALIGNMENT_JUSTIFY:
case ALIGNMENT_LEFT:
default:
break;
}
vString2Diagram(pDiag, pAnchor);
TRACE_MSG("leaving vAlign2Window #2");
} /* end of vAlign2Window */
/*
* vJustify2Window - Justify a string and insert it into the text
*/
void
vJustify2Window(diagram_type *pDiag, output_type *pAnchor,
long lScreenWidth, long lRightIndentation, UCHAR ucAlignment)
{
output_type *pTmp;
char *pcNew, *pcOld, *szStorage;
long lNetWidth, lSpaceWidth, lToAdd;
int iFillerLen, iHoles;
if (lScreenWidth > lChar2MilliPoints(MAX_SCREEN_WIDTH) ||
lNetWidth <= 0) {
/*
* Screenwidth is "infinite", so justify is not possible
* Don't bother to justify an empty line
*/
vString2Diagram(pDiag, pAnchor);
TRACE_MSG("leaving vJustify2Window #1");
return;
}
/*
* vRemoveRowEnd - remove the end of table row indicator
*
* Remove the double TABLE_SEPARATOR characters from the end of the string.
* Special: remove the TABLE_SEPARATOR, 0x0a sequence
*/
static void
vRemoveRowEnd(char *szRowTxt)
{
int iLastIndex;
/*
* tGetBreakingPoint - get the number of bytes that fit the column
*
* Returns the number of bytes that fit the column
*/
static size_t
tGetBreakingPoint(const char *szString,
size_t tLen, size_t tWidth, size_t tColumnWidthMax)
{
int iIndex;
/* Character sizes */
lCharWidthLarge = lComputeStringWidth("W", 1,
pOutput->tFontRef, pOutput->usFontSize);
NO_DBG_DEC(lCharWidthLarge);
lCharWidthSmall = lComputeStringWidth("i", 1,
pOutput->tFontRef, pOutput->usFontSize);
NO_DBG_DEC(lCharWidthSmall);
/* For the time being: use a fixed width font */
fail(lCharWidthLarge != lCharWidthSmall);
vRemoveRowEnd(pOutput->szStorage);
/* Split the row text into a set of column texts */
aszColTxt[0] = pOutput->szStorage;
for (iNbrOfColumns = 1;
iNbrOfColumns < TABLE_COLUMN_MAX;
iNbrOfColumns++) {
aszColTxt[iNbrOfColumns] =
strchr(aszColTxt[iNbrOfColumns - 1],
TABLE_SEPARATOR);
if (aszColTxt[iNbrOfColumns] == NULL) {
break;
}
*aszColTxt[iNbrOfColumns] = '\0';
aszColTxt[iNbrOfColumns]++;
NO_DBG_DEC(iNbrOfColumns);
NO_DBG_MSG(aszColTxt[iNbrOfColumns]);
}
/* Work around a bug in Word */
while (iNbrOfColumns > (int)pRowInfo->ucNumberOfColumns &&
pRowInfo->asColumnWidth[iNbrOfColumns] == 0) {
iNbrOfColumns--;
}
#if defined(__FULL_TEXT_SEARCH)
/* No table formatting: use for full-text search (untested) */
for (iIndex = 0; iIndex < iNbrOfColumns; iIndex++) {
fprintf(pDiag->pOutFile, "%s\n" , aszColTxt[iIndex]);
}
#else
if (bAddTableRow(pDiag, aszColTxt, iNbrOfColumns,
pRowInfo->asColumnWidth, pRowInfo->ucBorderInfo)) {
/* All work has been done */
return;
}
/*
* Get enough space for the row.
* Worst case: three bytes per UTF-8 character
*/
tSize = 3 * (1 + tColumnWidthTotal + (size_t)iNbrOfColumns + 3);
szLine = xmalloc(tSize);
do {
/* Print one line of a table row */
bNotReady = FALSE;
pcTxt = szLine;
*pcTxt++ = TABLE_SEPARATOR_CHAR;
for (iIndex = 0; iIndex < iNbrOfColumns; iIndex++) {
tColumnWidthMax = atColumnWidthMax[iIndex];
if (aszColTxt[iIndex] == NULL) {
/* Add an empty column */
for (iTmp = 0;
iTmp < (int)tColumnWidthMax;
iTmp++) {
*pcTxt++ = (char)FILLER_CHAR;
}
*pcTxt++ = TABLE_SEPARATOR_CHAR;
*pcTxt = '\0';
continue;
}
/* Compute the length and width of the column text */
tLen = tComputeStringLengthMax(
aszColTxt[iIndex], tColumnWidthMax);
NO_DBG_DEC(tLen);
while (tLen != 0 &&
(aszColTxt[iIndex][tLen - 1] == '\n' ||
aszColTxt[iIndex][tLen - 1] == ' ')) {
aszColTxt[iIndex][tLen - 1] = ' ';
tLen--;
}
tWidth = tCountColumns(aszColTxt[iIndex], tLen);
fail(tWidth > tColumnWidthMax);
tLen = tGetBreakingPoint(aszColTxt[iIndex],
tLen, tWidth, tColumnWidthMax);
tWidth = tCountColumns(aszColTxt[iIndex], tLen);
if (tLen == 0 && *aszColTxt[iIndex] == '\0') {
/* No text at all */
aszColTxt[iIndex] = NULL;
} else {
/* Add the text */
pcTxt += sprintf(pcTxt,
"%.*s", (int)tLen, aszColTxt[iIndex]);
if (tLen == 0 && *aszColTxt[iIndex] != ' ') {
tLen = tGetCharacterLength(
aszColTxt[iIndex]);
DBG_CHR(*aszColTxt[iIndex]);
DBG_FIXME();
fail(tLen == 0);
}
aszColTxt[iIndex] += tLen;
while (*aszColTxt[iIndex] == ' ') {
aszColTxt[iIndex]++;
}
if (*aszColTxt[iIndex] == '\0') {
/* This row is now complete */
aszColTxt[iIndex] = NULL;
} else {
/* This row needs more lines */
bNotReady = TRUE;
}
}
/* Fill up the rest */
for (iTmp = 0;
iTmp < (int)tColumnWidthMax - (int)tWidth;
iTmp++) {
*pcTxt++ = (char)FILLER_CHAR;
}
/* End of column */
*pcTxt++ = TABLE_SEPARATOR_CHAR;
*pcTxt = '\0';
}
/* Output the table row line */
*pcTxt = '\0';
tRow = *pOutput;
tRow.szStorage = szLine;
fail(pcTxt < szLine);
tRow.tNextFree = (size_t)(pcTxt - szLine);
tRow.lStringWidth = lComputeStringWidth(
tRow.szStorage,
tRow.tNextFree,
tRow.tFontRef,
tRow.usFontSize);
vString2Diagram(pDiag, &tRow);
TRACE_MSG("after vString2Diagram in vTableRow2Window");
} while (bNotReady);
/* Clean up before you leave */
szLine = xfree(szLine);
TRACE_MSG("leaving vTableRow2Window");
#endif /* __FULL_TEXT_SEARCH */
} /* end of vTableRow2Window */