blob: 243fa10df64741c06e2ce784343a250d3f68cae3 [file] [log] [blame]
Raph Leviena0336302013-05-22 16:16:59 -07001/*
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>
Raph Leviena0336302013-05-22 16:16:59 -070019
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
Raph Levien1a73f7322014-01-30 16:06:28 -080046static 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
Raph Leviena0336302013-05-22 16:16:59 -070053float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id,
54 const MinikinPaint &paint) const {
Raph Levien1a73f7322014-01-30 16:06:28 -080055 SkPaint skPaint;
Raph Leviena0336302013-05-22 16:16:59 -070056 uint16_t glyph16 = glyph_id;
57 SkScalar skWidth;
Raph Levien1a73f7322014-01-30 16:06:28 -080058 MinikinFontSkia_SetSkiaPaint(mTypeface, &skPaint, paint);
59 skPaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, NULL);
Raph Levien9d9ee3d2014-05-12 15:16:17 -070060#ifdef VERBOSE
Raph Levien1a73f7322014-01-30 16:06:28 -080061 ALOGD("width for typeface %d glyph %d = %f", mTypeface->uniqueID(), glyph_id, skWidth);
Raph Levien9d9ee3d2014-05-12 15:16:17 -070062#endif
Raph Levien1a73f7322014-01-30 16:06:28 -080063 return skWidth;
64}
65
66void MinikinFontSkia::GetBounds(MinikinRect* bounds, uint32_t glyph_id,
67 const MinikinPaint& paint) const {
68 SkPaint skPaint;
69 uint16_t glyph16 = glyph_id;
Raph Leviena0336302013-05-22 16:16:59 -070070 SkRect skBounds;
Raph Levien1a73f7322014-01-30 16:06:28 -080071 MinikinFontSkia_SetSkiaPaint(mTypeface, &skPaint, paint);
72 skPaint.getTextWidths(&glyph16, sizeof(glyph16), NULL, &skBounds);
73 bounds->mLeft = skBounds.fLeft;
74 bounds->mTop = skBounds.fTop;
75 bounds->mRight = skBounds.fRight;
76 bounds->mBottom = skBounds.fBottom;
Raph Leviena0336302013-05-22 16:16:59 -070077}
78
79bool MinikinFontSkia::GetTable(uint32_t tag, uint8_t *buf, size_t *size) {
80 if (buf == NULL) {
81 const size_t tableSize = mTypeface->getTableSize(tag);
82 *size = tableSize;
83 return tableSize != 0;
84 } else {
85 const size_t actualSize = mTypeface->getTableData(tag, 0, *size, buf);
86 *size = actualSize;
87 return actualSize != 0;
88 }
89}
90
91SkTypeface *MinikinFontSkia::GetSkTypeface() {
92 return mTypeface;
93}
94
95int32_t MinikinFontSkia::GetUniqueId() const {
96 return mTypeface->uniqueID();
97}
98
99}