diff options
author | Raph Levien <raph@google.com> | 2013-05-22 16:14:27 -0700 |
---|---|---|
committer | Raph Levien <raph@google.com> | 2013-06-14 11:22:35 -0700 |
commit | bcc3dc5a2591a95a57e379e27cbad69c18e91e67 (patch) | |
tree | 6ba40e86f2ef0d52b2e0796f1dda0eb6e5ec4377 /include | |
parent | 9cc9bbe1461f359f0b27c5e7645c17dda001ab1d (diff) | |
download | android_frameworks_minikin-bcc3dc5a2591a95a57e379e27cbad69c18e91e67.tar.gz android_frameworks_minikin-bcc3dc5a2591a95a57e379e27cbad69c18e91e67.tar.bz2 android_frameworks_minikin-bcc3dc5a2591a95a57e379e27cbad69c18e91e67.zip |
Introduce MinikinFont abstraction
This commit removes the direct dependency on FreeType and replaces it
with a MinikinFont abstraction, which is designed to support both
FreeType and Skia fonts (and possibly others in the future).
Also adds a "total advance" to the Layout, with an API for retrieving
it.
Change-Id: If20f92db9a43fd15b0fe9794b761ba00fb21338c
Diffstat (limited to 'include')
-rw-r--r-- | include/minikin/FontCollection.h | 19 | ||||
-rw-r--r-- | include/minikin/FontFamily.h | 12 | ||||
-rw-r--r-- | include/minikin/Layout.h | 13 | ||||
-rw-r--r-- | include/minikin/MinikinFont.h | 70 | ||||
-rw-r--r-- | include/minikin/MinikinFontFreeType.h | 71 |
5 files changed, 162 insertions, 23 deletions
diff --git a/include/minikin/FontCollection.h b/include/minikin/FontCollection.h index 3aa2aca..a2a5391 100644 --- a/include/minikin/FontCollection.h +++ b/include/minikin/FontCollection.h @@ -19,12 +19,9 @@ #include <vector> -#include <ft2build.h> -#include FT_FREETYPE_H -#include FT_TRUETYPE_TABLES_H - -#include "SparseBitSet.h" -#include "FontFamily.h" +#include <minikin/MinikinFont.h> +#include <minikin/SparseBitSet.h> +#include <minikin/FontFamily.h> namespace android { @@ -40,19 +37,19 @@ public: // Do copy constructor, assignment, destructor so it can be used in vectors Run() : font(NULL) { } Run(const Run& other): font(other.font), start(other.start), end(other.end) { - if (font) FT_Reference_Face(font); + if (font) font->Ref(); } Run& operator=(const Run& other) { - if (other.font) FT_Reference_Face(other.font); - if (font) FT_Done_Face(font); + if (other.font) other.font->Ref(); + if (font) font->Unref(); font = other.font; start = other.start; end = other.end; return *this; } - ~Run() { if (font) FT_Done_Face(font); } + ~Run() { if (font) font->Unref(); } - FT_Face font; + MinikinFont* font; int start; int end; }; diff --git a/include/minikin/FontFamily.h b/include/minikin/FontFamily.h index 01ea232..290220b 100644 --- a/include/minikin/FontFamily.h +++ b/include/minikin/FontFamily.h @@ -42,21 +42,21 @@ private: class FontFamily { public: // Add font to family, extracting style information from the font - bool addFont(FT_Face typeface); + bool addFont(MinikinFont* typeface); - void addFont(FT_Face typeface, FontStyle style); - FT_Face getClosestMatch(FontStyle style) const; + void addFont(MinikinFont* typeface, FontStyle style); + MinikinFont* getClosestMatch(FontStyle style) const; // API's for enumerating the fonts in a family. These don't guarantee any particular order size_t getNumFonts() const; - FT_Face getFont(size_t index) const; + MinikinFont* getFont(size_t index) const; FontStyle getStyle(size_t index) const; private: class Font { public: - Font(FT_Face typeface, FontStyle style) : + Font(MinikinFont* typeface, FontStyle style) : typeface(typeface), style(style) { } - FT_Face typeface; + MinikinFont* typeface; FontStyle style; }; std::vector<Font> mFonts; diff --git a/include/minikin/Layout.h b/include/minikin/Layout.h index 7a6c6cf..fd0ed75 100644 --- a/include/minikin/Layout.h +++ b/include/minikin/Layout.h @@ -17,15 +17,13 @@ #ifndef MINIKIN_LAYOUT_H #define MINIKIN_LAYOUT_H -#include <ft2build.h> -#include FT_FREETYPE_H - #include <hb.h> #include <vector> #include <minikin/CssParse.h> #include <minikin/FontCollection.h> +#include <minikin/MinikinFontFreeType.h> namespace android { @@ -37,7 +35,7 @@ public: Bitmap(int width, int height); ~Bitmap(); void writePnm(std::ofstream& o) const; - void drawGlyph(const FT_Bitmap& bitmap, int x, int y); + void drawGlyph(const GlyphBitmap& bitmap, int x, int y); private: int width; int height; @@ -65,12 +63,14 @@ public: void draw(Bitmap*, int x0, int y0) const; void setProperties(const std::string css); + float getAdvance() const; + // This must be called before any invocations. // TODO: probably have a factory instead static void init(); private: // Find a face in the mFaces vector, or create a new entry - int findFace(FT_Face face); + int findFace(MinikinFont* face, MinikinPaint* paint); CssProperties mProps; // TODO: want spans std::vector<LayoutGlyph> mGlyphs; @@ -80,8 +80,9 @@ private: // But for the time being, it should be ok to have just one // per layout. const FontCollection *mCollection; - std::vector<FT_Face> mFaces; + std::vector<MinikinFont *> mFaces; std::vector<hb_font_t *> mHbFonts; + float mAdvance; }; } // namespace android diff --git a/include/minikin/MinikinFont.h b/include/minikin/MinikinFont.h new file mode 100644 index 0000000..2c265c3 --- /dev/null +++ b/include/minikin/MinikinFont.h @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#ifndef MINIKIN_FONT_H +#define MINIKIN_FONT_H + +// An abstraction for platform fonts, allowing Minikin to be used with +// multiple actual implementations of fonts. + +namespace android { + +class MinikinFont; + +// Possibly move into own .h file? +struct MinikinPaint { + MinikinFont *font; + float size; + // todo: skew, stretch, hinting +}; + +class MinikinFontFreeType; + +class MinikinFont { +public: + void Ref() { mRefcount_++; } + void Unref() { if (--mRefcount_ == 0) { delete this; } } + + //MinikinFont(); + virtual ~MinikinFont() = 0; + + virtual bool GetGlyph(uint32_t codepoint, uint32_t *glyph) const = 0; + + virtual float GetHorizontalAdvance(uint32_t glyph_id, + const MinikinPaint &paint) const = 0; + + // If buf is NULL, just update size + virtual bool GetTable(uint32_t tag, uint8_t *buf, size_t *size) = 0; + + virtual int32_t GetUniqueId() const = 0; + + static uint32_t MakeTag(char c1, char c2, char c3, char c4) { + return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) | + ((uint32_t)c3 << 8) | (uint32_t)c4; + } + + // This is used to implement a downcast without RTTI + virtual MinikinFontFreeType* GetFreeType() { + return NULL; + } + +private: + int mRefcount_; +}; + +} // namespace android + +#endif // MINIKIN_FONT_H diff --git a/include/minikin/MinikinFontFreeType.h b/include/minikin/MinikinFontFreeType.h new file mode 100644 index 0000000..7051831 --- /dev/null +++ b/include/minikin/MinikinFontFreeType.h @@ -0,0 +1,71 @@ +/* + * 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. + */ + +#ifndef MINIKIN_FONT_FREETYPE_H +#define MINIKIN_FONT_FREETYPE_H + +#include <ft2build.h> +#include FT_FREETYPE_H +#include FT_TRUETYPE_TABLES_H + +#include <minikin/MinikinFont.h> + +// An abstraction for platform fonts, allowing Minikin to be used with +// multiple actual implementations of fonts. + +namespace android { + +struct GlyphBitmap { + uint8_t *buffer; + int width; + int height; + int left; + int top; +}; + +class MinikinFontFreeType : public MinikinFont { +public: + explicit MinikinFontFreeType(FT_Face typeface); + + ~MinikinFontFreeType(); + + bool GetGlyph(uint32_t codepoint, uint32_t *glyph) const; + + float GetHorizontalAdvance(uint32_t glyph_id, + const MinikinPaint &paint) const; + + // If buf is NULL, just update size + bool GetTable(uint32_t tag, uint8_t *buf, size_t *size); + + int32_t GetUniqueId() const; + + // Not a virtual method, as the protocol to access rendered + // glyph bitmaps is probably different depending on the + // backend. + bool Render(uint32_t glyph_id, + const MinikinPaint &paint, GlyphBitmap *result); + + MinikinFontFreeType* GetFreeType(); + +private: + FT_Face mTypeface; + int32_t mUniqueId; + static int32_t sIdCounter; +}; + +} // namespace android + +#endif // MINIKIN_FONT_FREETYPE_H |