/src/libreoffice/vcl/inc/TextLayoutCache.hxx
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
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 | | #pragma once |
21 | | |
22 | | #include <sal/types.h> |
23 | | #include <rtl/ustring.hxx> |
24 | | #include <o3tl/hash_combine.hxx> |
25 | | |
26 | | #include <vcl/dllapi.h> |
27 | | |
28 | | #include <unicode/uscript.h> |
29 | | |
30 | | #include <vector> |
31 | | |
32 | | namespace vcl::text |
33 | | { |
34 | | struct Run |
35 | | { |
36 | | int32_t nStart; |
37 | | int32_t nEnd; |
38 | | UScriptCode nCode; |
39 | | Run(int32_t nStart_, int32_t nEnd_, UScriptCode nCode_) |
40 | 11.7M | : nStart(nStart_) |
41 | 11.7M | , nEnd(nEnd_) |
42 | 11.7M | , nCode(nCode_) |
43 | 11.7M | { |
44 | 11.7M | } |
45 | | }; |
46 | | |
47 | | class VCL_DLLPUBLIC TextLayoutCache |
48 | | { |
49 | | public: |
50 | | std::vector<vcl::text::Run> runs; |
51 | | TextLayoutCache(sal_Unicode const* pStr, sal_Int32 const nEnd); |
52 | | // Creates a cached instance. |
53 | | static std::shared_ptr<const vcl::text::TextLayoutCache> Create(OUString const&); |
54 | | }; |
55 | | |
56 | | struct FirstCharsStringHash |
57 | | { |
58 | | size_t operator()(const OUString& str) const |
59 | 37.3M | { |
60 | | // Strings passed to GenericSalLayout::CreateTextLayoutCache() may be very long, |
61 | | // and computing an entire hash could almost negate the gain of hashing. Hash just first |
62 | | // characters, that should be good enough. |
63 | 37.3M | size_t hash |
64 | 37.3M | = rtl_ustr_hashCode_WithLength(str.getStr(), std::min<size_t>(100, str.getLength())); |
65 | 37.3M | o3tl::hash_combine(hash, str.getLength()); |
66 | 37.3M | return hash; |
67 | 37.3M | } |
68 | | }; |
69 | | |
70 | | struct FastStringCompareEqual |
71 | | { |
72 | | bool operator()(const OUString& str1, const OUString& str2) const |
73 | 24.9M | { |
74 | | // Strings passed to GenericSalLayout::CreateTextLayoutCache() may be very long, |
75 | | // and OUString operator == compares backwards and using hard-written code, while |
76 | | // memcmp() compares much faster. |
77 | 24.9M | if (str1.getLength() != str2.getLength()) |
78 | 0 | return false; |
79 | 24.9M | if (str1.getStr() == str2.getStr()) |
80 | 17.3M | return true; |
81 | 7.59M | return memcmp(str1.getStr(), str2.getStr(), str1.getLength() * sizeof(str1.getStr()[0])) |
82 | 7.59M | == 0; |
83 | 24.9M | } |
84 | | }; |
85 | | } |
86 | | |
87 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |