File: web_app_icon_generator.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (164 lines) | stat: -rw-r--r-- 5,466 bytes parent folder | download | duplicates (4)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_applications/web_app_icon_generator.h"

#include <string>
#include <utility>

#include "base/i18n/case_conversion.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/shortcuts/shortcut_icon_generator.h"
#include "chrome/grit/platform_locale_settings.h"
#include "components/url_formatter/url_formatter.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "skia/ext/image_operations.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_rep.h"

namespace web_app {

namespace {

// Adds a square container icon of |output_size| and 2 * |output_size| pixels
// to |bitmaps| by drawing the given |icon_letter| into a rounded background.
// For each size, if an icon of the requested size already exists in |bitmaps|,
// nothing will happen.
void GenerateIcon(SizeToBitmap* bitmaps,
                  SquareSizePx output_size,
                  char32_t icon_letter) {
  // Do nothing if there is already an icon of |output_size|.
  if (bitmaps->count(output_size))
    return;

  (*bitmaps)[output_size] = shortcuts::GenerateBitmap(output_size, icon_letter);
}

void GenerateIcons(const std::set<SquareSizePx>& generate_sizes,
                   char32_t icon_letter,
                   SizeToBitmap* bitmap_map) {
  for (SquareSizePx size : generate_sizes)
    GenerateIcon(bitmap_map, size, icon_letter);
}

}  // namespace

std::set<SquareSizePx> SizesToGenerate() {
  return std::set<SquareSizePx>({
      icon_size::k32,
      icon_size::k64,
      icon_size::k48,
      icon_size::k96,
      icon_size::k128,
      icon_size::k256,
  });
}

SizeToBitmap ConstrainBitmapsToSizes(const std::vector<SkBitmap>& bitmaps,
                                     const std::set<SquareSizePx>& sizes) {
  SizeToBitmap output_bitmaps;
  SizeToBitmap ordered_bitmaps;
  for (const SkBitmap& bitmap : bitmaps) {
    DCHECK(bitmap.width() == bitmap.height());
    ordered_bitmaps[bitmap.width()] = bitmap;
  }

  if (!ordered_bitmaps.empty()) {
    for (const auto& size : sizes) {
      // Find the closest not-smaller bitmap, or failing that use the largest
      // icon available.
      auto bitmaps_it = ordered_bitmaps.lower_bound(size);
      if (bitmaps_it != ordered_bitmaps.end())
        output_bitmaps[size] = bitmaps_it->second;
      else
        output_bitmaps[size] = ordered_bitmaps.rbegin()->second;

      // Resize the bitmap if it does not exactly match the desired size.
      if (output_bitmaps[size].width() != size) {
        output_bitmaps[size] = skia::ImageOperations::Resize(
            output_bitmaps[size], skia::ImageOperations::RESIZE_LANCZOS3, size,
            size);
      }
    }
  }

  return output_bitmaps;
}

SizeToBitmap ResizeIconsAndGenerateMissing(
    const std::vector<SkBitmap>& icons,
    const std::set<SquareSizePx>& sizes_to_generate,
    char32_t icon_letter,
    bool* is_generated_icon) {
  DCHECK(is_generated_icon);

  // Resize provided icons to make sure we have versions for each size in
  // |sizes_to_generate|.
  SizeToBitmap resized_bitmaps(
      ConstrainBitmapsToSizes(icons, sizes_to_generate));

  // Also add all provided icon sizes.
  for (const SkBitmap& icon : icons) {
    if (resized_bitmaps.find(icon.width()) == resized_bitmaps.end())
      resized_bitmaps.insert(std::make_pair(icon.width(), icon));
  }

  if (!resized_bitmaps.empty()) {
    *is_generated_icon = false;
    // ConstrainBitmapsToSizes generates versions for each size in
    // |sizes_to_generate|, so we don't need to generate icons.
    return resized_bitmaps;
  }

  *is_generated_icon = true;
  GenerateIcons(sizes_to_generate, icon_letter, &resized_bitmaps);

  return resized_bitmaps;
}

SizeToBitmap GenerateIcons(const std::string& app_name) {
  const std::u16string app_name_utf16 = base::UTF8ToUTF16(app_name);
  const char32_t icon_letter =
      shortcuts::GenerateIconLetterFromName(app_name_utf16);
  LOG(ERROR) << "Generated icons for " << app_name << " with letter "
             << std::string(1, icon_letter);

  SizeToBitmap icons;
  for (SquareSizePx size : SizesToGenerate()) {
    icons[size] = shortcuts::GenerateBitmap(size, icon_letter);
  }
  return icons;
}

gfx::ImageSkia ConvertImageToSolidFillMonochrome(SkColor solid_color,
                                                 const gfx::ImageSkia& image) {
  if (image.isNull())
    return image;

  // Create a monochrome image by combining alpha channel of |image| and
  // |solid_color|.
  gfx::ImageSkia solid_fill_image =
      gfx::ImageSkiaOperations::CreateColorMask(image, solid_color);

  // Does the actual conversion from the source image.
  for (const gfx::ImageSkiaRep& src_ui_scalefactor_rep : image.image_reps())
    solid_fill_image.GetRepresentation(src_ui_scalefactor_rep.scale());

  // Deletes the pointer to the source image.
  solid_fill_image.MakeThreadSafe();

  return solid_fill_image;
}

}  // namespace web_app