MinikinFontFreeType.cpp revision 210dfa01db0defb3c23b60c446e97b1ee6f1287e
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// Implementation of MinikinFont abstraction specialized for FreeType 18 19#include <stdint.h> 20 21#include <ft2build.h> 22#include FT_FREETYPE_H 23#include FT_TRUETYPE_TABLES_H 24#include FT_ADVANCES_H 25 26#include <minikin/MinikinFontFreeType.h> 27 28namespace android { 29 30int32_t MinikinFontFreeType::sIdCounter = 0; 31 32MinikinFontFreeType::MinikinFontFreeType(FT_Face typeface) : 33 mTypeface(typeface) { 34 mUniqueId = sIdCounter++; 35} 36 37MinikinFontFreeType::~MinikinFontFreeType() { 38 FT_Done_Face(mTypeface); 39} 40 41float MinikinFontFreeType::GetHorizontalAdvance(uint32_t glyph_id, 42 const MinikinPaint &paint) const { 43 FT_Set_Pixel_Sizes(mTypeface, 0, paint.size); 44 FT_UInt32 flags = FT_LOAD_DEFAULT; // TODO: respect hinting settings 45 FT_Fixed advance; 46 FT_Get_Advance(mTypeface, glyph_id, flags, &advance); 47 return advance * (1.0 / 65536); 48} 49 50void MinikinFontFreeType::GetBounds(MinikinRect* bounds, uint32_t glyph_id, 51 const MinikinPaint& paint) const { 52 // TODO: NYI 53} 54 55bool MinikinFontFreeType::GetTable(uint32_t tag, uint8_t *buf, size_t *size) { 56 FT_ULong ftsize = *size; 57 FT_Error error = FT_Load_Sfnt_Table(mTypeface, tag, 0, buf, &ftsize); 58 if (error != 0) { 59 return false; 60 } 61 *size = ftsize; 62 return true; 63} 64 65int32_t MinikinFontFreeType::GetUniqueId() const { 66 return mUniqueId; 67} 68 69bool MinikinFontFreeType::Render(uint32_t glyph_id, 70 const MinikinPaint &paint, GlyphBitmap *result) { 71 FT_Error error; 72 FT_Int32 load_flags = FT_LOAD_DEFAULT; // TODO: respect hinting settings 73 error = FT_Load_Glyph(mTypeface, glyph_id, load_flags); 74 if (error != 0) { 75 return false; 76 } 77 error = FT_Render_Glyph(mTypeface->glyph, FT_RENDER_MODE_NORMAL); 78 if (error != 0) { 79 return false; 80 } 81 FT_Bitmap &bitmap = mTypeface->glyph->bitmap; 82 result->buffer = bitmap.buffer; 83 result->width = bitmap.width; 84 result->height = bitmap.rows; 85 result->left = mTypeface->glyph->bitmap_left; 86 result->top = mTypeface->glyph->bitmap_top; 87 return true; 88} 89 90MinikinFontFreeType* MinikinFontFreeType::GetFreeType() { 91 return this; 92} 93 94} // namespace android 95