Source: Data/Byte.js

  1. /**
  2. * @created 11.07.2022
  3. * @author smiley <smiley@chillerlan.net>
  4. * @copyright 2022 smiley
  5. * @license MIT
  6. */
  7. import QRDataModeAbstract from './QRDataModeAbstract.js';
  8. import PHPJS from '../Common/PHPJS.js';
  9. import {MODE_BYTE} from '../Common/constants.js';
  10. /**
  11. * Byte mode, ISO-8859-1 or UTF-8
  12. *
  13. * ISO/IEC 18004:2000 Section 8.3.4
  14. * ISO/IEC 18004:2000 Section 8.4.4
  15. */
  16. export default class Byte extends QRDataModeAbstract{
  17. /**
  18. * @inheritDoc
  19. */
  20. datamode = MODE_BYTE;
  21. /**
  22. * @inheritDoc
  23. */
  24. getLengthInBits(){
  25. return this.getCharCount() * 8;
  26. }
  27. /**
  28. * @inheritDoc
  29. */
  30. static validateString($string){
  31. return typeof $string === 'string' && !!$string.length;
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. write($bitBuffer, $versionNumber){
  37. let $len = this.getCharCount();
  38. let $data = this.data.split('');
  39. $bitBuffer
  40. .put(this.datamode, 4)
  41. .put($len, this.getLengthBitsForVersion($versionNumber))
  42. ;
  43. let $i = 0;
  44. while($i < $len){
  45. $bitBuffer.put(PHPJS.ord($data[$i]), 8);
  46. $i++;
  47. }
  48. }
  49. }