Schrift.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. #include "Schrift.h"
  2. #include "Bild.h"
  3. #include "Text.h"
  4. #include "Scroll.h"
  5. #include "Globals.h"
  6. #ifdef WIN32
  7. #include <Windows.h>
  8. #endif
  9. #include "FrameworkMath.h"
  10. using namespace Framework;
  11. // Inhalt der Buchstabe Klasse aus Schrift.h
  12. // Konstruktor
  13. Buchstabe::Buchstabe()
  14. : size( 0, 0 ),
  15. alpha( 0 ),
  16. schriftSize( 0 ),
  17. ref( 1 )
  18. {}
  19. // Destruktor
  20. Buchstabe::~Buchstabe()
  21. {
  22. if( alpha )
  23. delete[]alpha;
  24. }
  25. // nicht constant
  26. void Buchstabe::NeuBuchstabe( Punkt &size ) // Initialisierung
  27. {
  28. this->size = size;
  29. if( alpha )
  30. delete[]alpha;
  31. alpha = new unsigned char[ size.x * size.y ];
  32. ZeroMemory( alpha, size.x * size.y );
  33. }
  34. void Buchstabe::setPixel( Punkt &pos, unsigned char alpha ) // setzt den alphawert des Pixels
  35. {
  36. this->alpha[ pos.x + pos.y * size.x ] = alpha;
  37. }
  38. void Buchstabe::setPixel( int x, int y, unsigned char alpha )
  39. {
  40. this->alpha[ x + y * size.x ] = alpha;
  41. }
  42. void Buchstabe::setPixel( int i, unsigned char alpha )
  43. {
  44. this->alpha[ i ] = alpha;
  45. }
  46. void Buchstabe::setSchriftSize( int sg ) // setzt die Schriftgröße des Buchstaben
  47. {
  48. schriftSize = sg;
  49. }
  50. int Buchstabe::getSchriftSize() const
  51. {
  52. return schriftSize;
  53. }
  54. // constant
  55. const Punkt &Buchstabe::getSize() const // gibt die Buchstabenbildgröße zurück
  56. {
  57. return size;
  58. }
  59. int Buchstabe::getBreite() const // Buchstabenbreite
  60. {
  61. return size.x;
  62. }
  63. int Buchstabe::getHeight() const // Buchstabenhöhe
  64. {
  65. return size.y;
  66. }
  67. unsigned char *Buchstabe::getBuff() const // gibt den Alphabuffer zurück
  68. {
  69. return alpha;
  70. }
  71. // Reference Counting
  72. Buchstabe *Buchstabe::getThis()
  73. {
  74. ++ref;
  75. return this;
  76. }
  77. Buchstabe *Buchstabe::release()
  78. {
  79. --ref;
  80. if( ref == 0 )
  81. delete this;
  82. return 0;
  83. }
  84. // Inhalt der Alphabet Klasse aus Schrift.h
  85. // Konstruktor
  86. Alphabet::Alphabet()
  87. : zeichen( new Buchstabe*[ 256 ] ),
  88. schriftSize( 12 ),
  89. ref( 1 )
  90. {
  91. for( int i = 0; i < 256; ++i )
  92. zeichen[ i ] = 0;
  93. }
  94. // Destruktor
  95. Alphabet::~Alphabet()
  96. {
  97. for( int i = 0; i < 256; ++i )
  98. {
  99. if( zeichen[ i ] )
  100. zeichen[ i ]->release();
  101. }
  102. delete[]zeichen;
  103. }
  104. // nicht constant
  105. void Alphabet::NeuAlphabet() // Initialisierung
  106. {
  107. for( int i = 0; i < 256; ++i )
  108. {
  109. if( zeichen[ i ] )
  110. zeichen[ i ]->release();
  111. }
  112. for( int i = 0; i < 256; ++i )
  113. zeichen[ i ] = 0;
  114. }
  115. void Alphabet::setBuchstabe( unsigned char i, Buchstabe *buchstabe ) // setzt einen Buchstaben
  116. {
  117. if( zeichen[ i ] )
  118. zeichen[ i ]->release();
  119. zeichen[ i ] = buchstabe;
  120. if( zeichen[ i ] )
  121. {
  122. zeichen[ i ]->setSchriftSize( schriftSize );
  123. }
  124. }
  125. void Alphabet::setSchriftSize( int gr ) // setzt die Schriftgröße
  126. {
  127. schriftSize = gr;
  128. for( int i = 0; i < 256; ++i )
  129. {
  130. if( zeichen[ i ] )
  131. zeichen[ i ]->setSchriftSize( gr );
  132. }
  133. }
  134. // constant
  135. Buchstabe *Alphabet::getBuchstabe( unsigned char i ) const // gibt einen Buchstaben zurück
  136. {
  137. if( zeichen[ i ] )
  138. return zeichen[ i ]->getThis();
  139. return 0;
  140. }
  141. Buchstabe *Alphabet::zBuchstabe( unsigned char i ) const
  142. {
  143. return zeichen[ i ];
  144. }
  145. bool Alphabet::hatBuchstabe( unsigned char b ) const
  146. {
  147. return zeichen[ b ] != 0;
  148. }
  149. int Alphabet::getSchriftSize() const // gibt die Schriftgröße zurück
  150. {
  151. return schriftSize;
  152. }
  153. // Reference Counting
  154. Alphabet *Alphabet::getThis()
  155. {
  156. ++ref;
  157. return this;
  158. }
  159. Alphabet *Alphabet::release()
  160. {
  161. --ref;
  162. if( ref == 0 )
  163. delete this;
  164. return 0;
  165. }
  166. // Inhalt der AlphabetArray Klasse aus Schrift.h
  167. // Konstruktor
  168. AlphabetArray::AlphabetArray()
  169. : next( 0 ),
  170. This( 0 )
  171. {}
  172. // Destruktor
  173. AlphabetArray::~AlphabetArray()
  174. {
  175. if( This )
  176. This->release();
  177. delete next;
  178. }
  179. // nicht constant
  180. bool AlphabetArray::addAlphabet( Alphabet *alphabet ) // Fügt ein Alphabet hinzu
  181. {
  182. if( This )
  183. {
  184. if( This->getSchriftSize() == alphabet->getSchriftSize() )
  185. {
  186. alphabet->release();
  187. return false;
  188. }
  189. }
  190. else
  191. {
  192. This = alphabet;
  193. return true;
  194. }
  195. if( !next )
  196. next = new AlphabetArray();
  197. return next->addAlphabet( alphabet );
  198. }
  199. bool AlphabetArray::removeAlphabet( int sg ) // entfernt ein Alphabet
  200. {
  201. if( This )
  202. {
  203. if( This->getSchriftSize() == sg )
  204. This = This->release();
  205. return 1;
  206. }
  207. if( !next )
  208. return 0;
  209. if( next->removeAlphabet( sg ) )
  210. {
  211. AlphabetArray *tmp = next->getNext();
  212. next->setNext0();
  213. delete next;
  214. next = tmp;
  215. }
  216. return 0;
  217. }
  218. void AlphabetArray::setNext0() // setzt den next Zeiger zu 0
  219. {
  220. next = 0;
  221. }
  222. // constant
  223. Alphabet *AlphabetArray::getAlphabet( unsigned char sg ) const // gibt getThis von einem Alphabet zurück
  224. {
  225. if( !This )
  226. return 0;
  227. if( This->getSchriftSize() == sg )
  228. return This->getThis();
  229. if( next )
  230. return next->getAlphabet( sg );
  231. return 0;
  232. }
  233. Alphabet *AlphabetArray::zAlphabet( unsigned char sg ) const // gibt ein Alphabet zurück
  234. {
  235. if( !This )
  236. return 0;
  237. if( This->getSchriftSize() == sg )
  238. return This;
  239. if( next )
  240. return next->zAlphabet( sg );
  241. return 0;
  242. }
  243. Alphabet *AlphabetArray::getAlphabetI( int index, int count ) const
  244. {
  245. if( count == index )
  246. return This->getThis();
  247. if( next )
  248. return next->getAlphabetI( index, count + 1 );
  249. return 0;
  250. }
  251. Alphabet *AlphabetArray::zAlphabetI( int index, int count ) const
  252. {
  253. if( count == index )
  254. return This;
  255. if( next )
  256. return next->zAlphabetI( index, count + 1 );
  257. return 0;
  258. }
  259. AlphabetArray *AlphabetArray::getNext() const // gibt das nächste Alphabet zurück
  260. {
  261. return next;
  262. }
  263. // Inhalt der Schrift Klasse aus Schrift.h
  264. // Konstruktor
  265. Schrift::Schrift()
  266. : alphabetAnzahl( 0 ),
  267. alphabet( new AlphabetArray() ),
  268. ref( 1 )
  269. {}
  270. // Destruktor
  271. Schrift::~Schrift()
  272. {
  273. delete alphabet;
  274. }
  275. bool Schrift::addAlphabet( Alphabet *alphabet ) // Fügt der Schrift ein Alphabet hinzu
  276. {
  277. if( this->alphabet->addAlphabet( alphabet ) )
  278. {
  279. ++alphabetAnzahl;
  280. return true;
  281. }
  282. return false;
  283. }
  284. void Schrift::removeAlphabet( int sg ) // Entfernt ein Alphabet
  285. {
  286. if( alphabet->removeAlphabet( sg ) )
  287. --alphabetAnzahl;
  288. }
  289. // constant
  290. Alphabet *Schrift::getAlphabet( int sg ) const
  291. {
  292. Alphabet *drawAlphabet = alphabet->zAlphabet( (unsigned char)sg );
  293. if( !drawAlphabet )
  294. {
  295. for( int i = 0; i < 256; ++i )
  296. {
  297. drawAlphabet = alphabet->zAlphabet( (unsigned char)( sg - i ) );
  298. if( drawAlphabet )
  299. break;
  300. drawAlphabet = alphabet->zAlphabet( (unsigned char)( sg + i ) );
  301. if( drawAlphabet )
  302. break;
  303. }
  304. }
  305. return drawAlphabet->getThis();
  306. }
  307. Alphabet *Schrift::zAlphabet( int sg ) const
  308. {
  309. Alphabet *drawAlphabet = alphabet->zAlphabet( (unsigned char)sg );
  310. if( !drawAlphabet )
  311. {
  312. for( int i = 0; i < 256; ++i )
  313. {
  314. drawAlphabet = alphabet->zAlphabet( (unsigned char)( sg - i ) );
  315. if( drawAlphabet )
  316. break;
  317. drawAlphabet = alphabet->zAlphabet( (unsigned char)( sg + i ) );
  318. if( drawAlphabet )
  319. break;
  320. }
  321. }
  322. return drawAlphabet;
  323. }
  324. Alphabet *Schrift::getAlphabetI( int index ) const
  325. {
  326. return alphabet->getAlphabetI( index, 0 );
  327. }
  328. Alphabet *Schrift::zAlphabetI( int index ) const
  329. {
  330. return alphabet->zAlphabetI( index, 0 );
  331. }
  332. unsigned char Schrift::getAlphabetAnzahl() const // gibt die anzahl von in der Schrift enthaltenen Alphabeten zurück
  333. {
  334. return alphabetAnzahl;
  335. }
  336. // Reference Counting
  337. Schrift *Schrift::getThis()
  338. {
  339. ++ref;
  340. return this;
  341. }
  342. Schrift *Schrift::release()
  343. {
  344. --ref;
  345. if( ref == 0 )
  346. delete this;
  347. return 0;
  348. }
  349. TextRenderer::TextRenderer()
  350. : TextRenderer( 0 )
  351. {}
  352. TextRenderer::TextRenderer( Schrift *schrift )
  353. {
  354. s = schrift;
  355. schriftSize = 12;
  356. zeilenAbstand = 5;
  357. zeichenAbstand = 0;
  358. ref = 1;
  359. }
  360. TextRenderer::~TextRenderer()
  361. {
  362. s->release();
  363. }
  364. void TextRenderer::setSchriftZ( Schrift *schrift )
  365. {
  366. if( s )
  367. s->release();
  368. s = schrift;
  369. }
  370. Schrift *TextRenderer::getSchrift()
  371. {
  372. if( s )
  373. return s->getThis();
  374. return 0;
  375. }
  376. Schrift *TextRenderer::zSchrift()
  377. {
  378. return s;
  379. }
  380. // Setzt die Schriftgröße, in der gezeichnet werden soll. Die Schrift wählt automatisch das passende Alphabet zum Zeichnen
  381. // sg: Die Schriftgröße
  382. void TextRenderer::setSchriftSize( int sg )
  383. {
  384. schriftSize = sg;
  385. }
  386. // Setzt den Zeilenabstand, der zum zeichnen verwendet werden soll
  387. // za: Der Zeilenabstand zum unteren Ende der darüber liegenden zeile in Pixeln
  388. void TextRenderer::setZeilenAbstand( int za )
  389. {
  390. zeilenAbstand = za;
  391. }
  392. // Setzt den Zeichenabstand, der zum zeichnen verwendet werden soll
  393. // za: Der Zeichenabstand zum unteren Ende der darüber liegenden zeile in Pixeln
  394. void TextRenderer::setZeichenAbstand( int za )
  395. {
  396. zeichenAbstand = za;
  397. }
  398. // Fügt Zeilenumbrüche in den Text ein, so dass er bei einer vorgegebenen Breite follständig angezeigt wird
  399. // zText: Der text, in den die Zeilenumbrüche eingefügt werden sollen
  400. // maxBreite: Die Breite in Pixeln auf der der Text follständig angezeigt werden soll
  401. void TextRenderer::textFormatieren( Text *zTxt, int maxBreite )
  402. {
  403. int lastPos = -1;
  404. int x = 0;
  405. const char *txt = zTxt->getText();
  406. Text result = txt;
  407. int len = zTxt->getLength();
  408. for( int i = 0; txt[ i ]; ++i )
  409. {
  410. if( txt[ i ] == ' ' )
  411. {
  412. lastPos = i;
  413. x += schriftSize / 2 + zeichenAbstand;
  414. continue;
  415. }
  416. if( txt[ i ] == '\t' )
  417. {
  418. lastPos = i;
  419. x += schriftSize + zeichenAbstand;
  420. continue;
  421. }
  422. if( txt[ i ] == '\n' )
  423. {
  424. x = 0;
  425. lastPos = -1;
  426. continue;
  427. }
  428. if( txt[ i ] == '\r' && len - i >= 11 )
  429. {
  430. i += 10;
  431. continue;
  432. }
  433. x += getCharWidth( txt[ i ] ) + zeichenAbstand;
  434. if( x > maxBreite && lastPos > -1 )
  435. {
  436. result.ersetzen( lastPos, lastPos + 1, "\n" );
  437. x = 0;
  438. i = lastPos;
  439. lastPos = -1;
  440. }
  441. }
  442. zTxt->setText( result );
  443. }
  444. // Zeichnet einen Bestimmten Text mit Cursor und einfärbung auf ein Bild
  445. // Nutze (setPosition) und (setDrawSchriftGröße) um die Position und die Größe zu verändern
  446. // x: x position des ersten zeichens
  447. // y: y position des ersten zeichens
  448. // txt: Der Text, der gezeichnet werden soll
  449. // zRObj: Das Bild, auf das gezeichnet werden soll
  450. // cpos: Die position des Cursors im Text
  451. // cf: Die Farbe des Cursors
  452. // fbeg: Die Position des Zeichens im Text, wo die Einfärbung beginnen soll. Der Text wird von dort bis zur Cursorposition eingefärbt
  453. // ff: Die Hintergrund Farbe des eingefärbten Textes
  454. // f: Eine Funktion die für jeden Buchstaben aufgerufen wird und seine Farbe zurückgibt
  455. void TextRenderer::renderText( int x, int y, const char *txt, Bild &zRObj, std::function< int( int, int, int ) > f, int cpos, int cf, int fbeg, int ff )
  456. {
  457. if( !s )
  458. return;
  459. int zRObjBr = zRObj.getBreite();
  460. int zRObjHi = zRObj.getHeight();
  461. int beginX = x;
  462. int zh = getZeilenHeight();
  463. if( y + ( zh + zeilenAbstand ) * Text( txt ).anzahlVon( '\n' ) + zh < 0 || x >= zRObjBr || y >= zRObjHi )
  464. return;
  465. bool faerb = 0;
  466. for( int i = 0; txt[ i ]; ++i )
  467. {
  468. if( i == fbeg )
  469. faerb = !faerb;
  470. if( i == cpos )
  471. {
  472. zRObj.drawLinieVAlpha( x, y, zh, cf );
  473. faerb = !faerb;
  474. }
  475. if( txt[ i ] == ' ' )
  476. {
  477. if( faerb )
  478. zRObj.alphaRegion( x, y, schriftSize / 2 + zeichenAbstand, zh, ff );
  479. x += schriftSize / 2 + zeichenAbstand;
  480. continue;
  481. }
  482. if( txt[ i ] == '\t' )
  483. {
  484. if( faerb )
  485. zRObj.alphaRegion( x, y, schriftSize + zeichenAbstand, zh, ff );
  486. x += schriftSize + zeichenAbstand;
  487. continue;
  488. }
  489. if( txt[ i ] == '\n' )
  490. {
  491. y += zh + zeilenAbstand;
  492. x = beginX;
  493. continue;
  494. }
  495. if( txt[ i ] == '\r' && textLength( txt ) - i >= 11 )
  496. {
  497. i += 3;
  498. Text *hex1 = Text( txt ).getTeilText( i, i + 6 );
  499. Text *hex2 = Text( txt ).getTeilText( i + 6, i + 8 );
  500. int nf = ( TextZuInt( hex1->getText(), 16 ) << 8 ) |
  501. ( TextZuInt( hex2->getText(), 16 ) );
  502. f = [ nf ]( int a, int b, int c )
  503. {
  504. return nf;
  505. };
  506. hex1->release();
  507. hex2->release();
  508. i += 7;
  509. continue;
  510. }
  511. renderChar( x, y, txt[ i ], zRObj, faerb, ff, f( x, y, i ) );
  512. }
  513. if( textLength( txt ) == cpos )
  514. zRObj.drawLinieVAlpha( x, y, zh, cf );
  515. }
  516. // Zeichnet einen Bestimmten Text mit Cursor und einfärbung auf ein Bild
  517. // Nutze (setPosition) und (setDrawSchriftGröße) um die Position und die Größe zu verändern
  518. // x: x position des ersten zeichens
  519. // y: y position des ersten zeichens
  520. // txt: Der Text, der gezeichnet werden soll
  521. // zRObj: Das Bild, auf das gezeichnet werden soll
  522. // cpos: Die position des Cursors im Text
  523. // cf: Die Farbe des Cursors
  524. // fbeg: Die Position des Zeichens im Text, wo die Einfärbung beginnen soll. Der Text wird von dort bis zur Cursorposition eingefärbt
  525. // ff: Die Hintergrund Farbe des eingefärbten Textes
  526. // f: Die Farbe, in der der Text gezeichnet werden soll
  527. void TextRenderer::renderText( int x, int y, const char *txt, Bild &zRObj, int f, int cpos, int cf, int fbeg, int ff )
  528. {
  529. return renderText( x, y, txt, zRObj, [ f ]( int a, int b, int c ) { return f; }, cpos, cf, fbeg, ff );
  530. }
  531. // Zeichnet einen Bestimmten Buchstaben mit einfärbung auf ein Bild
  532. // Nutze (setPosition) und (setDrawSchriftGröße) um die Position und die Größe zu verändern
  533. // x: x position des ersten zeichens
  534. // y: y position des ersten zeichens
  535. // txt: Der Text, der gezeichnet werden soll
  536. // zRObj: Das Bild, auf das gezeichnet werden soll
  537. // selected: 1, falls das zeichen eingefärbt sein soll
  538. // ff: Die Hintergrund Farbe des eingefärbten Textes
  539. // f: Die Farbe, in der der Text gezeichnet werden soll
  540. void TextRenderer::renderChar( int &x, int y, char c, Bild &zRObj, bool selected, int ff, int f )
  541. {
  542. if( !s )
  543. return;
  544. Alphabet *a = s->zAlphabet( schriftSize );
  545. Buchstabe *b = a->zBuchstabe( c );
  546. if( b )
  547. {
  548. if( x >= zRObj.getBreite() )
  549. return;
  550. if( zRObj.isAreaDrawable( x, y, getCharWidth( c ), getCharHeight( c ) ) )
  551. {
  552. if( selected )
  553. {
  554. int br = getCharWidth( c ) + zeichenAbstand;
  555. zRObj.alphaRegion( x, y, br, getZeilenHeight(), ff );
  556. }
  557. if( b->getBuff() )
  558. {
  559. const Punkt &zRObjGr = zRObj.getDrawGr();
  560. const Punkt &zRObjPos = zRObj.getDrawPos();
  561. const Punkt &zRObjOff = zRObj.getDrawOff();
  562. int xp = x + zRObjOff.x, yp = y + zRObjOff.y;
  563. int xs = xp < zRObjPos.x ? ( zRObjPos.x - xp ) : 0, ys = yp < zRObjPos.y ? ( zRObjPos.y - yp ) : 0;
  564. int br = b->getBreite(), h = b->getHeight();
  565. unsigned char a2 = (unsigned char)( 255 - ( f >> 24 ) );
  566. f &= 0x00FFFFFF;
  567. if( schriftSize == b->getSchriftSize() )
  568. {
  569. if( xp >= zRObjGr.x || yp >= zRObjGr.y || xp + br < zRObjPos.x || yp + h < zRObjPos.y )
  570. return;
  571. br = ( xp + br ) > zRObjGr.x ? ( zRObjGr.x - xp ) : br;
  572. h = ( yp + h ) > zRObjGr.y ? ( zRObjGr.y - yp ) : h;
  573. if( !a2 )
  574. {
  575. int xx, ygr = ( ys - 1 ) * b->getBreite(), ygr2 = ( yp + ys - 1 ) * zRObj.getBreite();
  576. for( int yy = ys; yy < h; ++yy )
  577. {
  578. ygr += b->getBreite();
  579. ygr2 += zRObj.getBreite();
  580. for( xx = xs; xx < br; ++xx )
  581. zRObj.alphaPixel( xp + xx + ygr2, f | ( b->getBuff()[ xx + ygr ] << 24 ) );
  582. }
  583. }
  584. else
  585. {
  586. int a;
  587. int xx, ygr = ( ys - 1 ) * b->getBreite(), ygr2 = ( yp + ys - 1 ) * zRObj.getBreite();
  588. for( int yy = ys; yy < h; ++yy )
  589. {
  590. ygr += b->getBreite();
  591. ygr2 += zRObj.getBreite();
  592. for( xx = xs; xx < br; ++xx )
  593. {
  594. a = b->getBuff()[ xx + ygr ] - a2;
  595. if( a > 0 )
  596. zRObj.alphaPixel( xp + xx + ygr2, f | ( a << 24 ) );
  597. }
  598. }
  599. }
  600. }
  601. else
  602. {
  603. double xoff = (double)b->getSchriftSize() / (double)schriftSize,
  604. yoff = (double)b->getSchriftSize() / (double)schriftSize;
  605. double x = xs * xoff, y = ys * yoff;
  606. int maxX = getCharWidth( c ), maxY = getCharHeight( c );
  607. maxX = ( xp + maxX ) >= zRObjGr.x ? ( zRObjGr.x - xp ) : maxX;
  608. maxY = ( yp + maxY ) >= zRObjGr.y ? ( zRObjGr.y - yp ) : maxY;
  609. if( !a2 )
  610. {
  611. int dx, ygr, ygr2;
  612. for( int dy = ys; dy < maxY; ++dy )
  613. {
  614. ygr2 = ( yp + dy ) * zRObj.getBreite();
  615. ygr = (int)y * br;
  616. for( dx = xs; dx < maxX; ++dx )
  617. {
  618. zRObj.alphaPixel( xp + dx + ygr2, f | ( b->getBuff()[ (int)x + ygr ] << 24 ) );
  619. x += xoff;
  620. }
  621. x = xs;
  622. y += yoff;
  623. }
  624. }
  625. else
  626. {
  627. int a, dx, ygr, ygr2;
  628. for( int dy = ys; dy < maxY; ++dy )
  629. {
  630. ygr2 = ( yp + dy ) * zRObj.getBreite();
  631. ygr = (int)y * br;
  632. for( dx = xs; dx < maxX; ++dx )
  633. {
  634. a = b->getBuff()[ (int)x + ygr ] - a2;
  635. zRObj.alphaPixel( xp + dx + ygr2, f | ( a << 24 ) );
  636. x += xoff;
  637. }
  638. x = xs;
  639. y += yoff;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. x += getCharWidth( c ) + zeichenAbstand;
  646. }
  647. }
  648. // Gibt die Schriftgröße zurück, die zum Zeichnen verwendet wird
  649. int TextRenderer::getSchriftSize() const
  650. {
  651. return schriftSize;
  652. }
  653. // Gibt den Abstand in Pixeln zum unteren Ende der darüber ligenden Zeile zurück
  654. int TextRenderer::getZeilenabstand() const
  655. {
  656. return zeilenAbstand;
  657. }
  658. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Text vollständig darzustellen
  659. // txt: Der Text, von dem die Breite in Pixeln ermitelt werden soll
  660. int TextRenderer::getTextBreite( const char *txt ) const
  661. {
  662. int ret = 0;
  663. int tmp = 0;
  664. for( int i = 0; txt[ i ]; ++i )
  665. {
  666. if( txt[ i ] == '\n' )
  667. {
  668. if( tmp > ret )
  669. ret = tmp;
  670. tmp = 0;
  671. }
  672. else if( txt[ i ] == '\r' )
  673. {
  674. i += 10;
  675. continue;
  676. }
  677. else if( txt[ i ] == '\t' )
  678. tmp += schriftSize + zeichenAbstand;
  679. else if( txt[ i ] == ' ' )
  680. tmp += schriftSize / 2 + zeichenAbstand;
  681. else
  682. tmp += getCharWidth( txt[ i ] ) + zeichenAbstand;
  683. }
  684. if( tmp > ret )
  685. ret = tmp;
  686. return ret;
  687. }
  688. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Text vollständig darzustellen
  689. // txt: Der Text, von dem die Höhe in Pixeln ermitelt werden soll
  690. int TextRenderer::getTextHeight( const char *txt ) const
  691. {
  692. int hi = getZeilenHeight();
  693. return hi + ( ( hi + zeilenAbstand ) * Text( txt ).anzahlVon( '\n' ) );
  694. }
  695. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Buchstaben vollständig darzustellen
  696. // c: Der Buchstabe, von dem die Breite in Pixeln ermitelt werden soll
  697. int TextRenderer::getCharWidth( const char c ) const
  698. {
  699. if( !s )
  700. return 0;
  701. Alphabet *a = s->zAlphabet( schriftSize );
  702. Buchstabe *b = a->zBuchstabe( c );
  703. if( b )
  704. return (int)( ( (double)b->getBreite() / (double)b->getSchriftSize() ) * (double)schriftSize + 0.5 );
  705. return 0;
  706. }
  707. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Text vollständig darzustellen
  708. // c: Der Buchstabe, von dem die Höhe in Pixeln ermitelt werden soll
  709. int TextRenderer::getCharHeight( const char c ) const
  710. {
  711. if( !s )
  712. return 0;
  713. Alphabet *a = s->zAlphabet( schriftSize );
  714. Buchstabe *b = a->zBuchstabe( c );
  715. if( b )
  716. return (int)( ( (double)b->getHeight() / (double)b->getSchriftSize() ) * (double)schriftSize + 0.5 );
  717. return 0;
  718. }
  719. // Gibt den Abstand in Pixeln zum unteren Ende der darüber ligenden Zeile zurück
  720. int TextRenderer::getZeilenAbstand() const
  721. {
  722. return zeilenAbstand;
  723. }
  724. // Gibt die skallierte Höhe zurück, die eine gezeichnete Zeile in Pixeln benötigt
  725. int TextRenderer::getZeilenHeight() const
  726. {
  727. int zh = 0;
  728. for( int i = 0; i < 256; ++i )
  729. {
  730. zh = maxInt( getCharHeight( (char)i ), zh );
  731. }
  732. return zh;
  733. }
  734. // Ermittelt das Zeichen im Text, auf das die Maus zeigt
  735. // txt: Der Text, auf den die Maus Zeigt
  736. // mausX: Die X Position der Maus in Pixeln Relativ zur Position des ersten Zeichens
  737. // mausY: Die Y Position der Maus in Pixeln Relativ zur Position des ersten Zeichens
  738. int TextRenderer::textPos( const char *txt, int mausX, int mausY ) const
  739. {
  740. int tx = 0;
  741. int ty = 0;
  742. int sh = getZeilenHeight();
  743. if( mausX < 0 || mausY < 0 )
  744. return -1;
  745. for( int i = 0; i < txt[ i ]; ++i )
  746. {
  747. if( txt[ i ] == '\n' )
  748. {
  749. ty += sh + zeilenAbstand;
  750. tx = 0;
  751. if( mausY < ty )
  752. return i;
  753. }
  754. if( txt[ i ] == '\t' )
  755. tx += schriftSize + zeichenAbstand;
  756. if( txt[ i ] == ' ' )
  757. tx += schriftSize / 2 + zeichenAbstand;
  758. tx += getCharWidth( txt[ i ] );
  759. int txpl = getCharWidth( txt[ i + 1 ] ) / 2;
  760. if( mausX < tx - txpl && mausY < ty + sh + zeilenAbstand )
  761. return i;
  762. }
  763. if( mausY < ty + sh + zeilenAbstand )
  764. return textLength( txt );
  765. return -1;
  766. }
  767. TextRenderer *TextRenderer::getThis()
  768. {
  769. ref++;
  770. return this;
  771. }
  772. TextRenderer *TextRenderer::release()
  773. {
  774. if( !--ref )
  775. delete this;
  776. return 0;
  777. }