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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::mem;
use std::sync::{Arc, Weak};

use image;
use image::{Rgba, RgbaImage};
use rusttype;
use rusttype::{point, vector};
use smallvec::SmallVec;

use crate::atlas::*;
use crate::draw::*;
use crate::layout::Rectangle;
use crate::text::Text;

type GlyphCache = rusttype::gpu_cache::Cache<'static>;
pub(crate) type Font = rusttype::Font<'static>;
pub(crate) type FontId = usize;

/// A cache for textures and text
pub struct Cache {
    #[allow(unused)]
    size: usize,
    glyphs: GlyphCache,
    textures: Vec<TextureSlot>,
    textures_offset: usize,
    updates: Vec<Update>,
    font_id_counter: usize,
    image_id_counter: usize,
}

enum TextureSlot {
    Atlas(Atlas<Weak<usize>>),
    Big,
}

impl Cache {
    /// Create a new cache. Size is the width and height of textures in pixels.
    /// Offset is the offset to apply to texture ids
    pub fn new(size: usize, offset: usize) -> Cache {
        let glyphs = GlyphCache::builder().dimensions(size as u32, size as u32).build();

        let atlas = Atlas::new(size);

        Cache {
            size,
            glyphs,
            textures: vec![
                // glyph cache
                TextureSlot::Big,
                // atlas for textures
                TextureSlot::Atlas(atlas),
            ],
            textures_offset: offset,
            updates: vec![
                // glyph cache
                Update::Texture {
                    id: offset,
                    size: [size as u32, size as u32],
                    data: Vec::new(),
                    atlas: true,
                },
                // atlas for textures
                Update::Texture {
                    id: offset + 1,
                    size: [size as u32, size as u32],
                    data: Vec::new(),
                    atlas: true,
                },
            ],
            font_id_counter: 0,
            image_id_counter: 1,
        }
    }

    /// Take updates for the texture system from the cache
    pub fn take_updates(&mut self) -> Vec<Update> {
        mem::replace(&mut self.updates, Vec::new())
    }

    pub(crate) fn draw_text<F: FnMut(Rectangle, Rectangle)>(
        &mut self,
        text: &Text,
        rect: Rectangle,
        mut place_glyph: F,
    ) {
        let start = point(rect.left, rect.top);

        let mut placed_glyphs = Vec::with_capacity(text.text.len());
        text.layout(rect, |g, x, _, y| {
            placed_glyphs.push(g.positioned(start + vector(x, y)));
        });

        for g in placed_glyphs.iter() {
            self.glyphs.queue_glyph(text.font.id as usize, g.clone());
        }

        let textures_offset = self.textures_offset;

        let updates = &mut self.updates;
        self.glyphs
            .cache_queued(|rect, data| {
                let mut new_data = Vec::with_capacity(data.len() * 4);
                for x in data {
                    new_data.push(255);
                    new_data.push(255);
                    new_data.push(255);
                    new_data.push(*x);
                }

                let update = Update::TextureSubresource {
                    id: textures_offset,
                    offset: [rect.min.x, rect.min.y],
                    size: [rect.width(), rect.height()],
                    data: new_data,
                };

                updates.push(update);
            })
            .unwrap();

        for g in placed_glyphs.iter() {
            self.glyphs
                .rect_for(text.font.id as usize, g)
                .unwrap()
                .map(|(uv, pos)| {
                    place_glyph(
                        Rectangle {
                            left: uv.min.x,
                            top: uv.min.y,
                            right: uv.max.x,
                            bottom: uv.max.y,
                        },
                        Rectangle {
                            left: pos.min.x as f32,
                            top: pos.min.y as f32,
                            right: pos.max.x as f32,
                            bottom: pos.max.y as f32,
                        },
                    );
                });
        }
    }

    pub(crate) fn load_image(&mut self, image: RgbaImage) -> Image {
        let size = Rectangle {
            left: 0.0,
            top: 0.0,
            right: image.width() as f32,
            bottom: image.height() as f32,
        };
        let (texture, cache_id, texcoords) = self.insert_image(image);
        Image {
            texture,
            cache_id,
            texcoords,
            size,
        }
    }

