Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "MinikinSkia.h"
     18 
     19 #include <SkPaint.h>
     20 #include <SkTypeface.h>
     21 #include <cutils/log.h>
     22 
     23 namespace android {
     24 
     25 MinikinFontSkia::MinikinFontSkia(SkTypeface* typeface, const void* fontData, size_t fontSize,
     26         int ttcIndex) :
     27     MinikinFont(typeface->uniqueID()), mTypeface(typeface), mFontData(fontData),
     28     mFontSize(fontSize), mTtcIndex(ttcIndex) {
     29 }
     30 
     31 MinikinFontSkia::~MinikinFontSkia() {
     32     SkSafeUnref(mTypeface);
     33 }
     34 
     35 static void MinikinFontSkia_SetSkiaPaint(const MinikinFont* font, SkPaint* skPaint, const MinikinPaint& paint) {
     36     skPaint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
     37     skPaint->setTextSize(paint.size);
     38     skPaint->setTextScaleX(paint.scaleX);
     39     skPaint->setTextSkewX(paint.skewX);
     40     MinikinFontSkia::unpackPaintFlags(skPaint, paint.paintFlags);
     41     // Apply font fakery on top of user-supplied flags.
     42     MinikinFontSkia::populateSkPaint(skPaint, font, paint.fakery);
     43 }
     44 
     45 float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id,
     46     const MinikinPaint &paint) const {
     47     SkPaint skPaint;
     48     uint16_t glyph16 = glyph_id;
     49     SkScalar skWidth;
     50     MinikinFontSkia_SetSkiaPaint(this, &skPaint, paint);
     51     skPaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, NULL);
     52 #ifdef VERBOSE
     53     ALOGD("width for typeface %d glyph %d = %f", mTypeface->uniqueID(), glyph_id, skWidth);
     54 #endif
     55     return skWidth;
     56 }
     57 
     58 void MinikinFontSkia::GetBounds(MinikinRect* bounds, uint32_t glyph_id,
     59     const MinikinPaint& paint) const {
     60     SkPaint skPaint;
     61     uint16_t glyph16 = glyph_id;
     62     SkRect skBounds;
     63     MinikinFontSkia_SetSkiaPaint(this, &skPaint, paint);
     64     skPaint.getTextWidths(&glyph16, sizeof(glyph16), NULL, &skBounds);
     65     bounds->mLeft = skBounds.fLeft;
     66     bounds->mTop = skBounds.fTop;
     67     bounds->mRight = skBounds.fRight;
     68     bounds->mBottom = skBounds.fBottom;
     69 }
     70 
     71 const void* MinikinFontSkia::GetTable(uint32_t tag, size_t* size, MinikinDestroyFunc* destroy) {
     72     // we don't have a buffer to the font data, copy to own buffer
     73     const size_t tableSize = mTypeface->getTableSize(tag);
     74     *size = tableSize;
     75     if (tableSize == 0) {
     76         return nullptr;
     77     }
     78     void* buf = malloc(tableSize);
     79     if (buf == nullptr) {
     80         return nullptr;
     81     }
     82     mTypeface->getTableData(tag, 0, tableSize, buf);
     83     *destroy = free;
     84     return buf;
     85 }
     86 
     87 SkTypeface *MinikinFontSkia::GetSkTypeface() const {
     88     return mTypeface;
     89 }
     90 
     91 const void* MinikinFontSkia::GetFontData() const {
     92     return mFontData;
     93 }
     94 
     95 size_t MinikinFontSkia::GetFontSize() const {
     96     return mFontSize;
     97 }
     98 
     99 int MinikinFontSkia::GetFontIndex() const {
    100     return mTtcIndex;
    101 }
    102 
    103 uint32_t MinikinFontSkia::packPaintFlags(const SkPaint* paint) {
    104     uint32_t flags = paint->getFlags();
    105     SkPaint::Hinting hinting = paint->getHinting();
    106     // select only flags that might affect text layout
    107     flags &= (SkPaint::kAntiAlias_Flag | SkPaint::kFakeBoldText_Flag | SkPaint::kLinearText_Flag |
    108             SkPaint::kSubpixelText_Flag | SkPaint::kDevKernText_Flag |
    109             SkPaint::kEmbeddedBitmapText_Flag | SkPaint::kAutoHinting_Flag |
    110             SkPaint::kVerticalText_Flag);
    111     flags |= (hinting << 16);
    112     return flags;
    113 }
    114 
    115 void MinikinFontSkia::unpackPaintFlags(SkPaint* paint, uint32_t paintFlags) {
    116     paint->setFlags(paintFlags & SkPaint::kAllFlags);
    117     paint->setHinting(static_cast<SkPaint::Hinting>(paintFlags >> 16));
    118 }
    119 
    120 void MinikinFontSkia::populateSkPaint(SkPaint* paint, const MinikinFont* font, FontFakery fakery) {
    121     paint->setTypeface(reinterpret_cast<const MinikinFontSkia*>(font)->GetSkTypeface());
    122     paint->setFakeBoldText(paint->isFakeBoldText() || fakery.isFakeBold());
    123     if (fakery.isFakeItalic()) {
    124         paint->setTextSkewX(paint->getTextSkewX() - 0.25f);
    125     }
    126 }
    127 
    128 }
    129