MinikinSkia.cpp revision a033630e805c407080221e20b236b6054f324670
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#include <SkFP.h>
20
21#define LOG_TAG "Minikin"
22#include <cutils/log.h>
23
24#include <minikin/MinikinFont.h>
25#include "MinikinSkia.h"
26
27namespace android {
28
29MinikinFontSkia::MinikinFontSkia(SkTypeface *typeface) :
30    mTypeface(typeface) {
31}
32
33MinikinFontSkia::~MinikinFontSkia() {
34    SkSafeUnref(mTypeface);
35}
36
37bool MinikinFontSkia::GetGlyph(uint32_t codepoint, uint32_t *glyph) const {
38    SkPaint paint;
39    paint.setTypeface(mTypeface);
40    paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
41    uint16_t glyph16;
42    paint.textToGlyphs(&codepoint, sizeof(codepoint), &glyph16);
43    *glyph  = glyph16;
44    return !!glyph;
45}
46
47float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id,
48    const MinikinPaint &paint) const {
49    SkPaint skpaint;
50    skpaint.setTypeface(mTypeface);
51    skpaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
52    // TODO: set more paint parameters from Minikin
53    skpaint.setTextSize(paint.size);
54    uint16_t glyph16 = glyph_id;
55    SkScalar skWidth;
56    SkRect skBounds;
57    skpaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, &skBounds);
58    // TODO: get bounds information
59    return SkScalarToFP(skWidth);
60}
61
62bool MinikinFontSkia::GetTable(uint32_t tag, uint8_t *buf, size_t *size) {
63    if (buf == NULL) {
64        const size_t tableSize = mTypeface->getTableSize(tag);
65        *size = tableSize;
66        return tableSize != 0;
67    } else {
68        const size_t actualSize = mTypeface->getTableData(tag, 0, *size, buf);
69        *size = actualSize;
70        return actualSize != 0;
71    }
72}
73
74SkTypeface *MinikinFontSkia::GetSkTypeface() {
75    return mTypeface;
76}
77
78int32_t MinikinFontSkia::GetUniqueId() const {
79    return mTypeface->uniqueID();
80}
81
82}
83