00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <algorithm>
00028
00029 #include <glib.h>
00030 #include <glib/gprintf.h>
00031
00032 #include <liberscribble/scb.h>
00033 #include "notepad_utils.h"
00034 #include "log.h"
00035
00036 namespace notepad
00037 {
00038 void GCtx::set_color(const ScbDevColor scribblecolor)
00039 {
00040
00041 switch (scribblecolor)
00042 {
00043 case ERSCRIBBLE_DEV_COLOR_WHITE:
00044 color = 0xFFFFFF;
00045 break;
00046 case ERSCRIBBLE_DEV_COLOR_LIGHT_GRAY:
00047 color = 0xAAAAAA;
00048 break;
00049 case ERSCRIBBLE_DEV_COLOR_DARK_GRAY:
00050 color = 0x888888;
00051 break;
00052 case ERSCRIBBLE_DEV_COLOR_BLACK:
00053 color = 0x000000;
00054 break;
00055 case ERSCRIBBLE_DEV_COLOR_UNKNOWN:
00056 default:
00057 color = 0x000000;
00058 break;
00059 }
00060 }
00061
00062
00063 void GCtx::draw_line(GdkImage* image, const int x1, const int y1, const int x2, const int y2)
00064 {
00065 g_assert(image != 0);
00066
00067 Point p1(x1, y1);
00068 Point p2(x2, y2);
00069
00070
00071 draw_brush(image, p2.x, p2.y);
00072
00073 bool is_steep = abs(p2.y - p1.y) > abs(p2.x - p1.x);
00074 if (is_steep)
00075 {
00076 std::swap(p1.x, p1.y);
00077 std::swap(p2.x, p2.y);
00078 }
00079
00080
00081 int deltax = abs(p2.x - p1.x);
00082 int deltaerr = abs(p2.y - p1.y);
00083 int error = 0;
00084 int x = p1.x;
00085 int y = p1.y;
00086
00087
00088 int xstep = (p1.x < p2.x) ? 1 : -1;
00089 int ystep = (p1.y < p2.y) ? 1 : -1;
00090
00091 if (is_steep)
00092 {
00093
00094 for (int numpix = 0; numpix < deltax; numpix++)
00095 {
00096 x += xstep;
00097 error += deltaerr;
00098
00099 if (2 * error > deltax)
00100 {
00101 y += ystep;
00102 error -= deltax;
00103 }
00104
00105 draw_brush(image, y, x);
00106 }
00107 }
00108 else
00109 {
00110
00111 for (int numpix = 0; numpix < deltax; numpix++)
00112 {
00113 x += xstep;
00114 error += deltaerr;
00115
00116 if (2 * error > deltax)
00117 {
00118 y += ystep;
00119 error -= deltax;
00120 }
00121
00122 draw_brush(image, x, y);
00123 }
00124 }
00125
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 void GCtx::draw_brush(GdkImage* image, const int x, const int y)
00171 {
00172 g_assert(image != 0);
00173
00174
00175 gint npoints_in_shape = 0;
00176
00177
00178 GdkPoint brush1[1] = { {x, y } };
00179 GdkPoint brush2[4] = {
00180 {x-1,y } , {x ,y } ,
00181 {x-1,y+1} , {x ,y+1}
00182 };
00183
00184 GdkPoint brush3[5] = {
00185 {x ,y-1} ,
00186 {x-1,y } , {x ,y } , {x+1,y } ,
00187 {x ,y+1}
00188 };
00189
00190 GdkPoint brush4[12] = {
00191 {x-1,y-1} , {x ,y-1} ,
00192 {x-2,y } , {x-1,y } , {x ,y } , {x+1,y },
00193 {x-2,y+1} , {x-1,y+1} , {x ,y+1} , {x+1,y+1},
00194 {x-1,y+2} , {x ,y+2}
00195 };
00196
00197 GdkPoint brush5[13] = {
00198 {x ,y-2} ,
00199 {x-1,y-1} , {x ,y-1} , {x+1,y-1} ,
00200 {x-2,y } , {x-1,y } , {x ,y } , {x+1,y } , {x+2,y} ,
00201 {x-1,y+1} , {x ,y+1} , {x+1,y+1} ,
00202 {x ,y+2}
00203 };
00204
00205 GdkPoint* shape = NULL;
00206
00207 switch(size)
00208 {
00209 case 1:
00210 npoints_in_shape = 1;
00211 shape = brush1;
00212 break;
00213 case 2:
00214 npoints_in_shape = 4;
00215 shape = brush2;
00216 break;
00217 case 3:
00218 npoints_in_shape = 5;
00219 shape = brush3;
00220 break;
00221 case 4:
00222 npoints_in_shape = 12;
00223 shape = brush4;
00224 break;
00225 case 5:
00226 npoints_in_shape = 13;
00227 shape = brush5;
00228 break;
00229 default:
00230 npoints_in_shape = 13;
00231 break;
00232
00233 }
00234 draw_points(image, shape, npoints_in_shape);
00235 }
00236
00237 void GCtx::draw_points(GdkImage* image, const GdkPoint* shape, const int npoints_in_shape)
00238 {
00239 g_assert(image != 0);
00240 g_assert(shape != 0);
00241 g_assert(npoints_in_shape > 0);
00242
00243 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00244 gint w = image->width;
00245 #endif
00246 #if MACHINE_IS_DR800S || MACHINE_IS_DR800SG || MACHINE_IS_DR800SW
00247 gint w = image->width - margin;
00248 #endif
00249 gint h = image->height;
00250
00251 for (int i = 0; i < npoints_in_shape; i++)
00252 {
00253
00254 if ( (shape[i].x > margin && shape[i].x < w)
00255 && (shape[i].y > margin && shape[i].y < h) )
00256 {
00257 gdk_image_put_pixel(image, shape[i].x, shape[i].y, color);
00258 }
00259 }
00260 }
00261
00262
00263 }
00264
00265