OSDN Git Service

Remove MinikinSkiaFont::GetGlyph.
[android-x86/frameworks-base.git] / core / jni / android / graphics / MinikinSkia.cpp
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 <SkTypeface.h>
18 #include <SkPaint.h>
19
20 #define LOG_TAG "Minikin"
21 #include <cutils/log.h>
22
23 #include "MinikinSkia.h"
24
25 namespace android {
26
27 MinikinFontSkia::MinikinFontSkia(SkTypeface *typeface) :
28     mTypeface(typeface) {
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 bool MinikinFontSkia::GetTable(uint32_t tag, uint8_t *buf, size_t *size) {
72     if (buf == NULL) {
73         const size_t tableSize = mTypeface->getTableSize(tag);
74         *size = tableSize;
75         return tableSize != 0;
76     } else {
77         const size_t actualSize = mTypeface->getTableData(tag, 0, *size, buf);
78         *size = actualSize;
79         return actualSize != 0;
80     }
81 }
82
83 SkTypeface *MinikinFontSkia::GetSkTypeface() const {
84     return mTypeface;
85 }
86
87 int32_t MinikinFontSkia::GetUniqueId() const {
88     return mTypeface->uniqueID();
89 }
90
91 uint32_t MinikinFontSkia::packPaintFlags(const SkPaint* paint) {
92     uint32_t flags = paint->getFlags();
93     SkPaint::Hinting hinting = paint->getHinting();
94     // select only flags that might affect text layout
95     flags &= (SkPaint::kAntiAlias_Flag | SkPaint::kFakeBoldText_Flag | SkPaint::kLinearText_Flag |
96             SkPaint::kSubpixelText_Flag | SkPaint::kDevKernText_Flag |
97             SkPaint::kEmbeddedBitmapText_Flag | SkPaint::kAutoHinting_Flag |
98             SkPaint::kVerticalText_Flag);
99     flags |= (hinting << 16);
100     return flags;
101 }
102
103 void MinikinFontSkia::unpackPaintFlags(SkPaint* paint, uint32_t paintFlags) {
104     paint->setFlags(paintFlags & SkPaint::kAllFlags);
105     paint->setHinting(static_cast<SkPaint::Hinting>(paintFlags >> 16));
106 }
107
108 void MinikinFontSkia::populateSkPaint(SkPaint* paint, const MinikinFont* font, FontFakery fakery) {
109     paint->setTypeface(reinterpret_cast<const MinikinFontSkia*>(font)->GetSkTypeface());
110     paint->setFakeBoldText(paint->isFakeBoldText() || fakery.isFakeBold());
111     if (fakery.isFakeItalic()) {
112         paint->setTextSkewX(paint->getTextSkewX() - 0.25f);
113     }
114 }
115
116 }