summaryrefslogtreecommitdiffstats
path: root/task1/cairodocument.cpp
blob: fb9b20e71c6e59a2be0da2e64a37fb4fd144e37d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * $Id: cairodocument.cpp 2 2009-10-31 02:48:23Z l0728348 $
 *
 * Copyright 2009
 *
 * @author Manuel Mausz (0728348)
 * @brief  implements some classes to better use cairomm.
 *         however, the design is however crappy.
 *         maybe try using papyrus.
 */

#include "cairodocument.h"

using namespace std;
using namespace Cairo;

static const double mmpt = 25.4/72;

void CairoObject::apply(const Cairo::RefPtr<Cairo::Context> context)
{
  if (m_objects.size())
    context->save();
  prepareObject(context);
  for(CairoObjectList::iterator it = m_objects.begin(); it != m_objects.end(); ++it)
    (*it)->apply(context);
  applyObject(context);
  if (m_objects.size())
    context->restore();
}

/*----------------------------------------------------------------------------*/

void CairoFont::applyObject(const Cairo::RefPtr<Cairo::Context> context)
{
  context->set_font_size(m_size);
  context->select_font_face(m_family, m_slant, m_weight);
}
/*----------------------------------------------------------------------------*/

double CairoFont::height()
{
  if (m_height >= 0)
    return m_height;
  Cairo::RefPtr<Cairo::Surface> surface = Cairo::ImageSurface::create(FORMAT_RGB24, 1, 1);
  Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create(surface);
  Cairo::FontExtents fextents;
  applyObject(context);
  context->get_font_extents(fextents);
  m_height = fextents.height * mmpt;
  return m_height;
}

/*----------------------------------------------------------------------------*/

void CairoColor::applyObject(const Cairo::RefPtr<Cairo::Context> context)
{
  context->set_source_rgba(m_red, m_green, m_blue, m_alpha);
}

/*----------------------------------------------------------------------------*/

void CairoRectangle::applyObject(const Cairo::RefPtr<Cairo::Context> context)
{
  context->set_line_width(m_linewidth / mmpt);
  context->rectangle(m_x / mmpt, m_y / mmpt, m_width / mmpt, m_height / mmpt);
  context->stroke();
}

/*----------------------------------------------------------------------------*/

CairoTextBox& CairoTextBox::addBorder(const double linewidth, CairoObject *obj)
{
  CairoRectangle *rectangle = new CairoRectangle(m_x, m_y, m_width, m_height, linewidth);
  m_rectangles.push_back(rectangle);
  if (obj != NULL)
    rectangle->add(*obj);
  add(*rectangle);
  return *this;
}

/*----------------------------------------------------------------------------*/

void CairoTextBox::applyObject(const Cairo::RefPtr<Cairo::Context> context)
{
  Cairo::FontExtents fextents;
  context->get_font_extents(fextents);
  size_t cut;
  int x = 1;
  string text = m_text.str();
  while((cut = text.find_first_of('\n')) != string::npos)
  {
    context->move_to(m_x / mmpt, (m_y + m_padding) / mmpt + x * fextents.height);
    context->show_text(text.substr(0, cut));
    text = text.substr(cut + 1);
    ++x;
  }
  context->move_to(m_x / mmpt, (m_y + m_padding) / mmpt + x * fextents.height);
  context->show_text(text);
}

/*----------------------------------------------------------------------------*/

bool CairoDocument::save(const std::string& filename, const OutputFormat format)
{
  Cairo::RefPtr<Cairo::Surface> surface;
  switch(format)
  {
#ifdef CAIRO_HAS_PDF_SURFACE
    case PDF:
      surface = PdfSurface::create(filename, m_width / mmpt, m_height / mmpt);
      break;
#endif
    default:
      return false;
  }

  Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create(surface);
  apply(context);
  surface->finish();

  return true;
}

/* vim: set et sw=2 ts=2: */