QREps.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. final public const MIME_TYPE = 'application/postscript';
  22. /**
  23. * @inheritDoc
  24. */
  25. public static function moduleValueIsValid(mixed $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. * @inheritDoc
  42. */
  43. protected function prepareModuleValue(mixed $value):string{
  44. $values = [];
  45. foreach(array_values($value) as $i => $val){
  46. if($i > 3){
  47. break;
  48. }
  49. // clamp value and convert from int 0-255 to float 0-1 RGB/CMYK range
  50. $values[] = round((max(0, min(255, intval($val))) / 255), 6);
  51. }
  52. return $this->formatColor($values);
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. protected function getDefaultModuleValue(bool $isDark):string{
  58. return $this->formatColor(($isDark) ? [0.0, 0.0, 0.0] : [1.0, 1.0, 1.0]);
  59. }
  60. /**
  61. * Set the color format string
  62. *
  63. * 4 values in the color array will be interpreted as CMYK, 3 as RGB
  64. *
  65. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  66. */
  67. protected function formatColor(array $values):string{
  68. $count = count($values);
  69. if($count < 3){
  70. throw new QRCodeOutputException('invalid color value');
  71. }
  72. $format = ($count === 4)
  73. // CMYK
  74. ? '%f %f %f %f C'
  75. // RGB
  76. :'%f %f %f R';
  77. return sprintf($format, ...$values);
  78. }
  79. /**
  80. * @inheritDoc
  81. */
  82. public function dump(string|null $file = null):string{
  83. [$width, $height] = $this->getOutputDimensions();
  84. $eps = [
  85. // main header
  86. '%!PS-Adobe-3.0 EPSF-3.0',
  87. '%%Creator: php-qrcode (https://github.com/chillerlan/php-qrcode)',
  88. '%%Title: QR Code',
  89. sprintf('%%%%CreationDate: %1$s', date('c')),
  90. '%%DocumentData: Clean7Bit',
  91. '%%LanguageLevel: 3',
  92. sprintf('%%%%BoundingBox: 0 0 %s %s', $width, $height),
  93. '%%EndComments',
  94. // function definitions
  95. '%%BeginProlog',
  96. '/F { rectfill } def',
  97. '/R { setrgbcolor } def',
  98. '/C { setcmykcolor } def',
  99. '%%EndProlog',
  100. ];
  101. if($this::moduleValueIsValid($this->options->bgColor)){
  102. $eps[] = $this->prepareModuleValue($this->options->bgColor);
  103. $eps[] = sprintf('0 0 %s %s F', $width, $height);
  104. }
  105. // create the path elements
  106. $paths = $this->collectModules($this->module(...));
  107. foreach($paths as $M_TYPE => $path){
  108. if(empty($path)){
  109. continue;
  110. }
  111. $eps[] = $this->getModuleValue($M_TYPE);
  112. $eps[] = implode("\n", $path);
  113. }
  114. // end file
  115. $eps[] = '%%EOF';
  116. $data = implode("\n", $eps);
  117. $this->saveToFile($data, $file);
  118. return $data;
  119. }
  120. /**
  121. * Returns a path segment for a single module
  122. */
  123. protected function module(int $x, int $y, int $M_TYPE):string{
  124. if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
  125. return '';
  126. }
  127. $outputX = ($x * $this->scale);
  128. // Actual size - one block = Topmost y pos.
  129. $top = ($this->length - $this->scale);
  130. // Apparently y-axis is inverted (y0 is at bottom and not top) in EPS, so we have to switch the y-axis here
  131. $outputY = ($top - ($y * $this->scale));
  132. return sprintf('%d %d %d %d F', $outputX, $outputY, $this->scale, $this->scale);
  133. }
  134. }