123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- <?php
- class PHPExcel_Shared_Font
- {
-
- const AUTOSIZE_METHOD_APPROX = 'approx';
- const AUTOSIZE_METHOD_EXACT = 'exact';
- private static $autoSizeMethods = array(
- self::AUTOSIZE_METHOD_APPROX,
- self::AUTOSIZE_METHOD_EXACT,
- );
-
- const CHARSET_ANSI_LATIN = 0x00;
- const CHARSET_SYSTEM_DEFAULT = 0x01;
- const CHARSET_SYMBOL = 0x02;
- const CHARSET_APPLE_ROMAN = 0x4D;
- const CHARSET_ANSI_JAPANESE_SHIFTJIS = 0x80;
- const CHARSET_ANSI_KOREAN_HANGUL = 0x81;
- const CHARSET_ANSI_KOREAN_JOHAB = 0x82;
- const CHARSET_ANSI_CHINESE_SIMIPLIFIED = 0x86;
- const CHARSET_ANSI_CHINESE_TRADITIONAL = 0x88;
- const CHARSET_ANSI_GREEK = 0xA1;
- const CHARSET_ANSI_TURKISH = 0xA2;
- const CHARSET_ANSI_VIETNAMESE = 0xA3;
- const CHARSET_ANSI_HEBREW = 0xB1;
- const CHARSET_ANSI_ARABIC = 0xB2;
- const CHARSET_ANSI_BALTIC = 0xBA;
- const CHARSET_ANSI_CYRILLIC = 0xCC;
- const CHARSET_ANSI_THAI = 0xDD;
- const CHARSET_ANSI_LATIN_II = 0xEE;
- const CHARSET_OEM_LATIN_I = 0xFF;
-
-
- const ARIAL = 'arial.ttf';
- const ARIAL_BOLD = 'arialbd.ttf';
- const ARIAL_ITALIC = 'ariali.ttf';
- const ARIAL_BOLD_ITALIC = 'arialbi.ttf';
- const CALIBRI = 'CALIBRI.TTF';
- const CALIBRI_BOLD = 'CALIBRIB.TTF';
- const CALIBRI_ITALIC = 'CALIBRII.TTF';
- const CALIBRI_BOLD_ITALIC = 'CALIBRIZ.TTF';
- const COMIC_SANS_MS = 'comic.ttf';
- const COMIC_SANS_MS_BOLD = 'comicbd.ttf';
- const COURIER_NEW = 'cour.ttf';
- const COURIER_NEW_BOLD = 'courbd.ttf';
- const COURIER_NEW_ITALIC = 'couri.ttf';
- const COURIER_NEW_BOLD_ITALIC = 'courbi.ttf';
- const GEORGIA = 'georgia.ttf';
- const GEORGIA_BOLD = 'georgiab.ttf';
- const GEORGIA_ITALIC = 'georgiai.ttf';
- const GEORGIA_BOLD_ITALIC = 'georgiaz.ttf';
- const IMPACT = 'impact.ttf';
- const LIBERATION_SANS = 'LiberationSans-Regular.ttf';
- const LIBERATION_SANS_BOLD = 'LiberationSans-Bold.ttf';
- const LIBERATION_SANS_ITALIC = 'LiberationSans-Italic.ttf';
- const LIBERATION_SANS_BOLD_ITALIC = 'LiberationSans-BoldItalic.ttf';
- const LUCIDA_CONSOLE = 'lucon.ttf';
- const LUCIDA_SANS_UNICODE = 'l_10646.ttf';
- const MICROSOFT_SANS_SERIF = 'micross.ttf';
- const PALATINO_LINOTYPE = 'pala.ttf';
- const PALATINO_LINOTYPE_BOLD = 'palab.ttf';
- const PALATINO_LINOTYPE_ITALIC = 'palai.ttf';
- const PALATINO_LINOTYPE_BOLD_ITALIC = 'palabi.ttf';
- const SYMBOL = 'symbol.ttf';
- const TAHOMA = 'tahoma.ttf';
- const TAHOMA_BOLD = 'tahomabd.ttf';
- const TIMES_NEW_ROMAN = 'times.ttf';
- const TIMES_NEW_ROMAN_BOLD = 'timesbd.ttf';
- const TIMES_NEW_ROMAN_ITALIC = 'timesi.ttf';
- const TIMES_NEW_ROMAN_BOLD_ITALIC = 'timesbi.ttf';
- const TREBUCHET_MS = 'trebuc.ttf';
- const TREBUCHET_MS_BOLD = 'trebucbd.ttf';
- const TREBUCHET_MS_ITALIC = 'trebucit.ttf';
- const TREBUCHET_MS_BOLD_ITALIC = 'trebucbi.ttf';
- const VERDANA = 'verdana.ttf';
- const VERDANA_BOLD = 'verdanab.ttf';
- const VERDANA_ITALIC = 'verdanai.ttf';
- const VERDANA_BOLD_ITALIC = 'verdanaz.ttf';
-
- private static $autoSizeMethod = self::AUTOSIZE_METHOD_APPROX;
-
- private static $trueTypeFontPath = null;
-
- public static $defaultColumnWidths = array(
- 'Arial' => array(
- 1 => array('px' => 24, 'width' => 12.00000000),
- 2 => array('px' => 24, 'width' => 12.00000000),
- 3 => array('px' => 32, 'width' => 10.66406250),
- 4 => array('px' => 32, 'width' => 10.66406250),
- 5 => array('px' => 40, 'width' => 10.00000000),
- 6 => array('px' => 48, 'width' => 9.59765625),
- 7 => array('px' => 48, 'width' => 9.59765625),
- 8 => array('px' => 56, 'width' => 9.33203125),
- 9 => array('px' => 64, 'width' => 9.14062500),
- 10 => array('px' => 64, 'width' => 9.14062500),
- ),
- 'Calibri' => array(
- 1 => array('px' => 24, 'width' => 12.00000000),
- 2 => array('px' => 24, 'width' => 12.00000000),
- 3 => array('px' => 32, 'width' => 10.66406250),
- 4 => array('px' => 32, 'width' => 10.66406250),
- 5 => array('px' => 40, 'width' => 10.00000000),
- 6 => array('px' => 48, 'width' => 9.59765625),
- 7 => array('px' => 48, 'width' => 9.59765625),
- 8 => array('px' => 56, 'width' => 9.33203125),
- 9 => array('px' => 56, 'width' => 9.33203125),
- 10 => array('px' => 64, 'width' => 9.14062500),
- 11 => array('px' => 64, 'width' => 9.14062500),
- ),
- 'Verdana' => array(
- 1 => array('px' => 24, 'width' => 12.00000000),
- 2 => array('px' => 24, 'width' => 12.00000000),
- 3 => array('px' => 32, 'width' => 10.66406250),
- 4 => array('px' => 32, 'width' => 10.66406250),
- 5 => array('px' => 40, 'width' => 10.00000000),
- 6 => array('px' => 48, 'width' => 9.59765625),
- 7 => array('px' => 48, 'width' => 9.59765625),
- 8 => array('px' => 64, 'width' => 9.14062500),
- 9 => array('px' => 72, 'width' => 9.00000000),
- 10 => array('px' => 72, 'width' => 9.00000000),
- ),
- );
-
- public static function setAutoSizeMethod($pValue = self::AUTOSIZE_METHOD_APPROX)
- {
- if (!in_array($pValue, self::$autoSizeMethods)) {
- return false;
- }
- self::$autoSizeMethod = $pValue;
- return true;
- }
-
- public static function getAutoSizeMethod()
- {
- return self::$autoSizeMethod;
- }
-
- public static function setTrueTypeFontPath($pValue = '')
- {
- self::$trueTypeFontPath = $pValue;
- }
-
- public static function getTrueTypeFontPath()
- {
- return self::$trueTypeFontPath;
- }
-
- public static function calculateColumnWidth(PHPExcel_Style_Font $font, $cellText = '', $rotation = 0, PHPExcel_Style_Font $defaultFont = null)
- {
-
- if ($cellText instanceof PHPExcel_RichText) {
- $cellText = $cellText->getPlainText();
- }
-
- if (strpos($cellText, "\n") !== false) {
- $lineTexts = explode("\n", $cellText);
- $lineWidths = array();
- foreach ($lineTexts as $lineText) {
- $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont);
- }
- return max($lineWidths);
- }
-
- $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
- if (!$approximate) {
- $columnWidthAdjust = ceil(self::getTextWidthPixelsExact('n', $font, 0) * 1.07);
- try {
-
-
- $columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + $columnWidthAdjust;
- } catch (PHPExcel_Exception $e) {
- $approximate = true;
- }
- }
- if ($approximate) {
- $columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0);
-
-
- $columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + $columnWidthAdjust;
- }
-
- $columnWidth = PHPExcel_Shared_Drawing::pixelsToCellDimension($columnWidth, $defaultFont);
-
- return round($columnWidth, 6);
- }
-
- public static function getTextWidthPixelsExact($text, PHPExcel_Style_Font $font, $rotation = 0)
- {
- if (!function_exists('imagettfbbox')) {
- throw new PHPExcel_Exception('GD library needs to be enabled');
- }
-
-
- $fontFile = self::getTrueTypeFontFileFromFont($font);
- $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text);
-
- $lowerLeftCornerX = $textBox[0];
- $lowerRightCornerX = $textBox[2];
- $upperRightCornerX = $textBox[4];
- $upperLeftCornerX = $textBox[6];
-
- $textWidth = max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
- return $textWidth;
- }
-
- public static function getTextWidthPixelsApprox($columnText, PHPExcel_Style_Font $font = null, $rotation = 0)
- {
- $fontName = $font->getName();
- $fontSize = $font->getSize();
-
- switch ($fontName) {
- case 'Calibri':
-
- $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
- $columnWidth = $columnWidth * $fontSize / 11;
- break;
- case 'Arial':
-
-
- $columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText));
- $columnWidth = $columnWidth * $fontSize / 10;
- break;
- case 'Verdana':
-
- $columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText));
- $columnWidth = $columnWidth * $fontSize / 10;
- break;
- default:
-
- $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
- $columnWidth = $columnWidth * $fontSize / 11;
- break;
- }
-
- if ($rotation !== 0) {
- if ($rotation == -165) {
-
- $columnWidth = 4;
- } else {
-
- $columnWidth = $columnWidth * cos(deg2rad($rotation))
- + $fontSize * abs(sin(deg2rad($rotation))) / 5;
- }
- }
-
- return (int) $columnWidth;
- }
-
- public static function fontSizeToPixels($fontSizeInPoints = 11)
- {
- return (int) ((4 / 3) * $fontSizeInPoints);
- }
-
- public static function inchSizeToPixels($sizeInInch = 1)
- {
- return ($sizeInInch * 96);
- }
-
- public static function centimeterSizeToPixels($sizeInCm = 1)
- {
- return ($sizeInCm * 37.795275591);
- }
-
- public static function getTrueTypeFontFileFromFont($font)
- {
- if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) {
- throw new PHPExcel_Exception('Valid directory to TrueType Font files not specified');
- }
- $name = $font->getName();
- $bold = $font->getBold();
- $italic = $font->getItalic();
-
- switch ($name) {
- case 'Arial':
- $fontFile = (
- $bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD)
- : ($italic ? self::ARIAL_ITALIC : self::ARIAL)
- );
- break;
- case 'Calibri':
- $fontFile = (
- $bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD)
- : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI)
- );
- break;
- case 'Courier New':
- $fontFile = (
- $bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD)
- : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW)
- );
- break;
- case 'Comic Sans MS':
- $fontFile = (
- $bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS
- );
- break;
- case 'Georgia':
- $fontFile = (
- $bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD)
- : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA)
- );
- break;
- case 'Impact':
- $fontFile = self::IMPACT;
- break;
- case 'Liberation Sans':
- $fontFile = (
- $bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD)
- : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS)
- );
- break;
- case 'Lucida Console':
- $fontFile = self::LUCIDA_CONSOLE;
- break;
- case 'Lucida Sans Unicode':
- $fontFile = self::LUCIDA_SANS_UNICODE;
- break;
- case 'Microsoft Sans Serif':
- $fontFile = self::MICROSOFT_SANS_SERIF;
- break;
- case 'Palatino Linotype':
- $fontFile = (
- $bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD)
- : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE)
- );
- break;
- case 'Symbol':
- $fontFile = self::SYMBOL;
- break;
- case 'Tahoma':
- $fontFile = (
- $bold ? self::TAHOMA_BOLD : self::TAHOMA
- );
- break;
- case 'Times New Roman':
- $fontFile = (
- $bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD)
- : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN)
- );
- break;
- case 'Trebuchet MS':
- $fontFile = (
- $bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD)
- : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS)
- );
- break;
- case 'Verdana':
- $fontFile = (
- $bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD)
- : ($italic ? self::VERDANA_ITALIC : self::VERDANA)
- );
- break;
- default:
- throw new PHPExcel_Exception('Unknown font name "'. $name .'". Cannot map to TrueType font file');
- break;
- }
- $fontFile = self::$trueTypeFontPath . $fontFile;
-
- if (!file_exists($fontFile)) {
- throw new PHPExcel_Exception('TrueType Font file not found');
- }
- return $fontFile;
- }
-
- public static function getCharsetFromFontName($name)
- {
- switch ($name) {
-
- case 'EucrosiaUPC':
- return self::CHARSET_ANSI_THAI;
- case 'Wingdings':
- return self::CHARSET_SYMBOL;
- case 'Wingdings 2':
- return self::CHARSET_SYMBOL;
- case 'Wingdings 3':
- return self::CHARSET_SYMBOL;
- default:
- return self::CHARSET_ANSI_LATIN;
- }
- }
-
- public static function getDefaultColumnWidthByFont(PHPExcel_Style_Font $font, $pPixels = false)
- {
- if (isset(self::$defaultColumnWidths[$font->getName()][$font->getSize()])) {
-
- $columnWidth = $pPixels ?
- self::$defaultColumnWidths[$font->getName()][$font->getSize()]['px']
- : self::$defaultColumnWidths[$font->getName()][$font->getSize()]['width'];
- } else {
-
-
- $columnWidth = $pPixels ?
- self::$defaultColumnWidths['Calibri'][11]['px']
- : self::$defaultColumnWidths['Calibri'][11]['width'];
- $columnWidth = $columnWidth * $font->getSize() / 11;
-
- if ($pPixels) {
- $columnWidth = (int) round($columnWidth);
- }
- }
- return $columnWidth;
- }
-
- public static function getDefaultRowHeightByFont(PHPExcel_Style_Font $font)
- {
- switch ($font->getName()) {
- case 'Arial':
- switch ($font->getSize()) {
- case 10:
-
- $rowHeight = 12.75;
- break;
- case 9:
-
- $rowHeight = 12;
- break;
- case 8:
-
- $rowHeight = 11.25;
- break;
- case 7:
-
- $rowHeight = 9;
- break;
- case 6:
- case 5:
-
- $rowHeight = 8.25;
- break;
- case 4:
-
- $rowHeight = 6.75;
- break;
- case 3:
-
- $rowHeight = 6;
- break;
- case 2:
- case 1:
-
- $rowHeight = 5.25;
- break;
- default:
-
- $rowHeight = 12.75 * $font->getSize() / 10;
- break;
- }
- break;
- case 'Calibri':
- switch ($font->getSize()) {
- case 11:
-
- $rowHeight = 15;
- break;
- case 10:
-
- $rowHeight = 12.75;
- break;
- case 9:
-
- $rowHeight = 12;
- break;
- case 8:
-
- $rowHeight = 11.25;
- break;
- case 7:
-
- $rowHeight = 9;
- break;
- case 6:
- case 5:
-
- $rowHeight = 8.25;
- break;
- case 4:
-
- $rowHeight = 6.75;
- break;
- case 3:
-
- $rowHeight = 6.00;
- break;
- case 2:
- case 1:
-
- $rowHeight = 5.25;
- break;
- default:
-
- $rowHeight = 15 * $font->getSize() / 11;
- break;
- }
- break;
- case 'Verdana':
- switch ($font->getSize()) {
- case 10:
-
- $rowHeight = 12.75;
- break;
- case 9:
-
- $rowHeight = 11.25;
- break;
- case 8:
-
- $rowHeight = 10.50;
- break;
- case 7:
-
- $rowHeight = 9.00;
- break;
- case 6:
- case 5:
-
- $rowHeight = 8.25;
- break;
- case 4:
-
- $rowHeight = 6.75;
- break;
- case 3:
-
- $rowHeight = 6;
- break;
- case 2:
- case 1:
-
- $rowHeight = 5.25;
- break;
- default:
-
- $rowHeight = 12.75 * $font->getSize() / 10;
- break;
- }
- break;
- default:
-
- $rowHeight = 15 * $font->getSize() / 11;
- break;
- }
- return $rowHeight;
- }
- }
|