Skip to content

Commit

Permalink
Measure double width char widths
Browse files Browse the repository at this point in the history
  • Loading branch information
as-cii committed Oct 15, 2015
1 parent 27dc3da commit 4c66341
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
10 changes: 10 additions & 0 deletions spec/text-editor-component-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2891,6 +2891,16 @@ describe "TextEditorComponent", ->
expect(editor.consolidateSelections).toHaveBeenCalled()
expect(event.abortKeyBinding).toHaveBeenCalled()

describe "when changing the font", ->
it "measures the default char width and the double width char width", ->
expect(editor.getDefaultCharWidth()).toBe(12)

component.setFontSize(10)
nextAnimationFrame()

expect(editor.getDefaultCharWidth()).toBe(6)
expect(editor.getDoubleWidthCharWidth()).toBe(10)

describe "hiding and showing the editor", ->
describe "when the editor is hidden when it is mounted", ->
it "defers measurement and rendering until the editor becomes visible", ->
Expand Down
18 changes: 15 additions & 3 deletions src/lines-component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ DummyLineNode.className = 'line'
DummyLineNode.style.position = 'absolute'
DummyLineNode.style.visibility = 'hidden'
DummyLineNode.appendChild(document.createElement('span'))
DummyLineNode.firstChild.textContent = 'x'
DummyLineNode.firstChild.textContent = 'xフ'

RangeForMeasurement = document.createRange()

module.exports =
class LinesComponent extends TiledComponent
Expand Down Expand Up @@ -76,12 +78,22 @@ class LinesComponent extends TiledComponent

measureLineHeightAndDefaultCharWidth: ->
@domNode.appendChild(DummyLineNode)
textNode = DummyLineNode.firstChild.childNodes[0]

lineHeightInPixels = DummyLineNode.getBoundingClientRect().height
charWidth = DummyLineNode.firstChild.getBoundingClientRect().width

RangeForMeasurement.setStart(textNode, 0)
RangeForMeasurement.setEnd(textNode, 1)
defaultCharWidth = RangeForMeasurement.getBoundingClientRect().width

RangeForMeasurement.setStart(textNode, 1)
RangeForMeasurement.setEnd(textNode, 2)
doubleWidthCharWidth = RangeForMeasurement.getBoundingClientRect().width

@domNode.removeChild(DummyLineNode)

@presenter.setLineHeight(lineHeightInPixels)
@presenter.setBaseCharacterWidth(charWidth)
@presenter.setBaseCharacterWidth(defaultCharWidth, doubleWidthCharWidth)

lineNodeForLineIdAndScreenRow: (lineId, screenRow) ->
tile = @presenter.tileForRow(screenRow)
Expand Down
7 changes: 4 additions & 3 deletions src/text-editor-presenter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1122,10 +1122,11 @@ class TextEditorPresenter
@mouseWheelScreenRow = screenRow
@didStartScrolling()

setBaseCharacterWidth: (baseCharacterWidth) ->
unless @baseCharacterWidth is baseCharacterWidth
setBaseCharacterWidth: (baseCharacterWidth, doubleWidthCharWidth) ->
unless @baseCharacterWidth is baseCharacterWidth and @doubleWidthCharWidth is doubleWidthCharWidth
@baseCharacterWidth = baseCharacterWidth
@model.setDefaultCharWidth(baseCharacterWidth)
@doubleWidthCharWidth = doubleWidthCharWidth
@model.setDefaultCharWidth(baseCharacterWidth, doubleWidthCharWidth)
@characterWidthsChanged()

characterWidthsChanged: ->
Expand Down
5 changes: 4 additions & 1 deletion src/text-editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2976,8 +2976,11 @@ class TextEditor extends Model
getLineHeightInPixels: -> @displayBuffer.getLineHeightInPixels()
setLineHeightInPixels: (lineHeightInPixels) -> @displayBuffer.setLineHeightInPixels(lineHeightInPixels)

getDoubleWidthCharWidth: -> @displayBuffer.getDoubleWidthCharWidth()

getDefaultCharWidth: -> @displayBuffer.getDefaultCharWidth()
setDefaultCharWidth: (defaultCharWidth) -> @displayBuffer.setDefaultCharWidth(defaultCharWidth)
setDefaultCharWidth: (defaultCharWidth, doubleWidthCharWidth=defaultCharWidth) ->
@displayBuffer.setDefaultCharWidth(defaultCharWidth, doubleWidthCharWidth)

setHeight: (height, reentrant=false) ->
if reentrant
Expand Down

0 comments on commit 4c66341

Please sign in to comment.