Coverage Report

Created: 2026-09-06 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libetonyek/src/lib/contexts/KEY1SpanElement.cpp
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/*
3
 * This file is part of the libetonyek project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include <boost/spirit/include/qi.hpp>
11
#include <memory>
12
13
#include "KEY1SpanElement.h"
14
15
#include "IWORKProperties.h"
16
#include "IWORKText.h"
17
#include "KEY1ParserState.h"
18
#include "KEY1StringConverter.h"
19
#include "KEY1Token.h"
20
#include "KEY1XMLContextBase.h"
21
22
#include "libetonyek_xml.h"
23
24
namespace libetonyek
25
{
26
KEY1SpanStyle::KEY1SpanStyle(KEY1ParserState &/*state*/, IWORKStylePtr_t parentStyle)
27
0
  : m_style()
28
0
  , m_parentStyle(parentStyle)
29
0
  , m_propMap()
30
0
{
31
0
}
32
33
bool KEY1SpanStyle::readAttribute(const int name, const char *const value)
34
0
{
35
0
  switch (name)
36
0
  {
37
0
  case KEY1Token::font_color :
38
0
  {
39
0
    boost::optional<IWORKColor> color=KEY1StringConverter<IWORKColor>::convert(value);
40
0
    if (color)
41
0
      m_propMap.put<property::FontColor>(get(color));
42
0
    else
43
0
    {
44
0
      ETONYEK_DEBUG_MSG(("KEY1SpanStyle::readAttribute[KEY1SpanElement.cpp]: unknown font color %s\n", value));
45
0
    }
46
0
    return true;
47
0
  }
48
0
  case KEY1Token::font_ligatures :   // with value=all
49
0
  {
50
0
    static bool first=true;
51
0
    if (first)
52
0
    {
53
0
      ETONYEK_DEBUG_MSG(("KEY1SpanStyle::readAttribute[KEY1SpanElement.cpp]: oops find some font ligatures\n"));
54
0
      first=false;
55
0
    }
56
0
    return true;
57
0
  }
58
0
  case KEY1Token::font_name :
59
0
    m_propMap.put<property::FontName>(value);
60
0
    return true;
61
0
  case KEY1Token::font_size :
62
0
  {
63
0
    boost::optional<double> size=try_double_cast(value);
64
0
    if (size)
65
0
      m_propMap.put<property::FontSize>(get(size));
66
0
    else
67
0
    {
68
0
      ETONYEK_DEBUG_MSG(("KEY1SpanStyle::readAttribute[KEY1SpanElement.cpp]: unknown font size %s\n", value));
69
0
    }
70
0
    return true;
71
0
  }
72
0
  case KEY1Token::font_kerning :
73
0
  {
74
0
    boost::optional<double> kerning=try_double_cast(value);
75
0
    if (kerning)
76
0
      m_propMap.put<property::Tracking>(get(kerning));
77
0
    else
78
0
    {
79
0
      ETONYEK_DEBUG_MSG(("KEY1SpanStyle::readAttribute[KEY1SpanElement.cpp]: unknown font kerning %s\n", value));
80
0
    }
81
0
    return true;
82
0
  }
83
0
  case KEY1Token::font_superscript :
84
0
  {
85
0
    boost::optional<int> val=try_int_cast(value);
86
0
    if (val)
87
0
      m_propMap.put<property::BaselineShift>(get(val));
88
0
    else
89
0
    {
90
0
      ETONYEK_DEBUG_MSG(("KEY1SpanStyle::readAttribute[KEY1SpanElement.cpp]: unknown superscript %s\n", value));
91
0
    }
92
0
    return true;
93
0
  }
94
0
  case KEY1Token::font_underline :
95
0
  {
96
0
    boost::optional<bool> val=try_bool_cast(value);
97
0
    if (val)
98
0
      m_propMap.put<property::Underline>(get(val));
99
0
    else
100
0
    {
101
0
      ETONYEK_DEBUG_MSG(("KEY1SpanStyle::readAttribute[KEY1SpanElement.cpp]: unknown underline %s\n", value));
102
0
    }
103
0
    return true;
104
0
  }
105
0
  default:
106
0
    return false;
107
0
  }
108
0
  return true;
109
0
}
110
111
IWORKStylePtr_t KEY1SpanStyle::getStyle()
112
0
{
113
0
  if (!m_style)
114
0
    m_style = std::make_shared<IWORKStyle>(m_propMap, boost::none, m_parentStyle);
115
0
  return m_style;
116
0
}
117
118
KEY1SpanElement::KEY1SpanElement(KEY1ParserState &state, IWORKStylePtr_t parentStyle, bool &delayedLineBreak)
119
0
  : KEY1XMLElementContextBase(state)
