diff options
author | Raph Levien <raph@google.com> | 2013-04-23 15:45:41 -0700 |
---|---|---|
committer | Raph Levien <raph@google.com> | 2013-04-25 12:23:57 -0700 |
commit | 9cc9bbe1461f359f0b27c5e7645c17dda001ab1d (patch) | |
tree | f7ef7ea18618b4be52dbc53a9b88fbdcb661a970 /libs/minikin/FontFamily.cpp | |
parent | cd404cb5e1aed30b46a7af7ddb91ba6e126fe4c2 (diff) | |
download | android_frameworks_minikin-9cc9bbe1461f359f0b27c5e7645c17dda001ab1d.tar.gz android_frameworks_minikin-9cc9bbe1461f359f0b27c5e7645c17dda001ab1d.tar.bz2 android_frameworks_minikin-9cc9bbe1461f359f0b27c5e7645c17dda001ab1d.zip |
Initial commit of Minikin library
This is the initial draft of Minikin, a library intended to perform text
layout functions. This version does basic weight selection and font runs
for scripts, and also has a simple renderer for drawing into bitmaps,
but is lacking measurement, line breaking, and a number of other
important features. It also lacks caching and other performance
refinements.
Change-Id: I789a2e47d11d71202dc84b4751b51a5e2cd9c451
Diffstat (limited to 'libs/minikin/FontFamily.cpp')
-rw-r--r-- | libs/minikin/FontFamily.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/libs/minikin/FontFamily.cpp b/libs/minikin/FontFamily.cpp new file mode 100644 index 0000000..dc6e16c --- /dev/null +++ b/libs/minikin/FontFamily.cpp @@ -0,0 +1,95 @@ +/* + * 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. + */ + +#define LOG_TAG "Minikin" + +#include <cutils/log.h> +#include <stdlib.h> +#include <stdint.h> +#include <utils/UniquePtr.h> +#include <ft2build.h> +#include FT_FREETYPE_H +#include FT_TRUETYPE_TABLES_H +#include <minikin/AnalyzeStyle.h> +#include <minikin/FontFamily.h> + +using std::vector; + +namespace android { + +bool FontFamily::addFont(FT_Face typeface) { + const uint32_t os2Tag = FT_MAKE_TAG('O', 'S', '/', '2'); + FT_ULong os2Size = 0; + FT_Error error = FT_Load_Sfnt_Table(typeface, os2Tag, 0, NULL, &os2Size); + if (error != 0) return false; + UniquePtr<uint8_t[]> os2Data(new uint8_t[os2Size]); + error = FT_Load_Sfnt_Table(typeface, os2Tag, 0, os2Data.get(), &os2Size); + if (error != 0) return false; + int weight; + bool italic; + if (analyzeStyle(os2Data.get(), os2Size, &weight, &italic)) { + //ALOGD("analyzed weight = %d, italic = %s", weight, italic ? "true" : "false"); + FontStyle style(weight, italic); + addFont(typeface, style); + return true; + } else { + ALOGD("failed to analyze style"); + } + return false; +} + +void FontFamily::addFont(FT_Face typeface, FontStyle style) { + mFonts.push_back(Font(typeface, style)); + ALOGD("added font, mFonts.size() = %d", mFonts.size()); +} + +// Compute a matching metric between two styles - 0 is an exact match +int computeMatch(FontStyle style1, FontStyle style2) { + if (style1 == style2) return 0; + int score = abs(style1.getWeight() - style2.getWeight()); + if (style1.getItalic() != style2.getItalic()) { + score += 2; + } + return score; +} + +FT_Face FontFamily::getClosestMatch(FontStyle style) const { + const Font* bestFont = NULL; + int bestMatch = 0; + for (size_t i = 0; i < mFonts.size(); i++) { + const Font& font = mFonts[i]; + int match = computeMatch(font.style, style); + if (i == 0 || match < bestMatch) { + bestFont = &font; + bestMatch = match; + } + } + return bestFont == NULL ? NULL : bestFont->typeface; +} + +size_t FontFamily::getNumFonts() const { + return mFonts.size(); +} + +FT_Face FontFamily::getFont(size_t index) const { + return mFonts[index].typeface; +} + +FontStyle FontFamily::getStyle(size_t index) const { + return mFonts[index].style; +} + +} // namespace android |