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
|
/************************************************************************/
/* */
/* Buffer administration routines relating to the text particules in a */
/* text paragraph. */
/* */
/************************************************************************/
# include "tedConfig.h"
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <appDebugon.h>
# include "docBuf.h"
# include "docEdit.h"
/*
# define DEB_PARTICULES
*/
/************************************************************************/
/* */
/* Split a text particule. */
/* */
/* E.G. To change the attributes of part of its text or to insert */
/* something in the middle. */
/* */
/************************************************************************/
int docSplitTextParticule( TextParticule ** pTpPart,
TextParticule ** pTpNext,
BufferItem * paraBi,
int part,
int stroff )
{
TextParticule tpScratch;
TextParticule * tpPart;
TextParticule * tpNext;
if ( part < 0 || part >= paraBi->biParaParticuleCount )
{ LLDEB(part,paraBi->biParaParticuleCount); return -1; }
tpPart= paraBi->biParaParticules+ part;
tpScratch= *tpPart;
if ( tpPart->tpKind != DOCkindTEXT ||
stroff <= tpPart->tpStroff ||
stroff >= tpPart->tpStroff+ tpPart->tpStrlen )
{
SDEB(docKindStr(tpPart->tpKind));
LLLDEB(stroff,tpPart->tpStroff,tpPart->tpStrlen);
return -1;
}
tpPart= docInsertTextParticule( paraBi, part,
tpScratch.tpStroff,
stroff- tpScratch.tpStroff,
tpScratch.tpKind,
tpScratch.tpTextAttributeNumber );
if ( ! tpPart )
{ LXDEB(part,tpPart); return -1; }
tpNext= tpPart+ 1;
tpNext->tpStroff= stroff;
tpNext->tpStrlen= ( tpScratch.tpStroff+ tpScratch.tpStrlen )- stroff;
*pTpPart= tpPart;
*pTpNext= tpNext;
return 0;
}
/************************************************************************/
/* */
/* Add a Particule to a paragraph. */
/* */
/************************************************************************/
static TextParticule * docInsertParticules( BufferItem * paraBi,
int part,
int count )
{
int i;
TextParticule * tp;
tp= (TextParticule *)realloc( paraBi->biParaParticules,
( paraBi->biParaParticuleCount+ count ) * sizeof( TextParticule ) );
if ( ! tp )
{ LLDEB(paraBi->biParaParticuleCount,tp); return tp; }
paraBi->biParaParticules= tp;
if ( part < 0 )
{ part= paraBi->biParaParticuleCount; }
else{
int tail= paraBi->biParaParticuleCount- part;
tp= paraBi->biParaParticules+ paraBi->biParaParticuleCount+ count- 1;
for ( i= 0; i < tail; tp--, i++ )
{ tp[0]= tp[-count]; }
}
tp= paraBi->biParaParticules+ part;
for ( i= 0; i < count; tp++, i++ )
{
tp->tpTextAttributeNumber= -1;
tp->tpStroff= 0;
tp->tpStrlen= 0;
tp->tpKind= DOCkindTEXT;
tp->tpObjectNumber= -1;
tp->tpX0= 0;
tp->tpPixelsWide= 0;
paraBi->biParaParticuleCount++;
}
return paraBi->biParaParticules+ part;
}
/************************************************************************/
/* */
/* Insert a text particule. */
/* */
/************************************************************************/
TextParticule * docInsertTextParticule( BufferItem * bi,
int part,
int off,
int len,
int kind,
int textAttributeNumber )
{
const int count= 1;
TextParticule * tp;
tp= docInsertParticules( bi, part, count );
if ( ! tp )
{ XDEB(tp); return tp; }
tp->tpStroff= off;
tp->tpStrlen= len;
tp->tpKind= kind;
tp->tpTextAttributeNumber= textAttributeNumber;
return tp;
}
/************************************************************************/
/* */
/* Delete a series of particules. */
/* */
/************************************************************************/
void docDeleteParticules( BufferItem * bi,
int first,
int count )
{
if ( first > bi->biParaParticuleCount )
{
LLDEB(first,bi->biParaParticuleCount);
first= bi->biParaParticuleCount;
}
if ( first+ count > bi->biParaParticuleCount )
{
LLDEB(first+count,bi->biParaParticuleCount);
count= bi->biParaParticuleCount- first;
}
if ( count <= 0 )
{ LDEB(count); return; }
bi->biParaParticuleCount -= count;
while( first < bi->biParaParticuleCount )
{
bi->biParaParticules[first]= bi->biParaParticules[first+ count];
first++;
}
return;
}
/************************************************************************/
/* */
/* Copy particules plus contents from one paragraph to another. */
/* */
/* This might be a copy to a different document, so font numbers and */
/* attribute numbers need to be recalculated. */
/* */
/* 1) Find out where to insert. */
/* (At the end of the paragraph or before particule[partTo]. */
/* 2) Find position and length of the source string. */
/* 3) Find out whether to replace the empty particule that we use to */
/* have at least one particule in a paragraph. */
/* 4) Insert the desired number of particules in the correct */
/* position. Note that that is one less in a paragraph that was */
/* originally empty. */
/* 5) Insert the new text into the paragraph string. */
/* 6) Shift the particules in the tail of the target paragraph to */
/* make place for the new text string bytes. The possible */
/* overwritten 'empty' particule should not be shifted. */
/* 7) Patch the particules in the hole created above to correct */
/* values derived from the source particules. */
/* */
/************************************************************************/
int docCopyParticules( DocumentCopyJob * dcj,
EditOperation * eo,
BufferItem * biTo,
const BufferItem * biFrom,
int partTo,
int partFrom,
int countFrom,
int * pParticulesInserted,
int * pCharactersCopied )
{
TextParticule * tpTo;
const TextParticule * tpFrom;
int stroffTo;
int stroffShift;
int i;
int replaceEmpty= 0;
int stroffFrom;
int strlenFrom;
int notesCopied= 0;
/* 1 */
if ( partTo > biTo->biParaParticuleCount )
{
LLDEB(partTo,biTo->biParaParticuleCount);
partTo= biTo->biParaParticuleCount;
}
if ( partTo == biTo->biParaParticuleCount )
{ stroffTo= biTo->biParaStrlen; }
else{ stroffTo= biTo->biParaParticules[partTo].tpStroff; }
/* 2 */
tpFrom= biFrom->biParaParticules+ partFrom;
stroffFrom= tpFrom->tpStroff;
strlenFrom= tpFrom[countFrom- 1].tpStroff+ tpFrom[countFrom- 1].tpStrlen-
tpFrom->tpStroff;
/* 3 */
if ( biTo->biParaStrlen == 0 && biTo->biParaParticuleCount == 1 )
{
if ( partTo < 0 || partTo > 1 )
{ LDEB(partTo); }
if ( partTo != 0 )
{ partTo= 0; }
replaceEmpty= 1;
}
/* 4 */
tpTo= docInsertParticules( biTo, partTo, countFrom- replaceEmpty );
if ( ! tpTo )
{ XDEB(tpTo); return -1; }
/* 5 */
if ( docParaStringReplace( &stroffShift, biTo, stroffTo, stroffTo,
biFrom->biParaString+ stroffFrom, strlenFrom ) )
{
LDEB(strlenFrom);
docDeleteParticules( biTo, partTo, countFrom- replaceEmpty );
return -1;
}
/* 6 */
if ( docShiftParticuleOffsets( dcj->dcjBdTo, biTo,
partTo+ countFrom,
biTo->biParaParticuleCount, stroffShift ) )
{ LDEB(stroffShift); }
/* 7 */
tpTo= biTo->biParaParticules+ partTo;
for ( i= 0; i < countFrom; tpTo++, tpFrom++, i++ )
{
int textAttributeNumberTo;
textAttributeNumberTo= docMapTextAttributeNumber( dcj,
tpFrom->tpTextAttributeNumber );
if ( textAttributeNumberTo < 0 )
{ LDEB(textAttributeNumberTo); return -1; }
tpTo->tpStroff= stroffTo;
tpTo->tpStrlen= tpFrom->tpStrlen;
tpTo->tpKind= tpFrom->tpKind;
tpTo->tpTextAttributeNumber= textAttributeNumberTo;
/* 4 */
if ( tpFrom->tpKind == DOCkindFIELDSTART &&
dcj->dcjRefFileName )
{
if ( docCopyFieldRelative( dcj, eo,
biTo, biFrom, tpTo, tpFrom ) )
{ LLDEB(partTo,i); return -1; }
}
else{
if ( docCopyParticuleData( dcj, eo,
biTo, biFrom, tpTo, tpFrom ) )
{ LLDEB(partTo,i); return -1; }
}
stroffTo += tpTo->tpStrlen;
if ( tpTo->tpKind == DOCkindNOTE )
{ notesCopied++; }
}
dcj->dcjNotesCopied += notesCopied;
*pParticulesInserted += countFrom- replaceEmpty;
*pCharactersCopied += stroffShift;
return 0;
}
int docCopyParticuleAndData( TextParticule ** pTpTo,
DocumentCopyJob * dcj,
EditOperation * eo,
BufferItem * paraBiTo,
int partTo,
int stroffTo,
int strlenTo,
const BufferItem * paraBiFrom,
const TextParticule * tpFrom )
{
TextParticule tpSaved;
int textAttributeNumberTo;
TextParticule * tpTo;
tpSaved= *tpFrom;
textAttributeNumberTo= docMapTextAttributeNumber( dcj,
tpSaved.tpTextAttributeNumber );
if ( textAttributeNumberTo < 0 )
{ LDEB(textAttributeNumberTo); return -1; }
if ( partTo < 0 )
{ partTo= paraBiTo->biParaParticuleCount; }
tpTo= docInsertTextParticule( paraBiTo, partTo,
stroffTo, strlenTo,
tpSaved.tpKind,
textAttributeNumberTo );
if ( ! tpTo )
{ LXDEB(partTo,tpTo); return -1; }
if ( docCopyParticuleData( dcj, eo,
paraBiTo, paraBiFrom, tpTo, &tpSaved ) )
{
docDeleteParticules( paraBiTo, partTo, 1 );
LDEB(partTo); return -1;
}
*pTpTo= tpTo;
return 0;
}
/************************************************************************/
/* */
/* Save paragraph contents for readers. */
/* */
/************************************************************************/
int docSaveParticules( BufferDocument * bd,
BufferItem * bi,
const TextAttribute * ta,
const unsigned char * map,
const unsigned char * text,
int len )
{
int textAttributeNumber;
textAttributeNumber= docTextAttributeNumber( bd, ta );
if ( textAttributeNumber < 0 )
{ LDEB(textAttributeNumber); return -1; }
if ( docInflateTextString( bi, len ) )
{ LLDEB(bi->biParaStrlen,len); return -1; }
while( len > 0 )
{
const unsigned char * particuleText= text;
int particuleLength= 0;
unsigned char * to;
int i;
int off= bi->biParaStrlen;
while( len > 0 && text[0] != ' ' )
{ text++; particuleLength++; len--; }
while( len > 0 && text[0] == ' ' )
{ text++; particuleLength++; len--; }
/*
appDebug( "%s(%3d): \"%.*s\"\n",
__FILE__, __LINE__, particuleLength, particuleText );
*/
to= bi->biParaString+ off;
if ( map )
{
for ( i= 0; i < particuleLength; i++ )
{ *(to++)= map[*(particuleText++)]; }
}
else{
for ( i= 0; i < particuleLength; i++ )
{ *(to++)= *(particuleText++); }
}
*to= '\0';
bi->biParaStrlen += particuleLength;
if ( ! docInsertTextParticule( bi, -1, off, bi->biParaStrlen- off,
DOCkindTEXT, textAttributeNumber ) )
{ LDEB(bi->biParaParticuleCount); return -1; }
}
return 0;
}
/************************************************************************/
/* */
/* Insert a length 1 particule that is used to represent tabs, breaks, */
/* or objects like images in the paragraph and in the string of the */
/* paragraph. */
/* */
/************************************************************************/
int docSaveSpecialParticule( BufferDocument * bd,
BufferItem * paraBi,
int atBegin,
const TextAttribute * ta,
int kind )
{
int textAttributeNumber;
int n= -1;
int stroffShift= 0;
int stroff= paraBi->biParaStrlen;
if ( paraBi->biLevel != DOClevPARA )
{ LLDEB(paraBi->biLevel,DOClevPARA); return -1; }
if ( atBegin )
{ n= 0; stroff= 0; }
textAttributeNumber= docTextAttributeNumber( bd, ta );
if ( textAttributeNumber < 0 )
{ LDEB(textAttributeNumber); return -1; }
if ( docParaStringReplace( &stroffShift, paraBi, stroff, stroff,
(const unsigned char *)" ", 1 ) )
{ LDEB(paraBi->biParaStrlen); return -1; }
if ( ! docInsertTextParticule( paraBi, n, stroff, 1,
kind, textAttributeNumber ) )
{ LDEB(paraBi->biParaParticuleCount); return -1; }
if ( atBegin )
{
docShiftParticuleOffsets( bd, paraBi,
n+ 1, paraBi->biParaParticuleCount, stroffShift );
}
return 0;
}
/************************************************************************/
/* */
/* Insert a zero length particule that is used for internal */
/* administration such as delimiting text fields. */
/* */
/* Also used to insert the one and only particule in an empty */
/* paragraph. */
/* */
/* 1) Paranoia: can be called directly from file readers. */
/* */
/************************************************************************/
int docInsertAdminParticule( BufferDocument * bd,
BufferItem * paraBi,
int off,
int n,
int objectNumber,
const TextAttribute * ta,
int kind )
{
const int len= 0;
TextParticule * tp;
int textAttrNr;
/* 1 */
if ( ! bd || ! paraBi )
{ XXDEB(bd,paraBi); return -1; }
textAttrNr= docTextAttributeNumber( bd, ta );
if ( textAttrNr < 0 )
{ LDEB(textAttrNr); return -1; }
tp= docInsertTextParticule( paraBi, n, off, len, kind, textAttrNr );
if ( ! tp )
{ XDEB(tp); return -1; }
tp->tpObjectNumber= objectNumber;
return 0;
}
/************************************************************************/
/* */
/* Redivide a piece of a paragraph in particules. */
/* */
/************************************************************************/
int docRedivideStringInParticules( BufferItem * bi,
int strOff,
int strLen,
int part,
int partsFree,
int textAttributeNumber )
{
int bytesDone= 0;
int partsDone= 0;
TextParticule * tp= bi->biParaParticules+ part;
while( bytesDone < strLen )
{
int len= 0;
# ifdef DEB_PARTICULES
char * label= "???";
# endif
while( bytesDone+ len < strLen &&
bi->biParaString[strOff+ len] != ' ' )
{ len++; }
while( bytesDone+ len < strLen &&
bi->biParaString[strOff+ len] == ' ' )
{ len++; }
if ( partsDone < partsFree )
{
tp->tpStroff= strOff;
tp->tpStrlen= len;
tp->tpKind= DOCkindTEXT;
tp->tpTextAttributeNumber= textAttributeNumber;
tp->tpX0= -1;
tp->tpPixelsWide= 0;
# ifdef DEB_PARTICULES
label= "NW.";
# endif
}
else{
tp= docInsertTextParticule( bi, part,
strOff, len, DOCkindTEXT, textAttributeNumber );
if ( ! tp )
{ XDEB(tp); return -1; }
# ifdef DEB_PARTICULES
label= "NW+";
# endif
}
# ifdef DEB_PARTICULES
appDebug( "%s %3d: [%4d..%4d] %s \"%.*s\" len= %d\n", label, part,
tp->tpStroff,
tp->tpStroff+ tp->tpStrlen,
docKindStr( tp->tpKind ),
(int)tp->tpStrlen,
bi->biParaString+ tp->tpStroff,
tp->tpStrlen );
# endif
strOff += len; bytesDone += len;
partsDone++; part++, tp++;
}
return partsDone;
}
/************************************************************************/
/* */
/* Shift the particules in a paragraph after an insertion or a */
/* deletion. */
/* */
/************************************************************************/
int docShiftParticuleOffsets( BufferDocument * bd,
BufferItem * paraBi,
int partFrom,
int partUpto,
int stroffShift )
{
int part;
TextParticule * tp;
if ( stroffShift < 0 )
{
tp= paraBi->biParaParticules+ partFrom;
for ( part= partFrom; part < partUpto; tp++, part++ )
{
if ( tp->tpKind == DOCkindNOTE )
{
DocumentNote * dn;
if ( docGetNote( &dn, bd, paraBi, tp->tpStroff ) < 0 )
{ LDEB(tp->tpStroff); }
else{ dn->dnStroff += stroffShift; }
}
tp->tpStroff += stroffShift;
}
}
if ( stroffShift > 0 )
{
tp= paraBi->biParaParticules+ partUpto- 1;
for ( part= partUpto- 1; part >= partFrom; tp--, part-- )
{
if ( tp->tpKind == DOCkindNOTE )
{
DocumentNote * dn;
if ( docGetNote( &dn, bd, paraBi, tp->tpStroff ) < 0 )
{ LDEB(tp->tpStroff); }
else{ dn->dnStroff += stroffShift; }
}
tp->tpStroff += stroffShift;
}
}
return 0;
}
/************************************************************************/
/* */
/* Determine a text attribute number for a particule. */
/* */
/************************************************************************/
int docTextAttributeNumber( BufferDocument * bd,
const TextAttribute * ta )
{
const DocumentProperties * dp= &(bd->bdProperties);
const DocumentFontList * dfl= &(dp->dpFontList);
int textAttributeNumber;
TextAttribute taCopy= *ta;
if ( taCopy.taFontNumber < 0 ||
taCopy.taFontNumber >= dfl->dflFontCount )
{
if ( taCopy.taFontNumber >= dfl->dflFontCount )
{ LLDEB(taCopy.taFontNumber,dfl->dflFontCount); }
taCopy.taFontNumber= dp->dpDefaultFont;
if ( taCopy.taFontNumber < 0 )
{ taCopy.taFontNumber= 0; }
if ( taCopy.taFontNumber >= dfl->dflFontCount )
{ LLDEB(taCopy.taFontNumber,dfl->dflFontCount); }
}
textAttributeNumber= utilTextAttributeNumber(
&(bd->bdTextAttributeList), &taCopy );
if ( textAttributeNumber < 0 )
{ LDEB(textAttributeNumber); return -1; }
return textAttributeNumber;
}
|