view libinterp/corefcn/txt-render.cc @ 17454:fd3e999305ea

Moved functionality from latex and ft renderer to base_text_renderer. * txt-render.cc/.h: Created three new methods: rotate_data, alignment and rotate_bounding_box. * txt-latex.cc/.h: Moved part of functionality to three new methods of base_text_render. * txt-eng-ft.cc/.h: Moved part of functionality to three new methods of base_text_render. To avoid multiple copy/paste code.
author Andrej Lojdl <andrej.lojdl@gmail.com>
date Tue, 17 Sep 2013 20:40:02 +0200
parents 4ee5b344a4e3
children 1647020d09ec
line wrap: on
line source

/*

   Copyright (C) 2013 Andrej Lojdl

   This file is part of Octave.

   Octave is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Octave 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 General Public License
   for more details.

   You should have received a copy of the GNU General Public License
   along with Octave; see the file COPYING.  If not, see
   <http://www.gnu.org/licenses/>.

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <dMatrix.h>
#include <uint8NDArray.h>
#include <string>
#include "txt-render.h"
#include "txt-latex.h"
#include "txt-eng-ft.h"
#include "oct-refcount.h"

dummy_render::dummy_render (void)
     : bbox (1, 4, 0.0),red (0),green (0),blue (0) {}
     dummy_render::~dummy_render (void) {}

void
dummy_render::set_font (const std::string& name, const std::string& weight,
                        const std::string& angle, double size)
{}

void
dummy_render::set_color (Matrix c) {}

void
dummy_render::text_to_pixels (const std::string& txt,
                              uint8NDArray& pixels, Matrix& bbox,
                              int halign, int valign, double rotation)
{
  pixels = uint8NDArray (dim_vector (4, 1, 1), static_cast<uint8_t> (0));
  bbox = Matrix (1, 4, 0.0);
  bbox(2) = pixels.dim2();
  bbox(3) = pixels.dim3();
}

Matrix
dummy_render::get_extent (text_element *elt, double rotation) {}


Matrix 
dummy_render::get_extent (const std::string& txt, double rotation) {}



base_text_render 
*text_render::make_text_render (const caseless_str& s)
{
  if (s.compare ("latex"))
#if ENABLE_LATEX
    rep = new latex_render ();
#else
  rep = new dummy_render ();
#endif
  else
    {
#if HAVE_FREETYPE
      rep = new ft_render (s);
#else
      rep = new dummy_render ();
#endif
    }
  return rep;
}

int
base_text_render::rotation_to_mode (double rotation) const
{
  if (rotation == 0.0)
    return ROTATION_0;
  else if (rotation == 90.0)
    return ROTATION_90;
  else if (rotation == 180.0)
    return ROTATION_180;
  else if (rotation == 270.0)
    return ROTATION_270;
  else
    return ROTATION_0;
}

uint8NDArray
base_text_render::rotate_data (int mode, uint8NDArray data)
{
  switch (mode)
        {
        case ROTATION_0:
          break;
        case ROTATION_90:
            {
              Array<octave_idx_type> perm (dim_vector (3, 1));
              perm(0) = 0;
              perm(1) = 2;
              perm(2) = 1;
              data = data.permute (perm);

              Array<idx_vector> idx (dim_vector (3, 1));
              idx(0) = idx_vector (':');
              idx(1) = idx_vector (data.dim2 ()-1, -1, -1);
              idx(2) = idx_vector (':');
              data = uint8NDArray (data.index (idx));
            }
          break;
        case ROTATION_180:
            {
              Array<idx_vector> idx (dim_vector (3, 1));
              idx(0) = idx_vector (':');
              idx(1) = idx_vector (data.dim2 ()-1, -1, -1);
              idx(2)=  idx_vector (data.dim3 ()-1, -1, -1);
              data = uint8NDArray (data.index (idx));
            }
          break;
        case ROTATION_270:
            {
              Array<octave_idx_type> perm (dim_vector (3, 1));
              perm(0) = 0;
              perm(1) = 2;
              perm(2) = 1;
              data = data.permute (perm);

              Array<idx_vector> idx (dim_vector (3, 1));
              idx(0) = idx_vector (':');
              idx(1) = idx_vector (':');
              idx(2) = idx_vector (data.dim3 ()-1, -1, -1);
              data = uint8NDArray (data.index (idx));
            }
          break;
        }
        return data;
}

Matrix
base_text_render::alignment (int halign, int valign, Matrix bounding_box)
{
  switch (halign)
      {
        default: bounding_box (0) = 0; break;
        case 1: bounding_box (0) = -bounding_box (2) / 2; break;
        case 2: bounding_box (0) = -bounding_box (2); break;
      }
      switch (valign)
      {
        default: bounding_box (1) = 0; break;
        case 1: bounding_box (1) = -bounding_box (3) / 2; break;
        case 2: bounding_box (1) = -bounding_box (3); break;
        case 3: break;
        case 4: bounding_box (1) = -bounding_box (3) - bounding_box (1); break;
      }
      
      return bounding_box;
}

Matrix 
base_text_render::rotate_bounding_box (int rotation, Matrix bounding_box)
{
   switch (rotation)
      {
      case ROTATION_90:
        std::swap (bounding_box (0), bounding_box (1));
        std::swap (bounding_box (2), bounding_box (3));
        bounding_box (0) = -bounding_box (0) - bounding_box (2);
        break;
      case ROTATION_180:
        bounding_box (0) = -bounding_box (0) - bounding_box (2);
        bounding_box (1) = -bounding_box (1) - bounding_box (3);
        break;
      case ROTATION_270:
        std::swap (bounding_box (0), bounding_box (1));
        std::swap (bounding_box (2), bounding_box (3));
        bounding_box (1) = -bounding_box (1) - bounding_box(3);
        break;
      }
      
      return bounding_box;
}