summaryrefslogtreecommitdiffstats
path: root/libs/minikin/MinikinFontFreeType.cpp
blob: 972f3f1d14fe94772bce465c27c89e3b12a88b07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Implementation of MinikinFont abstraction specialized for FreeType

#include <stdint.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H
#include FT_ADVANCES_H

#include <minikin/MinikinFontFreeType.h>

namespace android {

int32_t MinikinFontFreeType::sIdCounter = 0;

MinikinFontFreeType::MinikinFontFreeType(FT_Face typeface) : 
    mTypeface(typeface) {
    mUniqueId = sIdCounter++;
}

MinikinFontFreeType::~MinikinFontFreeType() {
    FT_Done_Face(mTypeface);
}

bool MinikinFontFreeType::GetGlyph(uint32_t codepoint, uint32_t *glyph) const {
    FT_UInt glyph_index = FT_Get_Char_Index(mTypeface, codepoint);
    *glyph = glyph_index;
    return !!glyph_index;
}

float MinikinFontFreeType::GetHorizontalAdvance(uint32_t glyph_id,
    const MinikinPaint &paint) const {
    FT_Set_Pixel_Sizes(mTypeface, 0, paint.size);
	FT_UInt32 flags = FT_LOAD_DEFAULT;  // TODO: respect hinting settings
	FT_Fixed advance;
    FT_Get_Advance(mTypeface, glyph_id, flags, &advance);
    return advance * (1.0 / 65536);
}

void MinikinFontFreeType::GetBounds(MinikinRect* bounds, uint32_t glyph_id,
    const MinikinPaint& paint) const {
    // TODO: NYI
}

bool MinikinFontFreeType::GetTable(uint32_t tag, uint8_t *buf, size_t *size) {
	FT_ULong ftsize = *size;
	FT_Error error = FT_Load_Sfnt_Table(mTypeface, tag, 0, buf, &ftsize);
	if (error != 0) {
		return false;
	}
	*size = ftsize;
	return true;
}

int32_t MinikinFontFreeType::GetUniqueId() const {
	return mUniqueId;
}

bool MinikinFontFreeType::Render(uint32_t glyph_id,
    const MinikinPaint &paint, GlyphBitmap *result) {
    FT_Error error;
    FT_Int32 load_flags = FT_LOAD_DEFAULT;  // TODO: respect hinting settings
    error = FT_Load_Glyph(mTypeface, glyph_id, load_flags);
    if (error != 0) {
        return false;
    }
    error = FT_Render_Glyph(mTypeface->glyph, FT_RENDER_MODE_NORMAL);
    if (error != 0) {
        return false;
    }
    FT_Bitmap &bitmap = mTypeface->glyph->bitmap;
    result->buffer = bitmap.buffer;
    result->width = bitmap.width;
    result->height = bitmap.rows;
    result->left = mTypeface->glyph->bitmap_left;
    result->top = mTypeface->glyph->bitmap_top;
    return true;
}

MinikinFontFreeType* MinikinFontFreeType::GetFreeType() {
    return this;
}

}  // namespace android