1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "SHWConverter.h"
#include "SExcept.h"
#include "SBinHashtable.h"
#include "SUniMap.h"
#include "SIO.h"
#include "SUtil.h"
#include "SAutogen.h"
static SLineCurve lineCurve32;
static SLineCurve lineCurve12;
/* This allows 20% fuzziness */
#define SD_CADIDATE_PERCENT 20
#define SD_CADIDATE_QUANTITY 5
#include <stdio.h>
#include <string.h>
/*
* Handwriting conversion module for Yudit.
* Gaspar Sinai <gaspar@yudit.org>
*
* A total replacement of
* gtkkanjipad-0.07 Sven Verdoolaege <skimo@kotnet.org>
* http://www.kotnet.org/~skimo/kanji/
* that was mostly copied from Owen Taylor's KanjiPad
* http://www.gtk.org/~otaylor/kanjipad/
* The handwriting recognition algorithms were invented by Todd David
* Rudick (TDR) for his program JavaDict:
* http://www.cs.arizona.edu/japan/JavaDict/
* and translated into C by Robert Wells for his program JStroke:
* http://www.wellscs.com/pilot/
* JStroke runs on Pilot handheld computers.
*
* Changes made by Gaspar Sinai <gaspar@yudit.org>
* o totally documented in the perl output file the
* magic letters and numbers.
* o a total rewrite/reinvention of the scoring system
* - renamed ranking system.
* - correctly calculates probabilities.
* o a total rewrite of all the routines found in original programs,
* for clearity.
* A did not make any change to the original stroke format.
* Reason: I want to be able to add stuff that is used for kanjipad.
*/
class SHWData
{
public:
SHWData(const SString& data);
~SHWData();
SStringVector convert(const SLineCurves& str, bool derected);
unsigned int getIndex(unsigned int stroke);
void reindex();
int count;
int version;
SV_UINT index;
private:
int scoreAll (const SLineCurves& strokes,
const char* buffer, unsigned int from, unsigned int to,
bool directed);
bool rewriteGuideElementV1 (unsigned int element, SV_UINT *guide);
unsigned int rewriteGuideElementsV2 (const char* buffer,
unsigned int size, SV_UINT *guide);
int fitOneStroke (const SLineCurve& stroke,
unsigned int sfrom, unsigned int sto,
const SV_UINT& guide, unsigned int pfrom,
unsigned int pto, int reversed);
int getFilterValue (const SLineCurves& strokes, const char* filter,
unsigned int filterSize);
int applyFilter (const SLineCurves& strokes, const char* operand,
const unsigned int * strokeIndex, bool must);
int calculateFilter (const SLineCurve& stroke, char operand);
SFileImage image;
SString name;
};
/* todo - on exist free this cache */
typedef SBinHashtable<SHWData*> SWTable;
static SWTable* hwcache=0;
/**
* Create a new SHWConverter using name.
* This is an empty converter.
*/
SHWConverter::SHWConverter(void)
{
shared = 0;
}
static int debugLevel = 0;
void
SHWConverter::setDebugLevel (int level)
{
debugLevel = level;
}
/**
* Create a new SHWConverter using name.
* @param name is the name of the conversion.
* SUniMap::getPath() will be searched for name.hwd file.
*/
SHWConverter::SHWConverter(const SString& _name)
{
shared = 0;
/* is it cached ? */
SHWData* data;
name = _name;
if (hwcache == 0) hwcache = new SWTable();
if ((data = hwcache->get (name)) == 0)
{
data = new SHWData(name);
CHECK_NEW (data);
if (data->count==0)
{
delete data;
data = 0;
}
else
{
hwcache->put (name, data);
}
}
else
{
data->count++;
}
shared = data;
}
const SString&
SHWConverter::getName() const
{
return name;
}
/**
* Copy constructor.
* Just shared the existing buffer.
*/
SHWConverter::SHWConverter(const SHWConverter& c)
{
if (&c != this)
{
SHWData* data = (SHWData*) c.shared;
if (data) data->count++;
this->shared = data;
this->name = c.name;
}
}
SHWConverter::~SHWConverter()
{
SHWData* data = (SHWData*) shared;
if (data) data->count--;
}
SHWConverter
SHWConverter::operator= (const SHWConverter& c)
{
if (&c != this)
{
SHWData* data = (SHWData*) shared;
if (data) data->count--;
data = (SHWData*) c.shared;
if (data) data->count++;
this->shared = data;
this->name = c.name;
}
return *this;
}
/**
* Check if this converter is ok
* @return false if this converter can not be used.
*/
bool
SHWConverter::isOK () const
{
SHWData* data = (SHWData*) shared;
if (data !=0) data->reindex();
return (data != 0 && data->index.size() > 0);
}
/**
* Convert hand-written data into a list of candidates.
* @str is the strokelist
* @param directed is true if direction is taken into account.
* @return the candidate list in utf-8
*/
SStringVector
SHWConverter::convert(const SLineCurves& str, bool directed) const
{
SHWData* data = (SHWData*) shared;
if (data == 0) return SStringVector();
return data->convert (str, directed);
}
/**
* Create a new SHWData and set index to 0 if failed, set index to
* 1 if it was successful.
*/
SHWData::SHWData(const SString& data)
{
count = 0;
version = 0;
name = data;
SString n (data);
n.append (".hwd");
SStringVector p = SUniMap::getPath();
SFile f (n, p);
if (f.size() >0)
{
name = f.getName();
image = f.getFileImage();
if (image.size() > 0) count++;
}
}
SHWData::~SHWData()
{
}
/**
* Recalculate indexes so that the index will hash to
* the appropriate stroke number.
* If it can not be, reset the image.
*/
void
SHWData::reindex()
{
if (image.size()<=0 || index.size() > 0) return;
char* buffer = image.array();
unsigned int length = (unsigned int) image.size();
/* filetype check */
if (length<6 || buffer[0] != '#' || buffer[1] != 'H'
|| buffer[2] != 'W' || buffer[3] != 'D'
|| buffer[4] != ' ')
{
/* empty fileimage */
image = SFileImage();
return;
}
/* Version 1 uses mystic letters for directions */
if (buffer[5] == '1')
{
version = 1;
}
/* Version 1 uses numbers for directions on a clock (0..11)*/
else if (buffer[5] == '2')
{
version = 2;
}
else
{
/* empty fileimage */
image = SFileImage();
return;
}
unsigned int i;
unsigned int j;
bool nl = true;
unsigned int linecount=1;
for (i=0; i<length; i++)
{
/* comments */
if (nl && buffer[i] == '\n')
{
linecount++;
continue;
}
if (nl && buffer[i] == '#')
{
nl = false;
continue;
}
/* skip bad lines */
if (nl && length>i + 2 + 1 + 1 + 1)
{
if (buffer[i]<'0' || buffer[i]>'9'
|| buffer[i+1]<'0' || buffer[i+1]>'9'
|| buffer[i+2] != ' ')
{
if (debugLevel > 0)
{
fprintf (stderr, "FileFormat error in SHWConverter: %*.*s line: %u\n",
SSARGS (name), linecount);
}
image = SFileImage();
index.clear();
return;
}
nl = false;
unsigned int strokeCnt = 10 * (unsigned int)(buffer[i]-'0')
+ (unsigned int)(buffer[i+1]-'0');
if (index.size() > strokeCnt+1)
{
if (debugLevel > 0)
{
fprintf (stderr, "SHWConverter: %*.*s line: %u unsorted!\n",
SSARGS (name), linecount);
}
image = SFileImage();
index.clear();
return;
}
/* fill index with zeros */
if (index.size() < strokeCnt+1)
{
j = index.size();
while (j++<strokeCnt) {
index.append (i); // YES, i
}
index.append (i);
}
}
if (buffer[i] == '\n')
{
linecount++;
nl = true;
continue;
}
}
}
/**
* Get index for stroke. Return size if index is too high.
*/
unsigned int
SHWData::getIndex (unsigned int i)
{
if (i>=index.size()) return (unsigned int) image.size();
return index[i];
}
/**
* Convert hand-written data into a list of candidates.
* @str is the strokelist
* @param directed is true if direction is taken into account.
* @return the candidate list in utf-8
*/
SStringVector
SHWData::convert(const SLineCurves& strokes, bool directed)
{
/* version 1.0 */
if (lineCurve32.size() ==0)
{
SLineCurve clock32 = getCurveCircle32();
/* scale it down, to fit on 1 byte */
for (unsigned i=0; i<clock32.size(); i++)
{
const SLocation &l = clock32[i];
lineCurve32.append (SLocation(l.x/512, l.y/512));
}
}
/* version 2.0 */
if (lineCurve12.size() ==0)
{
SLineCurve clock24 = getCurveCircle24();
/* scale it down, to fit on 1 byte */
for (unsigned i=0; i<clock24.size(); i++)
{
/* make it a twelve hour clock */
if (i+1<clock24.size() && (i&1)) continue;
const SLocation &l = clock24[i];
lineCurve12.append (SLocation(l.x/512, l.y/512));
}
}
if (image.size() <= 0 || strokes.size()==0) return SStringVector();
if (index.size() == 0) reindex();
/* FIXME */
unsigned int i = getIndex (strokes.size());
unsigned int j = getIndex (strokes.size()+1);
char* buffer = image.array();
SStringVector candidates;
SV_INT scores;
bool nl = true;
for (; i < j; i++) {
if (nl && buffer[i] == '\n') continue;
/* comments */
if (nl && buffer[i] == '#') { nl = false; continue; }
/* first, rest */
/* space */
if (nl)
{
unsigned int k=i+3;
while (k<j && buffer[k] != '\n' && buffer[k] != ' ') k++;
/* we are a bit corrupt */
if (k>=j || buffer[k] != ' ')
{
break;
}
SString str (&buffer[i+3], k-i-3);
candidates.append (str);
i=k+1; while (k<j && buffer[k] != '\n') k++;
/* add score */
scores.append (scoreAll (strokes, buffer, i, k, directed));
i=k;
}
if (buffer[i] == '\n') { nl = true; continue; }
}
SV_INT sorted;
if (scores.size()==0) return SStringVector();
/* sort stuff */
SStringVector bestOnes;
unsigned int vsize = scores.size();
for (i=0; i<vsize; i++)
{
/* the bigger the score the better it is */
unsigned int order = sorted.appendSorted(-scores[i]);
bestOnes.insert (order, candidates[i]);
}
/* print out the length of full stuff and this length. */
unsigned int fullsize = 0;
for (i=0; i<strokes.size(); i++)
{
fullsize += strokes[i].length();
}
if (debugLevel > 2)
{
fprintf (stderr, "Stokes: count=%u sorted=%u, length=%u bestlength=%d\n",
strokes.size(), sorted.size(), fullsize, -sorted[0]/lineCurve32[16].y);
}
/* the most negatives come first. */
int delta = sorted[0] * SD_CADIDATE_PERCENT / 100; /* 20 percent, negative */
if (delta<0) delta = -delta; // always
for (i=0; i<sorted.size(); i++)
{
if (sorted[i] > sorted[0]+delta || i>SD_CADIDATE_QUANTITY)
{
sorted.truncate (i);
bestOnes.truncate (i);
break;
}
}
return (SStringVector(bestOnes));
}
/*
* Simply put - it calculates the distance of the projection
* of the curve on the guideline. The best it fist the closest
* it gets to the real line-length of strokes.
* @param strokes contains all the strokes in the drawing.
* @param buffer contains the score description line (character guide)
* @param from is the starting point in buffer
* @param to is the ending point in buffer
* @param directed is true if writing direction matters.
* @return a score number. The bigger the number the better it is.
* at the best fit this should be the length of the whole strokes.
*/
int
SHWData::scoreAll (const SLineCurves& strokes, const char* buffer,
unsigned int from, unsigned int to, bool directed)
{
unsigned int cindex = from;
int currentLength = 0;
for (unsigned int i=0; i<strokes.size(); i++)
{
SV_UINT guide; /* start with a clear one */
if (version == 2)
{
/* "1-11-12, 2-3-4" */
cindex += rewriteGuideElementsV2 (&buffer[cindex], to-cindex, &guide);
/* no more guide */
if (cindex>to)
{
if (debugLevel > 0)
{
fprintf (stderr, "Not enough strokes in '%*.*s' expected=%u got=%u\n",
(int)(to-from), (int)(to-from), &buffer[from], strokes.size(), i);
}
return -1;
}
}
else
{
if (!rewriteGuideElementV1 ((unsigned int) buffer[cindex], &guide))
{
if (debugLevel > 0)
{
fprintf (stderr, "Not enough strokes in '%*.*s' expected=%u got=%u\n",
(int)(to-from), (int)(to-from), &buffer[from], strokes.size(), i);
}
return -1;
}
cindex++;
/* get the rest - all small letters - this is in version 1 only */
while (cindex < to && buffer[cindex] != '|'
&& (buffer[cindex] == ' ' || buffer[cindex] >= 'a'))
{
if (!rewriteGuideElementV1 ((unsigned int) buffer[cindex], &guide))
{
if (debugLevel > 0)
{
fprintf (stderr, "Bad syntax in '%*.*s' strokes=%u\n",
(int)(to-from), (int)(to-from), &buffer[from], strokes.size());
}
return -1;
}
cindex++;
}
}
/* this stroke is done */
const SLineCurve& stroke = strokes[i];
/**
* Try positive
*/
int projectedLength = fitOneStroke (stroke, 0, stroke.size(),
guide, 0, guide.size(), 1);
/**
* Try negative - reverse order
*/
if (!directed && projectedLength < 0)
{
projectedLength = fitOneStroke (stroke, 0, stroke.size(),
guide, 0, guide.size(), -1);
}
/*
* Don't add ranks up. Probabilities are multiplied.
* gaspar@yudit.org 2001-11-04
*/
currentLength += projectedLength;
}
/* apply additional filtering - this also return a length type object */
if (cindex+1 < to && buffer[cindex] == '|')
{
currentLength += getFilterValue (strokes, &buffer[cindex+1], to-cindex-1);
}
return currentLength;
}
/**
* Try to fit the Stroke to the guide. This is
* Gaspar Sinai original <gaspar@yudit.org>
* 2001-11-06, Tokyo
* @param stroke is the stroke to rank.
* @param strokefrom is the lower limit in stroke
* @param strokeend is the upper, not including limit in stroke
* @param guide is the angle path 0..31
* @param guidestart is the lower limit in guide
* @param guideend is the upper, not including limit in ppath
* @param directed is
* <ul>
* <li> 1 if directed in positive direction </li>
* <li> -1 if directed in negative direction </li>
* </ul>
* @return the length of the fit, that is projected with a
* unit vector to the angle. it can be negative if opposite
* fit fits.
*/
int
SHWData::fitOneStroke (const SLineCurve& stroke, unsigned int strokefrom,
unsigned int strokeend, const SV_UINT& guide, unsigned int guidestart,
unsigned int guideend, int directed)
{
/* Should not happen unless we have a corrupt guide */
if (guideend-guidestart < 1 || stroke.length()==0) return 0;
/* End of recursize loop - not dividable any more */
if (strokeend-strokefrom < 2 || guideend-guidestart==1)
{
if (strokeend-strokefrom < 1) return 0;
SLocation v = stroke.getVector (strokefrom, strokeend-1);
const SLocation& l = (version==1)
? lineCurve32[guide[guidestart]] // 36 JavaDict, JStroke, Kanjipad
: lineCurve12[guide[guidestart]]; // 12 Yudit version
int ret = v.scalarProduct(l) * directed;
if (debugLevel > 10)
{
fprintf (stderr,"fitOneStroke[%u..%u] guide [%u..%u] directed=%d ret=%d\n",
strokefrom, strokeend, guidestart, guideend, directed, ret);
fprintf (stderr, "unit=%d,%d stroke=%d,%d\n", l.x, l.y, v.x, v.y);
}
return ret;
}
/*
* cut the guide into two equal halves and find
* a partition that has longest projected length
* Gaspar Sinai <gaspar@yudit.org>
*/
int maxlength = -1;
unsigned int pmid = (guideend-guidestart) / 2 + guidestart;
unsigned int s0 = guidestart;
unsigned int s1 = pmid;
unsigned int e0 = pmid;
unsigned int e1 = guideend;
/* reverse direction */
if (directed<0)
{
s0=pmid;
s1=guideend;
e0=guidestart;
e1=pmid;
}
/*
No need
unsigned int sstep = (strokeend - strokefrom) / 2;
if (sstep <1) sstep = 1;
*/
/* slowly move it */
unsigned int sstep = 1;
int fulllength = (int)stroke.length();
for (unsigned int i=strokefrom; i+sstep<strokeend; i+=sstep)
{
/*
* continue if not evenly distributed
*/
int l0 = (int)stroke.length (strokefrom, i+sstep);
int l1 = (int)stroke.length (i+sstep, strokeend-1);
/* l0/full is like s1-s0/guideend - guidestart*/
/* best if: fulllength * (s1-s0) == l0 * (guideend - guidestart); */
/* best if half factor 2 - but let's not be so strict */
#define SD_DIVIDE_FACT 4
if ((fulllength * (s1-s0)) > SD_DIVIDE_FACT * l0 * (guideend - guidestart)
|| SD_DIVIDE_FACT * (fulllength * (s1-s0)) < l0 * (guideend - guidestart))
{
continue;
}
if ((fulllength * (e1-e0)) > SD_DIVIDE_FACT * l1 * (guideend - guidestart)
|| SD_DIVIDE_FACT * (fulllength * (e1-e0)) < l1 * (guideend - guidestart))
{
continue;
}
int length0 = fitOneStroke (stroke, strokefrom, i+sstep+1,
guide, s0, s1, directed);
int length1 = fitOneStroke (stroke, i+sstep, strokeend,
guide, e0, e1, directed);
int thislength = length0 + length1;
if (thislength > maxlength) maxlength = thislength;
}
return maxlength;
}
/**
* Filter is a very strong ranker.
* @return a direction-like object.
*/
int
SHWData::getFilterValue (const SLineCurves& strokes,
const char* filter, unsigned int filterSize)
{
char oparand[2];
unsigned int strokeIndex[2];
/* init */
oparand[0] = oparand[1] = 0;
strokeIndex[0] = strokeIndex[1] = 0;
bool must = false;
unsigned idx=0; /* 0 or 1 */
int currentLength = 0;
int minLength = 0;
int maxLength = 0;
int length = 0;
for (unsigned int i=0; i<filterSize; i++) {
switch (filter[i])
{
case 'x':
case 'y':
case 'i':
case 'j':
case 'a':
case 'b':
case 'l':
oparand [idx] = filter[i];
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
strokeIndex[idx] = strokeIndex[idx] * 10 + (filter[i] - '0');
break;
case '-': /* next operand */
idx = 1;
break;
case '!': /* filter must be applied */
must = true;
break;
case ' ':
if (idx==0) break;
default:
if (idx != 1 || strokeIndex[0] > strokes.size() || strokeIndex[0] == 0
|| strokeIndex[1] > strokes.size() || strokeIndex[1] == 0)
{
if (debugLevel > 0)
{
fprintf (stderr, "Filter syntax error in %*.*s at '%c'\n",
(int)filterSize, (int)filterSize, filter, filter[i]);
}
return 0;/* neutral */
}
/* apply filter */
length = applyFilter(strokes, oparand, strokeIndex, must);
currentLength += length;
if (length < minLength) minLength = length;
if (length > maxLength) maxLength = length;
/* init */
oparand[0] = oparand[1] = 0;
strokeIndex[0] = strokeIndex[1] = 0;
must = false;
idx = 0;
}
/* end case */
}
/* last filter */
if (idx == 1)
{
/* apply filter */
length = applyFilter(strokes, oparand, strokeIndex, must);
currentLength += length;
if (length < minLength) minLength = length;
if (length > maxLength) maxLength = length;
}
if (currentLength == 0) return 0;
if (currentLength < 0) return minLength;
return currentLength;
}
/**
* @return 0..127 31 being neutral
* @param strokes contains all the strokes.
* @param operand contains the operands (2 of them)
* @param strokeIndex contains the index in stoke array.
* 1 being the first element.
* @param must can enforce the filter.
*/
int
SHWData::applyFilter (const SLineCurves& strokes, const char* operand,
const unsigned int * strokeIndex, bool must)
{
int val0 = calculateFilter (strokes[strokeIndex[0]-1], operand[0]);
int val1 = calculateFilter (strokes[strokeIndex[1]-1], operand[1]);
int diff = val0 - val1;
/* plus is good, negative is a bad thing. */
if (debugLevel > 10)
{
fprintf (stderr, "apply %c%d-%c%d%s = %d %d-%d\n",
operand[0], strokeIndex[0],
operand[1], strokeIndex[1], (must?"!":""), diff, val0, val1);
}
if (must) return diff;
return 2 * diff * SD_CADIDATE_PERCENT / 100;
//return (must) ? diff : diff * SD_CADIDATE_PERCENT / 100 / 2;
}
/**
* calculate one filter element.
* @param stroke contains the stroke, only one.
* @param operand is the operand
* @return length type argument.
*/
int
SHWData::calculateFilter(const SLineCurve& stroke, char operand)
{
const SLocation& fl = stroke[0];
const SLocation& ll = stroke[stroke.size()-1];
switch (operand)
{
case 'x': return fl.x; /* first */
case 'y': return fl.y; /* first */
case 'i': return ll.x; /* second */
case 'j': return ll.y; /* second */
case 'a': return (ll.x+fl.x)/2; /* middle */
case 'b': return (ll.y+fl.y)/2; /* middle */
case 'l': return (ll-fl).distance(); /* length */
}
/* should not happen */
return 0;
}
/**
* Try to rewrite version 2 syntax guide till size or
* '|'. '\n' and white space are ok.
* Otherwise update guide with values according to 12 hour clock
* angles.
* @return the next element after white space has been eaten up.
* in case of error, return size+1.
*/
unsigned int
SHWData::rewriteGuideElementsV2 (const char* buffer,
unsigned int size, SV_UINT *guide)
{
bool param=false;
bool done=false;
bool separated=true;
unsigned int currentAngle = 0;
unsigned int i;
for (i=0; i<size; i++)
{
switch (buffer[i])
{
case ' ':
case '\t':
if (param)
{
if (currentAngle > 12)
{
if (debugLevel > 0)
{
fprintf (stderr, "Guide: angle too large %u (max. is 12).\n",
currentAngle);
}
return size+1;
}
guide->append (currentAngle);
param = false;
currentAngle=0;
separated = false;
}
break;
case ',':
done = true;
i++;
break;
case '-':
if (!param) return size+1;
if (currentAngle > 12)
{
if (debugLevel > 0)
{
fprintf (stderr, "Guide: angle too large %u (max. is 12).\n",
currentAngle);
}
return size+1;
}
guide->append (currentAngle);
param = false;
currentAngle=0;
separated=true;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (!separated)
{
if (debugLevel > 0)
{
fprintf (stderr,"Guide: parameters should be separated with '-'.\n");
}
return size+1;
}
currentAngle=currentAngle*10 + (unsigned int) (buffer[i] - '0');
param = true;
break;
case '|':
case '\n':
done = true;
break;
default:
if (debugLevel > 0)
{
fprintf (stderr, "Guide: illegal character.\n");
}
return size+1;
}
if (done) break;
}
if (param)
{
if (currentAngle > 12)
{
if (debugLevel > 0)
{
fprintf (stderr, "Guide: angle-param too large %u (max. is 12).\n",
currentAngle);
}
return size+1;
}
guide->append (currentAngle);
}
if (guide->size()==0)
{
if (debugLevel > 0)
{
fprintf (stderr, "Guide: empty guide.\n");
}
return size+1;
}
while (i<size && (buffer[i] == ' ' || buffer[i] == '\t')) i++;
return i;
}
/**
* Rewrite the guide element to angle in a 0.31 scale.
* 0 being 0 degrees 8 being 90
* @param element is the character to rewrite
* @param guide is the vector to put this.
*/
bool
SHWData::rewriteGuideElementV1 (unsigned int element, SV_UINT *guide)
{
switch ((char)element)
{
case ' ':
break;
case 'A':
case 'a':
guide->append (20); break; /* TDR='1' CLK=07:30 DEG=225 */
case 'B':
case 'b':
guide->append (16); break; /* TDR='2' CLK=06:00 DEG=180 */
case 'C':
case 'c':
guide->append (12); break; /* TDR='3' CLK=04:30 DEG=135 */
case 'D':
case 'd':
guide->append (24); break; /* TDR='4' CLK=09:00 DEG=270 */
case 'F':
case 'f':
guide->append (8); break; /* TDR='6' CLK=03:00 DEG=090 */
case 'G':
case 'g':
guide->append (28); break; /* TDR='7' CLK=10:30 DEG=315 */
case 'H':
case 'h':
guide->append (0); break; /* TDR='8' CLK=12:00 DEG=360 */
case 'I':
case 'i':
guide->append (4); break; /* TDR='9' CLK=01:30 DEG=045 */
/* double ones */
case 'J':
case 'j':
guide->append (16); guide->append (20);/* TDR='x' down 06:00 then 07:30 */
break;
case 'K':
case 'k':
guide->append (16); guide->append (12);/* TDR='y' down 06:00 then 04:30 */
break;
case 'L':
case 'l':
guide->append (16); guide->append (8);/* TDR='c' down 06:00 then 03:00 */
break;
case 'M':
case 'm':
guide->append (8); guide->append (16);/* TDR='b' across 03:00 then 06:00 */
break;
default:
return false;
}
return true;
}
|