summaryrefslogtreecommitdiffstats
path: root/chrome/common/gfx/text_elider.h
blob: e13dab6ac09df27eef8fd4567a65496a4311213f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_COMMON_GFX_TEXT_ELIDER_H_
#define CHROME_COMMON_GFX_TEXT_ELIDER_H_

#include <unicode/coll.h>
#include <unicode/uchar.h>

#include "base/basictypes.h"
#include "chrome/common/gfx/chrome_font.h"

class GURL;

namespace gfx {

// This function takes a GURL object and elides it. It returns a string
// which composed of parts from subdomain, domain, path, filename and query.
// A "..." is added automatically at the end if the elided string is bigger
// than the available pixel width. For available pixel width = 0, empty
// string is returned. |languages| is a comma separted list of ISO 639
// language codes and is used to determine what characters are understood
// by a user. It should come from |prefs::kAcceptLanguages|.
std::wstring ElideUrl(const GURL& url,
                      const ChromeFont& font,
                      int available_pixel_width,
                      const std::wstring& languages);

std::wstring ElideText(const std::wstring& text,
                       const ChromeFont& font,
                       int available_pixel_width);

// Elide a filename to fit a given pixel width, with an emphasis on not hiding
// the extension unless we have to. If filename contains a path, the path will
// be removed if filename doesn't fit into available_pixel_width.
std::wstring ElideFilename(const std::wstring& filename,
                           const ChromeFont& font,
                           int available_pixel_width);

// SortedDisplayURL maintains a string from a URL suitable for display to the
// use. SortedDisplayURL also provides a function used for comparing two
// SortedDisplayURLs for use in visually ordering the SortedDisplayURLs.
//
// SortedDisplayURL is relatively cheap and supports value semantics.
class SortedDisplayURL {
 public:
  SortedDisplayURL(const GURL& url, const std::wstring& languages);
  SortedDisplayURL() {}

  // Compares this SortedDisplayURL to |url| using |collator|. Returns a value
  // < 0, = 1 or > 0 as to whether this url is less then, equal to or greater
  // than the supplied url.
  int Compare(const SortedDisplayURL& other, Collator* collator) const;

  // Returns the display string for the URL.
  const std::wstring& display_url() const { return display_url_; }

 private:
  // Returns everything after the host. This is used by Compare if the hosts
  // match.
  std::wstring AfterHost() const;

  // Host name minus 'www.'. Used by Compare.
  std::wstring sort_host_;

  // End of the prefix (spec and separator) in display_url_.
  size_t prefix_end_;

  std::wstring display_url_;
};

} // namespace gfx.

#endif  // CHROME_COMMON_GFX_TEXT_ELIDER_H_