summaryrefslogtreecommitdiff
path: root/src/mbgl/text/font.cpp
blob: 34cfda4501345f7963f3e91bb80777a98418656f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <mbgl/text/font.hpp>

#include <harfbuzz/hb-ft.h>

#include <fstream>
#include <iostream>

#include <codecvt>

namespace mbgl {

Font::Font(const std::string &filename)
    : filename_(filename),
    font_(0),
    buffer_(hb_buffer_create()) {
      load();
}

Font::~Font() {
    hb_buffer_destroy(buffer_);
    hb_font_destroy(font_);
}

void Font::load() {
    if (font_) return;

    char *font_data;
    unsigned int size;

    std::ifstream file(filename_.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
    if (file.is_open())
    {
        size = file.tellg();
        font_data = new char[size];
        file.seekg(0, std::ios::beg);
        file.read(font_data, size);
        file.close();
    } else {
        std::cerr << "Could not open font!\n";
        return;
    }

    hb_blob_t *blob = hb_blob_create(font_data, size, HB_MEMORY_MODE_WRITABLE, font_data, [](void* data) {
        delete[] static_cast<char*>(data);
    });
    hb_face_t *face = hb_face_create(blob, 0 /*face_index*/);
    hb_blob_destroy(blob);
    font_ = hb_font_create(face);

    // TODO: Font size
    upem_ = hb_face_get_upem(face);
    hb_font_set_scale(font_, upem_, upem_);

    hb_face_destroy(face);
    hb_ft_font_set_funcs(font_);
}

Shaping Font::shape(const std::u32string &text, const float spacing, const vec2<float> &translate) {
    Shaping shaping(translate.x * 24, translate.y * 24, text);

    // TODO: the y offset *should* be part of the font metadata
    // const int32_t yOffset = -17;

    if (!font_) return shaping;

    /*
    std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cv;
    std::cout << cv.to_bytes(text) << std::endl;
    */

    hb_buffer_reset(buffer_);
    hb_buffer_add_utf32(buffer_, reinterpret_cast<const uint32_t*>(text.c_str()), text.length(), 0, text.length());

    hb_buffer_set_direction(buffer_, HB_DIRECTION_RTL);
    // hb_buffer_set_direction(buffer, hb_direction_from_string (direction, -1));

    hb_buffer_set_script(buffer_, HB_SCRIPT_ARABIC);
    // hb_buffer_set_script(buffer, hb_script_from_string (script, -1));

    hb_buffer_set_language(buffer_, hb_language_from_string("ar", -1));
    // hb_buffer_set_language(buffer, hb_language_from_string (language, -1));

    hb_shape(font_, buffer_, 0 /*features*/, 0 /*num_features*/);

    int num_glyphs = hb_buffer_get_length(buffer_);
    hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos(buffer_, NULL);
    hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions(buffer_, NULL);

    // Loop through all characters of this label and shape.
    for (int i = 0; i < num_glyphs; i++) {
        uint32_t codepoint = hb_glyph[i].codepoint;
        shaping.positionedGlyphs.emplace_back(codepoint,
            hb_position[i].x_advance + spacing,
            hb_position[i].y_advance);

        /*
        std::cout << "glyph codepoint:" << hb_glyph[i].codepoint <<
                     " cluster: " << hb_glyph[i].cluster <<
                     " mask: " << hb_glyph[i].mask <<
                     " x_advance: "<< hb_position[i].x_advance << "\n";
                     // " y_advance: "<< hb_position[i].y_advance <<
                     // " x_offset: "<< hb_position[i].x_offset <<
                     // " y_offset: "<< hb_position[i].y_offset << "\n";
        // std::cout << "glyph:" << hb_glyph->codepoint << "\n";
        */
    }

    return shaping;

    /*
    auto hb_buffer_deleter = [](hb_buffer_t * buffer) { hb_buffer_destroy(buffer);};
    const std::unique_ptr<hb_buffer_t, decltype(hb_buffer_deleter)> buffer(hb_buffer_create(),hb_buffer_deleter);
    hb_buffer_pre_allocate(buffer.get(), string.length());

    mapnik::value_unicode_string const& text = itemizer.text();

    for (auto const& text_item : list)
    {
        face_set_ptr face_set = font_manager.get_face_set(text_item.format_->face_name, text_item.format_->fontset);
        double size = text_item.format_->text_size * scale_factor;
        face_set->set_unscaled_character_sizes();
        std::size_t num_faces = face_set->size();
        std::size_t pos = 0;
        font_feature_settings const& ff_settings = text_item.format_->ff_settings;
        int ff_count = safe_cast<int>(ff_settings.count());

        // rendering information for a single glyph
        struct glyph_face_info
        {
            face_ptr face;
            hb_glyph_info_t glyph;
            hb_glyph_position_t position;
        };
        // this table is filled with information for rendering each glyph, so that 
        // several font faces can be used in a single text_item
        std::vector<glyph_face_info> glyphinfos;
        unsigned valid_glyphs = 0;

        for (auto const& face : *face_set)
        {
            ++pos;
            hb_buffer_clear_contents(buffer.get());
            hb_buffer_add_utf16(buffer.get(), uchar_to_utf16(text.getBuffer()), text.length(), text_item.start, static_cast<int>(text_item.end - text_item.start));
            hb_buffer_set_direction(buffer.get(), (text_item.dir == UBIDI_RTL)?HB_DIRECTION_RTL:HB_DIRECTION_LTR);
            hb_buffer_set_script(buffer.get(), _icu_script_to_script(text_item.script));
            hb_font_t *font(hb_ft_font_create(face->get_face(), nullptr));
            // https://github.com/mapnik/test-data-visual/pull/25
            #if HB_VERSION_MAJOR > 0
             #if HB_VERSION_ATLEAST(1, 0 , 5)
            hb_ft_font_set_load_flags(font,FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
             #endif
            #endif
            hb_shape(font, buffer.get(), ff_settings.get_features(), ff_count);
            hb_font_destroy(font);

            unsigned num_glyphs = hb_buffer_get_length(buffer.get());

            // if the number of rendered glyphs has increased, we need to resize the table 
            if (num_glyphs > glyphinfos.size())
            {
                glyphinfos.resize(num_glyphs);
            }

            hb_glyph_info_t *glyphs = hb_buffer_get_glyph_infos(buffer.get(), nullptr);
            hb_glyph_position_t *positions = hb_buffer_get_glyph_positions(buffer.get(), nullptr);

            // Check if all glyphs are valid.
            for (unsigned i=0; i<num_glyphs; ++i)
            {
                // if we have a valid codepoint, save rendering info.
                if (glyphs[i].codepoint)
                {
                    if (!glyphinfos[i].glyph.codepoint)
                    {
                        ++valid_glyphs;
                    }
                    glyphinfos[i] = { face, glyphs[i], positions[i] };
                }
            }
            if (valid_glyphs < num_glyphs && (pos < num_faces))
            {
                //Try next font in fontset
                continue;
            }

            double max_glyph_height = 0;
            for (unsigned i=0; i<num_glyphs; ++i)
            {
                auto& gpos = positions[i];
                auto& glyph = glyphs[i];
                face_ptr theface = face;
                if (glyphinfos[i].glyph.codepoint)
                {
                    gpos = glyphinfos[i].position;
                    glyph = glyphinfos[i].glyph;
                    theface = glyphinfos[i].face;
                }
                unsigned char_index = glyph.cluster;
                glyph_info g(glyph.codepoint,char_index,text_item.format_);
                if (theface->glyph_dimensions(g))
                {
                    g.face = theface;
                    g.scale_multiplier = size / theface->get_face()->units_per_EM;
                    //Overwrite default advance with better value provided by HarfBuzz
                    g.unscaled_advance = gpos.x_advance;
                    g.offset.set(gpos.x_offset * g.scale_multiplier, gpos.y_offset * g.scale_multiplier);
                    double tmp_height = g.height();
                    if (tmp_height > max_glyph_height) max_glyph_height = tmp_height;
                    width_map[char_index] += g.advance();
                    line.add_glyph(std::move(g), scale_factor);
                }
            }
            line.update_max_char_height(max_glyph_height);
            break; //When we reach this point the current font had all glyphs.
        }
    }
    */
}

} // end namespace mbgl