Skip to content

Commit

Permalink
add mapnik font stack, ref #17
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorris committed Mar 24, 2014
1 parent f4c4da2 commit c58cb13
Show file tree
Hide file tree
Showing 18 changed files with 1,648 additions and 5 deletions.
140 changes: 140 additions & 0 deletions src/face.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/text/face.hpp>
#include <mapnik/debug.hpp>

extern "C"
{
#include FT_GLYPH_H
}

namespace mapnik
{

font_face::font_face(FT_Face face)
: face_(face), dimension_cache_(), char_height_(0.0)
{
}

double font_face::get_char_height() const
{
if (char_height_ != 0.0) return char_height_;
glyph_info tmp;
tmp.glyph_index = FT_Get_Char_Index(face_, 'X');
glyph_dimensions(tmp);
char_height_ = tmp.height();
return char_height_;
}

bool font_face::set_character_sizes(double size)
{
char_height_ = 0.0;
return !FT_Set_Char_Size(face_,0,(FT_F26Dot6)(size * (1<<6)),0,0);
}

void font_face::glyph_dimensions(glyph_info & glyph) const
{
//TODO
//Check if char is already in cache
// std::map<glyph_index_t, glyph_info>::const_iterator itr;
// itr = dimension_cache_.find(glyph.glyph_index);
// if (itr != dimension_cache_.end()) {
// glyph = itr->second;
// return;
// }

FT_Vector pen;
pen.x = 0;
pen.y = 0;
/*
FT_Matrix matrix;
matrix.xx = (FT_Fixed)( 1 * 0x10000L );
matrix.xy = (FT_Fixed)( 0 * 0x10000L );
matrix.yx = (FT_Fixed)( 0 * 0x10000L );
matrix.yy = (FT_Fixed)( 1 * 0x10000L );
FT_Set_Transform(face_, &matrix, &pen);
*/
// TODO - any benefit to using a matrix here?
FT_Set_Transform(face_, 0, &pen);

if (FT_Load_Glyph (face_, glyph.glyph_index, FT_LOAD_NO_HINTING)) return;

FT_Glyph image;
if (FT_Get_Glyph(face_->glyph, &image)) return;
FT_BBox glyph_bbox;
FT_Glyph_Get_CBox(image, ft_glyph_bbox_pixels, &glyph_bbox);
FT_Done_Glyph(image);

glyph.ymin = glyph_bbox.yMin; //pixels!
glyph.ymax = glyph_bbox.yMax;
glyph.line_height = face_->size->metrics.height/64.0;
// TODO: we round to integers for now to maintain
// back compatibility with Mapnik 2.x
//glyph.width = face_->glyph->advance.x/64.0;
glyph.width = face_->glyph->advance.x >> 6;

//TODO: dimension_cache_.insert(std::pair<unsigned, char_info>(c, dim));
}

font_face::~font_face()
{
MAPNIK_LOG_DEBUG(font_face) <<
"font_face: Clean up face \"" << family_name() <<
" " << style_name() << "\"";

FT_Done_Face(face_);
}

/******************************************************************************/

void font_face_set::add(face_ptr face)
{
faces_.push_back(face);
}

void font_face_set::set_character_sizes(double size)
{
for (face_ptr const& face : faces_)
{
face->set_character_sizes(size);
}
}

/******************************************************************************/

void stroker::init(double radius)
{
FT_Stroker_Set(s_, (FT_Fixed) (radius * (1<<6)),
FT_STROKER_LINECAP_ROUND,
FT_STROKER_LINEJOIN_ROUND,
0);
}

stroker::~stroker()
{
MAPNIK_LOG_DEBUG(font_engine_freetype) << "stroker: Destroy stroker=" << s_;

FT_Stroker_Done(s_);
}

}//ns mapnik
111 changes: 111 additions & 0 deletions src/face.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_FACE_HPP
#define MAPNIK_FACE_HPP

//mapnik
#include "glyph_info.hpp"
#include "noncopyable.hpp"

// freetype2
extern "C"
{
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_STROKER_H
}

//stl
#include <map>
#include <memory>
#include <string>
#include <vector>

class font_face : noncopyable
{
public:
font_face(FT_Face face);

std::string family_name() const
{
return std::string(face_->family_name);
}

std::string style_name() const
{
return std::string(face_->style_name);
}

FT_Face get_face() const
{
return face_;
}

double get_char_height() const;

bool set_character_sizes(double size);

void glyph_dimensions(glyph_info &glyph) const;

~font_face();

private:
FT_Face face_;
mutable std::map<glyph_index_t, glyph_info> dimension_cache_;
mutable double char_height_;
};
typedef std::shared_ptr<font_face> face_ptr;


class font_face_set : private noncopyable
{
public:
typedef std::vector<face_ptr>::iterator iterator;
font_face_set(void) : faces_(){}

void add(face_ptr face);
void set_character_sizes(double size);

unsigned size() const { return faces_.size(); }
iterator begin() { return faces_.begin(); }
iterator end() { return faces_.end(); }
private:
std::vector<face_ptr> faces_;
};
typedef std::shared_ptr<font_face_set> face_set_ptr;


// FT_Stroker wrapper
class stroker : noncopyable
{
public:
explicit stroker(FT_Stroker s)
: s_(s) {}
~stroker();

void init(double radius);
FT_Stroker const& get() const { return s_; }
private:
FT_Stroker s_;
};

#endif // FACE_HPP
Loading

0 comments on commit c58cb13

Please sign in to comment.