2017-01-09 22:49:00 -05:00
|
|
|
// Copyright 2017 The Servo Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-01-10 16:37:01 -05:00
|
|
|
use euclid::{Point2D, Rect, Size2D};
|
2017-01-24 01:03:40 -05:00
|
|
|
use gl::types::{GLsizeiptr, GLuint};
|
|
|
|
use gl;
|
2017-01-26 13:27:38 -05:00
|
|
|
use otf::Font;
|
2017-01-24 01:03:40 -05:00
|
|
|
use std::mem;
|
|
|
|
use std::os::raw::c_void;
|
2017-01-09 22:49:00 -05:00
|
|
|
|
2017-01-24 18:30:14 -05:00
|
|
|
static DUMMY_VERTEX: Vertex = Vertex {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
glyph_index: 0,
|
|
|
|
};
|
2017-01-24 01:03:40 -05:00
|
|
|
|
2017-01-24 18:30:14 -05:00
|
|
|
pub struct GlyphBufferBuilder {
|
|
|
|
pub vertices: Vec<Vertex>,
|
|
|
|
pub indices: Vec<u32>,
|
2017-01-09 22:49:00 -05:00
|
|
|
pub descriptors: Vec<GlyphDescriptor>,
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:37:01 -05:00
|
|
|
impl GlyphBufferBuilder {
|
2017-01-09 22:49:00 -05:00
|
|
|
#[inline]
|
2017-01-10 16:37:01 -05:00
|
|
|
pub fn new() -> GlyphBufferBuilder {
|
|
|
|
GlyphBufferBuilder {
|
2017-01-24 18:30:14 -05:00
|
|
|
vertices: vec![DUMMY_VERTEX],
|
|
|
|
indices: vec![],
|
2017-01-09 22:49:00 -05:00
|
|
|
descriptors: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:53:50 -05:00
|
|
|
pub fn add_glyph(&mut self, font: &Font, glyph_id: u16) -> Result<(), ()> {
|
2017-01-24 01:03:40 -05:00
|
|
|
let glyph_index = self.descriptors.len() as u16;
|
|
|
|
|
2017-01-24 18:30:14 -05:00
|
|
|
let mut point_index = self.vertices.len() as u32;
|
|
|
|
let start_index = self.indices.len() as u32;
|
2017-01-10 22:57:08 -05:00
|
|
|
let start_point = point_index;
|
2017-01-24 18:30:14 -05:00
|
|
|
let mut last_point_on_curve = true;
|
2017-01-09 22:49:00 -05:00
|
|
|
|
2017-01-26 13:27:38 -05:00
|
|
|
let glyf_table = try!(font.glyf.ok_or(()));
|
|
|
|
let loca_table = try!(font.loca.as_ref().ok_or(()));
|
|
|
|
|
|
|
|
try!(glyf_table.for_each_point(&font.head, loca_table, glyph_id, |point| {
|
2017-01-24 18:30:14 -05:00
|
|
|
self.vertices.push(Vertex {
|
|
|
|
x: point.position.x,
|
|
|
|
y: point.position.y,
|
|
|
|
glyph_index: glyph_index,
|
|
|
|
});
|
|
|
|
|
|
|
|
if !point.first_point_in_contour && point.on_curve {
|
|
|
|
let indices = if last_point_on_curve {
|
2017-01-25 14:52:18 -05:00
|
|
|
[point_index - 1, 0, point_index]
|
2017-01-24 18:30:14 -05:00
|
|
|
} else {
|
2017-01-25 14:52:18 -05:00
|
|
|
[point_index - 2, point_index - 1, point_index]
|
2017-01-24 18:30:14 -05:00
|
|
|
};
|
|
|
|
self.indices.extend(indices.iter().cloned());
|
|
|
|
}
|
2017-01-09 22:49:00 -05:00
|
|
|
|
|
|
|
point_index += 1;
|
2017-01-24 18:30:14 -05:00
|
|
|
last_point_on_curve = point.on_curve
|
2017-01-09 22:49:00 -05:00
|
|
|
}));
|
|
|
|
|
2017-01-24 01:03:40 -05:00
|
|
|
// Add a glyph descriptor.
|
2017-01-10 22:57:08 -05:00
|
|
|
self.descriptors.push(GlyphDescriptor {
|
2017-01-26 23:56:14 -05:00
|
|
|
bounds: try!(glyf_table.glyph_bounds(&font.head, loca_table, glyph_id)),
|
2017-01-26 13:27:38 -05:00
|
|
|
units_per_em: font.head.units_per_em as u32,
|
2017-01-10 22:57:08 -05:00
|
|
|
start_point: start_point as u32,
|
2017-01-24 18:30:14 -05:00
|
|
|
start_index: start_index,
|
2017-01-26 21:53:50 -05:00
|
|
|
glyph_id: glyph_id,
|
2017-01-10 22:57:08 -05:00
|
|
|
});
|
2017-01-10 16:37:01 -05:00
|
|
|
|
2017-01-09 22:49:00 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-10 16:37:01 -05:00
|
|
|
|
2017-01-26 23:56:14 -05:00
|
|
|
/// Returns the glyph rectangle in units.
|
|
|
|
#[inline]
|
|
|
|
pub fn glyph_bounds(&self, glyph_index: u32) -> GlyphBounds {
|
|
|
|
self.descriptors[glyph_index as usize].bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_buffers(&self) -> Result<GlyphBuffers, ()> {
|
2017-01-24 18:30:14 -05:00
|
|
|
// TODO(pcwalton): Try using `glMapBuffer` here. Requires precomputing contour types and
|
|
|
|
// counts.
|
2017-01-24 01:03:40 -05:00
|
|
|
unsafe {
|
2017-01-24 18:30:14 -05:00
|
|
|
let (mut vertices, mut indices, mut descriptors) = (0, 0, 0);
|
|
|
|
gl::GenBuffers(1, &mut vertices);
|
|
|
|
gl::GenBuffers(1, &mut indices);
|
2017-01-24 01:03:40 -05:00
|
|
|
gl::GenBuffers(1, &mut descriptors);
|
|
|
|
|
2017-01-24 18:30:14 -05:00
|
|
|
gl::BindBuffer(gl::ARRAY_BUFFER, vertices);
|
2017-01-24 01:03:40 -05:00
|
|
|
gl::BufferData(gl::ARRAY_BUFFER,
|
2017-01-24 18:30:14 -05:00
|
|
|
(self.vertices.len() * mem::size_of::<Vertex>()) as GLsizeiptr,
|
|
|
|
self.vertices.as_ptr() as *const Vertex as *const c_void,
|
|
|
|
gl::STATIC_DRAW);
|
|
|
|
|
|
|
|
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, indices);
|
|
|
|
gl::BufferData(gl::ELEMENT_ARRAY_BUFFER,
|
|
|
|
(self.indices.len() * mem::size_of::<u32>()) as GLsizeiptr,
|
|
|
|
self.indices.as_ptr() as *const u32 as *const c_void,
|
2017-01-24 01:03:40 -05:00
|
|
|
gl::STATIC_DRAW);
|
|
|
|
|
|
|
|
let length = self.descriptors.len() * mem::size_of::<GlyphDescriptor>();
|
|
|
|
gl::BindBuffer(gl::UNIFORM_BUFFER, descriptors);
|
|
|
|
gl::BufferData(gl::UNIFORM_BUFFER,
|
|
|
|
length as GLsizeiptr,
|
|
|
|
self.descriptors.as_ptr() as *const GlyphDescriptor as *const c_void,
|
|
|
|
gl::STATIC_DRAW);
|
|
|
|
|
|
|
|
Ok(GlyphBuffers {
|
2017-01-24 18:30:14 -05:00
|
|
|
vertices: vertices,
|
|
|
|
indices: indices,
|
2017-01-24 01:03:40 -05:00
|
|
|
descriptors: descriptors,
|
|
|
|
})
|
|
|
|
}
|
2017-01-10 16:37:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GlyphBuffers {
|
2017-01-24 18:30:14 -05:00
|
|
|
pub vertices: GLuint,
|
|
|
|
pub indices: GLuint,
|
2017-01-24 01:03:40 -05:00
|
|
|
pub descriptors: GLuint,
|
2017-01-27 14:31:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for GlyphBuffers {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
gl::DeleteBuffers(1, &mut self.descriptors);
|
|
|
|
gl::DeleteBuffers(1, &mut self.indices);
|
|
|
|
gl::DeleteBuffers(1, &mut self.vertices);
|
|
|
|
}
|
|
|
|
}
|
2017-01-09 22:49:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2017-01-25 14:52:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2017-01-09 22:49:00 -05:00
|
|
|
pub struct GlyphDescriptor {
|
2017-01-26 23:56:14 -05:00
|
|
|
pub bounds: GlyphBounds,
|
2017-01-24 01:03:40 -05:00
|
|
|
pub units_per_em: u32,
|
2017-01-10 16:37:01 -05:00
|
|
|
pub start_point: u32,
|
2017-01-24 18:30:14 -05:00
|
|
|
pub start_index: u32,
|
2017-01-26 21:53:50 -05:00
|
|
|
pub glyph_id: u16,
|
2017-01-10 16:37:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GlyphDescriptor {
|
|
|
|
#[inline]
|
|
|
|
pub fn pixel_rect(&self, point_size: f32) -> Rect<f32> {
|
|
|
|
let pixels_per_unit = point_size / self.units_per_em as f32;
|
2017-01-26 23:56:14 -05:00
|
|
|
Rect::new(Point2D::new(self.bounds.left as f32, self.bounds.bottom as f32),
|
|
|
|
Size2D::new((self.bounds.right - self.bounds.left) as f32,
|
|
|
|
(self.bounds.top - self.bounds.bottom) as f32)) * pixels_per_unit
|
2017-01-10 16:37:01 -05:00
|
|
|
}
|
2017-01-09 22:49:00 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 14:52:18 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2017-01-24 18:30:14 -05:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct Vertex {
|
|
|
|
x: i16,
|
|
|
|
y: i16,
|
|
|
|
/// TODO(pcwalton): Try omitting this and binary search the glyph descriptors in the vertex
|
|
|
|
/// shader. Might or might not help.
|
|
|
|
glyph_index: u16,
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:56:14 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct GlyphBounds {
|
|
|
|
pub left: i32,
|
|
|
|
pub bottom: i32,
|
|
|
|
pub right: i32,
|
|
|
|
pub top: i32,
|
|
|
|
}
|
|
|
|
|