Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 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 Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 29 | import * as Common from '../common/common.js'; |
Tim van der Lippe | 93b57c3 | 2020-02-20 17:38:44 | [diff] [blame] | 30 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 31 | import * as TextUtils from '../text_utils/text_utils.js'; |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 32 | import * as UI from '../ui/ui.js'; |
| 33 | |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 34 | export class FontView extends UI.View.SimpleView { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 35 | /** |
| 36 | * @param {string} mimeType |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 37 | * @param {!TextUtils.ContentProvider.ContentProvider} contentProvider |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | */ |
| 39 | constructor(mimeType, contentProvider) { |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 40 | super(Common.UIString.UIString('Font')); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 41 | this.registerRequiredCSS('source_frame/fontView.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | this.element.classList.add('font-view'); |
| 43 | this._url = contentProvider.contentURL(); |
Amanda Baker | 10e95db | 2019-06-07 00:58:13 | [diff] [blame] | 44 | UI.ARIAUtils.setAccessibleName(this.element, ls`Preview of font from ${this._url}`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 45 | this._mimeType = mimeType; |
| 46 | this._contentProvider = contentProvider; |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 47 | this._mimeTypeLabel = new UI.Toolbar.ToolbarText(mimeType); |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 48 | /** @type {?HTMLElement} */ |
| 49 | this.fontPreviewElement; |
| 50 | /** @type {?HTMLElement} */ |
| 51 | this._dummyElement; |
| 52 | /** @type {?HTMLStyleElement} */ |
| 53 | this.fontStyleElement; |
| 54 | /** @type {?boolean} */ |
| 55 | this._inResize; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @override |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 60 | * @return {!Promise<!Array<!UI.Toolbar.ToolbarItem>>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 61 | */ |
Simon Zünd | 308c74f | 2020-01-16 06:53:37 | [diff] [blame] | 62 | async toolbarItems() { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 63 | return [this._mimeTypeLabel]; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param {string} uniqueFontName |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 68 | * @param {!TextUtils.ContentProvider.DeferredContent} deferredContent |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 69 | */ |
Rob Paveza | 2eb8c14 | 2019-10-13 18:02:38 | [diff] [blame] | 70 | _onFontContentLoaded(uniqueFontName, deferredContent) { |
| 71 | const {content} = deferredContent; |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 72 | const url = content ? TextUtils.ContentProvider.contentAsDataURL(content, this._mimeType, true) : this._url; |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 73 | if (!this.fontStyleElement) { |
| 74 | return; |
| 75 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 76 | this.fontStyleElement.textContent = |
Tim van der Lippe | 93b57c3 | 2020-02-20 17:38:44 | [diff] [blame] | 77 | Platform.StringUtilities.sprintf('@font-face { font-family: "%s"; src: url(%s); }', uniqueFontName, url); |
Alex Rudenko | 22759dc | 2020-06-16 12:59:24 | [diff] [blame] | 78 | this.updateFontPreviewSize(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | _createContentIfNeeded() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 82 | if (this.fontPreviewElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 84 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 86 | const uniqueFontName = 'WebInspectorFontPreview' + (++_fontId); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 87 | |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 88 | this.fontStyleElement = /** @type {!HTMLStyleElement} */ (document.createElement('style')); |
Rob Paveza | 2eb8c14 | 2019-10-13 18:02:38 | [diff] [blame] | 89 | this._contentProvider.requestContent().then(deferredContent => { |
| 90 | this._onFontContentLoaded(uniqueFontName, deferredContent); |
| 91 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 92 | this.element.appendChild(this.fontStyleElement); |
| 93 | |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 94 | const fontPreview = /** @type {!HTMLDivElement} */ (document.createElement('div')); |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 95 | for (let i = 0; i < _fontPreviewLines.length; ++i) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 96 | if (i > 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 97 | fontPreview.createChild('br'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 98 | } |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 99 | UI.UIUtils.createTextChild(fontPreview, _fontPreviewLines[i]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 100 | } |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 101 | |
| 102 | this.fontPreviewElement = /** @type {!HTMLDivElement} */ (fontPreview.cloneNode(true)); |
| 103 | if (!this.fontPreviewElement) { |
| 104 | return; |
| 105 | } |
Amanda Baker | 10e95db | 2019-06-07 00:58:13 | [diff] [blame] | 106 | UI.ARIAUtils.markAsHidden(this.fontPreviewElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 107 | 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 Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 117 | this._dummyElement.style.setProperty('font-size', _measureFontSize + 'px'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 118 | |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 135 | if (this._inResize) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 136 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 137 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 138 | |
| 139 | this._inResize = true; |
| 140 | try { |
| 141 | this.updateFontPreviewSize(); |
| 142 | } finally { |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 143 | this._inResize = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | _measureElement() { |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 148 | if (!this._dummyElement) { |
| 149 | throw new Error('No font preview loaded'); |
| 150 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 151 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 159 | if (!this.fontPreviewElement || !this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 160 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 161 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 162 | |
| 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 Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 180 | const finalFontSize = Math.floor(_measureFontSize * Math.min(widthRatio, heightRatio)) - 2; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 181 | |
Johan Bay | ddc41d7 | 2020-10-16 18:27:22 | [diff] [blame] | 182 | this.fontPreviewElement.style.setProperty('font-size', finalFontSize + 'px', undefined); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 183 | } |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 184 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 185 | |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 186 | let _fontId = 0; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 187 | |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 188 | const _fontPreviewLines = ['ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ', 'abcdefghijklm', 'nopqrstuvwxyz', '1234567890']; |
| 189 | const _measureFontSize = 50; |