// *************************************************************************************
// TTF.cpp
//
// True type (*.ttf) and OpenType (*.otf) font loading/rendering implementation.
//
// Ryan Bogean
// April 28, 2012
//
// *************************************************************************************
#include "TTF.h"
#include <vector>
#include <map>
#include <fstream>
#include <algorithm>
#include <limits>
#include <utility>
#include <cmath>
#include <sstream>
using namespace Utility;
using namespace TTFCore;
// ---------------------------------------------------------------------------------------------------------------------------
// exceptions
// ---------------------------------------------------------------------------------------------------------------------------
// ----- FontException -----
FontException::FontException () : msg("Unknown error message.") {}
FontException::FontException (const char* msg_) : msg(msg_) {}
FontException::FontException (const std::string& msg_) : msg(msg_) {}
const char* FontException::what() const throw() { return msg.c_str(); }
// ----- FileFailure -----
FileFailure::FileFailure (const std::string& flnm) {
std::stringstream ss;
ss << "Unknown error reading file: '" << flnm << "'.";
msg = ss.str();
}
// ----- FileLengthError -----
FileLengthError::FileLengthError () : FontException("Length error reading memory mapped file, file has size of 0.") {}
FileLengthError::FileLengthError (const std::string& flnm) {
std::stringstream ss;
ss << "Length error reading file: '" << flnm << "', file has size of 0.";
msg = ss.str();
}
// ----- TableDoesNotExist -----
TableDoesNotExist::TableDoesNotExist (const std::string& table) {
std::stringstream ss;
ss << "Cannot load font, '" << table << "' table does not exist.";
msg = ss.str();
}
// ----- ChecksumException -----
ChecksumException::ChecksumException (const std::string& table) {
std::stringstream ss;
ss << "Check sum error in '" << table << "'.";
msg = ss.str();
}
// ----- VersionException -----
VersionException::VersionException (const std::string& msg) : FontException(msg) {}
// ----- InvalidFontException -----
InvalidFontException::InvalidFontException (const std::string& msg) : FontException(msg) {}
// ----- UnsupportedCap -----
UnsupportedCap::UnsupportedCap (const std::string& msg) : FontException(msg) {}
// ---------------------------------------------------------------------------------------------------------------------------
// Simple math 'library'
// ---------------------------------------------------------------------------------------------------------------------------
// ----- vec2f -----
vec2f::vec2f () {}
vec2f::vec2f (float x_, float y_) : x(x_), y(y_) {}
bool vec2f::operator== (vec2f v) const { return x == v.x && y == v.y; }
bool vec2f::operator!= (vec2f v) const { return x != v.x || y != v.y; }
vec2f& vec2f::operator+= (vec2f v) {
x += v.x;
y += v.y;
return *this;
}
vec2f& vec2f::operator-= (vec2f v) {
x -= v.x;
y -= v.y;
return *this;
}
vec2f& vec2f::operator*= (vec2f v) {
x *= v.x;
y *= v.y;
return *this;
}
vec2f& vec2f::operator/= (vec2f v) {
x /= v.x;
y /= v.y;
return *this;
}
vec2f& vec2f::operator+= (float f) {
x += f;
y += f;
return *this;
}
vec2f& vec2f::operator-= (float f) {
x -= f;
y -= f;
return *this;
}
vec2f& vec2f::operator*= (float f) {
x *= f;
y *= f;
return *this;
}
vec2f& vec2f::operator/= (float f) {
x /= f;
y /= f;
return *this;
}
vec2f Utility::TTFCore::operator+ (vec2f v0, vec2f v1) {
return vec2f(v0.x + v1.x, v0.y + v1.y);
}
vec2f Utility::TTFCore::operator- (vec2f v0, vec2f v1) {
return vec2f(v0.x - v1.x, v0.y - v1.y);
}
vec2f Utility::TTFCore::operator* (vec2f v0, vec2f v1) {
return vec2f(v0.x * v1.x, v0.y * v1.y);
}
vec2f Utility::TTFCore::operator/ (vec2f v0, vec2f v1) {
return vec2f(v0.x / v1.x, v0.y / v1.y);
}
vec2f Utility::TTFCore::operator+ (float f, vec2f v1) {
return vec2f(f + v1.x, f + v1.y);
}
vec2f Utility::TTFCore::operator- (float f, vec2f v1) {
return vec2f(f - v1.x, f - v1.y);
}
vec2f Utility::TTFCore::operator* (float f, vec2f v1) {
return vec2f(f * v1.x, f * v1.y);
}
vec2f Utility::TTFCore::operator/ (float f, vec2f v1) {
return vec2f(f / v1.x, f / v1.y);
}
vec2f Utility::TTFCore::operator+ (vec2f v0, float f) {
return vec2f(v0.x + f, v0.y + f);
}
vec2f Utility::TTFCore::operator- (vec2f v0, float f) {
return vec2f(v0.x - f, v0.y - f);
}
vec2f Utility::TTFCore::operator* (vec2f v0, float f) {
return vec2f(v0.x * f, v0.y * f);
}
vec2f Utility::TTFCore::operator/ (vec2f v0, float f) {
return vec2f(v0.x / f, v0.y / f);
}
float Utility::TTFCore::dot (vec2f v0, vec2f v1) {
return (v0.x*v1.x) + (v0.y*v1.y);
}
float Utility::TTFCore::cross (vec2f v0, vec2f v1) {
return (v0.x*v1.y) - (v0.y*v1.x);
}
// ----- matrix3x2f -----
matrix3x2f::matrix3x2f () : a(1.0f), b(0.0f), c(0.0f), d(1.0f), e(0.0f), f(0.0f) {}
vec2f Utility::TTFCore::mul (vec2f v, const matrix3x2f& m) {
// obviously you can't multiply a 2d vector with a 3x2 matrix
// in this case we 'extend' the 2d vector to a 3d vector by appending 1.0f
// course this need'nt be done explicitly
return vec2f( v.x*m.a + v.y*m.c + m.e , v.x*m.b + v.y*m.d + m.f );
}
vec2f Utility::TTFCore::normal (vec2f v) {
return v / std::sqrt(dot(v,v));
}
// ----- vec4f -----
vec4f::vec4f () {}
vec4f::vec4f (float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
// ---------------------------------------------------------------------------------------------------------------------------
// Triangulator
// ---------------------------------------------------------------------------------------------------------------------------
// ----- Triangulator internal types -----
ContourPoint::ContourPoint () {
endPoint = false;
flags = 0;
pos = vec2f(0,0);
}
bool ContourPoint::OnCurve () const {
return (flags & 1) != 0;
}
bool ContourPoint::XShortVector () const {
return (flags & 2) != 0;
}
bool ContourPoint::YShortVector () const {
return (flags & 4) != 0;
}
bool ContourPoint::XIsSame () const {
return (flags & 16) != 0;
}
bool ContourPoint::XIsDifferent () const {
return (flags & 16) == 0;
}
bool ContourPoint::XIsPositive () const {
return (flags & 16) != 0;
}
bool ContourPoint::XIsNegative () const {
return (flags & 16) == 0;
}
bool ContourPoint::YIsSame () const {
return (flags & 32) != 0;
}
bool ContourPoint::YIsDifferent () const {
return (flags & 32) == 0;
}
bool ContourPoint::YIsPositive () const {
return (flags & 32) != 0;
}
bool ContourPoint::YIsNegative () const {
return (flags & 32) == 0;
}
Triangle::Triangle (size_t v0_, size_t v1_, size_t v2_, char coef_) {
v0 = v0_;
v1 = v1_;
v2 = v2_;
coef = coef_;
}
Edge::Edge (size_t v0_, size_t v1_) {
v0 = v0_;
v1 = v1_;
}
LineSegment::LineSegment (size_t v0_, size_t v1_, float length_) {
v0 = v0_;
v1 = v1_;
length = length_;
}
Bound::Bound (vec2f p_, vec2f d_) : p(p_), d(d_) {}
TriEdge::TriEdge (size_t v0_, size_t v1_) : v0(v0_), v1(v1_), inUse(true) {}
void Mesh::Clear () { verts.clear(); }
std::vector<MeshVertex>::iterator Mesh::begin () { return verts.begin(); }
std::vector<MeshVertex>::const_iterator Mesh::begin () const { return verts.begin(); }
std::vector<MeshVertex>::iterator Mesh::end () { return verts.end(); }
std::vector<MeshVertex>::const_iterator Mesh::end () const { return verts.end(); }
// ----- internal helper functions -----
void Triangulator::ClearCache () {
verts.clear();
edges.clear();
tris.clear();
seg.clear();
bounds.cle
Truetype格式解析类
需积分: 50 78 浏览量
2014-04-11
13:58:27
上传
评论 1
收藏 42KB RAR 举报
photowen
- 粉丝: 11
- 资源: 4
最新资源
- Python实现2048小游戏源码
- 基于spark的心脏病信息大数据分析源代码+数据(高分期末大作业).zip
- 基于spark的心脏病信息大数据分析源代码+数据(高分毕业设计).zip
- 《大数据原理》LSH算法实现
- 基于 Canvas 开发的 2d JavaScript框架库, 它可以轻松的实现桌面应用和移动应用中的图形交互交互效果
- 285699177712363PDF电子版.zip
- 基于JavaScript的批量图片裁剪插件
- 基于YOLOv8的蔬菜识别源代码+项目说明+数据集(高分项目).zip
- 基于风控模型的银行客户信用风险评估系统源码+pdf说明+数据集(高分项目).zip
- cn-excelserver2023-x64.exe
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