wer($subtypeRaw); $families = $this->getFontFamilies(); if ($familyRaw) { $family = str_replace(["'", '"'], "", strtolower($familyRaw)); if (isset($families[$family][$subtype])) { return $cache[$familyRaw][$subtypeRaw] = $families[$family][$subtype]; } return null; } $fallback_families = [strtolower($this->options->getDefaultFont()), "serif"]; foreach ($fallback_families as $family) { if (isset($families[$family][$subtype])) { return $cache[$familyRaw][$subtypeRaw] = $families[$family][$subtype]; } if (!isset($families[$family])) { continue; } $family = $families[$family]; foreach ($family as $sub => $font) { if (strpos($subtype, $sub) !== false) { return $cache[$familyRaw][$subtypeRaw] = $font; } } if ($subtype !== "normal") { foreach ($family as $sub => $font) { if ($sub !== "normal") { return $cache[$familyRaw][$subtypeRaw] = $font; } } } $subtype = "normal"; if (isset($family[$subtype])) { return $cache[$familyRaw][$subtypeRaw] = $family[$subtype]; } } return null; } /** * @param $family * @return null|string * @deprecated */ public function get_family($family) { return $this->getFamily($family); } /** * @param string $family * @return null|string */ public function getFamily($family) { $family = str_replace(["'", '"'], "", mb_strtolower($family)); $families = $this->getFontFamilies(); if (isset($families[$family])) { return $families[$family]; } return null; } /** * @param $type * @return string * @deprecated */ public function get_type($type) { return $this->getType($type); } /** * @param string $type * @return string */ public function getType($type) { if (preg_match('/bold/i', $type)) { $weight = 700; } elseif (preg_match('/([1-9]00)/', $type, $match)) { $weight = (int)$match[0]; } else { $weight = 400; } $weight = $weight === 400 ? 'normal' : $weight; $weight = $weight === 700 ? 'bold' : $weight; $style = preg_match('/italic|oblique/i', $type) ? 'italic' : null; if ($weight === 'normal' && $style !== null) { return $style; } return $style === null ? $weight : $weight.'_'.$style; } /** * @return array * @deprecated */ public function get_font_families() { return $this->getFontFamilies(); } /** * Returns the current font lookup table * * @return array */ public function getFontFamilies() { if (!isset($this->fontFamilies)) { $this->setFontFamilies(); } return $this->fontFamilies; } /** * Convert loaded fonts to font lookup table * * @return array */ public function setFontFamilies() { $fontFamilies = []; if (isset($this->bundledFonts) && is_array($this->bundledFonts)) { foreach ($this->bundledFonts as $family => $variants) { if (!isset($fontFamilies[$family])) { $fontFamilies[$family] = array_map(function ($variant) { return $this->getOptions()->getRootDir() . '/lib/fonts/' . $variant; }, $variants); } } } if (isset($this->userFonts) && is_array($this->userFonts)) { foreach ($this->userFonts as $family => $variants) { $fontFamilies[$family] = array_map(function ($variant) { $variantName = basename($variant); if ($variantName === $variant) { return $this->getOptions()->getFontDir() . '/' . $variant; } return $variant; }, $variants); } } $this->fontFamilies = $fontFamilies; } /** * @param string $fontname * @param mixed $entry * @deprecated */ public function set_font_family($fontname, $entry) { $this->setFontFamily($fontname, $entry); } /** * @param string $fontname * @param mixed $entry */ public function setFontFamily($fontname, $entry) { $this->userFonts[mb_strtolower($fontname)] = $entry; $this->saveFontFamilies(); unset($this->fontFamilies); } /** * @return string */ public function getUserFontsFilePath() { return $this->options->getFontDir() . '/' . self::USER_FONTS_FILE; } /** * @param Options $options * @return $this */ public function setOptions(Options $options) { $this->options = $options; unset($this->fontFamilies); return $this; } /** * @return Options */ public function getOptions() { return $this->options; } /** * @param Canvas $canvas * @return $this */ public function setCanvas(Canvas $canvas) { $this->canvas = $canvas; return $this; } /** * @return Canvas */ public function getCanvas() { return $this->canvas; } }