    pub(crate) fn load_patch(&mut self, mut image: RgbaImage) -> Patch {
        // find 9 patch borders in image data
        let black = Rgba([0u8, 0u8, 0u8, 255u8]);

        let mut h_stretch = SmallVec::<[(f32, f32); 2]>::new();
        let mut h_content = (1.0, 0.0);
        let mut v_stretch = SmallVec::<[(f32, f32); 2]>::new();
        let mut v_content = (1.0, 0.0);
        let mut h_current_stretch = None;
        let mut v_current_stretch = None;

        // scan horizontal stretch and content bars
        for x in 1..image.width() - 1 {
            let h_begin = (x - 1) as f32 / (image.width() - 2) as f32;
            let h_end = (x) as f32 / (image.width() - 2) as f32;

            // check stretch pixel
            if image[(x, 0)] == black {
                h_current_stretch = Some(h_current_stretch.map_or_else(|| (h_begin, h_end), |(s, _)| (s, h_end)));
            } else {
                h_current_stretch.take().map(|s| h_stretch.push(s));
            }

            // check content pixel
            if image[(x, image.height() - 1)] == black {
                h_content.0 = h_begin.min(h_content.0);
                h_content.1 = h_end.max(h_content.1);
            }
        }

        // scan vertical stretch and content bars
        for y in 1..image.height() - 1 {
            let v_begin = (y - 1) as f32 / (image.height() - 2) as f32;
            let v_end = (y) as f32 / (image.height() - 2) as f32;

            // check stretch pixel
            if image[(0, y)] == black {
                v_current_stretch = Some(v_current_stretch.map_or_else(|| (v_begin, v_end), |(s, _)| (s, v_end)));
            } else {
                v_current_stretch.take().map(|s| v_stretch.push(s));
            }

            // check content pixel
            if image[(image.width() - 1, y)] == black {
                v_content.0 = v_begin.min(v_content.0);
                v_content.1 = v_end.max(v_content.1);
            }
        }

        h_current_stretch.take().map(|s| h_stretch.push(s));
        v_current_stretch.take().map(|s| v_stretch.push(s));

        // strip stretch and content bars from the image
        let patch_width = image.width() - 2;
        let patch_height = image.height() - 2;
        let image = image::imageops::crop(&mut image, 1, 1, patch_width, patch_height).to_image();
        let size = Rectangle {
            left: 0.0,
            top: 0.0,
            right: image.width() as f32,
            bottom: image.height() as f32,
        };
        let (texture, cache_id, texcoords) = self.insert_image(image);

        Patch {
            image: Image {
                texture,
                cache_id,
                texcoords,
                size,
            },
            h_stretch,
            v_stretch,
            h_content,
            v_content,
        }
    }

    pub(crate) fn load_font<D: Into<Vec<u8>>>(&mut self, data: D) -> crate::text::Font {
        let inner = Font::try_from_vec(data.into()).expect("error loading font");

        let id = self.font_id_counter;
        self.font_id_counter += 1;

        crate::text::Font {
            inner,
            id,
            tex_slot: self.textures_offset,
        }
    }

    fn insert_image(&mut self, image: image::RgbaImage) -> (usize, Arc<usize>, Rectangle) {
        for slot in self.textures.iter_mut() {
            if let &mut TextureSlot::Atlas(ref mut atlas) = slot {
                atlas.remove_expired();
            }
        }

        let image_id = Arc::new(self.image_id_counter);
        self.image_id_counter += 1;

        let slot = self
            .textures
            .iter_mut()
            .enumerate()
            .filter_map(|(index, slot)| match slot {
                &mut TextureSlot::Atlas(ref mut atlas) => {
                    let image_size = image.width().max(image.height()) as usize;
                    atlas
                        .insert(Arc::downgrade(&image_id), image_size)
                        .ok()
                        .map(|area| (area, atlas.size() as f32, index))
                }
                &mut TextureSlot::Big => None,
            })
            .next();

        if let Some((mut area, atlas_size, tex_id)) = slot {
            area.right = area.left + image.width() as usize;
            area.bottom = area.top + image.height() as usize;

            let update = Update::TextureSubresource {
                id: tex_id + self.textures_offset,
                offset: [area.left as u32, area.top as u32],
                size: [image.width(), image.height()],
                data: image.to_vec(),
            };
            self.updates.push(update);

            (
                self.textures_offset + tex_id,
                image_id,
                Rectangle {
                    left: area.left as f32 / atlas_size,
                    top: area.top as f32 / atlas_size,
                    right: area.right as f32 / atlas_size,
                    bottom: area.bottom as f32 / atlas_size,
                },
            )
        } else {
            let tex_id = self.textures_offset + self.textures.len();

            let update = Update::Texture {
                id: tex_id,
                size: [image.width(), image.height()],
                data: image.to_vec(),
                atlas: false,
            };

            self.updates.push(update);
            self.textures.push(TextureSlot::Big);

            (tex_id, image_id, Rectangle::from_wh(1.0, 1.0))
        }
    }
}