FontCollection.h 2.6 KB
Newer Older
R
Raph Levien 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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_COLLECTION_H
#define MINIKIN_FONT_COLLECTION_H

#include <vector>

R
Raph Levien 已提交
22
#include <minikin/MinikinRefCounted.h>
R
Raph Levien 已提交
23 24 25
#include <minikin/MinikinFont.h>
#include <minikin/SparseBitSet.h>
#include <minikin/FontFamily.h>
R
Raph Levien 已提交
26 27 28

namespace android {

R
Raph Levien 已提交
29
class FontCollection : public MinikinRefCounted {
R
Raph Levien 已提交
30 31 32 33 34 35 36 37 38 39 40
public:
    explicit FontCollection(const std::vector<FontFamily*>& typefaces);

    ~FontCollection();

    const FontFamily* getFamilyForChar(uint32_t ch) const;
    class Run {
    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) {
R
Raph Levien 已提交
41
            if (font) font->RefLocked();
R
Raph Levien 已提交
42 43
        }
        Run& operator=(const Run& other) {
R
Raph Levien 已提交
44 45
            if (other.font) other.font->RefLocked();
            if (font) font->UnrefLocked();
R
Raph Levien 已提交
46 47 48 49 50
            font = other.font;
            start = other.start;
            end = other.end;
            return *this;
        }
R
Raph Levien 已提交
51
        ~Run() { if (font) font->UnrefLocked(); }
R
Raph Levien 已提交
52

R
Raph Levien 已提交
53
        MinikinFont* font;
R
Raph Levien 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        int start;
        int end;
    };
    void itemize(const uint16_t *string, size_t string_length, FontStyle style,
            std::vector<Run>* result) const;
    private:
    static const int kLogCharsPerPage = 8;
    static const int kPageMask = (1 << kLogCharsPerPage) - 1;

    struct FontInstance {
        SparseBitSet* mCoverage;
        FontFamily* mFamily;
    };

    struct Range {
        size_t start;
        size_t end;
    };

    // Highest UTF-32 code point that can be mapped
    uint32_t mMaxChar;

    // This vector has ownership of the bitsets and typeface objects.
    std::vector<FontInstance> mInstances;

    // This vector contains pointers into mInstances
    std::vector<const FontInstance*> mInstanceVec;

    // These are offsets into mInstanceVec, one range per page
    std::vector<Range> mRanges;
};

}  // namespace android

#endif  // MINIKIN_FONT_COLLECTION_H