Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / impglyphitem.hxx
blobbb08031f3ab698ff9e4349bf6684f4bd14afa89e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VCL_IMPGLYPHITEM_HXX
21 #define INCLUDED_VCL_IMPGLYPHITEM_HXX
23 #include <basegfx/range/b2drectangle.hxx>
24 #include <o3tl/typed_flags_set.hxx>
25 #include <vcl/dllapi.h>
26 #include <vcl/rendercontext/SalLayoutFlags.hxx>
27 #include <rtl/math.hxx>
28 #include <vector>
30 #include "font/LogicalFontInstance.hxx"
31 #include "glyphid.hxx"
33 enum class GlyphItemFlags : sal_uInt8
35 NONE = 0,
36 IS_IN_CLUSTER = 0x01,
37 IS_RTL_GLYPH = 0x02,
38 IS_VERTICAL = 0x04,
39 IS_SPACING = 0x08,
40 IS_DROPPED = 0x10,
41 IS_CLUSTER_START = 0x20,
42 IS_UNSAFE_TO_BREAK = 0x40, // HB_GLYPH_FLAG_UNSAFE_TO_BREAK from harfbuzz
43 IS_SAFE_TO_INSERT_KASHIDA = 0x80 // HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL from harfbuzz
45 namespace o3tl
47 template <> struct typed_flags<GlyphItemFlags> : is_typed_flags<GlyphItemFlags, 0xff>
52 class VCL_DLLPUBLIC GlyphItem
54 basegfx::B2DPoint m_aLinearPos; // absolute position of non rotated string
55 double m_nOrigWidth; // original glyph width
56 sal_Int32 m_nCharPos; // index in string
57 double m_nXOffset;
58 double m_nYOffset;
59 double m_nNewWidth; // width after adjustments
60 sal_GlyphId m_aGlyphId;
61 GlyphItemFlags m_nFlags;
62 sal_Int8 m_nCharCount; // number of characters making up this glyph
64 public:
65 GlyphItem(int nCharPos, int nCharCount, sal_GlyphId aGlyphId,
66 const basegfx::B2DPoint& rLinearPos, GlyphItemFlags nFlags, double nOrigWidth,
67 double nXOffset, double nYOffset)
68 : m_aLinearPos(rLinearPos)
69 , m_nOrigWidth(nOrigWidth)
70 , m_nCharPos(nCharPos)
71 , m_nXOffset(nXOffset)
72 , m_nYOffset(nYOffset)
73 , m_nNewWidth(nOrigWidth)
74 , m_aGlyphId(aGlyphId)
75 , m_nFlags(nFlags)
76 , m_nCharCount(nCharCount)
80 bool IsInCluster() const { return bool(m_nFlags & GlyphItemFlags::IS_IN_CLUSTER); }
81 bool IsRTLGlyph() const { return bool(m_nFlags & GlyphItemFlags::IS_RTL_GLYPH); }
82 bool IsVertical() const { return bool(m_nFlags & GlyphItemFlags::IS_VERTICAL); }
83 bool IsSpacing() const { return bool(m_nFlags & GlyphItemFlags::IS_SPACING); }
84 bool IsDropped() const { return bool(m_nFlags & GlyphItemFlags::IS_DROPPED); }
85 bool IsClusterStart() const { return bool(m_nFlags & GlyphItemFlags::IS_CLUSTER_START); }
86 bool IsUnsafeToBreak() const { return bool(m_nFlags & GlyphItemFlags::IS_UNSAFE_TO_BREAK); }
87 bool IsSafeToInsertKashida() const
89 return bool(m_nFlags & GlyphItemFlags::IS_SAFE_TO_INSERT_KASHIDA);
92 inline bool GetGlyphBoundRect(const LogicalFontInstance*, basegfx::B2DRectangle&) const;
93 inline bool GetGlyphOutline(const LogicalFontInstance*, basegfx::B2DPolyPolygon&) const;
94 inline void dropGlyph();
96 sal_GlyphId glyphId() const { return m_aGlyphId; }
97 int charCount() const { return m_nCharCount; }
98 double origWidth() const { return m_nOrigWidth; }
99 int charPos() const { return m_nCharPos; }
100 double xOffset() const { return m_nXOffset; }
101 double yOffset() const { return m_nYOffset; }
102 double newWidth() const { return m_nNewWidth; }
103 const basegfx::B2DPoint& linearPos() const { return m_aLinearPos; }
105 void setNewWidth(double width) { m_nNewWidth = width; }
106 void addNewWidth(double width) { m_nNewWidth += width; }
107 void setLinearPos(const basegfx::B2DPoint& point) { m_aLinearPos = point; }
108 void setLinearPosX(double x) { m_aLinearPos.setX(x); }
109 void adjustLinearPosX(double diff) { m_aLinearPos.adjustX(diff); }
110 bool isLayoutEquivalent(const GlyphItem& other) const
112 return rtl::math::approxEqual(m_aLinearPos.getX(), other.m_aLinearPos.getX(), 8)
113 && rtl::math::approxEqual(m_aLinearPos.getY(), other.m_aLinearPos.getY(), 8)
114 && m_nOrigWidth == other.m_nOrigWidth && m_nCharPos == other.m_nCharPos
115 && m_nXOffset == other.m_nXOffset && m_nYOffset == other.m_nYOffset
116 && m_nNewWidth == other.m_nNewWidth && m_aGlyphId == other.m_aGlyphId
117 && m_nCharCount == other.m_nCharCount
118 && (m_nFlags & ~GlyphItemFlags::IS_UNSAFE_TO_BREAK)
119 == (other.m_nFlags & ~GlyphItemFlags::IS_UNSAFE_TO_BREAK);
123 bool GlyphItem::GetGlyphBoundRect(const LogicalFontInstance* pFontInstance,
124 basegfx::B2DRectangle& rRect) const
126 return pFontInstance->GetGlyphBoundRect(m_aGlyphId, rRect, IsVertical());
129 bool GlyphItem::GetGlyphOutline(const LogicalFontInstance* pFontInstance,
130 basegfx::B2DPolyPolygon& rPoly) const
132 return pFontInstance->GetGlyphOutline(m_aGlyphId, rPoly, IsVertical());
135 void GlyphItem::dropGlyph()
137 m_nCharPos = -1;
138 m_nFlags |= GlyphItemFlags::IS_DROPPED;
141 class SalLayoutGlyphsImpl : public std::vector<GlyphItem>
143 public:
144 SalLayoutGlyphsImpl(LogicalFontInstance& rFontInstance)
145 : m_rFontInstance(&rFontInstance)
148 SalLayoutGlyphsImpl* clone() const;
149 SalLayoutGlyphsImpl* cloneCharRange(sal_Int32 index, sal_Int32 length) const;
150 const rtl::Reference<LogicalFontInstance>& GetFont() const { return m_rFontInstance; }
151 bool IsValid() const;
152 void SetFlags(SalLayoutFlags flags) { mnFlags = flags; }
153 SalLayoutFlags GetFlags() const { return mnFlags; }
154 #ifdef DBG_UTIL
155 bool isLayoutEquivalent(const SalLayoutGlyphsImpl* other) const;
156 #endif
158 private:
159 bool isSafeToBreak(const_iterator pos, bool rtl) const;
160 rtl::Reference<LogicalFontInstance> m_rFontInstance;
161 SalLayoutFlags mnFlags = SalLayoutFlags::NONE;
164 #endif // INCLUDED_VCL_IMPGLYPHITEM_HXX
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */