import { makeElement } from '../../common/dom.js'; import { showToast } from '../../controls/dialogs/dialogs.js'; import { Glyph } from '../../project_data/glyph.js'; import { getComponentInstancesFromRoot } from '../../project_editor/cross_item_actions.js'; import { glyphIterator } from './page.js'; // -------------------------------------------------------------- // Move // -------------------------------------------------------------- /** * Makes the content for the Move global action card. * @returns {Element} */ export function makeCard_Move() { const card = makeElement({ className: 'global-actions__card' }); card.appendChild(makeElement({ tag: 'h2', content: 'Move all glyphs' })); let description = makeElement({ className: 'global-actions__description', content: `Given a positive or negative X and/or Y value, all the glyphs in this font will have their position updated by the specified number of Em units.`, }); card.appendChild(description); let effect = makeElement({ className: 'global-actions__effect-description', content: `Individual Glyphs, Ligatures, and Components will be moved.
Note: Component Instances that are linked to Items that are also moved will be skipped. If a Component Root and an Item that contains a Component Instance are both moved, the Component Instance would have been moved twice.`, }); card.appendChild(effect); let table = makeElement({ className: 'settings-table', innerHTML: `
Em
Em
`, }); card.appendChild(table); const button = makeElement({ tag: 'fancy-button', attributes: { secondary: '' }, content: 'Move all glyphs', }); button.addEventListener('click', () => { /**@type {HTMLInputElement} */ const moveXInput = document.querySelector('#moveX'); const moveX = parseFloat(moveXInput.value) || 0; /**@type {HTMLInputElement} */ const moveYInput = document.querySelector('#moveY'); const moveY = parseFloat(moveYInput.value) || 0; glyphIterator({ title: 'Moving glyph', action: (/** @type {Glyph} */ glyph) => { if (!glyph.shapes || !glyph.shapes.length) return; glyph.shapes.forEach((shape) => { if (shape.objType !== 'ComponentInstance') { shape.updateShapePosition(moveX, moveY); glyph.changed(); } }); }, }); }); card.appendChild(button); return card; } // -------------------------------------------------------------- // Scale Horizontal // -------------------------------------------------------------- /** * Makes the content for the Scale Horizontal global action card. * @returns {Element} */ export function makeCard_ScaleHorizontal() { const card = makeElement({ className: 'global-actions__card' }); card.appendChild(makeElement({ tag: 'h2', content: 'Horizontally scale all glyphs' })); let description = makeElement({ className: 'global-actions__description', content: `Given a multiplier, scale all the glyph shapes in the horizontal direction. Entering 1 will result in no change, between zero and one will reduce the width, and above one will increase the width. Optionally scale the advance width of the glyph as well.`, }); card.appendChild(description); let effect = makeElement({ className: 'global-actions__effect-description', content: `Individual Glyphs, Ligatures, and Components will have a new width calculated based on the overall width of the shapes in that glyph (not the advance width of the glyph). The new width will be applied.
Note: Component Instances that are linked to Items that are also re-sized will be skipped. If a Component Root and an Item that contains a Component Instance are both re-sized, the Component Instance would have been re-sized twice.`, }); card.appendChild(effect); let table = makeElement({ className: 'settings-table', innerHTML: `
Scale factor
`, }); card.appendChild(table); table = makeElement({ className: 'settings-table', innerHTML: ` `, }); card.appendChild(table); const button = makeElement({ tag: 'fancy-button', attributes: { secondary: '' }, content: 'Scale all glyphs', }); button.addEventListener('click', () => { /** @type {HTMLInputElement} */ const scaleHorizontalInput = document.querySelector('#scaleHorizontal'); const scaleHorizontal = parseFloat(scaleHorizontalInput.value) || 1; /** @type {HTMLInputElement} */ const updateAdvanceWidthBox = document.querySelector('#scaleHorizontalUpdateAdvanceWidth'); const updateAdvanceWidth = updateAdvanceWidthBox.checked; glyphIterator({ title: 'Horizontally scaling glyph', action: (/** @type {Glyph} */ glyph, otherScaledItems) => { if (!glyph.shapes || !glyph.shapes.length) return; let newWidth = (glyph.maxes.xMax - glyph.maxes.xMin) * scaleHorizontal; glyph.setGlyphSize({ width: newWidth, transformOrigin: 'baseline-left', instanceIDsToSkip: otherScaledItems, }); if (updateAdvanceWidth) { const oldLSB = glyph.leftSideBearing; glyph.advanceWidth = glyph.advanceWidth * scaleHorizontal; glyph.setGlyphPosition(oldLSB * scaleHorizontal); } }, }); }); card.appendChild(button); return card; } // -------------------------------------------------------------- // Scale Vertical // -------------------------------------------------------------- /** * Makes the content for the Scale Vertical global action card. * @returns {Element} */ export function makeCard_ScaleVertical() { const card = makeElement({ className: 'global-actions__card' }); card.appendChild(makeElement({ tag: 'h2', content: 'Vertically scale all glyphs' })); let description = makeElement({ className: 'global-actions__description', content: `Given a multiplier, scale all the glyph shapes in the vertical direction. Entering 1 will result in no change, between zero and one will scale down the height, and above one will increase the height.`, }); card.appendChild(description); let effect = makeElement({ className: 'global-actions__effect-description', content: `Individual Glyphs, Ligatures, and Components will have a new height calculated based on the overall height of the shapes in that glyph (not the ascender or descender values from the font). The new height will be applied, and the shapes will be moved to maintain a common baseline.
Note: Component Instances that are linked to Items that are also re-sized will be skipped. If a Component Root and an Item that contains a Component Instance are both re-sized, the Component Instance would have been re-sized twice.`, }); card.appendChild(effect); const table = makeElement({ className: 'settings-table', innerHTML: `
Scale factor
`, }); card.appendChild(table); const button = makeElement({ tag: 'fancy-button', attributes: { secondary: '' }, content: 'Scale all glyphs', }); button.addEventListener('click', () => { /** @type {HTMLInputElement} */ const scaleVerticalInput = document.querySelector('#scaleVertical'); const scaleVertical = parseFloat(scaleVerticalInput.value) || 1; glyphIterator({ title: 'Vertically scaling glyph', action: (/** @type {Glyph} */ glyph, otherScaledItems) => { if (!glyph.shapes || !glyph.shapes.length) return; const newHeight = (glyph.maxes.yMax - glyph.maxes.yMin) * scaleVertical; glyph.setGlyphSize({ height: newHeight, transformOrigin: 'baseline-left', instanceIDsToSkip: otherScaledItems, }); }, }); }); card.appendChild(button); return card; } // -------------------------------------------------------------- // Resize // -------------------------------------------------------------- /** * Makes the content for the Resize global action card. * @returns {Element} */ export function makeCard_Resize() { const card = makeElement({ className: 'global-actions__card' }); card.appendChild(makeElement({ tag: 'h2', content: 'Re-size all glyphs' })); let description = makeElement({ className: 'global-actions__description', content: `Given a positive or negative Width and/or Height value, all the glyphs in this font will have their size updated by the specified number of Em units.`, }); card.appendChild(description); let effect = makeElement({ className: 'global-actions__effect-description', content: `Individual Glyphs, Ligatures, and Components will be re-sized.
Note: Component Instances that are linked to Items that are also re-sized will be skipped. If a Component Root and an Item that contains a Component Instance are both re-sized, the Component Instance would have been re-sized twice.`, }); card.appendChild(effect); let table = makeElement({ className: 'settings-table', innerHTML: `
Em
Em
`, }); card.appendChild(table); table = makeElement({ className: 'settings-table', innerHTML: `     `, }); card.appendChild(table); const button = makeElement({ tag: 'fancy-button', attributes: { secondary: '' }, content: 'Re-size all glyphs', }); button.addEventListener('click', () => { // log('updateAllGlyphSizesByEm', 'start'); /** @type {HTMLInputElement} */ const resizeWInput = document.querySelector('#resizeWidth'); let resizeW = parseFloat(resizeWInput.value) || 0; /** @type {HTMLInputElement} */ const resizeHInput = document.querySelector('#resizeHeight'); const resizeH = parseFloat(resizeHInput.value) || 0; /** @type {HTMLInputElement} */ const ratioBox = document.querySelector('#resizeMaintainAspectRatio'); const ratio = ratioBox.checked; /** @type {HTMLInputElement} */ const updateAdvanceWidthBox = document.querySelector('#resizeUpdateAdvanceWidth'); const updateAdvanceWidth = updateAdvanceWidthBox.checked; if (ratio && !resizeH && !resizeW) { // For ratio lock to work, one delta value has to be zero // Let's just choose width so we can still do scaleH calculations resizeW = 0; } // log(`after sanitizing - resizeW: ${resizeW}, resizeH: ${resizeH}, ratio lock: ${ratio}`); glyphIterator({ title: 'Re-sizing glyph', action: (/** @type {Glyph} */ glyph, otherResizedItems) => { if (!glyph.shapes || !glyph.shapes.length) return; glyph.updateGlyphSize({ width: resizeW, height: resizeH, ratioLock: ratio, transformOrigin: 'baseline-left', instanceIDsToSkip: otherResizedItems, }); if (updateAdvanceWidth) glyph.advanceWidth += resizeW; }, }); // log('updateAllGlyphSizesByEm', 'end'); }); card.appendChild(button); return card; } // -------------------------------------------------------------- // Skew // -------------------------------------------------------------- export function makeCard_Skew() { const card = makeElement({ className: 'global-actions__card' }); card.appendChild(makeElement({ tag: 'h2', content: 'Skew all paths' })); let description = makeElement({ className: 'global-actions__description', content: `Skew all paths by a given angle, measured in degrees. The degrees system starts with the twelve o'clock / up position as zero, and positive degrees will slant paths to the right, and negative degrees will slant paths to the left.

This could be a good first step if you are trying to make an italic version of an existing font.`, }); card.appendChild(description); let effect = makeElement({ className: 'global-actions__effect-description', content: `Individual Glyphs, Ligatures, and Components will have have all of their paths skewed by the given angle.
Note: Component Instances will not be skewed.`, }); card.appendChild(effect); let table = makeElement({ className: 'settings-table', innerHTML: `
Angle in degrees
`, }); card.appendChild(table); table = makeElement({ className: 'settings-table', innerHTML: ` `, }); card.appendChild(table); const button = makeElement({ tag: 'fancy-button', attributes: { secondary: '' }, content: 'Skew all glyphs', }); button.addEventListener('click', () => { /** @type {HTMLInputElement} */ const skewAngleInput = document.querySelector('#skewAngle'); const skewAngle = parseFloat(skewAngleInput.value) || 0; /** @type {HTMLInputElement} */ const updateAdvanceWidthBox = document.querySelector('#skewAngleUpdateAdvanceWidth'); const updateAdvanceWidth = updateAdvanceWidthBox.checked; glyphIterator({ title: 'Skewing glyphs', action: (/** @type {Glyph} */ glyph) => { if (!glyph.shapes || !glyph.shapes.length) return; const oldRSB = glyph.rightSideBearing; glyph.skewAngle(skewAngle); if (updateAdvanceWidth) glyph.rightSideBearing = oldRSB; }, }); }); card.appendChild(button); return card; } // -------------------------------------------------------------- // Side Bearings // -------------------------------------------------------------- /** * Makes the content for the Side Bearings global action card. * @returns {Element} */ export function makeCard_SideBearings() { const card = makeElement({ className: 'global-actions__card' }); card.appendChild(makeElement({ tag: 'h2', content: 'Side bearings' })); let description = makeElement({ className: 'global-actions__description', content: `Side Bearing is the empty space around a glyph's shapes. The Left Side Bearing is the space between x=0 to the leftmost side of the glyph shapes. The Right Side Bearing is the distance between the rightmost side of the glyph shapes and the advance width. Side Bearings will probably have to be adjusted for each glyph in order for it to work well... but this action could be useful if you want to re-set all Side Bearings to some common value.`, }); card.appendChild(description); let effect = makeElement({ className: 'global-actions__effect-description', content: `Each ligature and glyph will have their shapes moved and/or their advance width updated to create the specified side bearing widths.`, }); card.appendChild(effect); let table = makeElement({ className: 'settings-table', innerHTML: `
Em
Em
`, }); card.appendChild(table); let button = makeElement({ tag: 'fancy-button', attributes: { secondary: '' }, content: 'Update Side Bearings', }); button.addEventListener('click', () => { // log('updateSideBearings', 'start'); /** @type {HTMLInputElement} */ const leftInput = document.querySelector('#sideBearingLeft'); const left = parseFloat(leftInput.value); // log(`left input: ${left}`); /** @type {HTMLInputElement} */ const leftCheckboxBox = document.querySelector('#sideBearingLeftCheckbox'); let leftCheckbox = leftCheckboxBox.checked; /** @type {HTMLInputElement} */ const rightInput = document.querySelector('#sideBearingRight'); const right = parseFloat(rightInput.value); // log(`right input: ${right}`); /** @type {HTMLInputElement} */ const rightCheckboxBox = document.querySelector('#sideBearingRightCheckbox'); let rightCheckbox = rightCheckboxBox.checked; if (leftCheckbox || rightCheckbox) { if (isNaN(left) || isNaN(right)) { showToast('Side Bearing values must be numbers.'); } else { glyphIterator({ title: 'Updating Side Bearings', includeComponents: false, action: (/** @type {Glyph} */ glyph) => { if (glyph.shapes.length) { if (leftCheckbox && !isNaN(left)) { glyph.leftSideBearing = left; getComponentInstancesFromRoot(glyph).forEach((instance) => { instance.translateX -= left; }); } if (rightCheckbox && !isNaN(right)) glyph.rightSideBearing = right; } }, }); } } else { showToast('Select a checkbox to enable Left and/or Right Side Bearings.'); } // log('updateSideBearings', 'end'); }); card.appendChild(button); return card; }