summaryrefslogtreecommitdiff
path: root/poppler/MarkedContentOutputDev.cc
blob: 7fdd8f54c539a1221a6c77eaf37dffc12471ef1d (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//========================================================================
//
// MarkedContentOutputDev.cc
//
// This file is licensed under the GPLv2 or later
//
// Copyright 2013 Igalia S.L.
//
//========================================================================

#include "MarkedContentOutputDev.h"
#include "GlobalParams.h"
#include "UnicodeMap.h"
#include "GfxState.h"
#include "GfxFont.h"
#include "Annot.h"
#include <vector>


MarkedContentOutputDev::MarkedContentOutputDev(int mcidA):
  currentFont(NULL),
  currentText(NULL),
  mcid(mcidA),
  pageWidth(0.0),
  pageHeight(0.0),
  unicodeMap(NULL)
{
  currentColor.r = currentColor.g = currentColor.b = 0;
}


MarkedContentOutputDev::~MarkedContentOutputDev()
{
  if (unicodeMap)
    unicodeMap->decRefCnt();
  if (currentFont)
    currentFont->decRefCnt();
  delete currentText;
}


void MarkedContentOutputDev::endSpan()
{
  if (currentText && currentText->getLength()) {
    // The TextSpan takes ownership of currentText and
    // increases the reference count for currentFont.
    textSpans.push_back(TextSpan(currentText,
                                 currentFont,
                                 currentColor));
  }
  currentText = NULL;
}


void MarkedContentOutputDev::startPage(int pageNum, GfxState *state, XRef *xref)
{
  if (state) {
    pageWidth  = state->getPageWidth();
    pageHeight = state->getPageHeight();
  } else {
    pageWidth = pageHeight = 0.0;
  }
}


void MarkedContentOutputDev::endPage()
{
  pageWidth = pageHeight = 0.0;
}


void MarkedContentOutputDev::beginMarkedContent(char *name, Dict *properties)
{
  int id = -1;
  if (properties)
    properties->lookupInt("MCID", NULL, &id);

  if (id == -1)
    return;

  // The stack keep track of MCIDs of nested marked content.
  if (inMarkedContent() || id == mcid)
    mcidStack.push_back(id);
}


void MarkedContentOutputDev::endMarkedContent(GfxState *state)
{
  if (inMarkedContent()) {
      mcidStack.pop_back();
      // The outer marked content sequence MCID was popped, ensure
      // that the last piece of text collected ends up in a TextSpan.
      if (!inMarkedContent())
        endSpan();
  }
}


bool MarkedContentOutputDev::needFontChange(GfxFont* font) const
{
  if (currentFont == font)
    return gFalse;

  if (!currentFont)
    return font != NULL && font->isOk();

  if (font == NULL)
    return gTrue;

  // Two non-null valid fonts are the same if they point to the same Ref
  if (currentFont->getID()->num == font->getID()->num &&
      currentFont->getID()->gen == font->getID()->gen)
    return gFalse;

  return gTrue;
}


void MarkedContentOutputDev::drawChar(GfxState *state,
                                      double xx, double yy,
                                      double dx, double dy,
                                      double ox, double oy,
                                      CharCode c, int nBytes,
                                      Unicode *u, int uLen)
{
  if (!inMarkedContent() || !uLen)
    return;


  // Color changes are tracked here so the color can be chosen depending on
  // the render mode (for mode 1 stroke color is used), so there is no need
  // to implement both updateFillColor() and updateStrokeColor().
  GBool colorChange = gFalse;
  GfxRGB color;
  if ((state->getRender() & 3) == 1)
    state->getStrokeRGB(&color);
  else
    state->getFillRGB(&color);

  colorChange = (color.r != currentColor.r ||
                 color.g != currentColor.g ||
                 color.b != currentColor.b);

  // Check also for font changes.
  GBool fontChange = needFontChange(state->getFont());

  // Save a span with the current changes.
  if (colorChange || fontChange) {
    endSpan();
  }

  // Perform the color/font changes.
  if (colorChange)
    currentColor = color;

  if (fontChange) {
    if (currentFont != NULL) {
      currentFont->decRefCnt();
      currentFont = NULL;
    }
    if (state->getFont() != NULL) {
      currentFont = state->getFont();
      currentFont->incRefCnt();
    }
  }


  double sp, dx2, dy2, w1, h1, x1, y1;

  // Subtract char and word spacing from the (dx,dy) values
  sp = state->getCharSpace();
  if (c == (CharCode) 0x20)
    sp += state->getWordSpace();
  state->textTransformDelta(sp * state->getHorizScaling(), 0, &dx2, &dy2);
  dx -= dx2;
  dy -= dy2;
  state->transformDelta(dx, dy, &w1, &h1);
  state->transform(xx, yy, &x1, &y1);

  // Throw away characters that are not inside the page boundaries.
  if (x1 + w1 < 0 || x1 > pageWidth || y1 + h1 < 0 || y1 > pageHeight)
    return;

  // Make a sanity check on character size. Note: (x != x) <-> isnan(x)
  if (x1 != x1 || y1 != y1 || w1 != w1 || h1 != h1)
    return;

  for (int i = 0; i < uLen; i++) {
    // Soft hyphen markers are skipped, as they are invisible unless
    // rendering is done to an actual device and the hyphenation hint
    // used. MarkedContentOutputDev extracts the *visible* text content.
    if (u[i] != 0x00AD) {
      // Add the UTF-8 sequence to the current text span.
      if (!unicodeMap)
        unicodeMap = globalParams->getTextEncoding();

      char buf[8];
      int n = unicodeMap->mapUnicode(u[i], buf, sizeof(buf));
      if (n > 0) {
        if (currentText == NULL)
          currentText = new GooString();
        currentText->append(buf, n);
      }
    }
  }
}


const TextSpanArray& MarkedContentOutputDev::getTextSpans() const
{
  return textSpans;
}