MinikinSkia.cpp revision 1a73f732f91e97c9c66b808c245ddda36a10e987
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 <minikin/MinikinFont.h>
24#include "MinikinSkia.h"
25
26namespace android {
27
28MinikinFontSkia::MinikinFontSkia(SkTypeface *typeface) :
29    mTypeface(typeface) {
30}
31
32MinikinFontSkia::~MinikinFontSkia() {
33    SkSafeUnref(mTypeface);
34}
35
36bool MinikinFontSkia::GetGlyph(uint32_t codepoint, uint32_t *glyph) const {
37    SkPaint paint;
38    paint.setTypeface(mTypeface);
39    paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
40    uint16_t glyph16;
41    paint.textToGlyphs(&codepoint, sizeof(codepoint), &glyph16);
42    *glyph  = glyph16;
43    return !!glyph;
44}
45
46static void MinikinFontSkia_SetSkiaPaint(SkTypeface* typeface, SkPaint* skPaint, const MinikinPaint& paint) {
47    skPaint->setTypeface(typeface);
48    skPaint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
49    // TODO: set more paint parameters from Minikin
50    skPaint->setTextSize(paint.size);
51}
52
53float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id,
54    const MinikinPaint &paint) const {
55    SkPaint skPaint;
56    uint16_t glyph16 = glyph_id;
57    SkScalar skWidth;
58    MinikinFontSkia_SetSkiaPaint(mTypeface, &skPaint, paint);
59    skPaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, NULL);
60    ALOGD("width for typeface %d glyph %d = %f", mTypeface->uniqueID(), glyph_id, skWidth);
61    return skWidth;
62}
63
64void MinikinFontSkia::GetBounds(MinikinRect* bounds, uint32_t glyph_id,
65    const MinikinPaint& paint) const {
66    SkPaint skPaint;
67    uint16_t glyph16 = glyph_id;
68    SkRect skBounds;
69    MinikinFontSkia_SetSkiaPaint(mTypeface, &skPaint, paint);
70    skPaint.getTextWidths(&glyph16, sizeof(glyph16), NULL, &skBounds);
71    bounds->mLeft = skBounds.fLeft;
72    bounds->mTop = skBounds.fTop;
73    bounds->mRight = skBounds.fRight;
74    bounds->mBottom = skBounds.fBottom;
75}
76
77bool MinikinFontSkia::GetTable(uint32_t tag, uint8_t *buf, size_t *size) {
78    if (buf == NULL) {
79        const size_t tableSize = mTypeface->getTableSize(tag);
80        *size = tableSize;
81        return tableSize != 0;
82    } else {
83        const size_t actualSize = mTypeface->getTableData(tag, 0, *size, buf);
84        *size = actualSize;
85        return actualSize != 0;
86    }
87}
88
89SkTypeface *MinikinFontSkia::GetSkTypeface() {
90    return mTypeface;
91}
92
93int32_t MinikinFontSkia::GetUniqueId() const {
94    return mTypeface->uniqueID();
95}
96
97}
98