00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <gtk/gtk.h>
00038
00039 #include "ink_rw.h"
00040 #include "ScribbleLog.h"
00041 #include "ScribbleUtils.h"
00042
00043 #define MAX_XPATH 512
00044
00045
00046
00047 #define MAX_STOKE_STR 0x10000
00048
00049 gboolean xml_page_exist(erManifest *pXmlHandle,const char* sPageName)
00050 {
00051 if( NULL==pXmlHandle || NULL==sPageName)
00052 {
00053 SB_INKERRPRINTF("error:invalid parameter\n");
00054 return FALSE;
00055 }
00056
00057 char sCurrXPath[MAX_XPATH];
00058 snprintf(sCurrXPath,MAX_XPATH,"/notes/pages/page[@id=\"%s\"]",sPageName);
00059 SB_FMPRINTF("sCurrXPath=\n%s\n",sCurrXPath);
00060 if( RET_OK!=ermXmlExist(pXmlHandle, sCurrXPath) )
00061 {
00062 SB_INKERRPRINTF("page[%s] not existed\n",sPageName);
00063 return FALSE;
00064 }
00065 return TRUE;
00066 }
00067
00068
00069
00070 int xml_parse_stroke(PtrStroke pStroke,const char* strpoints)
00071 {
00072 if (strpoints == NULL || pStroke == NULL)
00073 {
00074 SB_INKERRPRINTF("failed:incorrect arguments\n");
00075 return -1;
00076 }
00077
00078 gchar** points_set=g_strsplit_set(strpoints," \n\t",-1);
00079 if ( NULL==points_set[0])
00080 {
00081 SB_INKERRPRINTF("returned:nPoints=0\n");
00082 pStroke->nPoints=0;
00083 return 0;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 PtrInkPoint point;
00098 int i=0;
00099 while( points_set[i] && strlen(points_set[i]) )
00100 {
00101 point = construct_point ();
00102 if ( NULL==point )
00103 {
00104 SB_INKERRPRINTF("construct point error\n");
00105 g_strfreev(points_set);
00106 return -1;
00107 }
00108 point->x=atoi(points_set[i]);
00109 i++;
00110 if(!points_set[i])
00111 {
00112 SB_INKERRPRINTF("failed getting point->y for the %dth point)\n",i/3+1);
00113 break;
00114 }
00115 point->y=atoi(points_set[i]);
00116 SB_INKPRINTF("point(%d,%d) \n", point->x, point->y);
00117 ink_add_point(pStroke, point);
00118 i++;
00119 if(!points_set[i])
00120 {
00121 SB_INKERRPRINTF("failed getting point->pressure for the %dth point)\n",i/3+1);
00122 break;
00123 }
00124
00125 i++;
00126 }
00127 pStroke->nPoints=i/3;
00128
00129 g_strfreev(points_set);
00130 return 0;
00131 }
00132
00133
00134
00135 PtrInk xml_read_ink(erManifest *pXmlHandle,const char* sPageName)
00136 {
00137 if( NULL==pXmlHandle || NULL==sPageName)
00138 {
00139 SB_INKERRPRINTF("error:invalid parameter\n");
00140 return NULL;
00141 }
00142 PtrInk pink = construct_ink ();
00143 if (NULL == pink )
00144 {
00145 SB_INKERRPRINTF("error:construct_ink\n");
00146 return NULL;
00147 }
00148
00149 if( !xml_page_exist(pXmlHandle,sPageName) )
00150 {
00151 return pink;
00152 }
00153
00154 char sStrPoints[MAX_STOKE_STR];
00155 char sStrokeXPath[MAX_XPATH];
00156 int i=1;
00157 char sHtmlColor[HTMLCOLOR_LEN];
00158 while(TRUE)
00159 {
00160 snprintf(sStrokeXPath,MAX_XPATH,
00161 "/notes/pages/page[@id=\"%s\"]/strokes/stroke[%d]",
00162 sPageName, i );
00163
00164 if( RET_OK!=ermXmlGetString(pXmlHandle,sStrokeXPath,
00165 sStrPoints, MAX_STOKE_STR) )
00166 {
00167 break;
00168 }
00169
00170 PtrStroke pStroke = construct_stroke ();
00171 if (pStroke == NULL)
00172 {
00173 SB_INKERRPRINTF("return: construct_stroke failed \n");
00174 break;
00175 }
00176 xml_parse_stroke (pStroke,sStrPoints);
00177
00178 ermXmlGetAttributeInt(pXmlHandle,sStrokeXPath,
00179 "pensize",&pStroke->iPenSize);
00180 ermXmlGetAttributeString(pXmlHandle,sStrokeXPath,
00181 "color",sHtmlColor,HTMLCOLOR_LEN);
00182 Util_GdkColorFromHtmlColor(&pStroke->gdkColor,sHtmlColor);
00183
00184 ink_add_stroke (pink, pStroke);
00185 i++;
00186 }
00187 SB_FMPRINTF("read %d strokes \n",i);
00188 return pink;
00189 }
00190
00191
00192 #define MAX_CHARS_IN_LINE 64
00193
00194
00195
00196
00197
00198
00199
00200
00201 gboolean xml_write_stroke (erManifest *pXmlHandle,
00202 const char* xpath,
00203 const PtrStroke pStroke)
00204 {
00205 if( NULL==xpath || NULL==pStroke || 0==pStroke->nPoints)
00206 {
00207 return FALSE;
00208 }
00209
00210 ermXmlSetAttributeInt(pXmlHandle,xpath,"layer",0);
00211 ermXmlSetAttributeString(pXmlHandle,xpath,"linestyle","solid",strlen("solid"));
00212 ermXmlSetAttributeInt(pXmlHandle,xpath,"pensize",pStroke->iPenSize);
00213
00214 char sHtmlColor[10];
00215 Util_HtmlColorFromGdkColor(sHtmlColor,10,&pStroke->gdkColor);
00216 ermXmlSetAttributeString(pXmlHandle,xpath,
00217 "color",sHtmlColor,HTMLCOLOR_LEN);
00218
00219 char dataLine[MAX_CHARS_IN_LINE];
00220 GArray* garr_stroke=g_array_sized_new(TRUE,FALSE,sizeof(char),1024);
00221
00222
00223 PtrInkPoint point = pStroke->firstPoint;
00224 while (point)
00225 {
00226
00227 snprintf(dataLine,MAX_CHARS_IN_LINE,"%d %d 0\n",point->x,point->y);
00228 g_array_append_vals(garr_stroke,dataLine,strlen(dataLine));
00229 point = point->nextPoint;
00230 }
00231
00232 ermXmlSetString(pXmlHandle, xpath, garr_stroke->data);
00233
00234 g_array_free(garr_stroke,FALSE);
00235 return TRUE;
00236 }
00237
00238
00239
00240 int xml_write_irxHead(erManifest *pErManiHandle)
00241 {
00242 ermXmlNewString(pErManiHandle, "/", "notes", "");
00243 ermXmlNewString(pErManiHandle, "/notes", "version", "");
00244 ermXmlNewString(pErManiHandle, "/notes/version", "number", "1.0");
00245 ermXmlNewString(pErManiHandle, "/notes/version","orgnization", "iRex Technologies");
00246
00247 ermXmlNewString(pErManiHandle, "/notes", "screen", "");
00248
00249 ermXmlNewString(pErManiHandle, "/notes/screen", "units", "px");
00250 ermXmlNewInt(pErManiHandle, "/notes/screen","dpi", 160);
00251
00252 ermXmlNewString(pErManiHandle, "/notes", "pages", "");
00253
00254 return 0;
00255 }
00256
00257
00258 void xml_write_page_head(erManifest *pXmlHandle,const char* xpath)
00259 {
00260 ermXmlSetAttributeString(pXmlHandle,xpath,
00261 "backgroundcolor","#000000",HTMLCOLOR_LEN);
00262 ermXmlNewInt(pXmlHandle, xpath, "orientation", 0);
00263 ermXmlNewInt(pXmlHandle, xpath, "height", 935);
00264 ermXmlNewInt(pXmlHandle, xpath, "width", 768);
00265 ermXmlNewString(pXmlHandle, xpath, "strokes", "");
00266 }
00267
00268
00269 gboolean xml_write_ink (erManifest *pXmlHandle,const char* xpath,
00270 const PtrInk pink,const char* sPageName)
00271 {
00272 if( NULL==pXmlHandle || NULL==xpath || NULL==sPageName )
00273 {
00274 return FALSE;
00275 }
00276 SB_FMPRINTF("--page,path=[%s],sPageName=%s",xpath,sPageName);
00277
00278 char sCurrXPath[MAX_XPATH];
00279
00280
00281 if( !xml_page_exist(pXmlHandle,sPageName) )
00282 {
00283 ermXmlNewString(pXmlHandle, xpath, "page", "");
00284
00285 snprintf(sCurrXPath,MAX_XPATH,"%s/page[last()]",xpath);
00286 ermXmlSetAttributeString(pXmlHandle,sCurrXPath,
00287 "id",sPageName,strlen(sPageName) );
00288 snprintf(sCurrXPath,MAX_XPATH,"%s/page[@id=\"%s\"]",xpath,sPageName);
00289 }
00290 else
00291 {
00292 snprintf(sCurrXPath,MAX_XPATH,"%s/page[@id=\"%s\"]",xpath,sPageName);
00293 ermXmlSetString(pXmlHandle,sCurrXPath, "");
00294 }
00295 if( NULL==pink || 0==pink->nStrokes )
00296 {
00297 return TRUE;
00298 }
00299
00300 xml_write_page_head(pXmlHandle,sCurrXPath);
00301
00302 SB_FMPRINTF("ink->nStrokes=%d \n", pink->nStrokes);
00303 PtrStroke pStroke = pink->firstStroke;
00304 int i=1;
00305 char strokesPath[MAX_XPATH];
00306 snprintf(strokesPath,MAX_XPATH,"%s/strokes",sCurrXPath);
00307 char sLastStrokeXPath[MAX_XPATH];
00308
00309 while (pStroke)
00310 {
00311 ermXmlNewString(pXmlHandle, strokesPath, "stroke", "");
00312 snprintf(sLastStrokeXPath,MAX_XPATH,"%s/stroke[%d]",strokesPath,i);
00313
00314 xml_write_stroke(pXmlHandle,sLastStrokeXPath,pStroke);
00315 pStroke = pStroke->nextStroke;
00316 i++;
00317 }
00318 return TRUE;
00319 }