Imported from antiword-0.30.tar.gz.
[antiword.git] / stylelist.c
blob3c70fef9b7ba5990b892120a9b2622e5ae101937
1 /*
2 * stylelist.c
3 * Copyright (C) 1998,1999 A.J. van Os
5 * Description:
6 * Build, read and destroy a list of Word style information
7 */
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include "antiword.h"
14 /* Variables needed to write the Style Information List */
15 static style_desc_type *pAnchor = NULL;
16 static style_desc_type *pStyleLast = NULL;
17 /* Variable needed to read the Style Information List */
18 static style_desc_type *pStyleCurrent = NULL;
22 * vDestroyStyleInfoList - destroy the Style Information List
24 void
25 vDestroyStyleInfoList(void)
27 style_desc_type *pCurr, *pNext;
29 DBG_MSG("vDestroyStyleInfoList");
31 /* Free the Style Information List */
32 pCurr = pAnchor;
33 while (pCurr != NULL) {
34 pNext = pCurr->pNext;
35 pCurr = xfree(pCurr);
36 pCurr = pNext;
38 pAnchor = NULL;
39 /* Reset all control variables */
40 pStyleLast = NULL;
41 pStyleCurrent = NULL;
42 } /* end of vDestroyStyleInfoList */
45 * ucChooseListCharacter - choose our list character
47 static unsigned char
48 ucChooseListCharacter(unsigned char ucListType, unsigned char ucListChar)
50 if (isprint(ucListChar)) {
51 return ucListChar;
53 if (ucListType == LIST_BULLETS) {
54 switch (ucListChar) {
55 case 0xa8:
56 return OUR_DIAMOND;
57 case 0xb7:
58 return OUR_BULLET;
59 case 0xde:
60 return '=';
61 case 0xe0:
62 return 'o';
63 case 0xfe:
64 return ' ';
65 default:
66 DBG_HEX(ucListChar);
67 return OUR_BULLET;
70 return '.';
71 } /* end of ucChooseListCharacter */
74 * vAdd2StyleInfoList - Add an element to the Style Information List
76 void
77 vAdd2StyleInfoList(const style_block_type *pStyleBlock)
79 static BOOL bMustStore = FALSE;
80 style_desc_type *pListMember;
82 fail(pStyleBlock == NULL);
83 fail(pStyleBlock->iOffset < -1);
85 NO_DBG_MSG("bAdd2StyleInfoList");
87 if (pStyleBlock->iOffset < 0) {
88 return;
91 switch (pStyleBlock->ucStyle) {
92 case 1: case 2: case 3:
93 case 4: case 5: case 6:
94 case 7: case 8: case 9:
95 /* These styles are the nine header levels */
96 break;
97 default:
98 if (pStyleBlock->sLeftIndent > 0 ||
99 pStyleBlock->ucAlignment != 0x00 ||
100 bMustStore) {
101 break;
104 * When reached here, the information is not useful to
105 * the current implementation, so we save memory by
106 * not storing them.
108 return;
111 NO_DBG_HEX(pStyleBlock->iOffset);
112 NO_DBG_DEC_C(pStyleBlock->sLeftIndent != 0,
113 pStyleBlock->sLeftIndent);
114 NO_DBG_DEC_C(pStyleBlock->sRightIndent != 0,
115 pStyleBlock->sRightIndent);
116 NO_DBG_DEC_C(pStyleBlock->bInList, pStyleBlock->bInList);
117 NO_DBG_DEC_C(pStyleBlock->bUnmarked, pStyleBlock->bUnmarked);
118 NO_DBG_DEC_C(pStyleBlock->ucStyle != 0, pStyleBlock->ucStyle);
119 NO_DBG_DEC_C(pStyleBlock->ucAlignment != 0, pStyleBlock->ucAlignment);
120 NO_DBG_DEC_C(pStyleBlock->bInList, pStyleBlock->ucListType);
121 NO_DBG_HEX_C(pStyleBlock->bInList, pStyleBlock->ucListCharacter);
124 * Always store the information about the paragraph
125 * following a member of a list.
127 bMustStore = pStyleBlock->bInList;
129 if (pStyleLast != NULL &&
130 pStyleLast->tInfo.iOffset == pStyleBlock->iOffset) {
132 * If two consecutive styles share the same
133 * offset, remember only the last style
135 fail(pStyleLast->pNext != NULL);
136 pStyleLast->tInfo = *pStyleBlock;
137 return;
140 /* Create list member */
141 pListMember = xmalloc(sizeof(style_desc_type));
142 /* Fill the list member */
143 pListMember->tInfo = *pStyleBlock;
144 pListMember->pNext = NULL;
145 /* Correct the values where needed */
146 if (pListMember->tInfo.sLeftIndent < 0) {
147 pListMember->tInfo.sLeftIndent = 0;
149 if (pListMember->tInfo.ucListLevel > 8) {
150 pListMember->tInfo.ucListLevel = 8;
152 pListMember->tInfo.ucListCharacter =
153 ucChooseListCharacter(pListMember->tInfo.ucListType,
154 pListMember->tInfo.ucListCharacter);
155 /* Add the new member to the list */
156 if (pAnchor == NULL) {
157 pAnchor = pListMember;
158 pStyleCurrent = pListMember;
159 } else {
160 fail(pStyleLast == NULL);
161 pStyleLast->pNext = pListMember;
163 pStyleLast = pListMember;
164 } /* end of vAdd2StyleInfoList */
167 * Get the next item in the Style Information List
169 const style_block_type *
170 pGetNextStyleInfoListItem(void)
172 const style_block_type *pItem;
174 if (pStyleCurrent == NULL) {
175 return NULL;
177 pItem = &pStyleCurrent->tInfo;
178 pStyleCurrent = pStyleCurrent->pNext;
179 return pItem;
180 } /* end of pGetNextStyleInfoListItem */