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
|
/**
* 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 "swindow/SFontImpl.h"
#include "swindow/SFontTTF.h"
#include "swindow/SFontNative.h"
#include "swindow/SFontBDF.h"
#include "swindow/SUniFont.h"
#include "swindow/SAwt.h"
#include "swindow/SImage.h"
#include "stoolkit/SIO.h"
#include "stoolkit/SBinHashtable.h"
#include "stoolkit/SHashtable.h"
#include "stoolkit/SExcept.h"
#include "stoolkit/SUtil.h"
#include "stoolkit/SCluster.h"
#include "stoolkit/STypes.h"
static SStringVector fontSearchPath(
"/,fonts,../fonts,/etc/fonts,/usr/share/yudit/fonts");
/* it makes a big difference in speed */
/**
* @author: Gaspar Sinai <gaspar@yudit.org>
* @version: 2000-04-23
* This is the abstract widget toolkit font package
*/
class SFontDelegate
{
public:
enum SE_Type { SE_NONE, SE_TTF, SE_NATIVE, SE_UNIFONT, SE_BDF };
SFontDelegate (const SFile& file, SE_Type type,
const SString& encoding=SString(""));
~SFontDelegate();
unsigned int count;
SE_Type type;
union {
SFontTTF* ttf;
SFontNative* native;
SUniFont* unifont;
SFontBDF* bdf;
} u;
};
/**
* Create a brand new delegate
*/
SFontDelegate::SFontDelegate (const SFile& file,
SE_Type _type, const SString& enc)
{
count = 1;
type = _type;
u.ttf = 0;
switch (type)
{
case SE_TTF:
u.ttf = new SFontTTF (file, enc);
if (!u.ttf->isOK())
{
delete u.ttf;
u.ttf = 0;
type = SE_NONE;
}
break;
case SE_NATIVE:
u.native = SAwt::getFont(enc);
break;
case SE_UNIFONT:
u.unifont = new SUniFont(file);
break;
case SE_BDF:
u.bdf = new SFontBDF(file);
break;
case SE_NONE:
break;
}
}
SFontDelegate::~SFontDelegate()
{
switch (type)
{
case SE_TTF:
if (u.ttf!=0) delete u.ttf;
break;
case SE_NATIVE:
if (u.native!=0) delete u.native;
break;
case SE_UNIFONT:
if (u.unifont!=0) delete u.unifont;
break;
case SE_BDF:
if (u.bdf!=0) delete u.bdf;
break;
case SE_NONE:
break;
}
}
typedef SBinHashtable<SFontDelegate*> SFontCache;
static SFontCache fontCache;
/**
* Create a new font face.
* @param _name is the logical x11 font descriptor that matches
* this font. "-%s-%s-%s-%s-*-%s-%g-*-*-*-%s-%s-%s-%s"
* foundry-family-weight-slant-*-style-pixel-*-*-*-spacing-avgWidth-registry-encoding
* @param encoding is not zero size if there is an external encoder.
*/
SFontImpl::SFontImpl (const SString& _name, const SString& _encoding) :
xlfd (_name), name(_name), encoding (_encoding)
{
/**
* Try to find TTF first
*/
rlFont = false;
lrFont = false;
SString decorated (name);
decorated.append ("~");
decorated.append (encoding);
//fprintf (stderr, "XLFD %*.*s\n", SSARGS(xlfd));
SFontDelegate* d = fontCache.get(decorated);
delegate = 0;
if (d!=0)
{
//fprintf (stderr, "resusing %*.*s %u\n", SSARGS(name), d->count);
delegate = d;
d->count++;
if (d->type == SFontDelegate::SE_NATIVE)
{
createSaneXLFD();
}
}
else
{
//n.append (".ttf");
SFile f(name, fontSearchPath);
if (f.size() > 0 && name.match ("*.hex"))
{
d = new SFontDelegate (f, SFontDelegate::SE_UNIFONT);
CHECK_NEW (d);
delegate = d;
fontCache.put (decorated, d);
}
else if (f.size() > 0 && name.match ("*.bdf"))
{
d = new SFontDelegate (f, SFontDelegate::SE_BDF);
CHECK_NEW (d);
delegate = d;
fontCache.put (decorated, d);
}
else if (f.size() > 0)
{
if (encoding.size()==0 || encoding == name || encoding == SString("unicode"))
{
d = new SFontDelegate (f, SFontDelegate::SE_TTF);
}
else
{
/* xlfd contains encoding */
d = new SFontDelegate (f, SFontDelegate::SE_TTF, encoding);
}
CHECK_NEW (d);
delegate = d;
// It is obvious that we don't use fontCache at this point.
// FIXME: this was commented out till yudit-2.3.beta-14 !!! Why?
fontCache.put (decorated, d);
}
else
{
createSaneXLFD();
//fprintf (stderr, "name=%*.*s xlfd=%*.*s\n", SSARGS(name), SSARGS(xlfd));
if (encoding.size()==0 || encoding == name)
{
d = new SFontDelegate (xlfd, SFontDelegate::SE_NATIVE);
}
else
{
d = new SFontDelegate (xlfd, SFontDelegate::SE_NATIVE, encoding);
}
CHECK_NEW (d);
delegate = d;
fontCache.put (decorated, d);
}
}
}
void
SFontImpl::createSaneXLFD ()
{
SStringVector l;
xlfd.replace("--", "-*-");
l.split (xlfd, "-");
if (l.size() < 14)
{
//fprintf (stderr, "SFontImpl: Need better XLFD %*.*s. Adding some -*-\n", SSARGS(xlfd));
/* find a good insertion point - FIXME this is not very good here */
unsigned int ipoint = 0;
if (l.size() > 2)
{
ipoint = l.size() -1;
while (ipoint > 0 && l[ipoint-1] != "*") ipoint--;
}
else if (xlfd[0] == '-')
{
while (ipoint < l.size() && l[ipoint] != "*") ipoint++;
if (ipoint >= l.size())
{
ipoint = 0;
}
}
while (l.size() < 14) l.insert(ipoint, "*");
xlfd = "-";
xlfd.append(l.join ("-"));
}
}
SFontImpl::SFontImpl (const SFontImpl& ff)
{
name = ff.name;
xlfd = ff.xlfd;
matrix = ff.matrix;
encoding = ff.encoding;
SFontDelegate* d = (SFontDelegate*) ff.delegate;
if (d)
{
d->count++;
}
lrFont = ff.lrFont;
rlFont = ff.rlFont;
delegate = d;
}
void
SFontImpl::setAttributes (const SProperties& properties)
{
if (properties.get("LR"))
{
lrFont = properties["LR"]==SString("true");
}
if (properties.get("RL"))
{
rlFont = properties["RL"]==SString("true");
}
}
SFontImpl
SFontImpl::operator= (const SFontImpl& ff)
{
if (&ff == this) return *this;
SFontDelegate* d;
if (ff.delegate == delegate)
{
xlfd = ff.xlfd;
matrix = ff.matrix;
name = ff.name;
encoding = ff.encoding;
lrFont = ff.lrFont;
rlFont = ff.rlFont;
return *this;
}
/* Get this till name exists */
SString decorated (name);
decorated.append ("~");
decorated.append (encoding);
xlfd = ff.xlfd;
matrix = ff.matrix;
name = ff.name;
encoding = ff.encoding;
lrFont = ff.lrFont;
rlFont = ff.rlFont;
if (delegate)
{
d = (SFontDelegate*) delegate;
d->count--;
if (d->count==0)
{
if (fontCache.get (decorated))
{
fontCache.remove (decorated);
delete d;
}
else
{
fprintf (stderr, "SFontImpl.cpp: FontCache does not have: %*.*s\n",
SSARGS(decorated));
}
}
}
d = (SFontDelegate*) ff.delegate;
if (d)
{
d->count++;
}
delegate = d;
return *this;
}
bool
SFontImpl::isTTF () const
{
if (delegate)
{
return (((SFontDelegate*) delegate)->type==SFontDelegate::SE_TTF);
}
return false;
}
/**
* \brief Try to make a fuzzy guess if we need to align the diacritics to
* the left or to the right.
* left aligned marks will be rendered this way:
* x----basewith----x
* x-markwidth-x
* right aligned marks will be rendered this way:
* x----basewith----x
* x-markwidth-x
*/
bool
SFontImpl::isLeftAligned (SS_UCS4 c) const
{
if (delegate && ((SFontDelegate*) delegate)->type==SFontDelegate::SE_TTF)
{
return (((SFontDelegate*)delegate)->u.ttf->isLeftAligned (c));
}
/*
* X11 font non-spacing marks will be right aligned.
* Except for RL scripts .
*/
/*
* FIXME:
* In reality should check if it is a non-spacing mark.
*/
if (c>= 0x500 && c < 0x900)
{
return true;
}
return false;
}
SFontImpl::~SFontImpl ()
{
if (delegate)
{
SFontDelegate* d = (SFontDelegate*) delegate;
d->count--;
if (isTTF())
if (d->count==0)
{
/* Get this till name exists */
SString decorated (name);
decorated.append ("~");
decorated.append (encoding);
if (fontCache.get (decorated))
{
fontCache.remove (decorated);
delete d;
}
else
{
fprintf (stderr, "SFontImpl.cpp: FontCache does not have: %*.*s\n",
SSARGS(decorated));
}
}
}
}
/**
* set the search path for the font
* This path will be used to locate font files on the disk
* Currently it is used for True Type fonts.
* @param l is the list of directories.
*/
void
SFontImpl::setPath(const SStringVector& l)
{
//fprintf (stderr, "set path ...\n");
fontSearchPath = l;
}
const SStringVector&
SFontImpl::getPath()
{
return fontSearchPath;
}
/**
* search files for property in order and set the path to the
* property. Always add YUDIT_DATA/fonts
*/
void
SFontImpl::guessPath ()
{
fontSearchPath = getUserPath ("yudit.fontpath", "fonts");
//fprintf (stderr, "fontpath is %*.*s\n", SSARGS(fontSearchPath.join(",")));
}
/**
* Scale the font to the given pointsize.
* @param x is the x axis pointsize
* @param y is the y axis pointsize
*/
void
SFontImpl::scale (double x, double y)
{
SS_Matrix2D m;
matrix = m;
if (!delegate) return;
SFontDelegate* d = (SFontDelegate*) delegate;;
SStringVector l;
char tmp[64];
switch (d->type)
{
case SFontDelegate::SE_TTF:
if (d->u.ttf->isOK())
{
double scale = d->u.ttf->scale ();
m.scale (x * scale, y * scale);
matrix = m;
}
break;
case SFontDelegate::SE_NATIVE:
/*
* Modify the currentXLFD and load the font.
* it will be our pixel size. nice eh?
* A reminder on xlfd:
* -foundry-family-weight-slant-*-style-pixel-*-*-*-spacing-avgWidth-registry-encoding
*/
if (d->u.native==0) break;
l.split (xlfd, "-");
snprintf (tmp, 64, "%u", (unsigned int) (y + 0.5));
if (l.size() < 14)
{
fprintf (stderr, "BAD XLFD %*.*s\n", SSARGS(xlfd));
break;
}
l.replace (6, tmp);
xlfd = "-";
xlfd.append(l.join ("-"));
break;
case SFontDelegate::SE_UNIFONT:
if (d->u.unifont==0) break;
m.scale (x, y);
matrix = m;
break;
case SFontDelegate::SE_BDF:
if (d->u.bdf==0) break;
m.scale (x, y);
matrix = m;
break;
case SFontDelegate::SE_NONE:
break;
}
}
/**
* Try to draw one single glyph.
* @param canvas is the canvas to draw to
* @param m is the conversion matrix
* @param uch is the array containing ucs4
* @prama len is the length of the array
* @return true is drawn.
*/
bool
SFontImpl::draw (SCanvas* canvas, const SPen& aPen, const SS_Matrix2D& m,
SS_UCS4 uch, bool isLRContext, bool isSelected, bool baseOK)
{
SPen pen (aPen);
if (!delegate) return 0;
SFontDelegate* d = (SFontDelegate*) delegate;;
SS_Matrix2D sd = m * matrix;
switch (d->type)
{
case SFontDelegate::SE_TTF:
if (d->u.ttf->isOK())
{
if (!isSelected && d->u.ttf->getIsEmoji())
{
pen.setForeground (SColor("black"));
pen.setBackground (SColor("white"));
}
if (!d->u.ttf->width (m, uch, 0)) return 0;
long larr[8];
larr[0] = 'T' ; larr[1] = 'T'; larr[2] = 'F';
larr[3] = (long)d->u.ttf; larr[4] = (long)uch;
SString key((char*) larr, 5 * sizeof (long));
/* FIXME: you should in fact use an instance counter. */
key.append ((long)(sd.x0*16000.0)); /* scale-x */
key.append ((long)(sd.y1*16000.0)); /* scale-y */
if (!isLRContext) key.append ("R");
SColor fg = pen.getForeground();
key.append ("fg:");
key.append ((char)fg.red);
key.append ((char)fg.green);
key.append ((char)fg.blue);
key.append ((char)fg.alpha);
double offsetX = 0.0;
double offsetY = 0.0;
/* for better positioning of diacritical marks */
d->u.ttf->getBaseOffsets (sd, uch, &offsetX, &offsetY);
// No need to be part of the key
//key.append ((long)(offsetX*16000.0));
//key.append ((long)(offsetY*16000.0));
sd.t0 += offsetX;
sd.t1 += offsetY;
//fprintf (stderr, "draw %x baseOK=%d\n", uch, (int)baseOK);
if (!isSelected && d->u.ttf->getIsEmoji() && !baseOK)
{
double width = 1.0;
#if 0
fprintf (stderr, "ascent: %g descent: %g offsetX: %g, offsetY: %g\n",
ascent(), descent(), offsetX, offsetY);
#endif
d->u.ttf->width (sd, uch, &width);
canvas->bitfill(pen.getBackground(),
(int) (m.t0), (int) (m.t1-ascent()),
(int) (width+1.0),
(int) (ascent()+descent()+1.0));
}
/* reverse coordinates Y, negative m.t1 */
if (!canvas->beginImage (m.t0+offsetX, m.t1+offsetY, key, pen.getBackground()))
{
// TODO: add color fonts.
canvas->newpath();
/* not cached yet */
d->u.ttf->draw (canvas, sd, uch, isLRContext);
canvas->fill (pen);
}
canvas->endImage ();
return true;
}
break;
case SFontDelegate::SE_NATIVE:
if (d->u.native==0) break;
return (d->u.native->draw (xlfd, canvas, pen, sd, uch));
case SFontDelegate::SE_UNIFONT:
if (d->u.unifont)
{
if (!d->u.unifont->width (matrix.y1, uch, 0)) return 0;
d->u.unifont->draw (matrix.y1, canvas, pen.getForeground(), sd, uch,
(m.x0 < 0));
return true;
}
break;
case SFontDelegate::SE_BDF:
if (d->u.bdf)
{
if (!d->u.bdf->width (matrix.y1, uch, 0)) return 0;
d->u.bdf->draw (matrix.y1, canvas, pen.getForeground(), sd, uch,
(m.x0 < 0));
return true;
}
break;
case SFontDelegate::SE_NONE:
break;
}
return false;
}
/**
* @param m is the conversion matrix
* @param uch is the array containing ucs4
* @prama len is the length of the array
* @return true if it has width.
* width_ will be set t width if exists.
*/
bool
SFontImpl::width (SS_UCS4 uch, double* width_)
{
if (!delegate) return false;
SFontDelegate* d = (SFontDelegate*) delegate;;
switch (d->type)
{
case SFontDelegate::SE_TTF:
if (d->u.ttf->isOK())
{
return (d->u.ttf->width (matrix, uch, width_));
}
break;
case SFontDelegate::SE_NATIVE:
if (d->u.native==0) break;
return (d->u.native->width (xlfd, uch, width_));
case SFontDelegate::SE_UNIFONT:
if (d->u.unifont)
{
return (d->u.unifont->width (matrix.y1, uch, width_));
}
break;
case SFontDelegate::SE_BDF:
if (d->u.bdf)
{
return (d->u.bdf->width (matrix.y1, uch, width_));
}
break;
case SFontDelegate::SE_NONE:
break;
}
return false;
}
/**
* set the base character for better glyph positioning
* @param base is the base character relative to which
* we will position all of out composing marks.
*/
void
SFontImpl::setBase(SS_UCS4 base)
{
SFontTTF::setBase (base);
}
/**
* return the overall width
*/
double
SFontImpl::width () const
{
if (!delegate) return 0.0;
SFontDelegate* d = (SFontDelegate*) delegate;;
switch (d->type)
{
case SFontDelegate::SE_TTF:
if (d->u.ttf->isOK())
{
return (d->u.ttf->width (matrix));
}
break;
case SFontDelegate::SE_NATIVE:
if (d->u.native==0) break;
return d->u.native->width (xlfd);
case SFontDelegate::SE_UNIFONT:
if (d->u.unifont)
{
return (d->u.unifont->width (matrix.y1));
}
break;
case SFontDelegate::SE_BDF:
if (d->u.bdf)
{
return (d->u.bdf->width (matrix.y1));
}
break;
case SFontDelegate::SE_NONE:
break;
}
return 0.0;
}
/**
* return the overall ascent
*/
double
SFontImpl::ascent () const
{
if (!delegate) return 0.0;
SFontDelegate* d = (SFontDelegate*) delegate;;
switch (d->type)
{
case SFontDelegate::SE_TTF:
if (d->u.ttf->isOK())
{
return (d->u.ttf->ascent (matrix));
}
break;
case SFontDelegate::SE_NATIVE:
if (d->u.native==0) break;
return d->u.native->ascent (xlfd);
case SFontDelegate::SE_UNIFONT:
if (d->u.unifont)
{
return (d->u.unifont->ascent (matrix.y1));
}
break;
case SFontDelegate::SE_BDF:
if (d->u.bdf)
{
return (d->u.bdf->ascent (matrix.y1));
}
break;
case SFontDelegate::SE_NONE:
break;
}
return 0.0;
}
/**
* return the overall descent
*/
double
SFontImpl::descent () const
{
if (!delegate) return 0.0;
SFontDelegate* d = (SFontDelegate*) delegate;;
switch (d->type)
{
case SFontDelegate::SE_TTF:
if (d->u.ttf->isOK())
{
return (d->u.ttf->descent (matrix));
}
break;
case SFontDelegate::SE_NATIVE:
if (d->u.native==0) break;
return d->u.native->descent (xlfd);
case SFontDelegate::SE_UNIFONT:
if (d->u.unifont)
{
return (d->u.unifont->descent (matrix.y1));
}
break;
case SFontDelegate::SE_BDF:
if (d->u.bdf)
{
return (d->u.bdf->descent (matrix.y1));
}
break;
case SFontDelegate::SE_NONE:
break;
}
return 0.0;
}
/**
* return the overall gap
*/
double
SFontImpl::gap () const
{
if (!delegate) return 0.0;
SFontDelegate* d = (SFontDelegate*) delegate;;
switch (d->type)
{
case SFontDelegate::SE_TTF:
if (d->u.ttf->isOK())
{
return (d->u.ttf->gap (matrix));
}
break;
case SFontDelegate::SE_NATIVE:
if (d->u.native==0) break;
return d->u.native->gap (xlfd);
case SFontDelegate::SE_UNIFONT:
if (d->u.unifont)
{
return (d->u.unifont->gap (matrix.y1));
}
break;
case SFontDelegate::SE_BDF:
if (d->u.bdf)
{
return (d->u.bdf->gap (matrix.y1));
}
break;
case SFontDelegate::SE_NONE:
break;
}
return 0.0;
}
SObject*
SFontImpl::clone () const
{
SFontImpl *ret = new SFontImpl(*this);
CHECK_NEW (ret);
return ret;
}
bool
SFontImpl::needSoftMirror (SS_UCS4 uch, bool isLRContext) const
{
// Sort out strong font types.
if (isLR() && !isLRContext) return true;
if (isRL() && isLRContext) return true;
if (isLR() || isRL()) return false;
if (getPUARovasType (uch) > 0) return !isLRContext;
if (getRovasType (uch) > 0) return isLRContext;
// rest is LR, need mirroring in RL context.
return !isLRContext;
}
|