| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifndef WPS_GRAPHIC_STYLE |
| 24 |
# define WPS_GRAPHIC_STYLE |
| 25 |
# include <ostream> |
| 26 |
# include <string> |
| 27 |
# include <vector> |
| 28 |
|
| 29 |
# include "librevenge/librevenge.h" |
| 30 |
# include "libwps_internal.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class WPSGraphicStyle |
| 38 |
{ |
| 39 |
public: |
| 40 |
|
| 41 |
enum LineCap { C_Butt, C_Square, C_Round }; |
| 42 |
|
| 43 |
enum LineJoin { J_Miter, J_Round, J_Bevel }; |
| 44 |
|
| 45 |
enum GradientType { G_None, G_Axial, G_Linear, G_Radial, G_Rectangular, G_Square, G_Ellipsoid }; |
| 46 |
|
| 47 |
|
| 48 |
struct GradientStop |
| 49 |
{ |
| 50 |
|
| 51 |
explicit GradientStop(float offset=0.0, WPSColor const &col=WPSColor::black(), float opacity=1.0) |
| 52 |
: m_offset(offset) |
| 53 |
, m_color(col) |
| 54 |
, m_opacity(opacity) |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
int cmp(GradientStop const &a) const |
| 59 |
{ |
| 60 |
if (m_offset < a.m_offset) return -1; |
| 61 |
if (m_offset > a.m_offset) return 1; |
| 62 |
if (m_color < a.m_color) return -1; |
| 63 |
if (m_color > a.m_color) return 1; |
| 64 |
if (m_opacity < a.m_opacity) return -1; |
| 65 |
if (m_opacity > a.m_opacity) return 1; |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
friend std::ostream &operator<<(std::ostream &o, GradientStop const &st) |
| 70 |
{ |
| 71 |
o << "offset=" << st.m_offset << ","; |
| 72 |
o << "color=" << st.m_color << ","; |
| 73 |
if (st.m_opacity<1.0f) |
| 74 |
o << "opacity=" << st.m_opacity*100.f << "%,"; |
| 75 |
return o; |
| 76 |
} |
| 77 |
|
| 78 |
float m_offset; |
| 79 |
|
| 80 |
WPSColor m_color; |
| 81 |
|
| 82 |
float m_opacity; |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
struct Pattern |
| 89 |
{ |
| 90 |
|
| 91 |
Pattern() |
| 92 |
: m_dim(0,0) |
| 93 |
, m_data() |
| 94 |
, m_picture() |
| 95 |
, m_pictureMime("") |
| 96 |
, m_pictureAverageColor(WPSColor::white()) |
| 97 |
{ |
| 98 |
m_colors[0]=WPSColor::black(); |
| 99 |
m_colors[1]=WPSColor::white(); |
| 100 |
} |
| 101 |
|
| 102 |
Pattern(Vec2i dim, librevenge::RVNGBinaryData const &picture, std::string const &mime, WPSColor const &avColor) |
| 103 |
: m_dim(dim) |
| 104 |
, m_data() |
| 105 |
, m_picture(picture) |
| 106 |
, m_pictureMime(mime) |
| 107 |
, m_pictureAverageColor(avColor) |
| 108 |
{ |
| 109 |
m_colors[0]=WPSColor::black(); |
| 110 |
m_colors[1]=WPSColor::white(); |
| 111 |
} |
| 112 |
Pattern(Pattern const &)=default; |
| 113 |
Pattern(Pattern &&)=default; |
| 114 |
|
| 115 |
virtual ~Pattern(); |
| 116 |
|
| 117 |
bool empty() const |
| 118 |
{ |
| 119 |
if (m_dim[0]==0 || m_dim[1]==0) return true; |
| 120 |
if (m_picture.size()) return false; |
| 121 |
if (m_dim[0]!=8 && m_dim[0]!=16 && m_dim[0]!=32) return true; |
| 122 |
return m_data.size()!=size_t((m_dim[0]/8)*m_dim[1]); |
| 123 |
} |
| 124 |
|
| 125 |
bool getAverageColor(WPSColor &col) const; |
| 126 |
|
| 127 |
bool getUniqueColor(WPSColor &col) const; |
| 128 |
|
| 129 |
bool getBinary(librevenge::RVNGBinaryData &data, std::string &type) const; |
| 130 |
|
| 131 |
|
| 132 |
int cmp(Pattern const &a) const |
| 133 |
{ |
| 134 |
int diff = m_dim.cmp(a.m_dim); |
| 135 |
if (diff) return diff; |
| 136 |
if (m_data.size() < a.m_data.size()) return -1; |
| 137 |
if (m_data.size() > a.m_data.size()) return 1; |
| 138 |
for (size_t h=0; h < m_data.size(); ++h) |
| 139 |
{ |
| 140 |
if (m_data[h]<a.m_data[h]) return 1; |
| 141 |
if (m_data[h]>a.m_data[h]) return -1; |
| 142 |
} |
| 143 |
for (int i=0; i<2; ++i) |
| 144 |
{ |
| 145 |
if (m_colors[i] < a.m_colors[i]) return 1; |
| 146 |
if (m_colors[i] > a.m_colors[i]) return -1; |
| 147 |
} |
| 148 |
if (m_pictureAverageColor < a.m_pictureAverageColor) return 1; |
| 149 |
if (m_pictureAverageColor > a.m_pictureAverageColor) return -1; |
| 150 |
if (m_pictureMime < a.m_pictureMime) return 1; |
| 151 |
if (m_pictureMime > a.m_pictureMime) return -1; |
| 152 |
if (m_picture.size() < a.m_picture.size()) return 1; |
| 153 |
if (m_picture.size() > a.m_picture.size()) return -1; |
| 154 |
const unsigned char *ptr=m_picture.getDataBuffer(); |
| 155 |
const unsigned char *aPtr=a.m_picture.getDataBuffer(); |
| 156 |
if (!ptr || !aPtr) return 0; |
| 157 |
for (unsigned long h=0; h < m_picture.size(); ++h, ++ptr, ++aPtr) |
| 158 |
{ |
| 159 |
if (*ptr < *aPtr) return 1; |
| 160 |
if (*ptr > *aPtr) return -1; |
| 161 |
} |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
friend std::ostream &operator<<(std::ostream &o, Pattern const &pat) |
| 166 |
{ |
| 167 |
o << "dim=" << pat.m_dim << ","; |
| 168 |
if (pat.m_picture.size()) |
| 169 |
{ |
| 170 |
o << "type=" << pat.m_pictureMime << ","; |
| 171 |
o << "col[average]=" << pat.m_pictureAverageColor << ","; |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
if (!pat.m_colors[0].isBlack()) o << "col0=" << pat.m_colors[0] << ","; |
| 176 |
if (!pat.m_colors[1].isWhite()) o << "col1=" << pat.m_colors[1] << ","; |
| 177 |
o << "["; |
| 178 |
for (auto const &data : pat.m_data) |
| 179 |
o << std::hex << int(data) << std::dec << ","; |
| 180 |
o << "],"; |
| 181 |
} |
| 182 |
return o; |
| 183 |
} |
| 184 |
|
| 185 |
Pattern &operator=(Pattern const &)=default; |
| 186 |
|
| 187 |
Vec2i m_dim; |
| 188 |
|
| 189 |
|
| 190 |
WPSColor m_colors[2]; |
| 191 |
|
| 192 |
std::vector<unsigned char> m_data; |
| 193 |
protected: |
| 194 |
|
| 195 |
librevenge::RVNGBinaryData m_picture; |
| 196 |
|
| 197 |
std::string m_pictureMime; |
| 198 |
|
| 199 |
WPSColor m_pictureAverageColor; |
| 200 |
}; |
| 201 |
|
| 202 |
WPSGraphicStyle() |
| 203 |
: m_lineWidth(1) |
| 204 |
, m_lineDashWidth() |
| 205 |
, m_lineCap(C_Butt) |
| 206 |
, m_lineJoin(J_Miter) |
| 207 |
, m_lineOpacity(1) |
| 208 |
, m_lineColor(WPSColor::black()) |
| 209 |
, m_fillRuleEvenOdd(false) |
| 210 |
, m_surfaceColor(WPSColor::white()) |
| 211 |
, m_surfaceOpacity(0) |
| 212 |
, m_shadowColor(WPSColor::black()) |
| 213 |
, m_shadowOpacity(0) |
| 214 |
, m_shadowOffset(1,1) |
| 215 |
, m_pattern() |
| 216 |
, m_gradientType(G_None) |
| 217 |
, m_gradientStopList() |
| 218 |
, m_gradientAngle(0) |
| 219 |
, m_gradientBorder(0) |
| 220 |
, m_gradientPercentCenter(0.5f,0.5f) |
| 221 |
, m_gradientRadius(1) |
| 222 |
, m_backgroundColor(WPSColor::white()) |
| 223 |
, m_backgroundOpacity(-1) |
| 224 |
, m_bordersList() |
| 225 |
, m_frameName("") |
| 226 |
, m_frameNextName("") |
| 227 |
, m_rotate(0) |
| 228 |
, m_extra("") |
| 229 |
{ |
| 230 |
m_arrows[0]=m_arrows[1]=false; |
| 231 |
m_flip[0]=m_flip[1]=false; |
| 232 |
m_gradientStopList.push_back(GradientStop(0.0, WPSColor::white())); |
| 233 |
m_gradientStopList.push_back(GradientStop(1.0, WPSColor::black())); |
| 234 |
} |
| 235 |
WPSGraphicStyle(WPSGraphicStyle const &)=default; |
| 236 |
WPSGraphicStyle(WPSGraphicStyle &&)=default; |
| 237 |
WPSGraphicStyle &operator=(WPSGraphicStyle &&)=default; |
| 238 |
WPSGraphicStyle &operator=(WPSGraphicStyle const &)=default; |
| 239 |
|
| 240 |
static WPSGraphicStyle emptyStyle() |
| 241 |
{ |
| 242 |
WPSGraphicStyle res; |
| 243 |
res.m_lineWidth=0; |
| 244 |
return res; |
| 245 |
} |
| 246 |
|
| 247 |
virtual ~WPSGraphicStyle(); |
| 248 |
|
| 249 |
bool hasLine() const |
| 250 |
{ |
| 251 |
return m_lineWidth>0 && m_lineOpacity>0; |
| 252 |
} |
| 253 |
|
| 254 |
void setSurfaceColor(WPSColor const &col, float opacity = 1) |
| 255 |
{ |
| 256 |
m_surfaceColor = col; |
| 257 |
m_surfaceOpacity = opacity; |
| 258 |
} |
| 259 |
|
| 260 |
bool hasSurfaceColor() const |
| 261 |
{ |
| 262 |
return m_surfaceOpacity > 0; |
| 263 |
} |
| 264 |
|
| 265 |
void setPattern(Pattern const &pat) |
| 266 |
{ |
| 267 |
m_pattern=pat; |
| 268 |
} |
| 269 |
|
| 270 |
bool hasPattern() const |
| 271 |
{ |
| 272 |
return !m_pattern.empty(); |
| 273 |
} |
| 274 |
|
| 275 |
bool hasGradient(bool complex=false) const |
| 276 |
{ |
| 277 |
return m_gradientType != G_None && int(m_gradientStopList.size()) >= (complex ? 3 : 2); |
| 278 |
} |
| 279 |
|
| 280 |
bool hasSurface() const |
| 281 |
{ |
| 282 |
return hasSurfaceColor() || hasPattern() || hasGradient(); |
| 283 |
} |
| 284 |
|
| 285 |
void setBackgroundColor(WPSColor const &col, float opacity = 1) |
| 286 |
{ |
| 287 |
m_backgroundColor = col; |
| 288 |
m_backgroundOpacity = opacity; |
| 289 |
} |
| 290 |
|
| 291 |
bool hasBackgroundColor() const |
| 292 |
{ |
| 293 |
return m_backgroundOpacity > 0; |
| 294 |
} |
| 295 |
|
| 296 |
void setShadowColor(WPSColor const &col, float opacity = 1) |
| 297 |
{ |
| 298 |
m_shadowColor = col; |
| 299 |
m_shadowOpacity = opacity; |
| 300 |
} |
| 301 |
|
| 302 |
bool hasShadow() const |
| 303 |
{ |
| 304 |
return m_shadowOpacity > 0; |
| 305 |
} |
| 306 |
|
| 307 |
bool hasBorders() const |
| 308 |
{ |
| 309 |
return !m_bordersList.empty(); |
| 310 |
} |
| 311 |
|
| 312 |
bool hasSameBorders() const |
| 313 |
{ |
| 314 |
if (m_bordersList.empty()) return true; |
| 315 |
if (m_bordersList.size()!=4) return false; |
| 316 |
for (size_t i=1; i<m_bordersList.size(); ++i) |
| 317 |
{ |
| 318 |
if (m_bordersList[i]!=m_bordersList[0]) |
| 319 |
return false; |
| 320 |
} |
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
std::vector<WPSBorder> const &borders() const |
| 325 |
{ |
| 326 |
return m_bordersList; |
| 327 |
} |
| 328 |
|
| 329 |
void resetBorders() |
| 330 |
{ |
| 331 |
m_bordersList.resize(0); |
| 332 |
} |
| 333 |
|
| 334 |
void setBorders(int wh, WPSBorder const &border); |
| 335 |
|
| 336 |
friend std::ostream &operator<<(std::ostream &o, WPSGraphicStyle const &st); |
| 337 |
|
| 338 |
void addTo(librevenge::RVNGPropertyList &pList, bool only1d=false) const; |
| 339 |
|
| 340 |
void addFrameTo(librevenge::RVNGPropertyList &pList) const; |
| 341 |
|
| 342 |
int cmp(WPSGraphicStyle const &a) const; |
| 343 |
|
| 344 |
|
| 345 |
float m_lineWidth; |
| 346 |
|
| 347 |
std::vector<float> m_lineDashWidth; |
| 348 |
|
| 349 |
LineCap m_lineCap; |
| 350 |
|
| 351 |
LineJoin m_lineJoin; |
| 352 |
|
| 353 |
float m_lineOpacity; |
| 354 |
|
| 355 |
WPSColor m_lineColor; |
| 356 |
|
| 357 |
bool m_fillRuleEvenOdd; |
| 358 |
|
| 359 |
WPSColor m_surfaceColor; |
| 360 |
|
| 361 |
float m_surfaceOpacity; |
| 362 |
|
| 363 |
|
| 364 |
WPSColor m_shadowColor; |
| 365 |
|
| 366 |
float m_shadowOpacity; |
| 367 |
|
| 368 |
Vec2f m_shadowOffset; |
| 369 |
|
| 370 |
|
| 371 |
Pattern m_pattern; |
| 372 |
|
| 373 |
|
| 374 |
GradientType m_gradientType; |
| 375 |
|
| 376 |
std::vector<GradientStop> m_gradientStopList; |
| 377 |
|
| 378 |
float m_gradientAngle; |
| 379 |
|
| 380 |
float m_gradientBorder; |
| 381 |
|
| 382 |
Vec2f m_gradientPercentCenter; |
| 383 |
|
| 384 |
float m_gradientRadius; |
| 385 |
|
| 386 |
|
| 387 |
bool m_arrows[2]; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
WPSColor m_backgroundColor; |
| 395 |
|
| 396 |
float m_backgroundOpacity; |
| 397 |
|
| 398 |
std::vector<WPSBorder> m_bordersList; |
| 399 |
|
| 400 |
librevenge::RVNGString m_frameName; |
| 401 |
|
| 402 |
librevenge::RVNGString m_frameNextName; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
float m_rotate; |
| 410 |
|
| 411 |
bool m_flip[2]; |
| 412 |
|
| 413 |
|
| 414 |
std::string m_extra; |
| 415 |
}; |
| 416 |
#endif |
| 417 |
|