のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

Typeface.DEFAULT

    335     static {
    344         sDefaults = new Typeface[] {
    338             create((String) null, 0);
    339             create((String) null, Typeface.BOLD);
    347             create((String) null, Typeface.ITALIC),
    348             create((String) null, Typeface.BOLD_ITALIC),
    349         };
    350 
    351     }
                                                 familyName = null

    115     public static Typeface create(String familyName, int style) {
    116         if (sSystemFontMap != null) {
    117             return create(sSystemFontMap.get(familyName), style);
    118         }
    119         return null;
    120     }
    133     public static Typeface create(Typeface family, int style) {
    134         if (style < 0 || style > 3) {
    135             style = 0;
    136         }
    137         long ni = 0;
    138         if (family != null) {
    139             // Return early if we're asked for the same face/style
    140             if (family.mStyle == style) {
    141                 return family;
    142             }
    143 
    144             ni = family.native_instance;
    145         }
    146 
    147         Typeface typeface;
    148         SparseArray<Typeface> styles = sTypefaceCache.get(ni);
    149 
    150         if (styles != null) {
    151             typeface = styles.get(style);
    152             if (typeface != null) {
    153                 return typeface;
    154             }
    155         }
    156 
    157         typeface = new Typeface(nativeCreateFromTypeface(ni, style));
    158         if (styles == null) {
    159             styles = new SparseArray<Typeface>(4);
    160             sTypefaceCache.put(ni, styles);
    161         }
    162         styles.put(style, typeface);
    163 
    164         return typeface;
    165     }
     30 static jlong Typeface_createFromTypeface(JNIEnv* env, jobject, jlong familyHandle, jint style) {
     31     TypefaceImpl* family = reinterpret_cast<TypefaceImpl*>(familyHandle);
     32     TypefaceImpl* face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)style);
     33     // TODO: the following logic shouldn't be necessary, the above should always succeed.
     34     // Try to find the closest matching font, using the standard heuristic
     35     if (NULL == face) {
     36         face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)(style ^ SkTypeface::kItalic));
     37     }
     38     for (int i = 0; NULL == face && i < 4; i++) {
     39         face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)i);
     40     }
     41     return reinterpret_cast<jlong>(face);
     42 }
    111 TypefaceImpl* TypefaceImpl_createFromTypeface(TypefaceImpl* src, SkTypeface::Style style) {
    112     TypefaceImpl* resolvedFace = TypefaceImpl_resolveDefault(src);
    113     TypefaceImpl* result = new TypefaceImpl;
    114     if (result != 0) {
    115         result->fFontCollection = resolvedFace->fFontCollection;
    116         result->fFontCollection->Ref();
    117         result->fSkiaStyle = style;
    118         result->fBaseWeight = resolvedFace->fBaseWeight;
    119         resolveStyle(result);
    120     }
    121     return result;
    122 }
    102 TypefaceImpl* TypefaceImpl_resolveDefault(TypefaceImpl* src) {
    103     if (src == NULL) {
    104         pthread_once(&gDefaultTypefaceOnce, getDefaultTypefaceOnce);
    105         return gDefaultTypeface;
    106     } else {
    107         return src;
    108     }
    109 }
    89 static void getDefaultTypefaceOnce() {
     90     Layout::init();
     91     if (gDefaultTypeface == NULL) {
     92         // We expect the client to set a default typeface, but provide a
     93         // default so we can make progress before that happens.
     94         gDefaultTypeface = new TypefaceImpl;
     95         gDefaultTypeface->fFontCollection = makeFontCollection();
     96         gDefaultTypeface->fSkiaStyle = SkTypeface::kNormal;
     97         gDefaultTypeface->fBaseWeight = 400;
     98         resolveStyle(gDefaultTypeface);
     99     }
    100 }
     63 static FontCollection *makeFontCollection() {
     64     std::vector<FontFamily *>typefaces;
     65     const char *fns[] = {
     66         "/system/fonts/Roboto-Regular.ttf",
     67     };
     68 
     69     FontFamily *family = new FontFamily();
     70     for (size_t i = 0; i < sizeof(fns)/sizeof(fns[0]); i++) {
     71         const char *fn = fns[i];
     72         ALOGD("makeFontCollection adding %s", fn);
     73         SkTypeface *skFace = SkTypeface::CreateFromFile(fn);
     74         if (skFace != NULL) {
     75             MinikinFont *font = new MinikinFontSkia(skFace);
     76             family->addFont(font);
     77             font->Unref();
     78         } else {
     79             ALOGE("failed to create font %s", fn);
     80         }
     81     }
     82     typefaces.push_back(family);
     83 
     84     FontCollection *result = new FontCollection(typefaces);
     85     family->Unref();
     86     return result;
     87 }
     43 static void resolveStyle(TypefaceImpl* typeface) {
     44     int weight = typeface->fBaseWeight / 100;
     45     if (typeface->fSkiaStyle & SkTypeface::kBold) {
     46         weight += 3;
     47     }
     48     if (weight > 9) {
     49         weight = 9;
     50     }
     51     bool italic = (typeface->fSkiaStyle & SkTypeface::kItalic) != 0;
     52     typeface->fStyle = FontStyle(weight, italic);
     53 }