120
0
  , m_style(state, parentStyle)
121
0
  , m_opened(false)
122
0
  , m_delayedLineBreak(delayedLineBreak)
123
0
{
124
0
}
125
126
void KEY1SpanElement::attribute(const int name, const char *const value)
127
0
{
128
0
  if (m_style.readAttribute(name, value))
129
0
    return;
130
0
  switch (name)
131
0
  {
132
0
  case KEY1Token::id :
133
0
    setId(value);
134
0
    break;
135
0
  default:
136
0
    ETONYEK_DEBUG_MSG(("KEY1SpanElement::attribute: unknown attribute with value=%s\n", value));
137
0
  }
138
0
}
139
140
IWORKXMLContextPtr_t KEY1SpanElement::element(const int /*name*/)
141
0
{
142
0
  ETONYEK_DEBUG_MSG(("KEY1SpanElement::element: unknown element\n"));
143
0
  return IWORKXMLContextPtr_t();
144
0
}
145
146
void KEY1SpanElement::CDATA(const char *value)
147
0
{
148
0
  ensureClosed();
149
0
  sendCDATA(value, getState().m_currentText, m_style.getStyle(), m_delayedLineBreak);
150
0
}
151
152
void KEY1SpanElement::text(const char *const value)
153
0
{
154
0
  if (m_delayedLineBreak)
155
0
  {
156
0
    ensureClosed();
157
0
    if (bool(getState().m_currentText))
158
0
      getState().m_currentText->flushParagraph();
159
0
    m_delayedLineBreak=false;
160
0
  }
161
0
  ensureOpened();
162
0
  if (bool(getState().m_currentText))
163
0
    getState().m_currentText->insertText(value);
164
0
}
165
166
void KEY1SpanElement::endOfElement()
167
0
{
168
0
  ensureClosed();
169
0
}
170
171
void KEY1SpanElement::ensureOpened()
172
0
{
173
0
  if (!m_opened)
174
0
  {
175
0
    if (bool(getState().m_currentText))
176
0
      getState().m_currentText->setSpanStyle(m_style.getStyle());
177
0
    m_opened = true;
178
0
  }
179
0
}
180
181
void KEY1SpanElement::ensureClosed()
182
0
{
183
0
  if (m_opened)
184
0
  {
185
0
    if (bool(getState().m_currentText))
186
0
    {
187
0
      getState().m_currentText->flushSpan();
188
0
      getState().m_currentText->setSpanStyle(IWORKStylePtr_t());
189
0
    }
190
0
    m_opened = false;
191
0
  }
192
0
}
193
194
void KEY1SpanElement::sendCDATA(const char *value, std::shared_ptr<IWORKText> currentText, IWORKStylePtr_t spanStyle, bool &delayedLineBreak)
195
0
{
196
0
  if (!currentText)
197
0
    return;
198
199
0
  if (delayedLineBreak)
200
0
  {
201
0
    currentText->flushParagraph();
202
0
    delayedLineBreak=false;
203
0
  }
204
0
  std::deque<std::string> listLines;
205
0
  std::string val(value);
206
0
  namespace spirit = boost::spirit;
207
0
  namespace qi =  boost::spirit::qi;
208
0
  using spirit::qi::char_;
209
0
  using spirit::eol;
210
0
  std::string::const_iterator it=val.begin(), end=val.end();
211
0
  if (qi::parse(it, end, - qi::as_string[*(char_ - eol)] % eol, listLines) && it==end)
212
0
  {
213
0
    size_t lastLine=listLines.size();
214
0
    if (lastLine>0 && listLines[lastLine-1].empty()) --lastLine;
215
0
    for (size_t i=0; i<lastLine; ++i)
216
0
    {
217
0
      currentText->setSpanStyle(spanStyle);
218
0
      currentText->insertText(listLines[i].c_str());
219
0
      currentText->flushSpan();
220
0
      if (i+1!=lastLine)
221
0
        currentText->flushParagraph();
222
0
      else
223
0
        delayedLineBreak=true;
224
0
    }
225
0
    currentText->setSpanStyle(IWORKStylePtr_t());
226
0
  }
227
0
  else
228
0
  {
229
0
    currentText->setSpanStyle(spanStyle);
230
0
    currentText->insertText(value);
231
0
    currentText->flushSpan();
232
0
    currentText->setSpanStyle(IWORKStylePtr_t());
233
0
  }
234
0
}
235
236
}
237
238
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */