blob: 8da2eabf78e23efa05f1a0cca6238cc9ba07c24d [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
Tim van der Lippe2543c5c2020-02-04 11:58:0729import * as Common from '../common/common.js';
Tim van der Lippe93b57c32020-02-20 17:38:4430import * as Platform from '../platform/platform.js';
Tim van der Lippe18f04892020-03-17 11:39:4031import * as TextUtils from '../text_utils/text_utils.js';
Tim van der Lippe2543c5c2020-02-04 11:58:0732import * as UI from '../ui/ui.js';
33
Tim van der Lippe2543c5c2020-02-04 11:58:0734export class FontView extends UI.View.SimpleView {
Blink Reformat4c46d092018-04-07 15:32:3735 /**
36 * @param {string} mimeType
Tim van der Lippe18f04892020-03-17 11:39:4037 * @param {!TextUtils.ContentProvider.ContentProvider} contentProvider
Blink Reformat4c46d092018-04-07 15:32:3738 */
39 constructor(mimeType, contentProvider) {
Tim van der Lippe2543c5c2020-02-04 11:58:0740 super(Common.UIString.UIString('Font'));
Jack Franklin71519f82020-11-03 12:08:5941 this.registerRequiredCSS('source_frame/fontView.css', {enableLegacyPatching: true});
Blink Reformat4c46d092018-04-07 15:32:3742 this.element.classList.add('font-view');
43 this._url = contentProvider.contentURL();
Amanda Baker10e95db2019-06-07 00:58:1344 UI.ARIAUtils.setAccessibleName(this.element, ls`Preview of font from ${this._url}`);
Blink Reformat4c46d092018-04-07 15:32:3745 this._mimeType = mimeType;
46 this._contentProvider = contentProvider;
Tim van der Lippe2543c5c2020-02-04 11:58:0747 this._mimeTypeLabel = new UI.Toolbar.ToolbarText(mimeType);
Johan Bayddc41d72020-10-16 18:27:2248 /** @type {?HTMLElement} */
49 this.fontPreviewElement;
50 /** @type {?HTMLElement} */
51 this._dummyElement;
52 /** @type {?HTMLStyleElement} */
53 this.fontStyleElement;
54 /** @type {?boolean} */
55 this._inResize;
Blink Reformat4c46d092018-04-07 15:32:3756 }
57
58 /**
59 * @override
Tim van der Lippe2543c5c2020-02-04 11:58:0760 * @return {!Promise<!Array<!UI.Toolbar.ToolbarItem>>}
Blink Reformat4c46d092018-04-07 15:32:3761 */
Simon Zünd308c74f2020-01-16 06:53:3762 async toolbarItems() {
Blink Reformat4c46d092018-04-07 15:32:3763 return [this._mimeTypeLabel];
64 }
65
66 /**
67 * @param {string} uniqueFontName
Tim van der Lippe18f04892020-03-17 11:39:4068 * @param {!TextUtils.ContentProvider.DeferredContent} deferredContent
Blink Reformat4c46d092018-04-07 15:32:3769 */
Rob Paveza2eb8c142019-10-13 18:02:3870 _onFontContentLoaded(uniqueFontName, deferredContent) {
71 const {content} = deferredContent;
Tim van der Lippe18f04892020-03-17 11:39:4072 const url = content ? TextUtils.ContentProvider.contentAsDataURL(content, this._mimeType, true) : this._url;
Johan Bayddc41d72020-10-16 18:27:2273 if (!this.fontStyleElement) {
74 return;
75 }
Blink Reformat4c46d092018-04-07 15:32:3776 this.fontStyleElement.textContent =
Tim van der Lippe93b57c32020-02-20 17:38:4477 Platform.StringUtilities.sprintf('@font-face { font-family: "%s"; src: url(%s); }', uniqueFontName, url);
Alex Rudenko22759dc2020-06-16 12:59:2478 this.updateFontPreviewSize();
Blink Reformat4c46d092018-04-07 15:32:3779 }
80
81 _createContentIfNeeded() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3482 if (this.fontPreviewElement) {
Blink Reformat4c46d092018-04-07 15:32:3783 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3484 }
Blink Reformat4c46d092018-04-07 15:32:3785
Tim van der Lippeb88663f2019-11-07 15:53:2186 const uniqueFontName = 'WebInspectorFontPreview' + (++_fontId);
Blink Reformat4c46d092018-04-07 15:32:3787
Johan Bayddc41d72020-10-16 18:27:2288 this.fontStyleElement = /** @type {!HTMLStyleElement} */ (document.createElement('style'));
Rob Paveza2eb8c142019-10-13 18:02:3889 this._contentProvider.requestContent().then(deferredContent => {
90 this._onFontContentLoaded(uniqueFontName, deferredContent);
91 });
Blink Reformat4c46d092018-04-07 15:32:3792 this.element.appendChild(this.fontStyleElement);
93
Johan Bayddc41d72020-10-16 18:27:2294 const fontPreview = /** @type {!HTMLDivElement} */ (document.createElement('div'));
Tim van der Lippeb88663f2019-11-07 15:53:2195 for (let i = 0; i < _fontPreviewLines.length; ++i) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3496 if (i > 0) {
Blink Reformat4c46d092018-04-07 15:32:3797 fontPreview.createChild('br');
Tim van der Lippe1d6e57a2019-09-30 11:55:3498 }
Sigurd Schneider23c52972020-10-13 09:31:1499 UI.UIUtils.createTextChild(fontPreview, _fontPreviewLines[i]);
Blink Reformat4c46d092018-04-07 15:32:37100 }
Johan Bayddc41d72020-10-16 18:27:22101
102 this.fontPreviewElement = /** @type {!HTMLDivElement} */ (fontPreview.cloneNode(true));
103 if (!this.fontPreviewElement) {
104 return;
105 }
Amanda Baker10e95db2019-06-07 00:58:13106 UI.ARIAUtils.markAsHidden(this.fontPreviewElement);
Blink Reformat4c46d092018-04-07 15:32:37107 this.fontPreviewElement.style.overflow = 'hidden';
108 this.fontPreviewElement.style.setProperty('font-family', uniqueFontName);
109 this.fontPreviewElement.style.setProperty('visibility', 'hidden');
110
111 this._dummyElement = fontPreview;
112 this._dummyElement.style.visibility = 'hidden';
113 this._dummyElement.style.zIndex = '-1';
114 this._dummyElement.style.display = 'inline';
115 this._dummyElement.style.position = 'absolute';
116 this._dummyElement.style.setProperty('font-family', uniqueFontName);
Tim van der Lippeb88663f2019-11-07 15:53:21117 this._dummyElement.style.setProperty('font-size', _measureFontSize + 'px');
Blink Reformat4c46d092018-04-07 15:32:37118
119 this.element.appendChild(this.fontPreviewElement);
120 }
121
122 /**
123 * @override
124 */
125 wasShown() {
126 this._createContentIfNeeded();
127
128 this.updateFontPreviewSize();
129 }
130
131 /**
132 * @override
133 */
134 onResize() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34135 if (this._inResize) {
Blink Reformat4c46d092018-04-07 15:32:37136 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34137 }
Blink Reformat4c46d092018-04-07 15:32:37138
139 this._inResize = true;
140 try {
141 this.updateFontPreviewSize();
142 } finally {
Johan Bayddc41d72020-10-16 18:27:22143 this._inResize = null;
Blink Reformat4c46d092018-04-07 15:32:37144 }
145 }
146
147 _measureElement() {
Johan Bayddc41d72020-10-16 18:27:22148 if (!this._dummyElement) {
149 throw new Error('No font preview loaded');
150 }
Blink Reformat4c46d092018-04-07 15:32:37151 this.element.appendChild(this._dummyElement);
152 const result = {width: this._dummyElement.offsetWidth, height: this._dummyElement.offsetHeight};
153 this.element.removeChild(this._dummyElement);
154
155 return result;
156 }
157
158 updateFontPreviewSize() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34159 if (!this.fontPreviewElement || !this.isShowing()) {
Blink Reformat4c46d092018-04-07 15:32:37160 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34161 }
Blink Reformat4c46d092018-04-07 15:32:37162
163 this.fontPreviewElement.style.removeProperty('visibility');
164 const dimension = this._measureElement();
165
166 const height = dimension.height;
167 const width = dimension.width;
168
169 // Subtract some padding. This should match the paddings in the CSS plus room for the scrollbar.
170 const containerWidth = this.element.offsetWidth - 50;
171 const containerHeight = this.element.offsetHeight - 30;
172
173 if (!height || !width || !containerWidth || !containerHeight) {
174 this.fontPreviewElement.style.removeProperty('font-size');
175 return;
176 }
177
178 const widthRatio = containerWidth / width;
179 const heightRatio = containerHeight / height;
Tim van der Lippeb88663f2019-11-07 15:53:21180 const finalFontSize = Math.floor(_measureFontSize * Math.min(widthRatio, heightRatio)) - 2;
Blink Reformat4c46d092018-04-07 15:32:37181
Johan Bayddc41d72020-10-16 18:27:22182 this.fontPreviewElement.style.setProperty('font-size', finalFontSize + 'px', undefined);
Blink Reformat4c46d092018-04-07 15:32:37183 }
Tim van der Lippeb88663f2019-11-07 15:53:21184}
Blink Reformat4c46d092018-04-07 15:32:37185
Tim van der Lippeb88663f2019-11-07 15:53:21186let _fontId = 0;
Blink Reformat4c46d092018-04-07 15:32:37187
Tim van der Lippeb88663f2019-11-07 15:53:21188const _fontPreviewLines = ['ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ', 'abcdefghijklm', 'nopqrstuvwxyz', '1234567890'];
189const _measureFontSize = 50;