Schrift.cpp 34 KB

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