scribble_manager.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: scribble_manager.cpp
00003  */
00004 
00005 /*
00006  * This file is part of liberscribble.
00007  *
00008  * liberscribble is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * liberscribble is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026  
00027 #include <math.h>
00028 #include <string>
00029 #include <cstring>
00030 
00031 #include "scribble_manager.h"
00032 
00033 namespace test
00034 {
00035 
00036 template<class T>
00037 void swap(T & a, T & b)
00038 {
00039     T tmp = a; a = b; b = tmp;
00040 }
00041 
00042 static const float e = 0.001;
00043 
00044 ScribbleMgr::ScribbleMgr(void)
00045 : doc(0)
00046 , page(0)
00047 , stroke(0)
00048 , last_absolute_point()
00049 , last_page_point()
00050 , last_page_position()
00051 , dirty_pages()
00052 {
00053     erscribble_init();
00054 }
00055 
00056 ScribbleMgr::~ScribbleMgr(void)
00057 {
00058     erscribble_uninit();
00059 }
00060 
00061 bool ScribbleMgr::open(const char *doc_path)
00062 {
00063     close();
00064     
00065     // check
00066     if (doc_path == 0)
00067     {
00068         printf("Invalid path name pointer!\n");
00069         return false;
00070     }
00071 
00072     // keep the path name.
00073     strncpy(path.document_path, doc_path, ERSCRIBBLE_MAX_PATH);
00074 
00075     // open file
00076     doc = erscribble_doc_open(&path);
00077 
00078     // if there is no scribble data stored in the data base
00079     // create a new scribble document
00080     if (doc == 0)
00081     {
00082         doc = erscribble_doc_new(&path);
00083     }
00084 
00085     return (doc != 0);
00086 }
00087 
00088 void ScribbleMgr::close(bool bSave)
00089 {
00090     // TODO. need save the data of current page
00091     if (bSave)
00092     {
00093         save();
00094     }
00095 
00096     erscribble_doc_free(doc);
00097     doc = 0;
00098     memset(&path, 0, sizeof(path));
00099     page = 0;
00100     stroke = 0;
00101 }
00102 
00103 bool ScribbleMgr::is_page_dirty(ScbPagePtr page)
00104 {
00105     DirtyPagesIter idx = dirty_pages.begin();
00106     for (; idx != dirty_pages.end(); ++idx)
00107     {
00108         if (*idx == page)
00109         {
00110             return true;
00111         }
00112     }
00113     return false;
00114 }
00115 
00116 // Add the page into dirty pages list
00117 // NOTE: This function can only be called after is_page_dirty
00118 void ScribbleMgr::set_page_dirty(ScbPagePtr page)
00119 {
00120     dirty_pages.push_back(page);
00121 }
00122 
00123 void ScribbleMgr::save()
00124 {
00125     if (doc)
00126     {
00127         DirtyPagesIter idx = dirty_pages.begin();
00128         for (; idx != dirty_pages.end(); ++idx)
00129         {
00130             ScbPagePtr ptr = *idx;
00131             erscribble_doc_save_page(doc, ptr);
00132         }
00133         // clear all dirty pages
00134         dirty_pages.clear();
00135     }
00136 }
00137 
00138 /// Removed should be provded by caller.
00139 /// Should compare PLUGIN_MAX_ANCHOR_LENGTH and erscribble_MAX_PAGEID_LEN.
00140 /// erscribble_MAX_PAGEID_LEN 256
00141 /// PLUGIN_MAX_ANCHOR_LENGTH 1024.
00142 /// It may cause some problem when anchors' first 256 bytes are equal.
00143 /// Quite impossible.
00144 void ScribbleMgr::generate_page_id(ScbPageId & pid, const std::string &anchor, const int pos)
00145 {
00146     erscribble_page_set_id(&pid, pos, anchor.c_str(), -1);
00147 }
00148 
00149 void ScribbleMgr::copy_page_id(ScbPageId & dst, const ScbPageId & src)
00150 {
00151     erscribble_page_copy_id(&dst, (const ScbPageIdPtr)(&src));
00152 }
00153 
00154 void ScribbleMgr::clear_page_id(ScbPageId & pageId)
00155 {
00156     erscribble_page_reset_id(&pageId);
00157 }
00158 
00159 bool ScribbleMgr::is_page_id_empty(const ScbPageId & pageId)
00160 {
00161     return pageId.id[0] == '\0';
00162 }
00163 
00164 bool ScribbleMgr::cmp_page_id(const ScbPageId & src, const ScbPageId & dst)
00165 {
00166     return 0 == strncmp(src.id, dst.id, ERSCRIBBLE_MAX_PAGEID_LEN);
00167 }
00168 
00169 void ScribbleMgr::begin_stroke(const ScbPageId & pageId,
00170                                ScribbleGC & sgc,
00171                                const int absoluteX,
00172                                const int absoluteY,
00173                                const int offsetX,
00174                                const int offsetY)
00175 {
00176     printf("Create new stroke!\n");
00177 
00178     // check page
00179     page = erscribble_doc_get_page(doc, (const ScbPageIdPtr)(&pageId));
00180     if (page == 0)
00181     {
00182         // does not exist, needs to create a new one
00183         printf("Page %s does not exist!\n", pageId.id);
00184         page = erscribble_page_new();
00185         erscribble_page_copy_id(&page->id, (const ScbPageIdPtr)(&pageId));
00186         page->attributes.style.orientation = sgc.rotation;
00187 
00188         // add page into dirty list
00189         set_page_dirty(page);
00190     }
00191     else if (!is_page_dirty(page))
00192     {
00193         // add page into dirty list
00194         set_page_dirty(page);
00195     }
00196 
00197     // check stroke
00198     if (stroke == 0)
00199     {
00200         stroke = erscribble_stroke_new_with_attributes(erscribble_doc_get_current_stroke_attributes(doc));
00201         stroke->attributes.zoom = sgc.realZoom;
00202         stroke->attributes.size_id = 3;
00203         erscribble_page_add_stroke(page, stroke);
00204 
00205         printf("Create new stroke %p!", stroke);
00206     }
00207 
00208     ScbDevPoint point;
00209     // stroke point data, in offset
00210     point.point.x = offsetX;
00211     point.point.y = offsetY;
00212     point.pressure = 0;
00213     erscribble_stroke_add_point(stroke, &point);
00214 
00215     // for drawing, use the absolute position now
00216     // TODO. Not a good way
00217     point.point.x = absoluteX;
00218     point.point.y = absoluteY;
00219     erscribble_stroke_driver_draw_point(stroke, &point, FALSE);
00220 
00221     // record the page id
00222     copy_page_id(last_page_id, pageId);
00223 
00224 }
00225 
00226 void ScribbleMgr::add_point(const ScbPageId & pageId,
00227                             ScribbleGC & sgc,
00228                             const int absoluteX,
00229                             const int absoluteY,
00230                             const int offsetX,
00231                             const int offsetY)
00232 {
00233     ScbDevPoint point;
00234     point.point.x = offsetX;
00235     point.point.y = offsetY;
00236     erscribble_stroke_add_point(stroke, &point);
00237     
00238     point.point.x = absoluteX;
00239     point.point.y = absoluteY;
00240     erscribble_stroke_driver_draw_point(stroke, &point, FALSE);
00241 
00242 }
00243 
00244 void ScribbleMgr::end_stroke(const ScbPageId & pageId,
00245                              ScribbleGC & sgc,
00246                              const int absoluteX,
00247                              const int absoluteY,
00248                              const int offsetX,
00249                              const int offsetY,
00250                              const int pageX,
00251                              const int pageY)
00252 {
00253     printf("End stroke!");
00254 
00255     ScbDevPoint point;
00256 
00257     // stroke data
00258     point.point.x = offsetX;
00259     point.point.y = offsetY;
00260     erscribble_stroke_add_point(stroke, &point);
00261     
00262     // drawing
00263     point.point.x = absoluteX;
00264     point.point.y = absoluteY;
00265     erscribble_stroke_driver_draw_point(stroke, &point, TRUE);
00266     
00267     // draw in display
00268     draw_stroke(stroke, sgc, pageX, pageY);
00269 
00270     // add & reset
00271     erscribble_doc_add_page(doc, page);
00272     page = 0;
00273     stroke = 0;
00274 
00275     // record the page id
00276     copy_page_id(last_page_id, pageId);
00277 
00278 }
00279 
00280 // end stroke without adding point
00281 void ScribbleMgr::end_stroke(ScribbleGC & sgc,
00282                              const int absoluteX, 
00283                              const int absoluteY,
00284                              const int pageX,
00285                              const int pageY)
00286 {
00287     printf("Finish stroke!");
00288     ScbDevPoint     point;
00289 
00290     // drawing
00291     point.point.x = absoluteX;
00292     point.point.y = absoluteY;
00293     erscribble_stroke_driver_draw_point(stroke, &point, TRUE);
00294     
00295     // draw
00296     draw_stroke(stroke, sgc, pageX, pageY);
00297     
00298     // add & reset
00299     erscribble_doc_add_page(doc, page);
00300     page = 0;
00301     stroke = 0;
00302 }
00303 
00304 void ScribbleMgr::on_scribble_begin(const ScbPageId & pageId,
00305                                     ScribbleGC & sgc,
00306                                     const int absoluteX,
00307                                     const int absoluteY,
00308                                     const int offsetX,
00309                                     const int offsetY,
00310                                     const int pageX,
00311                                     const int pageY)
00312 {
00313     printf("PageId %s page %p stroke %p!", pageId.id, page, stroke);
00314     if (!is_page_id_empty(pageId))
00315     {
00316         if (page && stroke)
00317         {
00318             add_point(pageId, sgc, absoluteX, absoluteY, offsetX, offsetY);
00319         }
00320         else if (page == 0 && stroke == 0)
00321         {
00322             begin_stroke(pageId, sgc, absoluteX, absoluteY, offsetX, offsetY);
00323         }
00324 
00325         // record the last point
00326         last_absolute_point.x = absoluteX;
00327         last_absolute_point.y = absoluteY;
00328         last_page_point.x = offsetX;
00329         last_page_point.y = offsetY;
00330         last_page_position.x = pageX;
00331         last_page_position.y = pageY;
00332     }
00333 }
00334 
00335 /// @brief Should consider following situations:
00336 /// - Move from page to window(out of page).
00337 /// - Move from window(out of page) to page.
00338 /// - Move from one page to the other.
00339 void ScribbleMgr::on_scribble_move(const ScbPageId & pageId, 
00340                                    ScribbleGC & sgc,
00341                                    const int absoluteX, 
00342                                    const int absoluteY,      
00343                                    const int offsetX, 
00344                                    const int offsetY,
00345                                    const int pageX,
00346                                    const int pageY)
00347 {
00348     // out of page, end stroke now.
00349     if (is_page_id_empty(pageId) && stroke && page)
00350     {
00351         // end without adding it
00352         end_stroke(sgc, absoluteX, absoluteY, pageX, pageY);
00353         return;
00354     }
00355 
00356     bool ret = false;
00357     // re-enter page bound
00358     if (!ret && !is_page_id_empty(pageId) && stroke == 0 && page == 0)
00359     {
00360         begin_stroke(pageId, sgc, absoluteX, absoluteY, offsetX, offsetY);
00361         ret = true;
00362     }
00363 
00364     // from one page to the other, should use old pagex and pagey
00365     if (!ret && !is_page_id_empty(last_page_id) && 
00366         !is_page_id_empty(pageId) && 
00367         !cmp_page_id(pageId, last_page_id) && stroke && page)
00368     {
00369         end_stroke(last_page_id, sgc, last_absolute_point.x, last_absolute_point.y
00370             , last_page_point.x, last_page_point.y, last_page_position.x, last_page_position.y);
00371         begin_stroke(pageId, sgc, absoluteX, absoluteY, offsetX, offsetY);
00372         ret = true;
00373     }
00374 
00375     // scribble mode
00376     if (!ret && stroke)
00377     {
00378         add_point(pageId, sgc, absoluteX, absoluteY, offsetX, offsetY);
00379         // record the last point
00380         last_absolute_point.x = absoluteX;
00381         last_absolute_point.y = absoluteY;
00382         last_page_point.x = offsetX;
00383         last_page_point.y = offsetY;
00384         last_page_position.x = pageX;
00385         last_page_position.y = pageY;
00386         ret = true;
00387     }
00388 }
00389 
00390 void ScribbleMgr::on_scribble_end(const ScbPageId & pageId,
00391                                   ScribbleGC & sgc,
00392                                   const int absoluteX,
00393                                   const int absoluteY,
00394                                   const int offsetX,
00395                                   const int offsetY,
00396                                   const int pageX,
00397                                   const int pageY)
00398 {
00399     if (!is_page_id_empty(pageId) && stroke && doc && page)
00400     {
00401         end_stroke(pageId, sgc, absoluteX, absoluteY, offsetX, offsetY, pageX, pageY);
00402         // record the last point
00403         last_absolute_point.x = absoluteX;
00404         last_absolute_point.y = absoluteY;
00405         last_page_point.x = offsetX;
00406         last_page_point.y = offsetY;
00407         last_page_position.x = pageX;
00408         last_page_position.y = pageY;
00409     }
00410 }
00411 
00412 /// When erasing, no need to check whether or not go into another page
00413 void ScribbleMgr::on_erase_begin(const ScbPageId & pageId,
00414                                  ScribbleGC & sgc,
00415                                  const int absoluteX,
00416                                  const int absoluteY,
00417                                  const int offsetX,
00418                                  const int offsetY,
00419                                  const int pageX,
00420                                  const int pageY)
00421 {
00422     if (is_page_id_empty(pageId))
00423     {
00424         return;
00425     }
00426 
00427     // prepare for erasing.
00428     erscribble_page_erase_init(&erase_context);
00429     page = erscribble_doc_get_page(doc, (const ScbPageIdPtr)(&pageId));
00430     if (page == 0)
00431     {
00432         return;
00433     }
00434 
00435     // record
00436     copy_page_id(last_page_id, pageId);
00437 
00438     ScbDevPoint point;
00439     point.point.x = offsetX, point.point.y = offsetY;
00440     erase_context.zoom = sgc.realZoom;
00441     ScbStrokesPtr strokes = erscribble_page_erase_hit_test(page, &point, &erase_context);
00442     if (strokes)
00443     {
00444         printf("Erase strokes!");
00445 
00446         // draw and free
00447         if (sgc.rotation == 270)
00448         {
00449             draw_erased_strokes_landscape(strokes, sgc, pageX, pageY);
00450         }
00451         else
00452         {
00453             draw_erased_strokes_portrait(strokes, sgc, pageX, pageY);
00454         }
00455 
00456         // free
00457         erscribble_strokes_free(strokes);
00458     }
00459 }
00460 
00461 void ScribbleMgr::on_erase_move(const ScbPageId & pageId,
00462                                 ScribbleGC & sgc,
00463                                 const int absoluteX,
00464                                 const int absoluteY,
00465                                 const int offsetX,
00466                                 const int offsetY,
00467                                 const int pageX,
00468                                 const int pageY)
00469 {
00470     if (is_page_id_empty(pageId))
00471     {
00472         return;
00473     }
00474 
00475     if (!cmp_page_id(last_page_id, pageId))
00476     {
00477         copy_page_id(last_page_id, pageId);
00478         erscribble_page_erase_init(&erase_context);
00479         erase_context.zoom = sgc.realZoom;
00480     }
00481 
00482     page = erscribble_doc_get_page(doc, (const ScbPageIdPtr)(&pageId));
00483     if (page == 0)
00484     {
00485         return;
00486     }
00487 
00488     ScbDevPoint point;
00489     point.point.x = offsetX; point.point.y = offsetY;
00490 
00491     ScbStrokesPtr strokes = erscribble_page_erase_hit_test(page, &point, &erase_context);
00492     if (strokes)
00493     {
00494         printf("Erase strokes!");
00495         if (sgc.rotation == 270)
00496         {
00497             draw_erased_strokes_landscape(strokes, sgc, pageX, pageY);
00498         }
00499         else
00500         {
00501             draw_erased_strokes_portrait(strokes, sgc, pageX, pageY);
00502         }
00503 
00504         // free 
00505         erscribble_strokes_free(strokes);
00506     }
00507 }
00508 
00509 void ScribbleMgr::clear_page(const ScbPageId &pageId) 
00510 {
00511     ScbPagePtr page;
00512     page = erscribble_doc_get_page(doc, (const ScbPageIdPtr)(&pageId));
00513     if (page == 0)
00514     {
00515         printf("no page to erase\n");
00516         return;
00517     }
00518     erscribble_page_clear_strokes(page);
00519 }
00520 
00521 void ScribbleMgr::on_erase_end(const ScbPageId & pageId, 
00522                                ScribbleGC & sgc,
00523                                const int absoluteX, 
00524                                const int absoluteY,      
00525                                const int offsetX, 
00526                                const int offsetY,
00527                                const int pageX,
00528                                const int pageY)
00529 {
00530     if (is_page_id_empty(pageId) || page == 0)
00531     {
00532         return;
00533     }
00534 
00535     ScbDevPoint point;
00536     point.point.x = offsetX;
00537     point.point.y = offsetY;
00538     ScbStrokesPtr strokes = erscribble_page_erase_hit_test(page, &point, &erase_context);
00539 
00540     if (!is_page_dirty(page))
00541     {
00542         // add page into dirty list
00543         set_page_dirty(page);
00544     }
00545 
00546     // reset
00547     page = 0;
00548     if (strokes) 
00549     {
00550         printf("Erase strokes!");
00551         if (sgc.rotation == 270)
00552         {
00553             draw_erased_strokes_landscape(strokes, sgc, pageX, pageY);
00554         }
00555         else
00556         {
00557             draw_erased_strokes_portrait(strokes, sgc, pageX, pageY);
00558         }
00559 
00560         // draw and free
00561         erscribble_strokes_free(strokes);
00562     }
00563 }
00564 
00565 //////////////////////////////////////////////////////////////////////////
00566 // x & y is the page start position in screen
00567 void ScribbleMgr::draw_scribble_page(const ScbPageId & pageId,
00568                                      ScribbleGC & sgc,
00569                                      const int pageX, 
00570                                      const int pageY)
00571 {
00572 
00573     // page from page id
00574     ScbPagePtr ptr = erscribble_doc_get_page(doc, (const ScbPageIdPtr)(&pageId));
00575     if (ptr == 0)
00576     {
00577         printf("page %s not found!", pageId.id);
00578         return;
00579     }
00580 
00581     if (sgc.rotation == 270)
00582     {
00583         draw_scribble_page_landscape(ptr, sgc, pageX, pageY);
00584     }
00585     else
00586     {
00587         draw_scribble_page_portrait(ptr, sgc, pageX, pageY);
00588     }
00589 }
00590 
00591 // should not exceed the max pen line size.
00592 int ScribbleMgr::calc_line_size(const int penSize, 
00593                                 const float strokeZoom, 
00594                                 const float dispZoom)
00595 {
00596     if (fabs(strokeZoom - dispZoom) < e)
00597     {
00598         return penSize;
00599     }
00600     int ret = (int)(penSize * strokeZoom / dispZoom);
00601     if (ret < 1)
00602     {
00603         return 1;
00604     }
00605     else if (ret > 10) 
00606     {
00607         return 10;
00608     }
00609     return ret;
00610 }
00611 
00612 void ScribbleMgr::draw_scribble_page_portrait(ScbPagePtr page,
00613                                               ScribbleGC & sgc,
00614                                               const int pageX,
00615                                               const int pageY)
00616 {
00617     ScbStrokesPtr strokes = erscribble_page_get_strokes(page);
00618     float z = sgc.realZoom;
00619     if (strokes)
00620     {
00621         ScbStrokePtr stroke = NULL;
00622         GList *ptr = g_list_first(strokes->strokes);
00623         while (ptr)
00624         {
00625             stroke = (ScbStrokePtr)ptr->data;
00626             if (stroke)
00627             {
00628                 // point data
00629                 int count  = erscribble_stroke_get_point_count(stroke);
00630                 ScbPoint * pts = (ScbPoint *)erscribble_stroke_get_point_data(stroke);
00631 
00632                 // select color. 
00633                 ScbColor color;
00634                 erscribble_dev_color_to_color(&color, stroke->attributes.color);
00635                 sgc.set_line_color(color.pixel);
00636 
00637                 // select line width and line style.
00638                 sgc.set_line_attributes(calc_line_size(stroke->attributes.size_id, z, stroke->attributes.zoom));
00639 
00640                 // draw lines now
00641                 if (fabs(z - stroke->attributes.zoom) < e)
00642                 {
00643                     int i = 0;
00644                     int x1 = pageX + pts[i].x, y1 = pageY + pts[i].y; ++i;
00645                     int x2, y2;
00646                     while (i < count)
00647                     {
00648                         x2 = pageX + pts[i].x, y2 = pageY + pts[i].y; ++i;
00649                         sgc.draw_line(x1, y1, x2, y2);
00650                         x1 = x2; y1 = y2;
00651                     }
00652                     
00653                     if (count <= 1)
00654                     {
00655                         sgc.draw_line(x1, y1, x1, y1);
00656                     }
00657                 }
00658                 else
00659                 {
00660                     float ratio = z / stroke->attributes.zoom;
00661                     int i = 0;
00662                     int x1 = pageX + (int)(ratio * pts[i].x), y1 = pageY + (int)(ratio * pts[i].y); ++i;
00663                     int x2, y2;
00664                     while (i < count)
00665                     {
00666                         x2 = pageX + (int)(ratio * pts[i].x); y2 = pageY + (int)(ratio * pts[i].y); ++i;
00667                         sgc.draw_line(x1, y1, x2, y2);
00668                         x1 = x2; y1 = y2;
00669                     }
00670 
00671                     if (count <= 1)
00672                     {
00673                         sgc.draw_line(x1, y1, x1, y1);
00674                     }
00675                 }
00676             }
00677             ptr = g_list_next(ptr);
00678         }
00679     }
00680 }
00681 
00682 void ScribbleMgr::draw_scribble_page_landscape(ScbPagePtr page, 
00683                                                ScribbleGC & sgc, 
00684                                                const int pageX, 
00685                                                const int pageY)
00686 {
00687     ScbStrokesPtr strokes = erscribble_page_get_strokes(page);
00688     float z = sgc.realZoom;
00689     if (strokes)
00690     {
00691         ScbStrokePtr stroke = NULL;
00692         GList *ptr = g_list_first(strokes->strokes);
00693         while (ptr)
00694         {
00695             stroke = (ScbStrokePtr)ptr->data;
00696             if (stroke)
00697             {
00698                 // point data
00699                 int count  = erscribble_stroke_get_point_count(stroke);
00700                 ScbPoint * pts = (ScbPoint *)erscribble_stroke_get_point_data(stroke);
00701 
00702                 // select color.
00703                 ScbColor color;
00704                 erscribble_dev_color_to_color(&color, stroke->attributes.color);
00705                 sgc.set_line_color(color.pixel);
00706 
00707                 // select pen size
00708                 sgc.set_line_attributes(calc_line_size(stroke->attributes.size_id, z, stroke->attributes.zoom));
00709 
00710                 // draw lines now
00711                 if (fabs(z - stroke->attributes.zoom) < e)
00712                 {
00713                     int i = 0;
00714                     int x1 = pageY - pts[i].x,
00715                         y1 = pageX + pts[i].y; ++i;
00716                     int x2, y2;
00717                     while (i < count)
00718                     {
00719                         x2 = pageY - pts[i].x,
00720                         y2 = pageX + pts[i].y; ++i;
00721                         sgc.draw_line(y1, x1, y2, x2);  // swap
00722                         x1 = x2; y1 = y2;
00723                     }
00724                     
00725                     // single point
00726                     if (1 >= count)
00727                     {
00728                         sgc.draw_line(y1, x1, y1, x1);
00729                     }
00730                 }
00731                 else
00732                 {
00733                     float ratio = z / stroke->attributes.zoom;
00734                     int i = 0;
00735                     int x1 = pageY - (int)(ratio * pts[i].x),
00736                         y1 = pageX + (int)(ratio * pts[i].y);
00737                     ++i;
00738                     int x2, y2;
00739                     while (i < count)
00740                     {
00741                         x2 = pageY - (int)(ratio * pts[i].x),
00742                         y2 = pageX + (int)(ratio * pts[i].y);
00743                         ++i;
00744                         sgc.draw_line(y1, x1, y2, x2);
00745                         x1 = x2; y1 = y2;
00746                     }
00747 
00748                     // single point
00749                     if (1 >= count)
00750                     {
00751                         sgc.draw_line(y1, x1, y1, x1);
00752                     }
00753                 }
00754             }
00755             ptr = g_list_next(ptr);
00756         }
00757     }
00758 }
00759 
00760 //////////////////////////////////////////////////////////////////////////
00761 // redraw stroke by driver
00762 void ScribbleMgr::draw_erased_strokes_portrait(ScbStrokesPtr strokes,
00763                                             ScribbleGC & sgc, 
00764                                             const int pageX, 
00765                                             const int pageY)
00766 {
00767     printf("Going to draw erased strokes %p!", strokes);
00768     float z = sgc.realZoom;
00769     if (strokes)
00770     {
00771         ScbStrokePtr stroke = NULL;
00772         GList *ptr = g_list_first(strokes->strokes);
00773         while (ptr)
00774         {
00775             stroke = (ScbStrokePtr)ptr->data;
00776             if (stroke)
00777             {
00778                 // adjust position
00779                 int count  = erscribble_stroke_get_point_count(stroke);
00780                 printf("Going to draw erased stroke %p count %d now!", stroke, count);
00781                 ScbPoint * pts = (ScbPoint *)erscribble_stroke_get_point_data(stroke);
00782                 
00783                 // draw                
00784                 if (fabs(z - stroke->attributes.zoom) < e)
00785                 {
00786                     int i = 0;
00787                     while (i < count)
00788                     {
00789                         pts[i].x += pageX, pts[i].y += pageY; ++i;
00790                     }                        
00791                 }
00792                 else
00793                 {
00794                     float ratio = z / stroke->attributes.zoom;
00795                     int i = 0;
00796                     while (i < count)
00797                     {
00798                         pts[i].x = pageX + (int)(pts[i].x * ratio);
00799                         pts[i].y = pageY + (int)(pts[i].y * ratio);
00800                         ++i;
00801                     }                        
00802                 }
00803                 stroke->attributes.color = ERSCRIBBLE_DEV_COLOR_WHITE;
00804                 erscribble_stroke_driver_draw(stroke);
00805                 draw_stroke_directly(stroke, sgc);                
00806             }
00807             ptr = g_list_next(ptr);
00808         }
00809     }
00810 }
00811 
00812 void ScribbleMgr::draw_erased_strokes_landscape(ScbStrokesPtr strokes,
00813                                              ScribbleGC & sgc, 
00814                                              const int pageX, 
00815                                              const int pageY)
00816 {
00817     printf("Going to draw erased strokes %p!", strokes);
00818     float z = sgc.realZoom;
00819     if (strokes)
00820     {
00821         ScbStrokePtr stroke = NULL;
00822         GList *ptr = g_list_first(strokes->strokes);
00823         while (ptr)
00824         {
00825             stroke = (ScbStrokePtr)ptr->data;
00826             if (stroke)
00827             {
00828                 printf("Going to draw erased stroke now %p!", stroke);
00829                 // adjust position
00830                 int count  = erscribble_stroke_get_point_count(stroke);
00831                 ScbPoint * pts = (ScbPoint *)erscribble_stroke_get_point_data(stroke);
00832                 
00833                 // draw                
00834                 if (fabs(z - stroke->attributes.zoom) < e)
00835                 {
00836                     int i = 0;
00837                     while (i < count)
00838                     {
00839                         pts[i].x = pageY - pts[i].x;
00840                         pts[i].y = pageX + pts[i].y;
00841                         swap(pts[i].x, pts[i].y);
00842                         ++i;
00843                     }                        
00844                 }
00845                 else
00846                 {
00847                     float ratio = z / stroke->attributes.zoom;
00848                     int i = 0;
00849                     while (i < count)
00850                     {
00851                         pts[i].x = pageY - (int)(pts[i].x * ratio);
00852                         pts[i].y = pageX + (int)(pts[i].y * ratio);
00853                         swap(pts[i].x, pts[i].y);
00854                         ++i;
00855                     }                        
00856                 }
00857                 stroke->attributes.color = ERSCRIBBLE_DEV_COLOR_WHITE;
00858                 erscribble_stroke_driver_draw(stroke);
00859                 printf("Draw stroke on gdk screen!");                
00860                 draw_stroke_directly(stroke, sgc);
00861             }
00862             ptr = g_list_next(ptr);
00863         }
00864     }
00865 }
00866 
00867 void ScribbleMgr::draw_stroke(ScbStrokePtr stroke, 
00868                               ScribbleGC & sgc, 
00869                               const int pageX, 
00870                               const int pageY)
00871 {
00872     if (sgc.rotation == 0)
00873     {
00874         // point data
00875         int count  = erscribble_stroke_get_point_count(stroke);
00876         ScbPoint * pts = (ScbPoint *)erscribble_stroke_get_point_data(stroke);
00877 
00878         // select color. 
00879         ScbColor color;
00880         erscribble_dev_color_to_color(&color, stroke->attributes.color);
00881         sgc.set_line_color(color.pixel);
00882 
00883         // select pen size
00884         sgc.set_line_attributes(stroke->attributes.size_id);
00885 
00886         int i = 0;
00887         int x1 = pageX + pts[i].x, y1 = pageY + pts[i].y; ++i;
00888         int x2, y2;
00889 
00890         while (i < count)
00891         {
00892             x2 = pageX + pts[i].x, y2 = pageY + pts[i].y; ++i;
00893             sgc.draw_line(x1, y1, x2, y2);
00894             x1 = x2; y1 = y2;
00895         }
00896         
00897         if (count <= 1)
00898         {
00899             sgc.draw_line(x1, y1, x1, y1);
00900         }            
00901     }
00902     else if (sgc.rotation == 270)
00903     {
00904         // point data
00905         int count  = erscribble_stroke_get_point_count(stroke);
00906         ScbPoint * pts = (ScbPoint *)erscribble_stroke_get_point_data(stroke);
00907 
00908         // select color. 
00909         ScbColor color;
00910         erscribble_dev_color_to_color(&color, stroke->attributes.color);
00911         sgc.set_line_color(color.pixel);
00912 
00913         // select pen size
00914         sgc.set_line_attributes(stroke->attributes.size_id);
00915         int i = 0;
00916         int x1 = pageY - pts[i].x, y1 = pageX + pts[i].y; ++i;
00917         int x2, y2;
00918         while (i < count)
00919         {
00920             x2 = pageY - pts[i].x, y2 = pageX + pts[i].y; ++i;
00921             sgc.draw_line(y1, x1, y2, x2);    // swap x & y
00922             x1 = x2; y1 = y2;
00923         }
00924          
00925         if (count <= 1)
00926         {
00927             sgc.draw_line(y1, x1, y1, x1);
00928         }            
00929     }
00930 }
00931 
00932 void ScribbleMgr::draw_stroke_directly(ScbStrokePtr stroke, 
00933                                      ScribbleGC & sgc)               
00934 {
00935     // point data
00936     int count  = erscribble_stroke_get_point_count(stroke);
00937     ScbPoint * pts = (ScbPoint *)erscribble_stroke_get_point_data(stroke);
00938 
00939     // select color. 
00940     ScbColor color;
00941     erscribble_dev_color_to_color(&color, stroke->attributes.color);
00942     sgc.set_line_color(color.pixel);
00943 
00944     // select pen size
00945     sgc.set_line_attributes(stroke->attributes.size_id);
00946 
00947     printf("Lines count %d!", count - 1);
00948     for(int i = 0; i < count - 1; ++i)
00949     {
00950         sgc.draw_line(pts[i].x, pts[i].y, pts[i + 1].x, pts[i + 1].y);
00951     }
00952 }
00953 
00954 }//namespace test
00955 
00956                 
Generated by  doxygen 1.6.2-20100208