Schrift.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. for( int i = 0; txt[ i ]; ++i )
  408. {
  409. if( txt[ i ] == ' ' )
  410. {
  411. lastPos = i;
  412. x += schriftSize / 2 + zeichenAbstand;
  413. continue;
  414. }
  415. if( txt[ i ] == '\t' )
  416. {
  417. lastPos = i;
  418. x += schriftSize + zeichenAbstand;
  419. continue;
  420. }
  421. if( txt[ i ] == '\n' )
  422. {
  423. x = 0;
  424. lastPos = -1;
  425. continue;
  426. }
  427. x += getCharWidth( txt[ i ] ) + zeichenAbstand;
  428. if( x > maxBreite && lastPos > -1 )
  429. {
  430. result.ersetzen( lastPos, lastPos + 1, "\n" );
  431. x = 0;
  432. i = lastPos;
  433. lastPos = -1;
  434. }
  435. }
  436. zTxt->setText( result );
  437. }
  438. // Zeichnet einen Bestimmten Text mit Cursor und einfärbung auf ein Bild
  439. // Nutze (setPosition) und (setDrawSchriftGröße) um die Position und die Größe zu verändern
  440. // x: x position des ersten zeichens
  441. // y: y position des ersten zeichens
  442. // txt: Der Text, der gezeichnet werden soll
  443. // zRObj: Das Bild, auf das gezeichnet werden soll
  444. // cpos: Die position des Cursors im Text
  445. // cf: Die Farbe des Cursors
  446. // fbeg: Die Position des Zeichens im Text, wo die Einfärbung beginnen soll. Der Text wird von dort bis zur Cursorposition eingefärbt
  447. // ff: Die Hintergrund Farbe des eingefärbten Textes
  448. // f: Eine Funktion die für jeden Buchstaben aufgerufen wird und seine Farbe zurückgibt
  449. 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 )
  450. {
  451. if( !s )
  452. return;
  453. if( fbeg == -1 )
  454. fbeg = cpos;
  455. int zRObjBr = zRObj.getBreite();
  456. int zRObjHi = zRObj.getHeight();
  457. int beginX = x;
  458. int zh = getZeilenHeight();
  459. if( y + ( zh + zeilenAbstand ) * Text( txt ).anzahlVon( '\n' ) + zh < 0 || x >= zRObjBr || y >= zRObjHi )
  460. return;
  461. bool faerb = 0;
  462. for( int i = 0; txt[ i ]; ++i )
  463. {
  464. if( i == fbeg )
  465. faerb = !faerb;
  466. if( i == cpos )
  467. {
  468. zRObj.drawLinieVAlpha( x, y, zh, cf );
  469. faerb = !faerb;
  470. }
  471. if( txt[ i ] == ' ' )
  472. {
  473. if( faerb )
  474. zRObj.alphaRegion( x, y, schriftSize / 2 + zeichenAbstand, zh, ff );
  475. x += schriftSize / 2 + zeichenAbstand;
  476. continue;
  477. }
  478. if( txt[ i ] == '\t' )
  479. {
  480. if( faerb )
  481. zRObj.alphaRegion( x, y, schriftSize + zeichenAbstand, zh, ff );
  482. x += schriftSize + zeichenAbstand;
  483. continue;
  484. }
  485. if( txt[ i ] == '\n' )
  486. {
  487. y += zh + zeilenAbstand;
  488. x = beginX;
  489. continue;
  490. }
  491. renderChar( x, y, txt[ i ], zRObj, faerb, ff, f( x, y, i ) );
  492. }
  493. if( textLength( txt ) == cpos )
  494. zRObj.drawLinieVAlpha( x, y, zh, cf );
  495. }
  496. // Zeichnet einen Bestimmten Text mit Cursor und einfärbung auf ein Bild
  497. // Nutze (setPosition) und (setDrawSchriftGröße) um die Position und die Größe zu verändern
  498. // x: x position des ersten zeichens
  499. // y: y position des ersten zeichens
  500. // txt: Der Text, der gezeichnet werden soll
  501. // zRObj: Das Bild, auf das gezeichnet werden soll
  502. // cpos: Die position des Cursors im Text
  503. // cf: Die Farbe des Cursors
  504. // fbeg: Die Position des Zeichens im Text, wo die Einfärbung beginnen soll. Der Text wird von dort bis zur Cursorposition eingefärbt
  505. // ff: Die Hintergrund Farbe des eingefärbten Textes
  506. // f: Die Farbe, in der der Text gezeichnet werden soll
  507. void TextRenderer::renderText( int x, int y, const char *txt, Bild &zRObj, int f, int cpos, int cf, int fbeg, int ff )
  508. {
  509. return renderText( x, y, txt, zRObj, [ f ]( int a, int b, int c ) { return f; }, cpos, cf, fbeg, ff );
  510. }
  511. // Zeichnet einen Bestimmten Buchstaben mit einfärbung auf ein Bild
  512. // Nutze (setPosition) und (setDrawSchriftGröße) um die Position und die Größe zu verändern
  513. // x: x position des ersten zeichens
  514. // y: y position des ersten zeichens
  515. // txt: Der Text, der gezeichnet werden soll
  516. // zRObj: Das Bild, auf das gezeichnet werden soll
  517. // color: Die Farbe, in der der Text gezeichnet werden soll
  518. // underlined: 1, falls der Text unterstrichen sein soll
  519. // selected: 1, falls das zeichen eingefärbt sein soll
  520. // selectedBackgroundColor: Die Hintergrund Farbe des eingefärbten Textes
  521. void TextRenderer::renderChar( int &x, int y, char c, Bild &zRObj, int color, bool underlined, bool selected, int selectedBackgroundColor )
  522. {
  523. if( !s )
  524. return;
  525. Alphabet *a = s->zAlphabet( schriftSize );
  526. if( !a )
  527. return;
  528. Buchstabe *b = a->zBuchstabe( c );
  529. if( b )
  530. {
  531. if( x >= zRObj.getBreite() )
  532. return;
  533. if( zRObj.isAreaDrawable( x, y, getCharWidth( c ), getCharHeight( c ) ) )
  534. {
  535. if( selected )
  536. {
  537. int br = getCharWidth( c ) + zeichenAbstand;
  538. zRObj.alphaRegion( x, y, br, getZeilenHeight(), selectedBackgroundColor );
  539. }
  540. if( b->getBuff() )
  541. {
  542. const Punkt &zRObjGr = zRObj.getDrawGr();
  543. const Punkt &zRObjPos = zRObj.getDrawPos();
  544. const Punkt &zRObjOff = zRObj.getDrawOff();
  545. int xp = x + zRObjOff.x, yp = y + zRObjOff.y;
  546. int xs = xp < zRObjPos.x ? ( zRObjPos.x - xp ) : 0, ys = yp < zRObjPos.y ? ( zRObjPos.y - yp ) : 0;
  547. int br = b->getBreite(), h = b->getHeight();
  548. unsigned char a2 = (unsigned char)( 255 - ( color >> 24 ) );
  549. color &= 0x00FFFFFF;
  550. if( schriftSize == b->getSchriftSize() )
  551. {
  552. if( xp >= zRObjGr.x || yp >= zRObjGr.y || xp + br < zRObjPos.x || yp + h < zRObjPos.y )
  553. return;
  554. br = ( xp + br ) > zRObjGr.x ? ( zRObjGr.x - xp ) : br;
  555. h = ( yp + h ) > zRObjGr.y ? ( zRObjGr.y - yp ) : h;
  556. if( !a2 )
  557. {
  558. int xx, ygr = ( ys - 1 ) * b->getBreite(), ygr2 = ( yp + ys - 1 ) * zRObj.getBreite();
  559. if( zRObj.hasAlpha3D() )
  560. {
  561. for( int yy = ys; yy < h; ++yy )
  562. {
  563. ygr += b->getBreite();
  564. ygr2 += zRObj.getBreite();
  565. for( xx = xs; xx < br; ++xx )
  566. zRObj.alphaPixel3D( xp + xx + ygr2, color | ( b->getBuff()[ xx + ygr ] << 24 ) );
  567. }
  568. }
  569. else
  570. {
  571. for( int yy = ys; yy < h; ++yy )
  572. {
  573. ygr += b->getBreite();
  574. ygr2 += zRObj.getBreite();
  575. for( xx = xs; xx < br; ++xx )
  576. zRObj.alphaPixel2D( xp + xx + ygr2, color | ( b->getBuff()[ xx + ygr ] << 24 ) );
  577. }
  578. }
  579. }
  580. else
  581. {
  582. int a;
  583. int xx, ygr = ( ys - 1 ) * b->getBreite(), ygr2 = ( yp + ys - 1 ) * zRObj.getBreite();
  584. if( zRObj.hasAlpha3D() )
  585. {
  586. for( int yy = ys; yy < h; ++yy )
  587. {
  588. ygr += b->getBreite();
  589. ygr2 += zRObj.getBreite();
  590. for( xx = xs; xx < br; ++xx )
  591. {
  592. a = b->getBuff()[ xx + ygr ] - a2;
  593. if( a > 0 )
  594. zRObj.alphaPixel3D( xp + xx + ygr2, color | ( a << 24 ) );
  595. }
  596. }
  597. }
  598. else
  599. {
  600. for( int yy = ys; yy < h; ++yy )
  601. {
  602. ygr += b->getBreite();
  603. ygr2 += zRObj.getBreite();
  604. for( xx = xs; xx < br; ++xx )
  605. {
  606. a = b->getBuff()[ xx + ygr ] - a2;
  607. if( a > 0 )
  608. zRObj.alphaPixel2D( xp + xx + ygr2, color | ( a << 24 ) );
  609. }
  610. }
  611. }
  612. }
  613. }
  614. else
  615. {
  616. double xoff = (double)b->getSchriftSize() / (double)schriftSize,
  617. yoff = (double)b->getSchriftSize() / (double)schriftSize;
  618. double x = xs * xoff, y = ys * yoff;
  619. int maxX = getCharWidth( c ), maxY = getCharHeight( c );
  620. maxX = ( xp + maxX ) >= zRObjGr.x ? ( zRObjGr.x - xp ) : maxX;
  621. maxY = ( yp + maxY ) >= zRObjGr.y ? ( zRObjGr.y - yp ) : maxY;
  622. if( !a2 )
  623. {
  624. int dx, ygr, ygr2;
  625. if( zRObj.hasAlpha3D() )
  626. {
  627. for( int dy = ys; dy < maxY; ++dy )
  628. {
  629. ygr2 = ( yp + dy ) * zRObj.getBreite();
  630. ygr = (int)y * br;
  631. for( dx = xs; dx < maxX; ++dx )
  632. {
  633. zRObj.alphaPixel3D( xp + dx + ygr2, color | ( b->getBuff()[ (int)x + ygr ] << 24 ) );
  634. x += xoff;
  635. }
  636. x = xs;
  637. y += yoff;
  638. }
  639. }
  640. else
  641. {
  642. for( int dy = ys; dy < maxY; ++dy )
  643. {
  644. ygr2 = ( yp + dy ) * zRObj.getBreite();
  645. ygr = (int)y * br;
  646. for( dx = xs; dx < maxX; ++dx )
  647. {
  648. zRObj.alphaPixel2D( xp + dx + ygr2, color | ( b->getBuff()[ (int)x + ygr ] << 24 ) );
  649. x += xoff;
  650. }
  651. x = xs;
  652. y += yoff;
  653. }
  654. }
  655. }
  656. else
  657. {
  658. int a, dx, ygr, ygr2;
  659. if( zRObj.hasAlpha3D() )
  660. {
  661. for( int dy = ys; dy < maxY; ++dy )
  662. {
  663. ygr2 = ( yp + dy ) * zRObj.getBreite();
  664. ygr = (int)y * br;
  665. for( dx = xs; dx < maxX; ++dx )
  666. {
  667. a = b->getBuff()[ (int)x + ygr ] - a2;
  668. zRObj.alphaPixel3D( xp + dx + ygr2, color | ( a << 24 ) );
  669. x += xoff;
  670. }
  671. x = xs;
  672. y += yoff;
  673. }
  674. }
  675. else
  676. {
  677. for( int dy = ys; dy < maxY; ++dy )
  678. {
  679. ygr2 = ( yp + dy ) * zRObj.getBreite();
  680. ygr = (int)y * br;
  681. for( dx = xs; dx < maxX; ++dx )
  682. {
  683. a = b->getBuff()[ (int)x + ygr ] - a2;
  684. zRObj.alphaPixel2D( xp + dx + ygr2, color | ( a << 24 ) );
  685. x += xoff;
  686. }
  687. x = xs;
  688. y += yoff;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. if( underlined )
  695. zRObj.drawLinieHAlpha( x - (int)( zeichenAbstand / 2.0 + 0.5 ), y + getZeilenHeight() + getZeichenAbstand() / 2, getCharWidth( c ) + (int)( zeichenAbstand / 2.0 + 0.5 ), color );
  696. }
  697. x += getCharWidth( c ) + zeichenAbstand;
  698. }
  699. else if( c == ' ' )
  700. {
  701. if( selected )
  702. zRObj.alphaRegion( x, y, schriftSize / 2 + zeichenAbstand, getZeilenHeight(), selectedBackgroundColor );
  703. x += schriftSize / 2 + zeichenAbstand;
  704. }
  705. else if( c == '\t' )
  706. {
  707. if( selected )
  708. zRObj.alphaRegion( x, y, schriftSize + zeichenAbstand, getZeilenHeight(), selectedBackgroundColor );
  709. x += schriftSize + zeichenAbstand;
  710. }
  711. }
  712. // Gibt die Schriftgröße zurück, die zum Zeichnen verwendet wird
  713. int TextRenderer::getSchriftSize() const
  714. {
  715. return schriftSize;
  716. }
  717. // Gibt den Abstand in Pixeln zum unteren Ende der darüber ligenden Zeile zurück
  718. int TextRenderer::getZeilenabstand() const
  719. {
  720. return zeilenAbstand;
  721. }
  722. // Gibt den Abstand in Pixeln zum zwischen zwei zeichen auf der x Achse zurück
  723. int TextRenderer::getZeichenAbstand() const
  724. {
  725. return zeichenAbstand;
  726. }
  727. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Text vollständig darzustellen
  728. // txt: Der Text, von dem die Breite in Pixeln ermitelt werden soll
  729. int TextRenderer::getTextBreite( const char *txt ) const
  730. {
  731. int ret = 0;
  732. int tmp = 0;
  733. for( int i = 0; txt[ i ]; ++i )
  734. {
  735. if( txt[ i ] == '\n' )
  736. {
  737. if( tmp > ret )
  738. ret = tmp;
  739. tmp = 0;
  740. }
  741. else if( txt[ i ] == '\t' )
  742. tmp += schriftSize + zeichenAbstand;
  743. else if( txt[ i ] == ' ' )
  744. tmp += schriftSize / 2 + zeichenAbstand;
  745. else
  746. tmp += getCharWidth( txt[ i ] ) + zeichenAbstand;
  747. }
  748. if( tmp > ret )
  749. ret = tmp;
  750. return ret;
  751. }
  752. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Text vollständig darzustellen
  753. // txt: Der Text, von dem die Höhe in Pixeln ermitelt werden soll
  754. int TextRenderer::getTextHeight( const char *txt ) const
  755. {
  756. int hi = getZeilenHeight();
  757. return hi + ( ( hi + zeilenAbstand ) * Text( txt ).anzahlVon( '\n' ) );
  758. }
  759. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Buchstaben vollständig darzustellen
  760. // c: Der Buchstabe, von dem die Breite in Pixeln ermitelt werden soll
  761. int TextRenderer::getCharWidth( const char c ) const
  762. {
  763. if( !s )
  764. return 0;
  765. Alphabet *a = s->zAlphabet( schriftSize );
  766. if( !a )
  767. return 0;
  768. Buchstabe *b = a->zBuchstabe( c );
  769. if( b )
  770. return (int)( ( (double)b->getBreite() / (double)b->getSchriftSize() ) * (double)schriftSize + 0.5 );
  771. return 0;
  772. }
  773. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Text vollständig darzustellen
  774. // c: Der Buchstabe, von dem die Höhe in Pixeln ermitelt werden soll
  775. int TextRenderer::getCharHeight( const char c ) const
  776. {
  777. if( !s )
  778. return 0;
  779. Alphabet *a = s->zAlphabet( schriftSize );
  780. if( !a )
  781. return 0;
  782. Buchstabe *b = a->zBuchstabe( c );
  783. if( b )
  784. return (int)( ( (double)b->getHeight() / (double)b->getSchriftSize() ) * (double)schriftSize + 0.5 );
  785. return 0;
  786. }
  787. // Gibt den Abstand in Pixeln zum unteren Ende der darüber ligenden Zeile zurück
  788. int TextRenderer::getZeilenAbstand() const
  789. {
  790. return zeilenAbstand;
  791. }
  792. // Gibt die skallierte Höhe zurück, die eine gezeichnete Zeile in Pixeln benötigt
  793. int TextRenderer::getZeilenHeight() const
  794. {
  795. int zh = 0;
  796. for( int i = 0; i < 256; ++i )
  797. {
  798. zh = maxInt( getCharHeight( (char)i ), zh );
  799. }
  800. return zh;
  801. }
  802. // Ermittelt das Zeichen im Text, auf das die Maus zeigt
  803. // txt: Der Text, auf den die Maus Zeigt
  804. // mausX: Die X Position der Maus in Pixeln Relativ zur Position des ersten Zeichens
  805. // mausY: Die Y Position der Maus in Pixeln Relativ zur Position des ersten Zeichens
  806. int TextRenderer::textPos( const char *txt, int mausX, int mausY ) const
  807. {
  808. int tx = 0;
  809. int ty = 0;
  810. int sh = getZeilenHeight();
  811. if( mausX < 0 || mausY < 0 )
  812. return -1;
  813. for( int i = 0; i < txt[ i ]; ++i )
  814. {
  815. if( txt[ i ] == '\n' )
  816. {
  817. ty += sh + zeilenAbstand;
  818. tx = 0;
  819. if( mausY < ty )
  820. return i;
  821. }
  822. if( txt[ i ] == '\t' )
  823. tx += schriftSize + zeichenAbstand;
  824. if( txt[ i ] == ' ' )
  825. tx += schriftSize / 2 + zeichenAbstand;
  826. tx += getCharWidth( txt[ i ] );
  827. int txpl = getCharWidth( txt[ i + 1 ] ) / 2;
  828. if( mausX < tx - txpl && mausY < ty + sh + zeilenAbstand )
  829. return i;
  830. }
  831. if( mausY < ty + sh + zeilenAbstand )
  832. return textLength( txt );
  833. return -1;
  834. }
  835. TextRenderer *TextRenderer::getThis()
  836. {
  837. ref++;
  838. return this;
  839. }
  840. TextRenderer *TextRenderer::release()
  841. {
  842. if( !--ref )
  843. delete this;
  844. return 0;
  845. }
  846. GravurTextRenderer::GravurTextRenderer()
  847. : GravurTextRenderer( 0 )
  848. {}
  849. GravurTextRenderer::GravurTextRenderer( Schrift *schrift )
  850. : TextRenderer( schrift )
  851. {}
  852. GravurTextRenderer::~GravurTextRenderer()
  853. {}
  854. // Zeichnet einen Bestimmten Buchstaben mit einfärbung auf ein Bild
  855. // Nutze (setPosition) und (setDrawSchriftGröße) um die Position und die Größe zu verändern
  856. // x: x position des ersten zeichens
  857. // y: y position des ersten zeichens
  858. // txt: Der Text, der gezeichnet werden soll
  859. // zRObj: Das Bild, auf das gezeichnet werden soll
  860. // color: Die Farbe, in der der Text gezeichnet werden soll
  861. // underlined: 1, falls der Text unterstrichen sein soll
  862. // selected: 1, falls das zeichen eingefärbt sein soll
  863. // selectedBackgroundColor: Die Hintergrund Farbe des eingefärbten Textes
  864. void GravurTextRenderer::renderChar( int &x, int y, char c, Bild &zRObj, int color, bool underlined, bool selected, int selectedBackgroundColor )
  865. {
  866. if( !s )
  867. return;
  868. Alphabet *a = s->zAlphabet( schriftSize );
  869. Buchstabe *b = a->zBuchstabe( c );
  870. if( b )
  871. {
  872. if( x >= zRObj.getBreite() )
  873. return;
  874. if( zRObj.isAreaDrawable( x, y, getCharWidth( c ), getCharHeight( c ) ) )
  875. {
  876. if( selected )
  877. {
  878. int br = getCharWidth( c ) + zeichenAbstand;
  879. zRObj.alphaRegion( x, y, br, getZeilenHeight(), selectedBackgroundColor );
  880. }
  881. if( b->getBuff() )
  882. {
  883. const Punkt &zRObjGr = zRObj.getDrawGr();
  884. const Punkt &zRObjPos = zRObj.getDrawPos();
  885. const Punkt &zRObjOff = zRObj.getDrawOff();
  886. int xp = x + zRObjOff.x, yp = y + zRObjOff.y;
  887. int xs = xp < zRObjPos.x ? ( zRObjPos.x - xp ) : 0, ys = yp < zRObjPos.y ? ( zRObjPos.y - yp ) : 0;
  888. int br = b->getBreite(), h = b->getHeight();
  889. color &= 0x00FFFFFF;
  890. double xoff = (double)b->getSchriftSize() / ( schriftSize * 2.0 ),
  891. yoff = (double)b->getSchriftSize() / ( schriftSize * 2.0 );
  892. double x = xs * xoff, y = ys * yoff;
  893. int maxX = getCharWidth( c ), maxY = getCharHeight( c );
  894. maxX = ( xp + maxX ) >= zRObjGr.x ? ( zRObjGr.x - xp ) : maxX;
  895. maxY = ( yp + maxY ) >= zRObjGr.y ? ( zRObjGr.y - yp ) : maxY;
  896. int dx, ygr, ygr2;
  897. if( zRObj.hasAlpha3D() )
  898. {
  899. for( int dy = ys; dy < maxY; ++dy )
  900. {
  901. ygr2 = ( yp + dy ) * zRObj.getBreite();
  902. ygr = (int)y * br;
  903. for( dx = xs; dx < maxX; ++dx )
  904. {
  905. int f = 0;
  906. if( b->getBuff()[ (int)x + ygr ] )
  907. f = 0x50000000;
  908. else if( ( (int)( x + xoff ) < br && b->getBuff()[ (int)( x + xoff ) + ygr ] ) || ( (int)( y - yoff ) < h && b->getBuff()[ (int)x + (int)( y - yoff ) * br ] > 0xF0 ) )
  909. f = 0xA0000000;
  910. else if( ( (int)( x - xoff ) < br && b->getBuff()[ (int)( x - xoff ) + ygr ] ) || ( (int)( y + yoff ) < h && b->getBuff()[ (int)x + (int)( y + yoff ) * br ] > 0xF0 ) )
  911. {
  912. f = 0xA0FFFFFF;
  913. }
  914. zRObj.alphaPixel3D( xp + dx + ygr2, f );
  915. x += xoff;
  916. }
  917. x = xs;
  918. y += yoff;
  919. }
  920. }
  921. else
  922. {
  923. for( int dy = ys; dy < maxY; ++dy )
  924. {
  925. ygr2 = ( yp + dy ) * zRObj.getBreite();
  926. ygr = (int)y * br;
  927. for( dx = xs; dx < maxX; ++dx )
  928. {
  929. int f = 0;
  930. if( b->getBuff()[ (int)x + ygr ] )
  931. f = 0x50000000;
  932. else if( ( (int)( x + xoff ) < br && b->getBuff()[ (int)( x + xoff ) + ygr ] ) || ( (int)( y - yoff ) < h && b->getBuff()[ (int)x + (int)( y - yoff ) * br ] > 0xF0 ) )
  933. f = 0xA0000000;
  934. else if( ( (int)( x - xoff ) < br && b->getBuff()[ (int)( x - xoff ) + ygr ] ) || ( (int)( y + yoff ) < h && b->getBuff()[ (int)x + (int)( y + yoff ) * br ] > 0xF0 ) )
  935. {
  936. f = 0xA0FFFFFF;
  937. }
  938. zRObj.alphaPixel2D( xp + dx + ygr2, f );
  939. x += xoff;
  940. }
  941. x = xs;
  942. y += yoff;
  943. }
  944. }
  945. }
  946. if( underlined )
  947. zRObj.drawLinieHAlpha( x - (int)( zeichenAbstand / 2.0 + 0.5 ), y + getZeilenHeight() + getZeichenAbstand() / 2, getCharWidth( c ) + (int)( zeichenAbstand / 2.0 + 0.5 ), color );
  948. }
  949. x += getCharWidth( c ) + zeichenAbstand;
  950. }
  951. else if( c == ' ' )
  952. {
  953. if( selected )
  954. zRObj.alphaRegion( x, y, schriftSize / 2 + zeichenAbstand, getZeilenHeight(), selectedBackgroundColor );
  955. x += schriftSize / 2 + zeichenAbstand;
  956. }
  957. else if( c == '\t' )
  958. {
  959. if( selected )
  960. zRObj.alphaRegion( x, y, schriftSize + zeichenAbstand, getZeilenHeight(), selectedBackgroundColor );
  961. x += schriftSize + zeichenAbstand;
  962. }
  963. }
  964. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Buchstaben vollständig darzustellen
  965. // c: Der Buchstabe, von dem die Breite in Pixeln ermitelt werden soll
  966. int GravurTextRenderer::getCharWidth( const char c ) const
  967. {
  968. if( !s )
  969. return 0;
  970. Alphabet *a = s->zAlphabet( schriftSize );
  971. Buchstabe *b = a->zBuchstabe( c );
  972. if( b )
  973. return (int)( ( (double)b->getBreite() / (double)b->getSchriftSize() ) * (double)schriftSize * 2 + 0.5 );
  974. return 0;
  975. }
  976. // Ermittelt, wie viele Pixel benötigt werden, um einen Bestimmten Text vollständig darzustellen
  977. // c: Der Buchstabe, von dem die Höhe in Pixeln ermitelt werden soll
  978. int GravurTextRenderer::getCharHeight( const char c ) const
  979. {
  980. if( !s )
  981. return 0;
  982. Alphabet *a = s->zAlphabet( schriftSize );
  983. Buchstabe *b = a->zBuchstabe( c );
  984. if( b )
  985. return (int)( ( (double)b->getHeight() / (double)b->getSchriftSize() ) * (double)schriftSize * 2 + 0.5 );
  986. return 0;
  987. }
  988. // Verringert den Reference Counting Zähler. Wenn der Zähler 0 erreicht, wird das Objekt automatisch gelöscht.
  989. // return: 0.
  990. TextRenderer *GravurTextRenderer::release()
  991. {
  992. if( !--ref )
  993. delete this;
  994. return 0;
  995. }