diff -r f345bda72bc4 -r 43e37759235e Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/drawfile_8c_source.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/drawfile_8c_source.html Tue Mar 30 16:16:55 2010 +0100 @@ -0,0 +1,438 @@ + + +
+ +00001 /* +00002 * drawfile.c +00003 * Copyright (C) 2005 A.J. van Os; Released under GPL +00004 * +00005 * Description: +00006 * Functions to process with the Draw diagram +00007 */ +00008 +00009 #include <string.h> +00010 #include "DeskLib:Error.h" +00011 #include "DeskLib:SWI.h" +00012 #include "drawfile.h" +00013 #include "antiword.h" +00014 +00015 #define DRAWFILE_OBJECT_TOO_SMALL 200 +00016 #define DRAWFILE_NO_TEXT 201 +00017 #define DRAWFILE_BAD_CHARACTER 202 +00018 #define DRAWFILE_SMALL_MEMORY 203 +00019 #define DRAWFILE_PATH_WITHOUT_LINES 204 +00020 #define DRAWFILE_BAD_PATH_TYPE 205 +00021 #define DRAWFILE_PATH_WITHOUT_END 206 +00022 #define DRAWFILE_BAD_SPRITE_SIZE 207 +00023 #define DRAWFILE_BAD_JPEG_SIZE 208 +00024 #define DRAWFILE_TOO_SMALL 209 +00025 #define DRAWFILE_NOT_A_DRAWFILE 210 +00026 #define DRAWFILE_OBJECT_SIZE 211 +00027 #define DRAWFILE_MANY_FONTTABLES 212 +00028 #define DRAWFILE_TEXT_NO_FONT 213 +00029 #define DRAWFILE_OBJECT_UNEXPECTED 214 +00030 #define DRAWFILE_SIZE_ERROR 215 +00031 +00032 typedef struct drawfile_error_tag { +00033 int iErrorNumber; +00034 const char *szErrorText; +00035 } drawfile_error_type; +00036 +00037 static const drawfile_error_type atErrors[] = { +00038 { DRAWFILE_OBJECT_TOO_SMALL, "Object too small"}, +00039 { DRAWFILE_NO_TEXT, "Text object without text"}, +00040 { DRAWFILE_BAD_CHARACTER, "Bad character in string"}, +00041 { DRAWFILE_SMALL_MEMORY, "Not enough memory reserved"}, +00042 { DRAWFILE_PATH_WITHOUT_LINES, "This path has no lines"}, +00043 { DRAWFILE_BAD_PATH_TYPE, "Bad path-type in path"}, +00044 { DRAWFILE_PATH_WITHOUT_END, "No end of path seen"}, +00045 { DRAWFILE_BAD_SPRITE_SIZE, "Bad sprite size"}, +00046 { DRAWFILE_BAD_JPEG_SIZE, "Bad jpeg size"}, +00047 { DRAWFILE_TOO_SMALL, "Too small to be a drawfile"}, +00048 { DRAWFILE_NOT_A_DRAWFILE, "Not a drawfile"}, +00049 { DRAWFILE_OBJECT_SIZE, "Object with incorrect size"}, +00050 { DRAWFILE_MANY_FONTTABLES, "More than one font table"}, +00051 { DRAWFILE_TEXT_NO_FONT, "Text, but no font table seen"}, +00052 { DRAWFILE_OBJECT_UNEXPECTED, "Unexpected object type"}, +00053 { DRAWFILE_SIZE_ERROR, "Sizes don't match"}, +00054 }; +00055 +00056 +00057 /* +00058 * pFillError - error number to error struct +00059 */ +00060 static os_error * +00061 pFillError(int iErrorNumber) +00062 { +00063 static os_error tError; +00064 const drawfile_error_type *pTmp; +00065 const char *szErrorText; +00066 +00067 szErrorText = "Unknown error"; +00068 for (pTmp = atErrors; pTmp < atErrors + elementsof(atErrors); pTmp++) { +00069 if (iErrorNumber == pTmp->iErrorNumber) { +00070 szErrorText = pTmp->szErrorText; +00071 break; +00072 } +00073 } +00074 tError.errnum = iErrorNumber; +00075 strncpy(tError.errmess, szErrorText, sizeof(tError.errmess) - 1); +00076 tError.errmess[sizeof(tError.errmess) - 1] = '\0'; +00077 DBG_DEC(tError.errnum); +00078 DBG_MSG(tError.errmess); +00079 return &tError; +00080 } /* end of pFillError */ +00081 +00082 /* +00083 * Drawfile_BBox - Find the bounding box of a diagram +00084 */ +00085 os_error * +00086 Drawfile_Bbox(drawfile_bbox_flags flags, +00087 drawfile_diagram const *diagram, +00088 int size, +00089 os_trfm const *trfm, +00090 wimp_box *bbox) +00091 { +00092 return SWI(5, 0, DrawFile_BBox | XOS_Bit, +00093 flags, diagram, size, trfm, bbox); +00094 } /* end of Drawfile_Bbox */ +00095 +00096 /* +00097 * Drawfile_CreateDiagram - create an empty drawfile diagram +00098 */ +00099 os_error * +00100 Drawfile_CreateDiagram(drawfile_info *pInfo, size_t tMemorySize, +00101 const char *szCreator, wimp_box tBbox) +00102 { +00103 drawfile_diagram *pDiag; +00104 +00105 if (tMemorySize < offsetof(drawfile_diagram, objects)) { +00106 return pFillError(DRAWFILE_SMALL_MEMORY); +00107 } +00108 pDiag = (drawfile_diagram *)pInfo->data; +00109 strncpy(pDiag->tag, "Draw", 4); +00110 pDiag->major_version = 201; +00111 pDiag->minor_version = 0; +00112 strncpy(pDiag->source, szCreator, sizeof(pDiag->source)); +00113 pDiag->bbox = tBbox; +00114 /* Memory in use */ +00115 pInfo->length = offsetof(drawfile_diagram, objects); +00116 return NULL; +00117 } /* end of Drawfile_CreateDiagram */ +00118 +00119 /* +00120 * Drawfile_AppendObject - append an object to a diagram +00121 */ +00122 os_error * +00123 Drawfile_AppendObject(drawfile_info *pInfo, size_t tMemorySize, +00124 const drawfile_object *pObject, BOOL bRebind) +00125 { +00126 wimp_box *pMainBbox; +00127 const wimp_box *pBbox; +00128 byte *pAfter; +00129 +00130 if (tMemorySize < pInfo->length + pObject->size) { +00131 return pFillError(DRAWFILE_OBJECT_TOO_SMALL); +00132 } +00133 /* After the last object */ +00134 pAfter = (byte *)pInfo->data + pInfo->length; +00135 /* Copy in the new data */ +00136 memcpy(pAfter, pObject, pObject->size); +00137 /* Rebind if needed */ +00138 if (bRebind) { +00139 pMainBbox = &((drawfile_diagram *)pInfo->data)->bbox; +00140 switch (pObject->type) { +00141 case drawfile_TYPE_FONT_TABLE: +00142 pBbox = NULL; +00143 break; +00144 case drawfile_TYPE_TEXT: +00145 pBbox = &pObject->data.text.bbox; +00146 break; +00147 case drawfile_TYPE_PATH: +00148 pBbox = &pObject->data.path.bbox; +00149 break; +00150 case drawfile_TYPE_SPRITE: +00151 pBbox = &pObject->data.sprite.bbox; +00152 break; +00153 case drawfile_TYPE_GROUP: +00154 pBbox = &pObject->data.group.bbox; +00155 break; +00156 case drawfile_TYPE_TAGGED: +00157 pBbox = &pObject->data.tagged.bbox; +00158 break; +00159 case drawfile_TYPE_TEXT_AREA: +00160 pBbox = &pObject->data.text_area.bbox; +00161 break; +00162 case drawfile_TYPE_TEXT_COLUMN: +00163 pBbox = NULL; +00164 break; +00165 case drawfile_TYPE_OPTIONS: +00166 pBbox = &pObject->data.options.bbox; +00167 break; +00168 case drawfile_TYPE_TRFM_TEXT: +00169 pBbox = &pObject->data.trfm_text.bbox; +00170 break; +00171 case drawfile_TYPE_TRFM_SPRITE: +00172 pBbox = &pObject->data.trfm_sprite.bbox; +00173 break; +00174 case drawfile_TYPE_JPEG: +00175 pBbox = &pObject->data.jpeg.bbox; +00176 break; +00177 default: +00178 pBbox = NULL; +00179 break; +00180 } +00181 if (pBbox != NULL) { +00182 if (pBbox->min.x < pMainBbox->min.x) { +00183 pMainBbox->min.x = pBbox->min.x; +00184 } +00185 if (pBbox->min.y < pMainBbox->min.y) { +00186 pMainBbox->min.y = pBbox->min.y; +00187 } +00188 if (pBbox->max.x > pMainBbox->max.x) { +00189 pMainBbox->max.x = pBbox->max.x; +00190 } +00191 if (pBbox->max.y > pMainBbox->max.y) { +00192 pMainBbox->max.y = pBbox->max.y; +00193 } +00194 } +00195 } +00196 /* Memory in use */ +00197 pInfo->length += pObject->size; +00198 return NULL; +00199 } /* end of Drawfile_AppendObject */ +00200 +00201 /* +00202 * Replaces the draw_render_diag function from RISC_OSLib +00203 */ +00204 os_error * +00205 Drawfile_RenderDiagram(drawfile_info *pInfo, window_redrawblock *pRedraw, +00206 double dScale) +00207 { +00208 int aiTransform[6]; +00209 +00210 fail(pInfo == NULL); +00211 fail(pInfo->data == NULL); +00212 fail(pRedraw == NULL); +00213 fail(dScale < 0.01); +00214 +00215 aiTransform[0] = (int)(dScale * 0x10000); +00216 aiTransform[1] = 0; +00217 aiTransform[2] = 0; +00218 aiTransform[3] = (int)(dScale * 0x10000); +00219 aiTransform[4] = (pRedraw->rect.min.x - pRedraw->scroll.x) * 256; +00220 aiTransform[5] = (pRedraw->rect.max.y - pRedraw->scroll.y) * 256; +00221 +00222 return SWI(6, 0, DrawFile_Render | XOS_Bit, +00223 0, pInfo->data, pInfo->length, aiTransform, &pRedraw->rect, 0); +00224 } /* end of Drawfile_RenderDiagram */ +00225 +00226 /* +00227 * pVerifyText - verify a text object +00228 */ +00229 static os_error * +00230 pVerifyText(const drawfile_text *pText) +00231 { +00232 const unsigned char *pucTmp; +00233 +00234 if (pText->text[0] == '\0') { +00235 return pFillError(DRAWFILE_NO_TEXT); +00236 } +00237 pucTmp = (const unsigned char *)pText->text; +00238 while (*pucTmp != '\0') { +00239 if (*pucTmp < 0x20 || *pucTmp == 0x7f) { +00240 return pFillError(DRAWFILE_BAD_CHARACTER); +00241 } +00242 pucTmp++; +00243 } +00244 return NULL; +00245 } /* end of pVerifyText */ +00246 +00247 /* +00248 * pVerifyPath - verify a path object +00249 */ +00250 static os_error * +00251 pVerifyPath(const drawfile_path *pPath, int iSize) +00252 { +00253 const int *piTmp; +00254 int iElements; +00255 BOOL bLine; +00256 +00257 bLine = FALSE; +00258 iElements = (iSize - offsetof(drawfile_path, path)) / 4; +00259 +00260 for (piTmp = pPath->path; piTmp < pPath->path + iElements; piTmp++) { +00261 switch(*piTmp) { +00262 case drawfile_PATH_END_PATH: +00263 if (bLine) { +00264 return NULL; +00265 } +00266 return pFillError(DRAWFILE_PATH_WITHOUT_LINES); +00267 case drawfile_PATH_LINE_TO: +00268 bLine = TRUE; +00269 piTmp += 2; +00270 break; +00271 case drawfile_PATH_MOVE_TO: +00272 piTmp += 2; +00273 break; +00274 case drawfile_PATH_CLOSE_LINE: +00275 bLine = TRUE; +00276 break; +00277 default: +00278 return pFillError(DRAWFILE_BAD_PATH_TYPE); +00279 } +00280 } +00281 return pFillError(DRAWFILE_PATH_WITHOUT_END); +00282 } /* end of pVerifyPath */ +00283 +00284 /* +00285 * pVerifySprite - verify a sprite object +00286 */ +00287 static os_error * +00288 pVerifySprite(const drawfile_sprite *pSprite, int iSize) +00289 { +00290 iSize -= offsetof(drawfile_sprite, header); +00291 if (iSize < pSprite->header.offset_next) { +00292 DBG_DEC(iSize); +00293 DBG_DEC(pSprite->header.offset_next); +00294 return pFillError(DRAWFILE_BAD_SPRITE_SIZE); +00295 } +00296 return NULL; +00297 } /* end of pVerifySprite */ +00298 +00299 /* +00300 * pVerifyJpeg - verify a jpeg object +00301 */ +00302 static os_error * +00303 pVerifyJpeg(const drawfile_jpeg *pJpeg, int iSize) +00304 { +00305 iSize -= offsetof(drawfile_jpeg, data); +00306 if (iSize < pJpeg->len) { +00307 DBG_DEC(iSize); +00308 DBG_DEC(pJpeg->len); +00309 return pFillError(DRAWFILE_BAD_JPEG_SIZE); +00310 } +00311 return NULL; +00312 } /* end of pVerifyJpeg */ +00313 +00314 /* +00315 * Drawfile_VerifyDiagram - Verify the diagram generated from the Word file +00316 * +00317 * returns NULL if the diagram is correct +00318 */ +00319 os_error * +00320 Drawfile_VerifyDiagram(drawfile_info *pInfo) +00321 { +00322 drawfile_diagram *pDiag; +00323 drawfile_object *pObj; +00324 os_error *pError; +00325 const char *pcTmp; +00326 int iToGo, iFontTables; +00327 BOOL bTypeFontTable; +00328 +00329 TRACE_MSG("Drawfile_VerifyDiagram"); +00330 +00331 fail(pInfo == NULL); +00332 +00333 if (pInfo->length < offsetof(drawfile_diagram, objects)) { +00334 return pFillError(DRAWFILE_TOO_SMALL); +00335 } +00336 +00337 pDiag = (drawfile_diagram *)pInfo->data; +00338 if (strncmp(pDiag->tag, "Draw", 4) != 0 || +00339 pDiag->major_version != 201 || +00340 pDiag->minor_version != 0) { +00341 return pFillError(DRAWFILE_NOT_A_DRAWFILE); +00342 } +00343 +00344 iToGo = pInfo->length - offsetof(drawfile_diagram, objects); +00345 pcTmp = (const char *)pInfo->data + offsetof(drawfile_diagram, objects); +00346 iFontTables = 0; +00347 bTypeFontTable = FALSE; +00348 +00349 while (iToGo > 0) { +00350 pObj = (drawfile_object *)pcTmp; +00351 if (pObj->size < 0 || pObj->size % 4 != 0) { +00352 return pFillError(DRAWFILE_OBJECT_SIZE); +00353 } +00354 switch (pObj->type) { +00355 case drawfile_TYPE_FONT_TABLE: +00356 if (bTypeFontTable) { +00357 return pFillError(DRAWFILE_MANY_FONTTABLES); +00358 } +00359 bTypeFontTable = TRUE; +00360 break; +00361 case drawfile_TYPE_TEXT: +00362 if (pObj->data.text.style.font_ref != 0 && +00363 !bTypeFontTable) { +00364 return pFillError(DRAWFILE_TEXT_NO_FONT); +00365 } +00366 pError = pVerifyText(&pObj->data.text); +00367 if (pError != NULL) { +00368 return pError; +00369 } +00370 break; +00371 case drawfile_TYPE_PATH: +00372 pError = pVerifyPath(&pObj->data.path, +00373 pObj->size - offsetof(drawfile_object, data)); +00374 if (pError != NULL) { +00375 return pError; +00376 } +00377 break; +00378 case drawfile_TYPE_SPRITE: +00379 pError = pVerifySprite(&pObj->data.sprite, +00380 pObj->size - offsetof(drawfile_object, data)); +00381 if (pError != NULL) { +00382 return pError; +00383 } +00384 break; +00385 case drawfile_TYPE_JPEG: +00386 pError = pVerifyJpeg(&pObj->data.jpeg, +00387 pObj->size - offsetof(drawfile_object, data)); +00388 if (pError != NULL) { +00389 return pError; +00390 } +00391 break; +00392 default: +00393 DBG_DEC(pObj->type); +00394 return pFillError(DRAWFILE_OBJECT_UNEXPECTED); +00395 } +00396 pcTmp += pObj->size; +00397 iToGo -= pObj->size; +00398 } +00399 if (iToGo < 0) { +00400 return pFillError(DRAWFILE_SIZE_ERROR); +00401 } +00402 return NULL; +00403 } /* end of Drawfile_VerifyDiagram */ +00404 +00405 /* +00406 * Drawfile_QueryBox - Find the bounding box of a diagram +00407 */ +00408 void +00409 Drawfile_QueryBox(drawfile_info *pInfo, wimp_box *pRect, BOOL bScreenUnits) +00410 { +00411 fail(pInfo == NULL); +00412 fail(pRect == NULL); +00413 +00414 Error_CheckFatal(Drawfile_Bbox(0, +00415 pInfo->data, pInfo->length, NULL, pRect)); +00416 if (bScreenUnits) { +00417 pRect->min.x = Drawfile_DrawToScreen(pRect->min.x); +00418 pRect->min.y = Drawfile_DrawToScreen(pRect->min.y); +00419 pRect->max.x = Drawfile_DrawToScreen(pRect->max.x); +00420 pRect->max.y = Drawfile_DrawToScreen(pRect->max.y); +00421 } +00422 } /* end of Drawfile_QueryBox */ +