QREps.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Class QREps
  4. *
  5. * @created 09.05.2022
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2022 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Output;
  11. use function array_values, count, date, implode, is_array, is_numeric, max, min, round, sprintf;
  12. /**
  13. * Encapsulated Postscript (EPS) output
  14. *
  15. * @see https://github.com/t0k4rt/phpqrcode/blob/bb29e6eb77e0a2a85bb0eb62725e0adc11ff5a90/qrvect.php#L52-L137
  16. * @see https://web.archive.org/web/20170818010030/http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/postscript/pdfs/5002.EPSF_Spec.pdf
  17. * @see https://web.archive.org/web/20210419003859/https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf
  18. * @see https://github.com/chillerlan/php-qrcode/discussions/148
  19. */
  20. class QREps extends QROutputAbstract{
  21. public const MIME_TYPE = 'application/postscript';
  22. /**
  23. * @inheritDoc
  24. */
  25. public static function moduleValueIsValid($value):bool{
  26. if(!is_array($value) || count($value) < 3){
  27. return false;
  28. }
  29. // check the first values of the array
  30. foreach(array_values($value) as $i => $val){
  31. if($i > 3){
  32. break;
  33. }
  34. if(!is_numeric($val)){
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. /**
  41. * @param array $value
  42. *
  43. * @inheritDoc
  44. */
  45. protected function prepareModuleValue($value):string{
  46. $values = [];
  47. foreach(array_values($value) as $i => $val){
  48. if($i > 3){
  49. break;
  50. }
  51. // clamp value and convert from int 0-255 to float 0-1 RGB/CMYK range
  52. $values[] = round((max(0, min(255, intval($val))) / 255), 6);
  53. }
  54. return $this->formatColor($values);
  55. }
  56. /**
  57. * @inheritDoc
  58. */
  59. protected function getDefaultModuleValue(bool $isDark):string{
  60. return $this->formatColor(($isDark) ? [0.0, 0.0, 0.0] : [1.0, 1.0, 1.0]);
  61. }
  62. /**
  63. * Set the color format string
  64. *
  65. * 4 values in the color array will be interpreted as CMYK, 3 as RGB
  66. *
  67. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  68. */
  69. protected function formatColor(array $values):string{
  70. $count = count($values);
  71. if($count < 3){
  72. throw new QRCodeOutputException('invalid color value');
  73. }
  74. $format = ($count === 4)
  75. // CMYK
  76. ? '%f %f %f %f C'
  77. // RGB
  78. :'%f %f %f R';
  79. return sprintf($format, ...$values);
  80. }
  81. /**
  82. * @inheritDoc
  83. */
  84. public function dump(string $file = null):string{
  85. [$width, $height] = $this->getOutputDimensions();
  86. $eps = [
  87. // main header
  88. '%!PS-Adobe-3.0 EPSF-3.0',
  89. '%%Creator: php-qrcode (https://github.com/chillerlan/php-qrcode)',
  90. '%%Title: QR Code',
  91. sprintf('%%%%CreationDate: %1$s', date('c')),
  92. '%%DocumentData: Clean7Bit',
  93. '%%LanguageLevel: 3',
  94. sprintf('%%%%BoundingBox: 0 0 %s %s', $width, $height),
  95. '%%EndComments',
  96. // function definitions
  97. '%%BeginProlog',
  98. '/F { rectfill } def',
  99. '/R { setrgbcolor } def',
  100. '/C { setcmykcolor } def',
  101. '%%EndProlog',
  102. ];
  103. if($this::moduleValueIsValid($this->options->bgColor)){
  104. $eps[] = $this->prepareModuleValue($this->options->bgColor);
  105. $eps[] = sprintf('0 0 %s %s F', $width, $height);
  106. }
  107. // create the path elements
  108. $paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE):string => $this->module($x, $y, $M_TYPE));
  109. foreach($paths as $M_TYPE => $path){
  110. if(empty($path)){
  111. continue;
  112. }
  113. $eps[] = $this->getModuleValue($M_TYPE);
  114. $eps[] = implode("\n", $path);
  115. }
  116. // end file
  117. $eps[] = '%%EOF';
  118. $data = implode("\n", $eps);
  119. $this->saveToFile($data, $file);
  120. return $data;
  121. }
  122. /**
  123. * Returns a path segment for a single module
  124. */
  125. protected function module(int $x, int $y, int $M_TYPE):string{
  126. if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
  127. return '';
  128. }
  129. $outputX = ($x * $this->scale);
  130. // Actual size - one block = Topmost y pos.
  131. $top = ($this->length - $this->scale);
  132. // Apparently y-axis is inverted (y0 is at bottom and not top) in EPS, so we have to switch the y-axis here
  133. $outputY = ($top - ($y * $this->scale));
  134. return sprintf('%d %d %d %d F', $outputX, $outputY, $this->scale, $this->scale);
  135. }
  136. }