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 <string_view>
26 #include <unordered_map>
27 #include <vector>
28 #include <rtl/ustring.hxx>
29 #include <rtl/string.hxx>
30 #include <treevisiting.hxx>
31 
32 namespace pdfi
33 {
34  struct Element;
35  struct EmitContext;
36 
38  {
39  public:
40  struct Style
41  {
42  OString Name;
44  OUString Contents;
46  std::vector< Style* > SubStyles;
47 
48  Style( const OString& rName, const PropertyMap& rProps ) :
49  Name( rName ),
50  Properties( rProps ),
51  ContainedElement( nullptr )
52  {}
53  };
54 
55  private:
56  struct HashedStyle
57  {
58  OString Name;
60  OUString Contents;
62  std::vector<sal_Int32> SubStyles;
63 
64  bool IsSubStyle;
65 
66  HashedStyle() : ContainedElement( nullptr ), IsSubStyle( true ) {}
67 
68  size_t hashCode() const
69  {
70  size_t nRet = std::accumulate(Properties.begin(), Properties.end(), size_t(Name.hashCode()),
71  [](const size_t& sum, const PropertyMap::value_type& rEntry) {
72  return sum ^ size_t(rEntry.first.hashCode()) ^ size_t(rEntry.second.hashCode());
73  });
74  nRet ^= size_t(Contents.hashCode());
75  nRet ^= reinterpret_cast<size_t>(ContainedElement);
76  for( size_t n = 0; n < SubStyles.size(); ++n )
77  nRet ^= size_t(SubStyles[n]);
78  return nRet;
79  }
80 
81  bool operator==(const HashedStyle& rRight) const
82  {
83  if( Name != rRight.Name ||
84  Properties != rRight.Properties ||
85  Contents != rRight.Contents ||
86  ContainedElement != rRight.ContainedElement ||
87  SubStyles.size() != rRight.SubStyles.size()
88  )
89  return false;
90  for( size_t n = 0; n < SubStyles.size(); ++n )
91  {
92  if( SubStyles[n] != rRight.SubStyles[n] )
93  return false;
94  }
95  return true;
96  }
97  };
98 
101  sal_Int32 RefCount = 0;
102  };
103 
104  struct StyleHash;
105  friend struct StyleHash;
106  struct StyleHash
107  {
108  size_t operator()( const StyleContainer::HashedStyle& rStyle ) const
109  {
110  return rStyle.hashCode();
111  }
112  };
113 
115  friend struct StyleIdNameSort;
117  {
118  const std::unordered_map< sal_Int32, RefCountedHashedStyle >* m_pMap;
119 
120  explicit StyleIdNameSort( const std::unordered_map< sal_Int32, RefCountedHashedStyle >* pMap ) :
121  m_pMap(pMap)
122  {}
123  bool operator()( sal_Int32 nLeft, sal_Int32 nRight )
124  {
125  const std::unordered_map< sal_Int32, RefCountedHashedStyle >::const_iterator left_it =
126  m_pMap->find( nLeft );
127  const std::unordered_map< sal_Int32, RefCountedHashedStyle >::const_iterator right_it =
128  m_pMap->find( nRight );
129  if( left_it == m_pMap->end() )
130  return false;
131  else if( right_it == m_pMap->end() )
132  return true;
133  else
134  return left_it->second.style.Name < right_it->second.style.Name;
135  }
136  };
137 
138  sal_Int32 m_nNextId;
139  std::unordered_map< sal_Int32, RefCountedHashedStyle > m_aIdToStyle;
140  std::unordered_map< HashedStyle, sal_Int32, StyleHash > m_aStyleToId;
141 
142  void impl_emitStyle( sal_Int32 nStyleId,
143  EmitContext& rContext,
144  ElementTreeVisitor& rContainedElemVisitor );
145 
146  public:
147  StyleContainer();
148 
149  void emit( EmitContext& rContext,
150  ElementTreeVisitor& rContainedElemVisitor );
151 
152  sal_Int32 impl_getStyleId( const Style& rStyle, bool bSubStyle );
153  sal_Int32 getStyleId( const Style& rStyle )
154  { return impl_getStyleId( rStyle, false ); }
155  sal_Int32 getStandardStyleId( std::string_view rFamily );
156 
157  // returns NULL for an invalid style id
158  const PropertyMap* getProperties( sal_Int32 nStyleId ) const;
159  sal_Int32 setProperties( sal_Int32 nStyleId, const PropertyMap &rNewProps );
160  OUString getStyleName( sal_Int32 nStyle ) const;
161  };
162 }
163 
164 #endif
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unordered_map< OUString, OUString > PropertyMap
Definition: pdfihelper.hxx:44
sal_Int32 m_nNextId
Definition: style.hxx:138
std::vector< Style * > SubStyles
Definition: style.hxx:46
PropertyMap Properties
Definition: style.hxx:43
std::unordered_map< HashedStyle, sal_Int32, StyleHash > m_aStyleToId
Definition: style.hxx:140
sal_Int64 n
sal_Int32 impl_getStyleId(const Style &rStyle, bool bSubStyle)
Definition: style.cxx:37
Element * ContainedElement
Definition: style.hxx:45
std::vector< sal_Int32 > SubStyles
Definition: style.hxx:62
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
sal_Int32 getStandardStyleId(std::string_view rFamily)
Definition: style.cxx:76
bool operator()(sal_Int32 nLeft, sal_Int32 nRight)
Definition: style.hxx:123
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:108
const std::unordered_map< sal_Int32, RefCountedHashedStyle > * m_pMap
Definition: style.hxx:118
Style(const OString &rName, const PropertyMap &rProps)
Definition: style.hxx:48
sal_Int32 getStyleId(const Style &rStyle)
Definition: style.hxx:153
void emit(EmitContext &rContext, ElementTreeVisitor &rContainedElemVisitor)
Definition: style.cxx:209
std::unordered_map< sal_Int32, RefCountedHashedStyle > m_aIdToStyle
Definition: style.hxx:139
bool operator==(const HashedStyle &rRight) const
Definition: style.hxx:81
StyleIdNameSort(const std::unordered_map< sal_Int32, RefCountedHashedStyle > *pMap)
Definition: style.hxx:120