Home | History | Annotate | Download | only in minikin
      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 #ifndef MINIKIN_FONT_H
     18 #define MINIKIN_FONT_H
     19 
     20 #include <string>
     21 
     22 #include <minikin/MinikinRefCounted.h>
     23 #include <minikin/FontFamily.h>
     24 
     25 // An abstraction for platform fonts, allowing Minikin to be used with
     26 // multiple actual implementations of fonts.
     27 
     28 namespace android {
     29 
     30 class MinikinFont;
     31 
     32 // Possibly move into own .h file?
     33 // Note: if you add a field here, either add it to LayoutCacheKey or to skipCache()
     34 struct MinikinPaint {
     35     MinikinPaint() : font(0), size(0), scaleX(0), skewX(0), letterSpacing(0), paintFlags(0),
     36             fakery(), fontFeatureSettings() { }
     37 
     38     bool skipCache() const {
     39       return !fontFeatureSettings.empty();
     40     }
     41 
     42     MinikinFont *font;
     43     float size;
     44     float scaleX;
     45     float skewX;
     46     float letterSpacing;
     47     uint32_t paintFlags;
     48     FontFakery fakery;
     49     std::string fontFeatureSettings;
     50 };
     51 
     52 // Only a few flags affect layout, but those that do should have values
     53 // consistent with Android's paint flags.
     54 enum MinikinPaintFlags {
     55     LinearTextFlag = 0x40,
     56 };
     57 
     58 struct MinikinRect {
     59     float mLeft, mTop, mRight, mBottom;
     60     bool isEmpty() const {
     61         return mLeft == mRight || mTop == mBottom;
     62     }
     63     void set(const MinikinRect& r) {
     64         mLeft = r.mLeft;
     65         mTop = r.mTop;
     66         mRight = r.mRight;
     67         mBottom = r.mBottom;
     68     }
     69     void offset(float dx, float dy) {
     70         mLeft += dx;
     71         mTop += dy;
     72         mRight += dx;
     73         mBottom += dy;
     74     }
     75     void setEmpty() {
     76         mLeft = mTop = mRight = mBottom = 0;
     77     }
     78     void join(const MinikinRect& r);
     79 };
     80 
     81 class MinikinFontFreeType;
     82 
     83 class MinikinFont : public MinikinRefCounted {
     84 public:
     85     virtual bool GetGlyph(uint32_t codepoint, uint32_t *glyph) const = 0;
     86 
     87     virtual float GetHorizontalAdvance(uint32_t glyph_id,
     88         const MinikinPaint &paint) const = 0;
     89 
     90     virtual void GetBounds(MinikinRect* bounds, uint32_t glyph_id,
     91         const MinikinPaint &paint) const = 0;
     92 
     93     // If buf is NULL, just update size
     94     virtual bool GetTable(uint32_t tag, uint8_t *buf, size_t *size) = 0;
     95 
     96     virtual int32_t GetUniqueId() const = 0;
     97 
     98     static uint32_t MakeTag(char c1, char c2, char c3, char c4) {
     99         return ((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) |
    100             ((uint32_t)c3 << 8) | (uint32_t)c4;
    101     }
    102 };
    103 
    104 }  // namespace android
    105 
    106 #endif  // MINIKIN_FONT_H
    107