string.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. //*****************************************************************************
  2. //
  3. // string.c - Routines for drawing text.
  4. //
  5. // Copyright (c) 2007-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 6288 of the Stellaris Graphics Library.
  22. //
  23. //*****************************************************************************
  24. #include "debug.h"
  25. #include "grlib.h"
  26. //*****************************************************************************
  27. //
  28. //! \addtogroup primitives_api
  29. //! @{
  30. //
  31. //*****************************************************************************
  32. //*****************************************************************************
  33. //
  34. // The character printed by GrStringDraw in place of any character in the
  35. // string which does not appear in the font.
  36. //
  37. //*****************************************************************************
  38. #define ABSENT_CHAR_REPLACEMENT '.'
  39. //*****************************************************************************
  40. //
  41. // Counts the number of zeros at the start of a word. This macro uses
  42. // compiler-specific constructs to perform an inline insertion of the "clz"
  43. // instruction, which counts the leading zeros directly.
  44. //
  45. //*****************************************************************************
  46. #if defined(ewarm)
  47. #include <intrinsics.h>
  48. #define NumLeadingZeros(x) __CLZ(x)
  49. #elif defined(codered) || defined(gcc) || defined(sourcerygxx)
  50. #define NumLeadingZeros(x) __extension__ \
  51. ({ \
  52. register unsigned int __ret, __inp = x; \
  53. __asm__("clz %0, %1" : "=r" (__ret) : "r" (__inp)); \
  54. __ret; \
  55. })
  56. #elif defined(rvmdk) || defined(__ARMCC_VERSION)
  57. #define NumLeadingZeros(x) __clz(x)
  58. #elif defined(ccs)
  59. //
  60. // The CCS/TI compiler _norm intrinsic function will generate an inline CLZ
  61. // instruction.
  62. //
  63. #define NumLeadingZeros(x) _norm(x)
  64. #else
  65. // Compiler independent code for counting Number of leading zeros in a number.
  66. // This is to be used when the Processor doesnt support asm code to count the zeros.
  67. unsigned char NumLeadingZeros(unsigned int x)
  68. {
  69. register unsigned char count = 0;//sizeof(x)*8;
  70. if (x == 0)
  71. {
  72. return 32;
  73. }
  74. while(x)
  75. {
  76. if(!(x & 0xFF000000))
  77. {
  78. count = count+8;
  79. x = x << 8;
  80. }
  81. else
  82. {
  83. while(!(x & 0x80000000))
  84. {
  85. x = x << 1;
  86. count++;
  87. }
  88. break;
  89. }
  90. }
  91. return count;
  92. }
  93. #endif
  94. //*****************************************************************************
  95. //
  96. //! Determines the width of a string.
  97. //!
  98. //! \param pContext is a pointer to the drawing context to use.
  99. //! \param pcString is the string in question.
  100. //! \param lLength is the length of the string.
  101. //!
  102. //! This function determines the width of a string (or portion of the string)
  103. //! when drawn with a particular font. The \e lLength parameter allows a
  104. //! portion of the string to be examined without having to insert a NULL
  105. //! character at the stopping point (would not be possible if the string was
  106. //! located in flash); specifying a length of -1 will cause the width of the
  107. //! entire string to be computed.
  108. //!
  109. //! \return Returns the width of the string in pixels.
  110. //
  111. //*****************************************************************************
  112. int
  113. GrStringWidthGet(const tContext *pContext, const char *pcString, int lLength)
  114. {
  115. const unsigned short *pusOffset;
  116. const unsigned char *pucData;
  117. int lWidth;
  118. //
  119. // Check the arguments.
  120. //
  121. ASSERT(pContext);
  122. ASSERT(pcString);
  123. //
  124. // Get some pointers to relevant information in the font to make things
  125. // easier, and give the compiler a hint about extraneous loads that it can
  126. // avoid.
  127. //
  128. pucData = pContext->pFont->pucData;
  129. pusOffset = pContext->pFont->pusOffset;
  130. //
  131. // Loop through the characters in the string.
  132. //
  133. for(lWidth = 0; *pcString && lLength; pcString++, lLength--)
  134. {
  135. //
  136. // Get a pointer to the font data for the next character from the
  137. // string. If there is not a glyph for the next character, replace it
  138. // with a ".".
  139. //
  140. if((*pcString >= ' ') && (*pcString <= '~'))
  141. {
  142. //
  143. // Add the width of this character as drawn with the given font.
  144. //
  145. lWidth += pucData[pusOffset[*pcString - ' '] + 1];
  146. }
  147. else
  148. {
  149. //
  150. // This character does not exist in the font so replace it with
  151. // a '.' instead. This matches the approach taken in GrStringDraw
  152. // and ensures that the width returned here represents the
  153. // rendered dimension of the string.
  154. //
  155. lWidth += pucData[pusOffset[ABSENT_CHAR_REPLACEMENT - ' '] + 1];
  156. }
  157. }
  158. //
  159. // Return the width of the string.
  160. //
  161. return(lWidth);
  162. }
  163. //*****************************************************************************
  164. //
  165. //! Draws a string.
  166. //!
  167. //! \param pContext is a pointer to the drawing context to use.
  168. //! \param pcString is a pointer to the string to be drawn.
  169. //! \param lLength is the number of characters from the string that should be
  170. //! drawn on the screen.
  171. //! \param lX is the X coordinate of the upper left corner of the string
  172. //! position on the screen.
  173. //! \param lY is the Y coordinate of the upper left corner of the string
  174. //! position on the screen.
  175. //! \param bOpaque is true of the background of each character should be drawn
  176. //! and false if it should not (leaving the background as is).
  177. //!
  178. //! This function draws a string of test on the screen. The \e lLength
  179. //! parameter allows a portion of the string to be examined without having to
  180. //! insert a NULL character at the stopping point (which would not be possible
  181. //! if the string was located in flash); specifying a length of -1 will cause
  182. //! the entire string to be rendered (subject to clipping).
  183. //!
  184. //! \return None.
  185. //
  186. //*****************************************************************************
  187. void
  188. GrStringDraw(const tContext *pContext, const char *pcString, int lLength,
  189. int lX, int lY, unsigned int bOpaque)
  190. {
  191. int lIdx, lX0, lY0, lCount, lOff, lOn, lBit;
  192. const unsigned char *pucData;
  193. tContext sCon;
  194. //
  195. // Check the arguments.
  196. //
  197. ASSERT(pContext);
  198. ASSERT(pcString);
  199. //
  200. // Copy the drawing context into a local structure that can be modified.
  201. //
  202. sCon = *pContext;
  203. //
  204. // Loop through the characters in the string.
  205. //
  206. while(*pcString && lLength--)
  207. {
  208. //
  209. // Stop drawing the string if the right edge of the clipping region has
  210. // been exceeded.
  211. //
  212. if(lX > sCon.sClipRegion.sXMax)
  213. {
  214. break;
  215. }
  216. //
  217. // Get a pointer to the font data for the next character from the
  218. // string. If there is not a glyph for the next character, replace it
  219. // with a ".".
  220. //
  221. if((*pcString >= ' ') && (*pcString <= '~'))
  222. {
  223. pucData = (sCon.pFont->pucData +
  224. sCon.pFont->pusOffset[*pcString++ - ' ']);
  225. }
  226. else
  227. {
  228. pucData = (sCon.pFont->pucData +
  229. sCon.pFont->pusOffset[ABSENT_CHAR_REPLACEMENT - ' ']);
  230. pcString++;
  231. }
  232. //
  233. // See if the entire character is to the left of the clipping region.
  234. //
  235. if((lX + pucData[1]) < sCon.sClipRegion.sXMin)
  236. {
  237. //
  238. // Increment the X coordinate by the width of the character.
  239. //
  240. lX += pucData[1];
  241. //
  242. // Go to the next character in the string.
  243. //
  244. continue;
  245. }
  246. //
  247. // Loop through the bytes in the encoded data for this glyph.
  248. //
  249. for(lIdx = 2, lX0 = 0, lBit = 0, lY0 = 0; lIdx < pucData[0]; )
  250. {
  251. //
  252. // See if the bottom of the clipping region has been exceeded.
  253. //
  254. if((lY + lY0) > sCon.sClipRegion.sYMax)
  255. {
  256. //
  257. // Stop drawing this character.
  258. //
  259. break;
  260. }
  261. //
  262. // See if the font is uncompressed.
  263. //
  264. if(sCon.pFont->ucFormat == FONT_FMT_UNCOMPRESSED)
  265. {
  266. //
  267. // Count the number of off pixels from this position in the
  268. // glyph image.
  269. //
  270. for(lOff = 0; lIdx < pucData[0]; )
  271. {
  272. //
  273. // Get the number of zero pixels at this position.
  274. //
  275. lCount = NumLeadingZeros(pucData[lIdx] << (24 + lBit));
  276. //
  277. // If there were more than 8, then it is a "false" result
  278. // since it counted beyond the end of the current byte.
  279. // Therefore, simply limit it to the number of pixels
  280. // remaining in this byte.
  281. //
  282. if(lCount > 8)
  283. {
  284. lCount = 8 - lBit;
  285. }
  286. //
  287. // Increment the number of off pixels.
  288. //
  289. lOff += lCount;
  290. //
  291. // Increment the bit position within the byte.
  292. //
  293. lBit += lCount;
  294. //
  295. // See if the end of the byte has been reached.
  296. //
  297. if(lBit == 8)
  298. {
  299. //
  300. // Advance to the next byte and continue counting off
  301. // pixels.
  302. //
  303. lBit = 0;
  304. lIdx++;
  305. }
  306. else
  307. {
  308. //
  309. // Since the end of the byte was not reached, there
  310. // must be an on pixel. Therefore, stop counting off
  311. // pixels.
  312. //
  313. break;
  314. }
  315. }
  316. //
  317. // Count the number of on pixels from this position in the
  318. // glyph image.
  319. //
  320. for(lOn = 0; lIdx < pucData[0]; )
  321. {
  322. //
  323. // Get the number of one pixels at this location (by
  324. // inverting the data and counting the number of zeros).
  325. //
  326. lCount = NumLeadingZeros(~(pucData[lIdx] << (24 + lBit)));
  327. //
  328. // If there were more than 8, then it is a "false" result
  329. // since it counted beyond the end of the current byte.
  330. // Therefore, simply limit it to the number of pixels
  331. // remaining in this byte.
  332. //
  333. if(lCount > 8)
  334. {
  335. lCount = 8 - lBit;
  336. }
  337. //
  338. // Increment the number of on pixels.
  339. //
  340. lOn += lCount;
  341. //
  342. // Increment the bit position within the byte.
  343. //
  344. lBit += lCount;
  345. //
  346. // See if the end of the byte has been reached.
  347. //
  348. if(lBit == 8)
  349. {
  350. //
  351. // Advance to the next byte and continue counting on
  352. // pixels.
  353. //
  354. lBit = 0;
  355. lIdx++;
  356. }
  357. else
  358. {
  359. //
  360. // Since the end of the byte was not reached, there
  361. // must be an off pixel. Therefore, stop counting on
  362. // pixels.
  363. //
  364. break;
  365. }
  366. }
  367. }
  368. //
  369. // Otherwise, the font is compressed with a pixel RLE scheme.
  370. //
  371. else
  372. {
  373. //
  374. // See if this is a byte that encodes some on and off pixels.
  375. //
  376. if(pucData[lIdx])
  377. {
  378. //
  379. // Extract the number of off pixels.
  380. //
  381. lOff = (pucData[lIdx] >> 4) & 15;
  382. //
  383. // Extract the number of on pixels.
  384. //
  385. lOn = pucData[lIdx] & 15;
  386. //
  387. // Skip past this encoded byte.
  388. //
  389. lIdx++;
  390. }
  391. //
  392. // Otherwise, see if this is a repeated on pixel byte.
  393. //
  394. else if(pucData[lIdx + 1] & 0x80)
  395. {
  396. //
  397. // There are no off pixels in this encoding.
  398. //
  399. lOff = 0;
  400. //
  401. // Extract the number of on pixels.
  402. //
  403. lOn = (pucData[lIdx + 1] & 0x7f) * 8;
  404. //
  405. // Skip past these two encoded bytes.
  406. //
  407. lIdx += 2;
  408. }
  409. //
  410. // Otherwise, this is a repeated off pixel byte.
  411. //
  412. else
  413. {
  414. //
  415. // Extract the number of off pixels.
  416. //
  417. lOff = pucData[lIdx + 1] * 8;
  418. //
  419. // There are no on pixels in this encoding.
  420. //
  421. lOn = 0;
  422. //
  423. // Skip past these two encoded bytes.
  424. //
  425. lIdx += 2;
  426. }
  427. }
  428. //
  429. // Loop while there are any off pixels.
  430. //
  431. while(lOff)
  432. {
  433. //
  434. // See if the bottom of the clipping region has been exceeded.
  435. //
  436. if((lY + lY0) > sCon.sClipRegion.sYMax)
  437. {
  438. //
  439. // Ignore the remainder of the on pixels.
  440. //
  441. break;
  442. }
  443. //
  444. // See if there is more than one on pixel that will fit onto
  445. // the current row.
  446. //
  447. if((lOff > 1) && ((lX0 + 1) < pucData[1]))
  448. {
  449. //
  450. // Determine the number of on pixels that will fit on this
  451. // row.
  452. //
  453. lCount = (((lX0 + lOff) > pucData[1]) ? pucData[1] - lX0 :
  454. lOff);
  455. //
  456. // If this row is within the clipping region, draw a
  457. // horizontal line that corresponds to the sequence of on
  458. // pixels.
  459. //
  460. if(((lY + lY0) >= sCon.sClipRegion.sYMin) && bOpaque)
  461. {
  462. sCon.ulForeground = pContext->ulBackground;
  463. GrLineDrawH(&sCon, lX + lX0, lX + lX0 + lCount - 1,
  464. lY + lY0);
  465. }
  466. //
  467. // Decrement the count of on pixels by the number on this
  468. // row.
  469. //
  470. lOff -= lCount;
  471. //
  472. // Increment the X offset by the number of on pixels.
  473. //
  474. lX0 += lCount;
  475. }
  476. //
  477. // Otherwise, there is only a single on pixel that can be
  478. // drawn.
  479. //
  480. else
  481. {
  482. //
  483. // If this pixel is within the clipping region, then draw
  484. // it.
  485. //
  486. if(((lX + lX0) >= sCon.sClipRegion.sXMin) &&
  487. ((lX + lX0) <= sCon.sClipRegion.sXMax) &&
  488. ((lY + lY0) >= sCon.sClipRegion.sYMin) && bOpaque)
  489. {
  490. DpyPixelDraw(pContext->pDisplay, lX + lX0, lY + lY0,
  491. pContext->ulBackground);
  492. }
  493. //
  494. // Decrement the count of on pixels.
  495. //
  496. lOff--;
  497. //
  498. // Increment the X offset.
  499. //
  500. lX0++;
  501. }
  502. //
  503. // See if the X offset has reached the right side of the
  504. // character glyph.
  505. //
  506. if(lX0 == pucData[1])
  507. {
  508. //
  509. // Increment the Y offset.
  510. //
  511. lY0++;
  512. //
  513. // Reset the X offset to the left side of the character
  514. // glyph.
  515. //
  516. lX0 = 0;
  517. }
  518. }
  519. //
  520. // Loop while there are any on pixels.
  521. //
  522. while(lOn)
  523. {
  524. //
  525. // See if the bottom of the clipping region has been exceeded.
  526. //
  527. if((lY + lY0) > sCon.sClipRegion.sYMax)
  528. {
  529. //
  530. // Ignore the remainder of the on pixels.
  531. //
  532. break;
  533. }
  534. //
  535. // See if there is more than one on pixel that will fit onto
  536. // the current row.
  537. //
  538. if((lOn > 1) && ((lX0 + 1) < pucData[1]))
  539. {
  540. //
  541. // Determine the number of on pixels that will fit on this
  542. // row.
  543. //
  544. lCount = (((lX0 + lOn) > pucData[1]) ? pucData[1] - lX0 :
  545. lOn);
  546. //
  547. // If this row is within the clipping region, draw a
  548. // horizontal line that corresponds to the sequence of on
  549. // pixels.
  550. //
  551. if((lY + lY0) >= sCon.sClipRegion.sYMin)
  552. {
  553. sCon.ulForeground = pContext->ulForeground;
  554. GrLineDrawH(&sCon, lX + lX0, lX + lX0 + lCount - 1,
  555. lY + lY0);
  556. }
  557. //
  558. // Decrement the count of on pixels by the number on this
  559. // row.
  560. //
  561. lOn -= lCount;
  562. //
  563. // Increment the X offset by the number of on pixels.
  564. //
  565. lX0 += lCount;
  566. }
  567. //
  568. // Otherwise, there is only a single on pixel that can be
  569. // drawn.
  570. //
  571. else
  572. {
  573. //
  574. // If this pixel is within the clipping region, then draw
  575. // it.
  576. //
  577. if(((lX + lX0) >= sCon.sClipRegion.sXMin) &&
  578. ((lX + lX0) <= sCon.sClipRegion.sXMax) &&
  579. ((lY + lY0) >= sCon.sClipRegion.sYMin))
  580. {
  581. DpyPixelDraw(pContext->pDisplay, lX + lX0, lY + lY0,
  582. pContext->ulForeground);
  583. }
  584. //
  585. // Decrement the count of on pixels.
  586. //
  587. lOn--;
  588. //
  589. // Increment the X offset.
  590. //
  591. lX0++;
  592. }
  593. //
  594. // See if the X offset has reached the right side of the
  595. // character glyph.
  596. //
  597. if(lX0 == pucData[1])
  598. {
  599. //
  600. // Increment the Y offset.
  601. //
  602. lY0++;
  603. //
  604. // Reset the X offset to the left side of the character
  605. // glyph.
  606. //
  607. lX0 = 0;
  608. }
  609. }
  610. }
  611. //
  612. // Increment the X coordinate by the width of the character.
  613. //
  614. lX += pucData[1];
  615. }
  616. }
  617. //*****************************************************************************
  618. //
  619. // Definitions and variables used by the decompression routine for the string
  620. // table.
  621. //
  622. //*****************************************************************************
  623. #define SC_MAX_INDEX 2047
  624. #define SC_IS_NULL 0x0000ffff
  625. #define SC_GET_LEN(v) ((v) >> (32 - 5))
  626. #define SC_GET_INDEX(v) (((v) >> 16) & SC_MAX_INDEX)
  627. #define SC_GET_OFF(v) ((v) & SC_IS_NULL)
  628. #define SC_FLAG_COMPRESSED 0x00008000
  629. #define SC_OFFSET_M 0x00007fff
  630. //*****************************************************************************
  631. //
  632. // The globals that hold the shortcuts to various locations and values in the
  633. // table.
  634. //
  635. //*****************************************************************************
  636. static const unsigned int *g_pulStringTable;
  637. static const unsigned short *g_pusLanguageTable;
  638. static const unsigned char *g_pucStringData;
  639. static unsigned short g_usLanguage;
  640. static unsigned short g_usNumLanguages;
  641. static unsigned short g_usNumStrings;
  642. //*****************************************************************************
  643. //
  644. //! This function sets the location of the current string table.
  645. //!
  646. //! \param pvTable is a pointer to a string table that was generated by the
  647. //! string compression utility.
  648. //!
  649. //! This function is used to set the string table to use for strings in an
  650. //! application. This string table is created by the string compression
  651. //! utility. This function is used to swap out multiple string tables if the
  652. //! application requires more than one table. It does not allow using more
  653. //! than one string table at a time.
  654. //!
  655. //! \return None.
  656. //
  657. //*****************************************************************************
  658. void
  659. GrStringTableSet(const void *pvTable)
  660. {
  661. //
  662. // Save the number of languages and number of strings.
  663. //
  664. g_usNumStrings = ((unsigned short *)pvTable)[0];
  665. g_usNumLanguages = ((unsigned short *)pvTable)[1];
  666. //
  667. // Save a pointer to the Language Identifier table.
  668. //
  669. g_pusLanguageTable = (unsigned short *)pvTable + 2;
  670. //
  671. // Save a pointer to the String Index table.
  672. //
  673. g_pulStringTable = (unsigned int *)(g_pusLanguageTable +
  674. g_usNumLanguages);
  675. //
  676. // Save a pointer to the String Data.
  677. //
  678. g_pucStringData = (unsigned char *)(g_pulStringTable +
  679. (g_usNumStrings * g_usNumLanguages));
  680. }
  681. //*****************************************************************************
  682. //
  683. //! This function sets the current language for strings returned by the
  684. //! GrStringGet() function.
  685. //!
  686. //! \param usLangID is one of the language identifiers provided in the string
  687. //! table.
  688. //!
  689. //! This function is used to set the language identifier for the strings
  690. //! returned by the GrStringGet() function. The \e usLangID parameter should
  691. //! match one of the identifiers that was included in the string table. These
  692. //! are provided in a header file in the graphics library and must match the
  693. //! values that were passed through the sting compression utility.
  694. //!
  695. //! \return This function returns 0 if the language was not found and a
  696. //! non-zero value if the laguage was found.
  697. //
  698. //*****************************************************************************
  699. unsigned int
  700. GrStringLanguageSet(unsigned short usLangID)
  701. {
  702. int iLang;
  703. //
  704. // Search for the requested language.
  705. //
  706. for(iLang = 0; iLang < g_usNumLanguages; iLang++)
  707. {
  708. //
  709. // Once found, break out and save the new language.
  710. //
  711. if(g_pusLanguageTable[iLang] == usLangID)
  712. {
  713. break;
  714. }
  715. }
  716. //
  717. // Only accept the language if it was found, otherwise continue using
  718. // previous language.
  719. //
  720. if(iLang != g_usNumLanguages)
  721. {
  722. g_usLanguage = iLang;
  723. return(1);
  724. }
  725. return(0);
  726. }
  727. //*****************************************************************************
  728. //
  729. //! This function returns a string from the current string table.
  730. //!
  731. //! \param iIndex is the index of the string to retrieve.
  732. //! \param pcData is the pointer to the buffer to store the string into.
  733. //! \param ulSize is the size of the buffer provided by pcData.
  734. //!
  735. //! This function will return a string from the string table in the language
  736. //! set by the GrStringLanguageSet() function. The value passed in \e iIndex
  737. //! parameter is the string that is being requested and will be returned in
  738. //! the buffer provided in the \e pcData parameter. The amount of data
  739. //! returned will be limited by the ulSize parameter.
  740. //!
  741. //! \return Returns the number of valid bytes returned in the \e pcData buffer.
  742. //
  743. //*****************************************************************************
  744. unsigned int
  745. GrStringGet(int iIndex, char *pcData, unsigned int ulSize)
  746. {
  747. unsigned int ulLen, ulOffset, ulSubCode[16];
  748. int iPos, iIdx, iBit, iSkip, iBuf;
  749. unsigned char *pucBufferOut;
  750. const unsigned char *pucString;
  751. ASSERT(iIndex < g_usNumStrings);
  752. ASSERT(pcData != 0);
  753. //
  754. // Initialize the output buffer state.
  755. //
  756. iPos = 0;
  757. pucBufferOut = 0;
  758. //
  759. // if built up from another string, we need to process that
  760. // this could nest multiple layers, so we follow in
  761. //
  762. ulSubCode[iPos] = g_pulStringTable[(g_usLanguage * g_usNumStrings) +
  763. iIndex];
  764. if(SC_GET_LEN(ulSubCode[iPos]))
  765. {
  766. //
  767. // recurse down
  768. //
  769. while(iPos < 16)
  770. {
  771. //
  772. // Copy over the partial (if any) from a previous string.
  773. //
  774. iIdx = SC_GET_INDEX(ulSubCode[iPos++]);
  775. ulSubCode[iPos] = g_pulStringTable[(g_usLanguage *
  776. g_usNumStrings) + iIdx];
  777. if(!SC_GET_LEN(ulSubCode[iPos]))
  778. {
  779. //
  780. // not linked, just string
  781. //
  782. break;
  783. }
  784. }
  785. }
  786. //
  787. // Now work backwards out.
  788. //
  789. iIdx = 0;
  790. //
  791. // Build up the string in pieces.
  792. //
  793. while(iPos >= 0)
  794. {
  795. //
  796. // Get the offset in string table.
  797. //
  798. ulOffset = SC_GET_OFF(ulSubCode[iPos]);
  799. if(ulOffset == SC_IS_NULL)
  800. {
  801. //
  802. // An empty string.
  803. //
  804. pcData[iIdx] = 0;
  805. }
  806. else if(ulOffset & SC_FLAG_COMPRESSED)
  807. {
  808. //
  809. // This is a compressed string so initialize the pointer to the
  810. // compressed data.
  811. //
  812. pucString = g_pucStringData + (ulOffset & SC_OFFSET_M);
  813. //
  814. // Initialize the bit variables.
  815. //
  816. iBit = 0;
  817. iSkip = 0;
  818. //
  819. // Make a pointer to the current buffer out location.
  820. //
  821. pucBufferOut = (unsigned char *)pcData + iIdx;
  822. //
  823. // If the out buffer is beyond the maximum size then just break
  824. // out and return what we have so far.
  825. //
  826. if((char *)pucBufferOut > (pcData + ulSize))
  827. {
  828. break;
  829. }
  830. //
  831. // Now build up real string by decompressing bits.
  832. //
  833. if(!SC_GET_LEN(ulSubCode[iPos]) && SC_GET_INDEX(ulSubCode[iPos]))
  834. {
  835. iSkip = SC_GET_INDEX(ulSubCode[iPos]);
  836. if(iPos)
  837. {
  838. ulLen = SC_GET_LEN(ulSubCode[iPos-1]);
  839. }
  840. else
  841. {
  842. ulLen = (iSkip & 0x3f);
  843. }
  844. iSkip >>= 6;
  845. iIdx += ulLen;
  846. ulLen += iSkip;
  847. }
  848. else if(iPos)
  849. {
  850. //
  851. // Get the length of the partial string.
  852. //
  853. ulLen = SC_GET_LEN(ulSubCode[iPos-1]) - iIdx;
  854. iIdx += ulLen;
  855. }
  856. else if(!SC_GET_LEN(ulSubCode[0]) && SC_GET_INDEX(ulSubCode[0]))
  857. {
  858. ulLen = SC_GET_INDEX(ulSubCode[0]);
  859. iSkip = ulLen >> 6;
  860. ulLen = (ulLen & 0x3f) + iSkip;
  861. }
  862. else
  863. {
  864. //
  865. // Arbitrary as null character ends the string.
  866. //
  867. ulLen = 1024;
  868. }
  869. for(; ulLen; ulLen--)
  870. {
  871. //
  872. // Packed 6 bits for each char
  873. //
  874. *pucBufferOut = (*pucString >> iBit) & 0x3f;
  875. if(iBit >= 2)
  876. {
  877. *pucBufferOut |= (*++pucString << (8-iBit)) & 0x3f;
  878. }
  879. iBit = (iBit + 6) & 0x7;
  880. if(!*pucBufferOut)
  881. {
  882. //
  883. // end of string
  884. //
  885. break;
  886. }
  887. if(iSkip)
  888. {
  889. iSkip--;
  890. continue;
  891. }
  892. //
  893. // Put back removed bit
  894. //
  895. *pucBufferOut |= 0x40;
  896. //
  897. // Now look for a few special chars we mapped up into other
  898. // characters.
  899. //
  900. if(*pucBufferOut == '`')
  901. {
  902. *pucBufferOut = ' ';
  903. }
  904. else if(*pucBufferOut == '~')
  905. {
  906. *pucBufferOut = '-';
  907. }
  908. else if(*pucBufferOut == 0x7f)
  909. {
  910. *pucBufferOut = '.';
  911. }
  912. else if(*pucBufferOut == '\\')
  913. {
  914. *pucBufferOut = ':';
  915. }
  916. //
  917. // Increment the pointer and break out if the pointer is now
  918. // beyond the end of the buffer provided.
  919. //
  920. pucBufferOut++;
  921. if((char *)pucBufferOut >= (pcData + ulSize))
  922. {
  923. break;
  924. }
  925. }
  926. }
  927. else if(iPos)
  928. {
  929. //
  930. // Part of another string
  931. //
  932. ulLen = SC_GET_LEN(ulSubCode[iPos - 1]) - iIdx;
  933. //
  934. // Prevent this copy from going beyond the end of the buffer
  935. // provided.
  936. //
  937. if((iIdx + ulLen) > ulSize)
  938. {
  939. ulLen = ulSize - iIdx;
  940. }
  941. //
  942. // Copy this portion of the string to the output buffer.
  943. //
  944. for(iBuf = 0; iBuf < ulLen; iBuf++)
  945. {
  946. pcData[iIdx + iBuf] = g_pucStringData[ulOffset + iBuf];
  947. }
  948. iIdx += ulLen;
  949. }
  950. else if(SC_GET_INDEX(ulSubCode[0]) && !SC_GET_LEN(ulSubCode[0]))
  951. {
  952. //
  953. // Copy this portion of the string to the output buffer.
  954. //
  955. for(iBuf = 0; iBuf < SC_GET_INDEX(ulSubCode[0]); iBuf++)
  956. {
  957. if((iIdx + iBuf) < ulSize)
  958. {
  959. pcData[iIdx + iBuf] = g_pucStringData[ulOffset + iBuf];
  960. }
  961. else
  962. {
  963. break;
  964. }
  965. }
  966. }
  967. else
  968. {
  969. //
  970. // The last piece is the string ending
  971. //
  972. for(iBuf = 0; iBuf < (ulSize - iIdx); iBuf++)
  973. {
  974. //
  975. // Copy the string to the output buffer.
  976. //
  977. pcData[iIdx + iBuf] = g_pucStringData[ulOffset + iBuf];
  978. //
  979. // If a null is hit then terminate the copy.
  980. //
  981. if(pcData[iIdx + iBuf] == 0)
  982. {
  983. break;
  984. }
  985. }
  986. }
  987. iPos--;
  988. }
  989. //
  990. // Return the number of bytes copied into the output buffer.
  991. //
  992. if(pucBufferOut)
  993. {
  994. ulLen = ((unsigned int)pucBufferOut - (unsigned int)pcData);
  995. //
  996. // Null terminate the string if there is room.
  997. //
  998. if(ulLen < ulSize)
  999. {
  1000. pcData[ulLen] = 0;
  1001. }
  1002. }
  1003. else
  1004. {
  1005. ulLen = 0;
  1006. }
  1007. return(ulLen);
  1008. }
  1009. //*****************************************************************************
  1010. //
  1011. // Close the Doxygen group.
  1012. //! @}
  1013. //
  1014. //*****************************************************************************