OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 #ifdef SK_BUILD_FOR_MAC | 10 #ifdef SK_BUILD_FOR_MAC |
(...skipping 1805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1816 | 1816 |
1817 desc->setFamilyName(get_str(CTFontCopyFamilyName(fFontRef), &tmpStr)); | 1817 desc->setFamilyName(get_str(CTFontCopyFamilyName(fFontRef), &tmpStr)); |
1818 desc->setFullName(get_str(CTFontCopyFullName(fFontRef), &tmpStr)); | 1818 desc->setFullName(get_str(CTFontCopyFullName(fFontRef), &tmpStr)); |
1819 desc->setPostscriptName(get_str(CTFontCopyPostScriptName(fFontRef), &tmpStr)
); | 1819 desc->setPostscriptName(get_str(CTFontCopyPostScriptName(fFontRef), &tmpStr)
); |
1820 // TODO: need to add support for local-streams (here and openStream) | 1820 // TODO: need to add support for local-streams (here and openStream) |
1821 *isLocalStream = false; | 1821 *isLocalStream = false; |
1822 } | 1822 } |
1823 | 1823 |
1824 /////////////////////////////////////////////////////////////////////////////// | 1824 /////////////////////////////////////////////////////////////////////////////// |
1825 /////////////////////////////////////////////////////////////////////////////// | 1825 /////////////////////////////////////////////////////////////////////////////// |
| 1826 #if 1 |
1826 | 1827 |
1827 #if 1 | 1828 static bool find_desc_str(CTFontDescriptorRef desc, CFStringRef name, SkString*
value) { |
| 1829 AutoCFRelease<CFStringRef> ref((CFStringRef)CTFontDescriptorCopyAttribute(de
sc, name)); |
| 1830 if (NULL == ref.get()) { |
| 1831 return false; |
| 1832 } |
| 1833 CFStringToSkString(ref, value); |
| 1834 return true; |
| 1835 } |
| 1836 |
| 1837 static bool find_dict_float(CFDictionaryRef dict, CFStringRef name, float* value
) { |
| 1838 CFNumberRef num; |
| 1839 return CFDictionaryGetValueIfPresent(dict, name, (const void**)&num) |
| 1840 && CFNumberIsFloatType(num) |
| 1841 && CFNumberGetValue(num, kCFNumberFloatType, value); |
| 1842 } |
| 1843 |
| 1844 static SkTypeface* createFromDesc(CFStringRef cfFamilyName, |
| 1845 CTFontDescriptorRef desc) { |
| 1846 AutoCFRelease<CTFontRef> ctNamed(CTFontCreateWithName(cfFamilyName, 1, NULL)
); |
| 1847 CTFontRef ctFont = CTFontCreateCopyWithAttributes(ctNamed, 1, NULL, desc); |
| 1848 |
| 1849 SkString str; |
| 1850 CFStringToSkString(cfFamilyName, &str); |
| 1851 return ctFont ? NewFromFontRef(ctFont, str.c_str()) : NULL; |
| 1852 } |
| 1853 |
1828 #include "SkFontMgr.h" | 1854 #include "SkFontMgr.h" |
1829 | 1855 |
| 1856 static int unit_weight_to_fontstyle(float unit) { |
| 1857 float value; |
| 1858 if (unit < 0) { |
| 1859 value = 100 + (1 + unit) * 300; |
| 1860 } else { |
| 1861 value = 400 + unit * 500; |
| 1862 } |
| 1863 return sk_float_round2int(value); |
| 1864 } |
| 1865 |
| 1866 static int unit_width_to_fontstyle(float unit) { |
| 1867 float value; |
| 1868 if (unit < 0) { |
| 1869 value = 1 + (1 + unit) * 4; |
| 1870 } else { |
| 1871 value = 5 + unit * 4; |
| 1872 } |
| 1873 return sk_float_round2int(value); |
| 1874 } |
| 1875 |
| 1876 static SkFontStyle desc2fontstyle(CTFontDescriptorRef desc) { |
| 1877 AutoCFRelease<CFDictionaryRef> dict( |
| 1878 (CFDictionaryRef)CTFontDescriptorCopyAttribute(desc, |
| 1879 kCTFontTraitsAttribute)); |
| 1880 if (NULL == dict.get()) { |
| 1881 return SkFontStyle(); |
| 1882 } |
| 1883 |
| 1884 float weight, width, slant; |
| 1885 if (!find_dict_float(dict, kCTFontWeightTrait, &weight)) { |
| 1886 weight = 0; |
| 1887 } |
| 1888 if (!find_dict_float(dict, kCTFontWidthTrait, &width)) { |
| 1889 width = 0; |
| 1890 } |
| 1891 if (!find_dict_float(dict, kCTFontSlantTrait, &slant)) { |
| 1892 slant = 0; |
| 1893 } |
| 1894 |
| 1895 return SkFontStyle(unit_weight_to_fontstyle(weight), |
| 1896 unit_width_to_fontstyle(width), |
| 1897 slant ? SkFontStyle::kItalic_Slant |
| 1898 : SkFontStyle::kUpright_Slant); |
| 1899 } |
| 1900 |
| 1901 class SkFontStyleSet_Mac : public SkFontStyleSet { |
| 1902 public: |
| 1903 SkFontStyleSet_Mac(CFStringRef familyName, CTFontDescriptorRef desc) |
| 1904 : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL)) |
| 1905 , fFamilyName(familyName) { |
| 1906 CFRetain(familyName); |
| 1907 } |
| 1908 |
| 1909 virtual ~SkFontStyleSet_Mac() { |
| 1910 CFSafeRelease(fArray); |
| 1911 CFRelease(fFamilyName); |
| 1912 } |
| 1913 |
| 1914 virtual int count() SK_OVERRIDE { |
| 1915 return CFArrayGetCount(fArray); |
| 1916 } |
| 1917 |
| 1918 virtual void getStyle(int index, SkFontStyle* style, |
| 1919 SkString* name) SK_OVERRIDE { |
| 1920 SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray)); |
| 1921 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(f
Array, index); |
| 1922 if (style) { |
| 1923 *style = desc2fontstyle(desc); |
| 1924 } |
| 1925 if (name) { |
| 1926 if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) { |
| 1927 name->reset(); |
| 1928 } |
| 1929 } |
| 1930 } |
| 1931 |
| 1932 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { |
| 1933 SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray)); |
| 1934 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(f
Array, index); |
| 1935 return createFromDesc(fFamilyName, desc); |
| 1936 } |
| 1937 |
| 1938 private: |
| 1939 CFArrayRef fArray; |
| 1940 CFStringRef fFamilyName; |
| 1941 }; |
| 1942 |
1830 class SkFontMgr_Mac : public SkFontMgr { | 1943 class SkFontMgr_Mac : public SkFontMgr { |
| 1944 int fCount; |
| 1945 CFArrayRef fNames; |
| 1946 |
| 1947 CFStringRef stringAt(int index) const { |
| 1948 SkASSERT((unsigned)index < (unsigned)fCount); |
| 1949 return (CFStringRef)CFArrayGetValueAtIndex(fNames, index); |
| 1950 } |
| 1951 |
| 1952 void lazyInit() { |
| 1953 if (NULL == fNames) { |
| 1954 fNames = CTFontManagerCopyAvailableFontFamilyNames(); |
| 1955 fCount = fNames ? CFArrayGetCount(fNames) : 0; |
| 1956 } |
| 1957 } |
| 1958 |
1831 public: | 1959 public: |
1832 SkFontMgr_Mac() {} | 1960 SkFontMgr_Mac() : fCount(0), fNames(NULL) {} |
| 1961 |
| 1962 virtual ~SkFontMgr_Mac() { |
| 1963 CFSafeRelease(fNames); |
| 1964 } |
1833 | 1965 |
1834 protected: | 1966 protected: |
1835 virtual int onCountFamilies() SK_OVERRIDE { | 1967 virtual int onCountFamilies() SK_OVERRIDE { |
1836 return 0; | 1968 this->lazyInit(); |
| 1969 return fCount; |
1837 } | 1970 } |
1838 | 1971 |
1839 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE { | 1972 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE { |
| 1973 this->lazyInit(); |
| 1974 if ((unsigned)index < (unsigned)fCount) { |
| 1975 CFStringToSkString(this->stringAt(index), familyName); |
| 1976 } else { |
| 1977 familyName->reset(); |
| 1978 } |
1840 } | 1979 } |
1841 | 1980 |
1842 virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE { | 1981 virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE { |
1843 return NULL; | 1982 this->lazyInit(); |
| 1983 if ((unsigned)index >= (unsigned)fCount) { |
| 1984 return NULL; |
| 1985 } |
| 1986 |
| 1987 AutoCFRelease<CFMutableDictionaryRef> cfAttr( |
| 1988 CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 1989 &kCFTypeDictionaryKeyCallBacks, |
| 1990 &kCFTypeDictionaryValueCallBacks)); |
| 1991 |
| 1992 CFDictionaryAddValue(cfAttr, kCTFontFamilyNameAttribute, |
| 1993 this->stringAt(index)); |
| 1994 |
| 1995 AutoCFRelease<CTFontDescriptorRef> desc( |
| 1996 CTFontDescriptorCreateWithAttributes(cfAttr)); |
| 1997 return SkNEW_ARGS(SkFontStyleSet_Mac, (this->stringAt(index), desc)); |
1844 } | 1998 } |
1845 | 1999 |
1846 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], | 2000 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
1847 const SkFontStyle&) SK_OVERRIDE { | 2001 const SkFontStyle&) SK_OVERRIDE { |
1848 return NULL; | 2002 return NULL; |
1849 } | 2003 } |
1850 | 2004 |
1851 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, | 2005 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
1852 const SkFontStyle&) SK_OVERRIDE { | 2006 const SkFontStyle&) SK_OVERRIDE { |
1853 return NULL; | 2007 return NULL; |
(...skipping 24 matching lines...) Expand all Loading... |
1878 return NULL; | 2032 return NULL; |
1879 } | 2033 } |
1880 return create_from_dataProvider(pr); | 2034 return create_from_dataProvider(pr); |
1881 } | 2035 } |
1882 }; | 2036 }; |
1883 | 2037 |
1884 SkFontMgr* SkFontMgr::Factory() { | 2038 SkFontMgr* SkFontMgr::Factory() { |
1885 return SkNEW(SkFontMgr_Mac); | 2039 return SkNEW(SkFontMgr_Mac); |
1886 } | 2040 } |
1887 #endif | 2041 #endif |
OLD | NEW |