// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_sg_tools
#define tools_sg_tools

#include "separator"
#include "matrix"
#include "text_hershey"
#include "base_freetype"

#include <cstdio> //sscanf

namespace tools {
namespace sg {

inline void add_string(
 separator& a_sep
,const std::string& a_font
,font_modeling& a_font_modeling
,const std::string& a_encoding
,bool //a_smoothing
,const std::string& a_string
,float a_x,float a_y,float a_z
,const vec3f& a_X
,const vec3f& a_Y
,float a_size
,hjust a_hjust
,vjust a_vjust
,const base_freetype& a_ttf
){
  if(a_string.empty()) return;

  matrix* tsf = new matrix;

 {tsf->mul_translate(a_x,a_y,a_z);
  vec3f X = a_X;
  vec3f Y = a_Y;
  X.normalize();
  Y.normalize();
  vec3f Z;X.cross(Y,Z);
  Z.cross(X,Y);
  mat4f r(X.v0(),Y.v0(),Z.v0(),0, //first row
                 X.v1(),Y.v1(),Z.v1(),0,
                 X.v2(),Y.v2(),Z.v2(),0,
                 0,0,0,1);
  tsf->mul_mtx(r);
  tsf->mul_scale(a_size,a_size,1);} //applied first on GL

  a_sep.add(tsf);

  if(a_font==font_hershey()) {

    text_hershey* text = new text_hershey;
    text->encoding.value(a_encoding);
    text->strings.add(a_string);
    text->hjust.value(a_hjust);
    text->vjust.value(a_vjust);
    a_sep.add(text);

  } else {

    base_freetype* text = base_freetype::create(a_ttf);

    text->font = a_font;

    text->strings.add(a_string);
    text->hjust.value(a_hjust);
    text->vjust.value(a_vjust);
    text->modeling = a_font_modeling;
    a_sep.add(text);

  }

}

inline matrix* add_string_opt(
 separator& a_sep
,const std::string& a_font
,font_modeling a_font_modeling
,const std::string& a_encoding
,bool //a_smoothing
,const std::string& a_string
,float a_x,float a_y,float a_z
,mat4f& a_scale_rot
,hjust a_hjust
,vjust a_vjust
,const base_freetype& a_ttf
){
  //used in axis::update_sg()
  if(a_string.empty()) return 0;

  matrix* tsf = new matrix;
  tsf->mul_translate(a_x,a_y,a_z);
  tsf->mul_mtx(a_scale_rot);
  a_sep.add(tsf);

  if(a_font==font_hershey()) {

    text_hershey* text = new text_hershey;
    text->encoding.value(a_encoding);
    text->strings.add(a_string);
    text->hjust.value(a_hjust);
    text->vjust.value(a_vjust);
    a_sep.add(text);

  } else {

    std::string _s = a_string;
    if(a_encoding==encoding_PAW()) {
      int mag;
      if(::sscanf(a_string.c_str(),"10^%d?",&mag)==1) {
        _s[2] = 'e';
        _s = _s.substr(0,_s.size()-1);
      }
    }

    base_freetype* text = base_freetype::create(a_ttf);

    text->font = a_font;

    text->strings.add(_s);
    text->hjust.value(a_hjust);
    text->vjust.value(a_vjust);
    text->modeling = a_font_modeling;
    a_sep.add(text);

  }

  return tsf;
}

}}

#endif
