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
// font-kit/src/canvas.rs
//
// Copyright © 2018 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! An in-memory bitmap surface for glyph rasterization.

use euclid::Size2D;
use std::cmp;

use utils;

lazy_static! {
    static ref BITMAP_1BPP_TO_8BPP_LUT: [[u8; 8]; 256] = {
        let mut lut = [[0; 8]; 256];
        for byte in 0..0x100 {
            let mut value = [0; 8];
            for bit in 0..8 {
                if (byte & (0x80 >> bit)) != 0 {
                    value[bit] = 0xff;
                }
            }
            lut[byte] = value
        }
        lut
    };
}

/// An in-memory bitmap surface for glyph rasterization.
pub struct Canvas {
    /// The raw pixel data.
    pub pixels: Vec<u8>,
    /// The size of the buffer, in pixels.
    pub size: Size2D<u32>,
    /// The number of *bytes* between successive rows.
    pub stride: usize,
    /// The image format of the canvas.
    pub format: Format,
}

impl Canvas {
    /// Creates a new blank canvas with the given pixel size and format.
    ///
    /// Stride is automatically calculated from width.
    ///
    /// The canvas is initialized with transparent black (all values 0).
    #[inline]
    pub fn new(size: &Size2D<u32>, format: Format) -> Canvas {
        Canvas::with_stride(size, size.width as usize * format.bytes_per_pixel() as usize, format)
    }

    /// Creates a new blank canvas with the given pixel size, stride (number of bytes between
    /// successive rows), and format.
    ///
    /// The canvas is initialized with transparent black (all values 0).
    pub fn with_stride(size: &Size2D<u32>, stride: usize, format: Format) -> Canvas {
        Canvas {
            pixels: vec![0; stride * size.height as usize],
            size: *size,
            stride,
            format,
        }
    }

    #[allow(dead_code)]
    pub(crate) fn blit_from_canvas(&mut self, src: &Canvas) {
        self.blit_from(&src.pixels, &src.size, src.stride, src.format)
    }

    #[allow(dead_code)]
    pub(crate) fn blit_from(&mut self,
                            src_bytes: &[u8],
                            src_size: &Size2D<u32>,
                            src_stride: usize,
                            src_format: Format) {
        let width = cmp::min(src_size.width as usize, self.size.width as usize);
        let height = cmp::min(src_size.height as usize, self.size.height as usize);
        let size = Size2D::new(width, height);

        match (self.format, src_format) {
            (Format::A8, Format::A8) |
            (Format::Rgb24, Format::Rgb24) |
            (Format::Rgba32, Format::Rgba32) => {
                self.blit_from_with::<BlitMemcpy>(src_bytes, &size, src_stride, src_format)
            }
            (Format::A8, Format::Rgb24) => {
                self.blit_from_with::<BlitRgb24ToA8>(src_bytes, &size, src_stride, src_format)
            }
            (Format::Rgb24, Format::Rgba32) => {
                self.blit_from_with::<BlitRgba32ToRgb24>(src_bytes, &size, src_stride, src_format)
            }
            (Format::Rgba32, Format::Rgb24) |
            (Format::Rgba32, Format::A8) |
            (Format::Rgb24, Format::A8) |
            (Format::A8, Format::Rgba32) => unimplemented!(),
        }
    }

    #[allow(dead_code)]
    pub(crate) fn blit_from_bitmap_1bpp(&mut self,
                                        src_bytes: &[u8],
                                        src_size: &Size2D<u32>,
                                        src_stride: usize) {
        if self.format != Format::A8 {
            unimplemented!()
        }

        let width = cmp::min(src_size.width as usize, self.size.width as usize);
        let height = cmp::min(src_size.height as usize, self.size.height as usize);
        let size = Size2D::new(width, height);

        let dest_bytes_per_pixel = self.format.bytes_per_pixel() as usize;
        let dest_row_stride = size.width * dest_bytes_per_pixel;
        let src_row_stride = utils::div_round_up(size.width, 8);

        for y in 0..size.height {
            let (dest_row_start, src_row_start) = (y * self.stride, y * src_stride);
            let dest_row_end = dest_row_start + dest_row_stride;
            let src_row_end = src_row_start + src_row_stride;
            let dest_row_pixels = &mut self.pixels[dest_row_start..dest_row_end];
            let src_row_pixels = &src_bytes[src_row_start..src_row_end];
            for x in 0..src_row_stride {
                let pattern = &BITMAP_1BPP_TO_8BPP_LUT[src_row_pixels[x] as usize];
                let dest_start = x * 8;
                let dest_end = cmp::min(dest_start + 8, dest_row_stride);
                let src = &pattern[0..(dest_end - dest_start)];
                dest_row_pixels[dest_start..dest_end].clone_from_slice(src);
            }
        }
    }

