LibreOffice Module sdext (master)  1
style.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice 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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_TREE_STYLE_HXX
21 #define INCLUDED_SDEXT_SOURCE_PDFIMPORT_TREE_STYLE_HXX
22 
23 #include <pdfihelper.hxx>
24 #include <numeric>
25 #include <unordered_map>
26 #include <vector>
27 #include <rtl/ustring.hxx>
28 #include <rtl/string.hxx>
29 #include <treevisiting.hxx>
30 
31 namespace pdfi
32 {
33  struct Element;
34  struct EmitContext;
35 
37  {
38  public:
39  struct Style
40  {
41  OString Name;
43  OUString Contents;
45  std::vector< Style* > SubStyles;
46 
47  Style( const OString& rName, const PropertyMap& rProps ) :
48  Name( rName ),
49  Properties( rProps ),
50  ContainedElement( nullptr )
51  {}
52  };
53 
54  private:
55  struct HashedStyle
56  {
57  OString Name;
59  OUString Contents;
61  std::vector<sal_Int32> SubStyles;
62 
63  bool IsSubStyle;
64 
65  HashedStyle() : ContainedElement( nullptr ), IsSubStyle( true ) {}
66 
67  size_t hashCode() const
68  {
69  size_t nRet = std::accumulate(Properties.begin(), Properties.end(), size_t(Name.hashCode()),
70  [](const size_t& sum, const PropertyMap::value_type& rEntry) {
71  return sum ^ size_t(rEntry.first.hashCode()) ^ size_t(rEntry.second.hashCode());
72  });
73  nRet ^= size_t(Contents.hashCode());
74  nRet ^= reinterpret_cast<size_t>(ContainedElement);
75  for( size_t n = 0; n < SubStyles.size(); ++n )
76  nRet ^= size_t(SubStyles[n]);
77  return nRet;
78  }
79 
80  bool operator==(const HashedStyle& rRight) const
81  {
82  if( Name != rRight.Name ||
83  Properties != rRight.Properties ||
84  Contents != rRight.Contents ||
85  ContainedElement != rRight.ContainedElement ||
86  SubStyles.size() != rRight.SubStyles.size()
87  )
88  return false;
89  for( size_t n = 0; n < SubStyles.size(); ++n )
90  {
91  if( SubStyles[n] != rRight.SubStyles[n] )
92  return false;
93  }
94  return true;
95  }
96  };
97 
100  sal_Int32 RefCount = 0;
101  };
102 
103  struct StyleHash;
104  friend struct StyleHash;
105  struct StyleHash
106  {
107  size_t operator()( const StyleContainer::HashedStyle& rStyle ) const
108  {
109  return rStyle.hashCode();
110  }
111  };
112 
114  friend struct StyleIdNameSort;
116  {
117  const std::unordered_map< sal_Int32, RefCountedHashedStyle >* m_pMap;
118 
119  explicit StyleIdNameSort( const std::unordered_map< sal_Int32, RefCountedHashedStyle >* pMap ) :
120  m_pMap(pMap)
121  {}
122  bool operator()( sal_Int32 nLeft, sal_Int32 nRight )
123  {
124  const std::unordered_map< sal_Int32, RefCountedHashedStyle >::const_iterator left_it =
125  m_pMap->find( nLeft );
126  const std::unordered_map< sal_Int32, RefCountedHashedStyle >::const_iterator right_it =
127  m_pMap->find( nRight );
128  if( left_it == m_pMap->end() )
129  return false;
130  else if( right_it == m_pMap->end() )
131  return true;
132  else
133  return left_it->second.style.Name < right_it->second.style.Name;
134  }
135  };
136 
137  sal_Int32 m_nNextId;
138  std::unordered_map< sal_Int32, RefCountedHashedStyle > m_aIdToStyle;
139  std::unordered_map< HashedStyle, sal_Int32, StyleHash > m_aStyleToId;
140 
141  void impl_emitStyle( sal_Int32 nStyleId,
142  EmitContext& rContext,
143  ElementTreeVisitor& rContainedElemVisitor );
144 
145  public:
146  StyleContainer();
147 
148  void emit( EmitContext& rContext,
149  ElementTreeVisitor& rContainedElemVisitor );
150 
151  sal_Int32 impl_getStyleId( const Style& rStyle, bool bSubStyle );
152  sal_Int32 getStyleId( const Style& rStyle )
153  { return impl_getStyleId( rStyle, false ); }
154  sal_Int32 getStandardStyleId( const OString& rFamily );
155 
156  // returns NULL for an invalid style id
157  const PropertyMap* getProperties( sal_Int32 nStyleId ) const;
158  sal_Int32 setProperties( sal_Int32 nStyleId, const PropertyMap &rNewProps );
159  OUString getStyleName( sal_Int32 nStyle ) const;
160  };
161 }
162 
163 #endif
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unordered_map< OUString, OUString > PropertyMap
Definition: pdfihelper.hxx:45
sal_Int32 m_nNextId
Definition: style.hxx:137
std::vector< Style * > SubStyles
Definition: style.hxx:45
PropertyMap Properties
Definition: style.hxx:42
std::unordered_map< HashedStyle, sal_Int32, StyleHash > m_aStyleToId
Definition: style.hxx:139
sal_Int64 n
sal_Int32 impl_getStyleId(const Style &rStyle, bool bSubStyle)
Definition: style.cxx:37
Element * ContainedElement
Definition: style.hxx:44
std::vector< sal_Int32 > SubStyles
Definition: style.hxx:61
To be visited by all tree element types.
const PropertyMap * getProperties(sal_Int32 nStyleId) const
Definition: style.cxx:86
sal_Int32 setProperties(sal_Int32 nStyleId, const PropertyMap &rNewProps)
Definition: style.cxx:93
OUString getStyleName(sal_Int32 nStyle) const
Definition: style.cxx:146
bool operator()(sal_Int32 nLeft, sal_Int32 nRight)
Definition: style.hxx:122
void impl_emitStyle(sal_Int32 nStyleId, EmitContext &rContext, ElementTreeVisitor &rContainedElemVisitor)
Definition: style.cxx:183
size_t operator()(const StyleContainer::HashedStyle &rStyle) const
Definition: style.hxx:107
const std::unordered_map< sal_Int32, RefCountedHashedStyle > * m_pMap
Definition: style.hxx:117
Style(const OString &rName, const PropertyMap &rProps)
Definition: style.hxx:47
sal_Int32 getStyleId(const Style &rStyle)
Definition: style.hxx:152
void emit(EmitContext &rContext, ElementTreeVisitor &rContainedElemVisitor)
Definition: style.cxx:209
std::unordered_map< sal_Int32, RefCountedHashedStyle > m_aIdToStyle
Definition: style.hxx:138
sal_Int32 getStandardStyleId(const OString &rFamily)
Definition: style.cxx:76
bool operator==(const HashedStyle &rRight) const
Definition: style.hxx:80
StyleIdNameSort(const std::unordered_map< sal_Int32, RefCountedHashedStyle > *pMap)
Definition: style.hxx:119