19cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien/*
29cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Copyright (C) 2013 The Android Open Source Project
39cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
49cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Licensed under the Apache License, Version 2.0 (the "License");
59cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * you may not use this file except in compliance with the License.
69cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * You may obtain a copy of the License at
79cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
89cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *      http://www.apache.org/licenses/LICENSE-2.0
99cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
109cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Unless required by applicable law or agreed to in writing, software
119cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * distributed under the License is distributed on an "AS IS" BASIS,
129cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * See the License for the specific language governing permissions and
149cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * limitations under the License.
159cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien */
169cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
179cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#ifndef MINIKIN_LAYOUT_H
189cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#define MINIKIN_LAYOUT_H
199cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
209cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <hb.h>
219cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
229cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <vector>
239cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
249cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <minikin/FontCollection.h>
25bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien#include <minikin/MinikinFontFreeType.h>
269cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
278920e81717c6e51b92ff8f4479a1f959af260556John Recknamespace minikin {
289cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
299cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien// The Bitmap class is for debugging. We'll probably move it out
309cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien// of here into a separate lightweight software rendering module
319cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien// (optional, as we'd hope most clients would do their own)
329cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienclass Bitmap {
339cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienpublic:
349cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    Bitmap(int width, int height);
359cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    ~Bitmap();
369cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    void writePnm(std::ofstream& o) const;
378920e81717c6e51b92ff8f4479a1f959af260556John Reck    void drawGlyph(const android::GlyphBitmap& bitmap, int x, int y);
389cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienprivate:
399cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    int width;
409cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    int height;
419cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    uint8_t* buf;
429cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien};
439cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
448920e81717c6e51b92ff8f4479a1f959af260556John Reck} // namespace minikin
458920e81717c6e51b92ff8f4479a1f959af260556John Reck
468920e81717c6e51b92ff8f4479a1f959af260556John Recknamespace android {
478920e81717c6e51b92ff8f4479a1f959af260556John Reck
489cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstruct LayoutGlyph {
499cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // index into mFaces and mHbFonts vectors. We could imagine
509cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // moving this into a run length representation, because it's
519cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // more efficient for long strings, and we'll probably need
529cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // something like that for paint attributes (color, underline,
539cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // fake b/i, etc), as having those per-glyph is bloated.
549cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    int font_ix;
559cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
569cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    unsigned int glyph_id;
579cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    float x;
589cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    float y;
599cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien};
609cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
614d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien// Internal state used during layout operation
62bae347682989d2627081310129a5b60541ed6ad0Seigo Nonakastruct LayoutContext;
634d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
6401f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levienenum {
6501f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien    kBidi_LTR = 0,
6601f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien    kBidi_RTL = 1,
6701f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien    kBidi_Default_LTR = 2,
6801f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien    kBidi_Default_RTL = 3,
6901f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien    kBidi_Force_LTR = 4,
7001f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien    kBidi_Force_RTL = 5,
7101f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien
7201f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien    kBidi_Mask = 0x7
7301f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien};
7401f526614431e3a0a6e1a48039e00b8a9b7d6fbfRaph Levien
75c31e3883456e018d742e9f29815ba5ff8b315ea1Raph Levien// Lifecycle and threading assumptions for Layout:
76c31e3883456e018d742e9f29815ba5ff8b315ea1Raph Levien// The object is assumed to be owned by a single thread; multiple threads
77c31e3883456e018d742e9f29815ba5ff8b315ea1Raph Levien// may not mutate it at the same time.
78c31e3883456e018d742e9f29815ba5ff8b315ea1Raph Levien// The lifetime of the FontCollection set through setFontCollection must
79c31e3883456e018d742e9f29815ba5ff8b315ea1Raph Levien// extend through the lifetime of the Layout object.
809cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienclass Layout {
819cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienpublic:
826da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod
836da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    Layout() : mGlyphs(), mAdvances(), mCollection(0), mFaces(), mAdvance(0), mBounds() {
846da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod        mBounds.setEmpty();
856da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    }
866da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod
876da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    // Clears layout, ready to be used again
886da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    void reset();
896da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod
909cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    void dump() const;
91ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    void setFontCollection(const FontCollection* collection);
924d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
9309f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod    void doLayout(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
9409f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod        int bidiFlags, const FontStyle &style, const MinikinPaint &paint);
9509f1901d6befcab49ed46cb77151a5d4af14a3b9Behdad Esfahbod
966344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    static float measureText(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
976344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        int bidiFlags, const FontStyle &style, const MinikinPaint &paint,
986344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        const FontCollection* collection, float* advances);
996344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi
1008920e81717c6e51b92ff8f4479a1f959af260556John Reck    void draw(minikin::Bitmap*, int x0, int y0, float size) const;
1014d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1026da7796cbe8a17efd61a3302369e69bb222fdb4fBehdad Esfahbod    // Deprecated. Nont needed. Remove when callers are removed.
1039cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    static void init();
104ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
105ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    // public accessors
106ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    size_t nGlyphs() const;
107ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    // Does not bump reference; ownership is still layout
108ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    MinikinFont *getFont(int i) const;
1099a5f713add8cfb91ac2c9ed5c917309053201ab6Raph Levien    FontFakery getFakery(int i) const;
110ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    unsigned int getGlyphId(int i) const;
111ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    float getX(int i) const;
112ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    float getY(int i) const;
113ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
114ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    float getAdvance() const;
115ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
116ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    // Get advances, copying into caller-provided buffer. The size of this
11740beb7744a61248de82a6077996c83c14e0122c2Raph Levien    // buffer must match the length of the string (count arg to doLayout).
118ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    void getAdvances(float* advances);
119ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
12040beb7744a61248de82a6077996c83c14e0122c2Raph Levien    // The i parameter is an offset within the buf relative to start, it is < count, where
12140beb7744a61248de82a6077996c83c14e0122c2Raph Levien    // start and count are the parameters to doLayout
12240beb7744a61248de82a6077996c83c14e0122c2Raph Levien    float getCharAdvance(size_t i) const { return mAdvances[i]; }
12340beb7744a61248de82a6077996c83c14e0122c2Raph Levien
124ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    void getBounds(MinikinRect* rect);
125ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
12622e41754f6470ff1f4c0e0a56d01f7f555b59e21Raph Levien    // Purge all caches, useful in low memory conditions
12722e41754f6470ff1f4c0e0a56d01f7f555b59e21Raph Levien    static void purgeCaches();
12822e41754f6470ff1f4c0e0a56d01f7f555b59e21Raph Levien
1299cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienprivate:
130288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod    friend class LayoutCacheKey;
131288c915963b3500c7efb958ba613650e2ecdfdfaBehdad Esfahbod
1329cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    // Find a face in the mFaces vector, or create a new entry
1339a5f713add8cfb91ac2c9ed5c917309053201ab6Raph Levien    int findFace(FakedFont face, LayoutContext* ctx);
1344d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1354d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // Lay out a single bidi run
1366344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    // When layout is not null, layout info will be stored in the object.
1376344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    // When advances is not null, measurement results will be stored in the array.
1386344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    static float doLayoutRunCached(const uint16_t* buf, size_t runStart, size_t runLength,
1396344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        size_t bufSize, bool isRtl, LayoutContext* ctx, size_t dstStart,
1406344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        const FontCollection* collection, Layout* layout, float* advances);
1414d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1424d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // Lay out a single word
1436344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi    static float doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
1446344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        bool isRtl, LayoutContext* ctx, size_t bufStart, const FontCollection* collection,
1456344de60fa37c4e4246cb37d82215564826c2b8bKeisuke Kuroyanagi        Layout* layout, float* advances);
1469cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
14786fa46c5ebb0d2c3319e08f4fbf487d8c2abbbfcRaph Levien    // Lay out a single bidi run
14886fa46c5ebb0d2c3319e08f4fbf487d8c2abbbfcRaph Levien    void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
1494d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien        bool isRtl, LayoutContext* ctx);
1504d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1514d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    // Append another layout (for example, cached value) into this one
1524d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien    void appendLayout(Layout* src, size_t start);
1534d4e6bc8118d15542f1f2a9218f0f7a91a29474fRaph Levien
1549cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    std::vector<LayoutGlyph> mGlyphs;
155ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    std::vector<float> mAdvances;
1569cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
157ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    const FontCollection* mCollection;
1589a5f713add8cfb91ac2c9ed5c917309053201ab6Raph Levien    std::vector<FakedFont> mFaces;
159bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    float mAdvance;
160ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    MinikinRect mBounds;
1619cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien};
1629cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1639cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}  // namespace android
1649cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1659cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#endif  // MINIKIN_LAYOUT_H
166