<?php

namespace WP_Ultimo\Dependencies\Mpdf;

class TTFontFileAnalysis extends TTFontFile
{
    // Used to get font information from files in directory
    function extractCoreInfo($file, $TTCfontID = 0)
    {
        $this->filename = $file;
        $this->fh = \fopen($file, 'rb');
        if (!$this->fh) {
            throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException('ERROR - Can\'t open file ' . $file);
        }
        $this->_pos = 0;
        $this->charWidths = '';
        $this->glyphPos = [];
        $this->charToGlyph = [];
        $this->tables = [];
        $this->otables = [];
        $this->ascent = 0;
        $this->descent = 0;
        $this->numTTCFonts = 0;
        $this->TTCFonts = [];
        $this->version = $version = $this->read_ulong();
        $this->panose = [];
        // mPDF 5.0
        if ($version == 0x4f54544f) {
            throw new \WP_Ultimo\Dependencies\Mpdf\Exception\FontException(\sprintf('Fonts with postscript outlines are not supported (%s)', $file));
        }
        if ($version == 0x74746366) {
            if ($TTCfontID > 0) {
                $this->version = $version = $this->read_ulong();
                // TTC Header version now
                if (!\in_array($version, [0x10000, 0x20000])) {
                    throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException("ERROR - NOT ADDED as Error parsing TrueType Collection: version=" . $version . " - " . $file);
                }
            } else {
                throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException("ERROR - Error parsing TrueType Collection - " . $file);
            }
            $this->numTTCFonts = $this->read_ulong();
            for ($i = 1; $i <= $this->numTTCFonts; $i++) {
                $this->TTCFonts[$i]['offset'] = $this->read_ulong();
            }
            $this->seek($this->TTCFonts[$TTCfontID]['offset']);
            $this->version = $version = $this->read_ulong();
            // TTFont version again now
            $this->readTableDirectory(\false);
        } else {
            if (!\in_array($version, [0x10000, 0x74727565])) {
                throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException("ERROR - NOT ADDED as Not a TrueType font: version=" . $version . " - " . $file);
            }
            $this->readTableDirectory(\false);
        }
        /* Included for testing...
        		  $cmap_offset = $this->seek_table("cmap");
        		  $this->skip(2);
        		  $cmapTableCount = $this->read_ushort();
        		  $unicode_cmap_offset = 0;
        		  for ($i=0;$i<$cmapTableCount;$i++) {
        		  $x[$i]['platformId'] = $this->read_ushort();
        		  $x[$i]['encodingId'] = $this->read_ushort();
        		  $x[$i]['offset'] = $this->read_ulong();
        		  $save_pos = $this->_pos;
        		  $x[$i]['format'] = $this->get_ushort($cmap_offset + $x[$i]['offset'] );
        		  $this->seek($save_pos );
        		  }
        		  print_r($x); exit;
        		 */
        ///////////////////////////////////
        // name - Naming table
        ///////////////////////////////////
        /* Test purposes - displays table of names
        		  $name_offset = $this->seek_table("name");
        		  $format = $this->read_ushort();
        		  if ($format != 0 && $format != 1)	// mPDF 5.3.73
        		  die("Unknown name table format ".$format);
        		  $numRecords = $this->read_ushort();
        		  $string_data_offset = $name_offset + $this->read_ushort();
        		  for ($i=0;$i<$numRecords; $i++) {
        		  $x[$i]['platformId'] = $this->read_ushort();
        		  $x[$i]['encodingId'] = $this->read_ushort();
        		  $x[$i]['languageId'] = $this->read_ushort();
        		  $x[$i]['nameId'] = $this->read_ushort();
        		  $x[$i]['length'] = $this->read_ushort();
        		  $x[$i]['offset'] = $this->read_ushort();
        
        		  $N = '';
        		  if ($x[$i]['platformId'] == 1 && $x[$i]['encodingId'] == 0 && $x[$i]['languageId'] == 0) { // Roman
        		  $opos = $this->_pos;
        		  $N = $this->get_chunk($string_data_offset + $x[$i]['offset'] , $x[$i]['length'] );
        		  $this->_pos = $opos;
        		  $this->seek($opos);
        		  }
        		  else { 	// Unicode
        		  $opos = $this->_pos;
        		  $this->seek($string_data_offset + $x[$i]['offset'] );
        		  $length = $x[$i]['length'] ;
        		  if ($length % 2 != 0)
        		  $length -= 1;
        		  //		die("PostScript name is UTF-16BE string of odd length");
        		  $length /= 2;
        		  $N = '';
        		  while ($length > 0) {
        		  $char = $this->read_ushort();
        		  $N .= (chr($char));
        		  $length -= 1;
        		  }
        		  $this->_pos = $opos;
        		  $this->seek($opos);
        		  }
        		  $x[$i]['names'][$nameId] = $N;
        		  }
        		  print_r($x); exit;
        		 */
        $name_offset = $this->seek_table("name");
        $format = $this->read_ushort();
        if ($format != 0 && $format != 1) {
            // mPDF 5.3.73
            throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException("ERROR - NOT ADDED as Unknown name table format " . $format . " - " . $file);
        }
        $numRecords = $this->read_ushort();
        $string_data_offset = $name_offset + $this->read_ushort();
        $names = [1 => '', 2 => '', 3 => '', 4 => '', 6 => ''];
        $K = \array_keys($names);
        $nameCount = \count($names);
        for ($i = 0; $i < $numRecords; $i++) {
            $platformId = $this->read_ushort();
            $encodingId = $this->read_ushort();
            $languageId = $this->read_ushort();
            $nameId = $this->read_ushort();
            $length = $this->read_ushort();
            $offset = $this->read_ushort();
            if (!\in_array($nameId, $K)) {
                continue;
            }
            $N = '';
            if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) {
                // Microsoft, Unicode, US English, PS Name
                $opos = $this->_pos;
                $this->seek($string_data_offset + $offset);
                if ($length % 2 != 0) {
                    $length += 1;
                }
                $length /= 2;
                $N = '';
                while ($length > 0) {
                    $char = $this->read_ushort();
                    $N .= \chr($char);
                    $length -= 1;
                }
                $this->_pos = $opos;
                $this->seek($opos);
            } elseif ($platformId == 1 && $encodingId == 0 && $languageId == 0) {
                // Macintosh, Roman, English, PS Name
                $opos = $this->_pos;
                $N = $this->get_chunk($string_data_offset + $offset, $length);
                $this->_pos = $opos;
                $this->seek($opos);
            }
            if ($N && $names[$nameId] == '') {
                $names[$nameId] = $N;
                $nameCount -= 1;
                if ($nameCount == 0) {
                    break;
                }
            }
        }
        if ($names[6]) {
            $psName = \preg_replace('/ /', '-', $names[6]);
        } elseif ($names[4]) {
            $psName = \preg_replace('/ /', '-', $names[4]);
        } elseif ($names[1]) {
            $psName = \preg_replace('/ /', '-', $names[1]);
        } else {
            $psName = '';
        }
        if (!$names[1] && !$psName) {
            throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException("ERROR - NOT ADDED as Could not find valid font name - " . $file);
        }
        $this->name = $psName;
        if ($names[1]) {
            $this->familyName = $names[1];
        } else {
            $this->familyName = $psName;
        }
        if ($names[2]) {
            $this->styleName = $names[2];
        } else {
            $this->styleName = 'Regular';
        }
        ///////////////////////////////////
        // head - Font header table
        ///////////////////////////////////
        $this->seek_table("head");
        $ver_maj = $this->read_ushort();
        $ver_min = $this->read_ushort();
        if ($ver_maj != 1) {
            throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException('ERROR - NOT ADDED as Unknown head table version ' . $ver_maj . '.' . $ver_min . " - " . $file);
        }
        $this->fontRevision = $this->read_ushort() . $this->read_ushort();
        $this->skip(4);
        $magic = $this->read_ulong();
        if ($magic != 0x5f0f3cf5) {
            throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException('ERROR - NOT ADDED as Invalid head table magic ' . $magic . " - " . $file);
        }
        $this->skip(2);
        $this->unitsPerEm = $unitsPerEm = $this->read_ushort();
        $scale = 1000 / $unitsPerEm;
        $this->skip(24);
        $macStyle = $this->read_short();
        $this->skip(4);
        $indexLocFormat = $this->read_short();
        ///////////////////////////////////
        // OS/2 - OS/2 and Windows metrics table
        ///////////////////////////////////
        $sFamily = '';
        $panose = '';
        $fsSelection = '';
        if (isset($this->tables["OS/2"])) {
            $this->seek_table("OS/2");
            $this->skip(30);
            $sF = $this->read_short();
            $sFamily = $sF >> 8;
            $this->_pos += 10;
            //PANOSE = 10 byte length
            $panose = \fread($this->fh, 10);
            $this->panose = [];
            for ($p = 0; $p < \strlen($panose); $p++) {
                $this->panose[] = \ord($panose[$p]);
            }
            $this->skip(20);
            $fsSelection = $this->read_short();
        }
        ///////////////////////////////////
        // post - PostScript table
        ///////////////////////////////////
        $this->seek_table("post");
        $this->skip(4);
        $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0;
        $this->skip(4);
        $isFixedPitch = $this->read_ulong();
        ///////////////////////////////////
        // cmap - Character to glyph index mapping table
        ///////////////////////////////////
        $cmap_offset = $this->seek_table("cmap");
        $this->skip(2);
        $cmapTableCount = $this->read_ushort();
        $unicode_cmap_offset = 0;
        for ($i = 0; $i < $cmapTableCount; $i++) {
            $platformID = $this->read_ushort();
            $encodingID = $this->read_ushort();
            $offset = $this->read_ulong();
            $save_pos = $this->_pos;
            if ($platformID == 3 && $encodingID == 1 || $platformID == 0) {
                // Microsoft, Unicode
                $format = $this->get_ushort($cmap_offset + $offset);
                if ($format == 4) {
                    if (!$unicode_cmap_offset) {
                        $unicode_cmap_offset = $cmap_offset + $offset;
                    }
                }
            } elseif ($platformID == 3 && $encodingID == 10 || $platformID == 0) {
                // Microsoft, Unicode Format 12 table HKCS
                $format = $this->get_ushort($cmap_offset + $offset);
                if ($format == 12) {
                    $unicode_cmap_offset = $cmap_offset + $offset;
                    break;
                }
            }
            $this->seek($save_pos);
        }
        if (!$unicode_cmap_offset) {
            throw new \WP_Ultimo\Dependencies\Mpdf\MpdfException('ERROR - Font (' . $this->filename . ') NOT ADDED as it is not Unicode encoded, and cannot be used by mPDF');
        }
        $rtl = \false;
        $indic = \false;
        $cjk = \false;
        $sip = \false;
        $smp = \false;
        $pua = \false;
        $puaag = \false;
        $glyphToChar = [];
        $unAGlyphs = '';
        // Format 12 CMAP does characters above Unicode BMP i.e. some HKCS characters U+20000 and above
        if ($format == 12) {
            $this->seek($unicode_cmap_offset + 4);
            $length = $this->read_ulong();
            $limit = $unicode_cmap_offset + $length;
            $this->skip(4);
            $nGroups = $this->read_ulong();
            for ($i = 0; $i < $nGroups; $i++) {
                $startCharCode = $this->read_ulong();
                $endCharCode = $this->read_ulong();
                $startGlyphCode = $this->read_ulong();
                if ($endCharCode > 0x20000 && $endCharCode < 0x2a6df || $endCharCode > 0x2f800 && $endCharCode < 0x2fa1f) {
                    $sip = \true;
                }
                if ($endCharCode > 0x10000 && $endCharCode < 0x1ffff) {
                    $smp = \true;
                }
                if ($endCharCode > 0x590 && $endCharCode < 0x77f || $endCharCode > 0xfe70 && $endCharCode < 0xfeff || $endCharCode > 0xfb50 && $endCharCode < 0xfdff) {
                    $rtl = \true;
                }
                if ($endCharCode > 0x900 && $endCharCode < 0xdff) {
                    $indic = \true;
                }
                if ($endCharCode > 0xe000 && $endCharCode < 0xf8ff) {
                    $pua = \true;
                    if ($endCharCode > 0xf500 && $endCharCode < 0xf7ff) {
                        $puaag = \true;
                    }
                }
                if ($endCharCode > 0x2e80 && $endCharCode < 0x4dc0 || $endCharCode > 0x4e00 && $endCharCode < 0xa4cf || $endCharCode > 0xac00 && $endCharCode < 0xd7af || $endCharCode > 0xf900 && $endCharCode < 0xfaff || $endCharCode > 0xfe30 && $endCharCode < 0xfe4f) {
                    $cjk = \true;
                }
                $offset = 0;
                // Get each glyphToChar - only point if going to analyse un-mapped Arabic Glyphs
                if (isset($this->tables['post'])) {
                    for ($unichar = $startCharCode; $unichar <= $endCharCode; $unichar++) {
                        $glyph = $startGlyphCode + $offset;
                        $offset++;
                        $glyphToChar[$glyph][] = $unichar;
                    }
                }
            }
        } else {
            // Format 4 CMap
            $this->seek($unicode_cmap_offset + 2);
            $length = $this->read_ushort();
            $limit = $unicode_cmap_offset + $length;
            $this->skip(2);
            $segCount = $this->read_ushort() / 2;
            $this->skip(6);
            $endCount = [];
            for ($i = 0; $i < $segCount; $i++) {
                $endCount[] = $this->read_ushort();
            }
            $this->skip(2);
            $startCount = [];
            for ($i = 0; $i < $segCount; $i++) {
                $startCount[] = $this->read_ushort();
            }
            $idDelta = [];
            for ($i = 0; $i < $segCount; $i++) {
                $idDelta[] = $this->read_short();
            }
            $idRangeOffset_start = $this->_pos;
            $idRangeOffset = [];
            for ($i = 0; $i < $segCount; $i++) {
                $idRangeOffset[] = $this->read_ushort();
            }
            for ($n = 0; $n < $segCount; $n++) {
                if ($endCount[$n] > 0x590 && $endCount[$n] < 0x77f || $endCount[$n] > 0xfe70 && $endCount[$n] < 0xfeff || $endCount[$n] > 0xfb50 && $endCount[$n] < 0xfdff) {
                    $rtl = \true;
                }
                if ($endCount[$n] > 0x900 && $endCount[$n] < 0xdff) {
                    $indic = \true;
                }
                if ($endCount[$n] > 0x2e80 && $endCount[$n] < 0x4dc0 || $endCount[$n] > 0x4e00 && $endCount[$n] < 0xa4cf || $endCount[$n] > 0xac00 && $endCount[$n] < 0xd7af || $endCount[$n] > 0xf900 && $endCount[$n] < 0xfaff || $endCount[$n] > 0xfe30 && $endCount[$n] < 0xfe4f) {
                    $cjk = \true;
                }
                if ($endCount[$n] > 0xe000 && $endCount[$n] < 0xf8ff) {
                    $pua = \true;
                    if ($endCount[$n] > 0xf500 && $endCount[$n] < 0xf7ff) {
                        $puaag = \true;
                    }
                }
                // Get each glyphToChar - only point if going to analyse un-mapped Arabic Glyphs
                if (isset($this->tables['post'])) {
                    $endpoint = $endCount[$n] + 1;
                    for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) {
                        if ($idRangeOffset[$n] == 0) {
                            $glyph = $unichar + $idDelta[$n] & 0xffff;
                        } else {
                            $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n];
                            $offset = $idRangeOffset_start + 2 * $n + $offset;
                            if ($offset >= $limit) {
                                $glyph = 0;
                            } else {
                                $glyph = $this->get_ushort($offset);
                                if ($glyph != 0) {
                                    $glyph = $glyph + $idDelta[$n] & 0xffff;
                                }
                            }
                        }
                        $glyphToChar[$glyph][] = $unichar;
                    }
                }
            }
        }
        $bold = \false;
        $italic = \false;
        $ftype = '';
        if ($macStyle & 1 << 0) {
            $bold = \true;
        } elseif ($fsSelection & 1 << 5) {
            $bold = \true;
        }
        // 5 	BOLD 	Characters are emboldened
        if ($macStyle & 1 << 1) {
            $italic = \true;
        } elseif ($fsSelection & 1 << 0) {
            $italic = \true;
        } elseif ($this->italicAngle != 0) {
            $italic = \true;
        }
        if ($isFixedPitch) {
            $ftype = 'mono';
        } elseif ($sFamily > 0 && $sFamily < 8) {
            $ftype = 'serif';
        } elseif ($sFamily == 8) {
            $ftype = 'sans';
        } elseif ($sFamily == 10) {
            $ftype = 'cursive';
        }
        // Use PANOSE
        if ($panose) {
            $bFamilyType = \ord($panose[0]);
            if ($bFamilyType == 2) {
                $bSerifStyle = \ord($panose[1]);
                if (!$ftype) {
                    if ($bSerifStyle > 1 && $bSerifStyle < 11) {
                        $ftype = 'serif';
                    } elseif ($bSerifStyle > 10) {
                        $ftype = 'sans';
                    }
                }
                $bProportion = \ord($panose[3]);
                if ($bProportion == 9 || $bProportion == 1) {
                    $ftype = 'mono';
                }
                // ==1 i.e. No Fit needed for OCR-a and -b
            } elseif ($bFamilyType == 3) {
                $ftype = 'cursive';
            }
        }
        \fclose($this->fh);
        return [$this->familyName, $bold, $italic, $ftype, $TTCfontID, $rtl, $indic, $cjk, $sip, $smp, $puaag, $pua, $unAGlyphs];
    }
}
