00001 #include <stdio.h>
00002 #include <avr/pgmspace.h>
00003 #include <math.h>
00004 #include <inttypes.h>
00005
00006 #include "motori.h"
00007 #include "shvars.h"
00008 #include "scale.h"
00009 #include "configs.h"
00010 #include "htext.h"
00011
00012 USER_POINT fontscale;
00013 USER_POINT charorigin;
00014 USER_POINT labelorigin;
00015
00016 USER_COORD sintheta, costheta;
00017
00018 void text_init() {
00019 text_setscale(10,10);
00020 text_direction(0, 1);
00021 }
00022
00023 void text_setscale(double sx, double sy) {
00024 fontscale.x = sx;
00025 fontscale.y = sy;
00026
00027 printf_P(PSTR("Text scale is (%f, %f)\n"), fontscale.x, fontscale.y);
00028 }
00029
00030 void text_scale_cm(double cx, double cy) {
00031 double sx = cx*10/7/0.025;
00032 double sy = cy*10/10/0.025;
00033 userprescale(sx, sy, &fontscale.x, &fontscale.y);
00034 text_setscale(fontscale.x, fontscale.y);
00035 }
00036
00037 void text_scale_rel(double rx, double ry) {
00038 USER_POINT prect = scale_P1P2();
00039 double sx = rx/100 * prect.x/7;
00040 double sy = ry/100 * prect.y/10;
00041 text_setscale(sx, sy);
00042 }
00043
00044 void text_direction(double cost, double sint) {
00045 sintheta = -sint;
00046 costheta = cost;
00047
00048 printf_P(PSTR("Label rotation: sin=%f cos=%f\n"), sintheta, costheta);
00049 }
00050
00051 static void rotate(double* x, double* y) {
00052 double xc = *x - charorigin.x;
00053 double yc = *y - charorigin.y;
00054 double xrot = xc*costheta + yc*sintheta;
00055 double yrot = -xc*sintheta + yc*costheta;
00056
00057 *x = xrot + charorigin.x;
00058 *y = yrot + charorigin.y;
00059 }
00060
00061 static PGM_P coffs;
00062
00063 void text_beginlabel() {
00064 labelorigin = user_loc;
00065 printf_P(PSTR("Label origin: (%f,%f)\n"), labelorigin.x, labelorigin.y);
00066 }
00067
00068 uint8_t text_char(uint8_t c, STEPPER_COORD* dstx, STEPPER_COORD* dsty, uint8_t* pen) {
00069 double xd, yd;
00070 uint8_t encoded;
00071 static uint8_t noadvance = 0;
00072
00073 if (c != 0) {
00074 noadvance = 0;
00075 switch (c) {
00076 case '\r':
00077 printf_P(PSTR("CR"));
00078 xd = labelorigin.x;
00079 yd = labelorigin.y;
00080
00081 userscale(xd, yd, dstx, dsty, &user_loc.x, &user_loc.y);
00082 charorigin = user_loc;
00083 *pen = 0;
00084 encoded = 1;
00085 coffs = charset0[0];
00086 noadvance = 1;
00087 break;
00088 case '\n':
00089 printf_P(PSTR("LF"));
00090 xd = user_loc.x;
00091 yd = user_loc.y - fontscale.y*10;
00092 rotate(&xd, &yd);
00093 userscale(xd, yd, dstx, dsty, &user_loc.x, &user_loc.y);
00094 charorigin = user_loc;
00095 labelorigin = user_loc;
00096 *pen = 0;
00097 encoded = 1;
00098 coffs = charset0[0];
00099 noadvance = 1;
00100 break;
00101 default:
00102 coffs = charset0[c];
00103 *pen = 0;
00104 charorigin = user_loc;
00105
00106 encoded = 1;
00107
00108 break;
00109 }
00110 } else {
00111 encoded = *coffs++;
00112
00113 *pen = encoded & 0200 ? 1 : 0;
00114
00115 if (encoded) {
00116 xd = charorigin.x + fontscale.x * ((encoded >> 4) & 007);
00117 yd = charorigin.y + fontscale.y * ((encoded & 017) - 4);
00118 } else {
00119 if (!noadvance) {
00120 xd = charorigin.x + fontscale.x * 5;
00121 yd = charorigin.y;
00122 }
00123 }
00124
00125 if (!noadvance) {
00126 rotate(&xd, &yd);
00127
00128 userscale(xd, yd, dstx, dsty, &user_loc.x, &user_loc.y);
00129 }
00130 }
00131
00132 return encoded != 0;
00133 }