    fn blit_from_with<B>(&mut self,
                         src_bytes: &[u8],
                         size: &Size2D<usize>,
                         src_stride: usize,
                         src_format: Format)
                         where B: Blit {
        let src_bytes_per_pixel = src_format.bytes_per_pixel() as usize;
        let dest_bytes_per_pixel = self.format.bytes_per_pixel() as usize;

        for y in 0..size.height {
            let (dest_row_start, src_row_start) = (y * self.stride, y * src_stride);
            let dest_row_end = dest_row_start + size.width * dest_bytes_per_pixel;
            let src_row_end = src_row_start + size.width * src_bytes_per_pixel;
            let dest_row_pixels = &mut self.pixels[dest_row_start..dest_row_end];
            let src_row_pixels = &src_bytes[src_row_start..src_row_end];
            B::blit(dest_row_pixels, src_row_pixels)
        }
    }
}

/// The image format for the canvas.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Format {
    /// Premultiplied R8G8B8A8, little-endian.
    Rgba32,
    /// R8G8B8, little-endian.
    Rgb24,
    /// A8.
    A8,
}

impl Format {
    /// Returns the number of bits per pixel that this image format corresponds to.
    #[inline]
    pub fn bits_per_pixel(self) -> u8 {
        match self {
            Format::Rgba32 => 32,
            Format::Rgb24 => 24,
            Format::A8 => 8,
        }
    }

    /// Returns the number of color channels per pixel that this image format corresponds to.
    #[inline]
    pub fn components_per_pixel(self) -> u8 {
        match self {
            Format::Rgba32 => 4,
            Format::Rgb24 => 3,
            Format::A8 => 1,
        }
    }

    /// Returns the number of bits per color channel that this image format contains.
    #[inline]
    pub fn bits_per_component(self) -> u8 {
        self.bits_per_pixel() / self.components_per_pixel()
    }

    /// Returns the number of bytes per pixel that this image format corresponds to.
    #[inline]
    pub fn bytes_per_pixel(self) -> u8 {
        self.bits_per_pixel() / 8
    }
}

/// The antialiasing strategy that should be used when rasterizing glyphs.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RasterizationOptions {
    /// "Black-and-white" rendering. Each pixel is either entirely on or off.
    Bilevel,
    /// Grayscale antialiasing. Only one channel is used.
    GrayscaleAa,
    /// Subpixel RGB antialiasing, for LCD screens.
    SubpixelAa,
}

trait Blit {
    fn blit(dest: &mut [u8], src: &[u8]);
}

struct BlitMemcpy;

impl Blit for BlitMemcpy {
    #[inline]
    fn blit(dest: &mut [u8], src: &[u8]) {
        dest.clone_from_slice(src)
    }
}

struct BlitRgb24ToA8;

impl Blit for BlitRgb24ToA8 {
    #[inline]
    fn blit(dest: &mut [u8], src: &[u8]) {
        // TODO(pcwalton): SIMD.
        for (dest, src) in dest.iter_mut().zip(src.chunks(3)) {
            *dest = src[1]
        }
    }
}

struct BlitRgba32ToRgb24;

impl Blit for BlitRgba32ToRgb24 {
    #[inline]
    fn blit(dest: &mut [u8], src: &[u8]) {
        // TODO(pcwalton): SIMD.
        for (dest, src) in dest.chunks_mut(3).zip(src.chunks(4)) {
            dest.copy_from_slice(&src[0..3])
        }
    }
}