<commit_before>Foundation = require 'art-foundation'
Atomic = require 'art-atomic'
Canvas = require 'art-canvas'
FillableBase = require '../fillable_base'

{ceil, round} = Math
{inspect, min, max, bound, log, createWithPostCreate, isString, isNumber, BaseObject} = Foundation
{color, Color, point, Point, rect, Rectangle, matrix, Matrix, point0, point1} = Atomic

module.exports = createWithPostCreate class BitmapElement extends FillableBase

  class BitmapElement.SourceToBitmapCache extends BaseObject
    @singletonClass()

    constructor: ->
      @_cache = {}
      @_referenceCounts = {}

    # OUT: promise.then (bitmap) ->
    get: (url, initializerPromise) ->
      if url.match "ImagePicker"
        log "SourceToBitmapCache#get: cached:#{!!@_cache[url]} #{url}"
        log "initializerPromise: #{!!initializerPromise}"
      @_referenceCounts[url] = (@_referenceCounts[url] || 0) + 1
      @_cache[url] ||= initializerPromise || Canvas.Bitmap.get url

    # returns true if the bitmap was released
    # returns false if there are still other references
    release: (url) ->
      return unless url
      # log "SourceToBitmapCache#release: #{url}"
      throw new Error "no references for #{url}" unless isNumber @_referenceCounts[url]
      if @_referenceCounts[url] == 0 || !isNumber @_referenceCounts[url]
        return console.error "invalid referenceCount: #{inspect @_referenceCounts[url]} for url: #{url}"

      @_referenceCounts[url]--
      if @_referenceCounts[url] == 0
        delete @_cache[url]
        true
      else
        false

  sourceToBitmapCache = BitmapElement.SourceToBitmapCache.singleton

  constructor: (options) ->
    super
    @_bitmapToElementMatrix = new Matrix

  _unregister: ->
    sourceToBitmapCache.release @getSource()
    super

  @getter
    cacheable: -> false

  # returns childrenSize
  customLayoutChildrenFirstPass: (size) ->
    @getPendingBitmap()?.pointSize || point0

  customLayoutChildrenSecondPass: (size) ->

  @drawProperty
    focus:      default: null,      preprocess: (v) -> if v? then point(v).bound(point0, point1) else null
    mode:       default: "stretch", preprocess: (v) -> v?.toString() || null
    sourceArea: default: null,      preprocess: (v) -> if v? then rect v else null

  @concreteProperty
    ###
    This works pretty-much like the HTMLImageElement's "src" field.
    It will fetch a bitmap from the specified URL.
    It will fire off the following events: onLoad and onError
    It will set the @bitmap property on success.
    If it changes, it will attempt to load the new URL and fire another onLoad or onError.

    NOTE on naming vs HTMLImageElement:
      The naming choices here are for consistency and full-words.
      The DOM is inconsistent uses shortend words like "src."

      DOM     Art.Engine
      src     source
      onload  load
      onerror error
    ###
    source:
      default:    null
      validate:   (v) -> !v || isString v
      postSetter: (v) -> @_loadBitmapFromSource v

  _loadBitmapFromSource: (source) ->
    sourceToBitmapCache.get source
    .then (bitmap) =>
      @onNextReady => @queueEvent "load", => bitmap:bitmap
      @setBitmap bitmap<commit_msg>fixed bug setting bitmap-source with null values<commit_after>Foundation = require 'art-foundation'
Atomic = require 'art-atomic'
Canvas = require 'art-canvas'
FillableBase = require '../fillable_base'

{ceil, round} = Math
{inspect, min, max, bound, log, createWithPostCreate, isString, isNumber, BaseObject} = Foundation
{color, Color, point, Point, rect, Rectangle, matrix, Matrix, point0, point1} = Atomic

module.exports = createWithPostCreate class BitmapElement extends FillableBase

  class BitmapElement.SourceToBitmapCache extends BaseObject
    @singletonClass()

    constructor: ->
      @_cache = {}
      @_referenceCounts = {}

    # OUT: promise.then (bitmap) ->
    get: (url, initializerPromise) ->
      if url.match "ImagePicker"
        log "SourceToBitmapCache#get: cached:#{!!@_cache[url]} #{url}"
        log "initializerPromise: #{!!initializerPromise}"
      @_referenceCounts[url] = (@_referenceCounts[url] || 0) + 1
      @_cache[url] ||= initializerPromise || Canvas.Bitmap.get url

    # returns true if the bitmap was released
    # returns false if there are still other references
    release: (url) ->
      return unless url
      # log "SourceToBitmapCache#release: #{url}"
      throw new Error "no references for #{url}" unless isNumber @_referenceCounts[url]
      if @_referenceCounts[url] == 0 || !isNumber @_referenceCounts[url]
        return console.error "invalid referenceCount: #{inspect @_referenceCounts[url]} for url: #{url}"

      @_referenceCounts[url]--
      if @_referenceCounts[url] == 0
        delete @_cache[url]
        true
      else
        false

  sourceToBitmapCache = BitmapElement.SourceToBitmapCache.singleton

  constructor: (options) ->
    super
    @_bitmapToElementMatrix = new Matrix

  _unregister: ->
    sourceToBitmapCache.release @getSource()
    super

  @getter
    cacheable: -> false

  # returns childrenSize
  customLayoutChildrenFirstPass: (size) ->
    @getPendingBitmap()?.pointSize || point0

  customLayoutChildrenSecondPass: (size) ->

  @drawProperty
    focus:      default: null,      preprocess: (v) -> if v? then point(v).bound(point0, point1) else null
    mode:       default: "stretch", preprocess: (v) -> v?.toString() || null
    sourceArea: default: null,      preprocess: (v) -> if v? then rect v else null

  @concreteProperty
    ###
    This works pretty-much like the HTMLImageElement's "src" field.
    It will fetch a bitmap from the specified URL.
    It will fire off the following events: onLoad and onError
    It will set the @bitmap property on success.
    If it changes, it will attempt to load the new URL and fire another onLoad or onError.

    NOTE on naming vs HTMLImageElement:
      The naming choices here are for consistency and full-words.
      The DOM is inconsistent uses shortend words like "src."

      DOM     Art.Engine
      src     source
      onload  load
      onerror error
    ###
    source:
      default:    null
      validate:   (v) -> !v || isString v
      postSetter: (v) -> v && @_loadBitmapFromSource v

  _loadBitmapFromSource: (source) ->
    sourceToBitmapCache.get source
    .then (bitmap) =>
      @onNextReady => @queueEvent "load", => bitmap:bitmap
      @setBitmap bitmap
{'fraction_non_alphanumeric': 0.10342146189735614, 'fraction_numerical': 0.0027993779160186624, 'mean_word_length': 3.2142857142857144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7584796', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1832, 'n_words': 592}